1. Main Plugin File (sepay-ebook-plugin.php)
<?php
/**
* Plugin Name: Sepay Ebook Integration
* Description: Tích hợp Sepay.vn để bán ebook và đồng bộ với Systeme.io
* Version: 1.0
* Author: Your Name
*/
// Prevent direct access
if (!defined(‘ABSPATH’)) {
exit;
}
class SepayEbookPlugin {
public function __construct() {
add_action(‘init’, array($this, ‘init’));
add_action(‘wp_ajax_create_sepay_payment’, array($this, ‘create_sepay_payment’));
add_action(‘wp_ajax_nopriv_create_sepay_payment’, array($this, ‘create_sepay_payment’));
add_action(‘wp_ajax_sepay_webhook’, array($this, ‘handle_sepay_webhook’));
add_action(‘wp_ajax_nopriv_sepay_webhook’, array($this, ‘handle_sepay_webhook’));
// Tạo database table
register_activation_hook(__FILE__, array($this, ‘create_tables’));
}
public function init() {
// Enqueue scripts
wp_enqueue_script(‘jquery’);
wp_localize_script(‘jquery’, ‘sepay_ajax’, array(
‘ajax_url’ => admin_url(‘admin-ajax.php’),
‘nonce’ => wp_create_nonce(‘sepay_nonce’)
));
}
public function create_tables() {
global $wpdb;
$table_name = $wpdb->prefix . ‘sepay_transactions’;
$charset_collate = $wpdb->get_charset_collate();
$sql = “CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
transaction_id varchar(100) NOT NULL,
customer_name varchar(255) NOT NULL,
customer_email varchar(255) NOT NULL,
amount decimal(10,2) NOT NULL,
status varchar(50) DEFAULT ‘pending’,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY transaction_id (transaction_id)
) $charset_collate;”;
require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);
}
public function create_sepay_payment() {
// Verify nonce
if (!wp_verify_nonce($_POST[‘nonce’], ‘sepay_nonce’)) {
wp_die(‘Security check failed’);
}
$customer_name = sanitize_text_field($_POST[‘customer_name’]);
$customer_email = sanitize_email($_POST[‘customer_email’]);
$amount = 2000; // Fixed amount
// Tạo transaction ID unique
$transaction_id = ‘EBOOK_’ . time() . ‘_’ . rand(1000, 9999);
// Lưu vào database
global $wpdb;
$table_name = $wpdb->prefix . ‘sepay_transactions’;
$wpdb->insert(
$table_name,
array(
‘transaction_id’ => $transaction_id,
‘customer_name’ => $customer_name,
‘customer_email’ => $customer_email,
‘amount’ => $amount,
‘status’ => ‘pending’
)
);
// Tạo QR code với Sepay API
$qr_data = $this->generate_sepay_qr($transaction_id, $amount);
wp_send_json_success(array(
‘qr_code’ => $qr_data[‘qr_code’],
‘transaction_id’ => $transaction_id,
‘content’ => $qr_data[‘content’]
));
}
private function generate_sepay_qr($transaction_id, $amount) {
// Cấu hình Sepay
$sepay_config = array(
‘account_number’ => ‘YOUR_ACCOUNT_NUMBER’, // Số tài khoản
‘account_name’ => ‘YOUR_ACCOUNT_NAME’, // Tên tài khoản
‘bank_id’ => ‘BANK_ID’, // Mã ngân hàng (VCB, TCB, etc.)
‘template’ => ‘compact’ // Template QR
);
$content = $transaction_id; // Nội dung chuyển khoản
// URL tạo QR Sepay
$qr_url = “https://my.sepay.vn/userapi/qr/create?” . http_build_query(array(
‘account_number’ => $sepay_config[‘account_number’],
‘account_name’ => $sepay_config[‘account_name’],
‘amount’ => $amount,
‘content’ => $content,
‘bank_id’ => $sepay_config[‘bank_id’],
‘template’ => $sepay_config[‘template’]
));
return array(
‘qr_code’ => $qr_url,
‘content’ => $content
);
}
public function handle_sepay_webhook() {
// Nhận webhook từ Sepay
$input = file_get_contents(‘php://input’);
$data = json_decode($input, true);
if (!$data) {
wp_die(‘Invalid data’);
}
// Verify webhook (nếu Sepay có signature)
// $this->verify_sepay_signature($data);
$transaction_id = $data[‘content’]; // Nội dung chuyển khoản
$amount = $data[‘amount’];
$status = $data[‘status’]; // success, failed, etc.
if ($status === ‘success’) {
$this->process_successful_payment($transaction_id, $amount, $data);
}
http_response_code(200);
echo ‘OK’;
exit;
}
private function process_successful_payment($transaction_id, $amount, $webhook_data) {
global $wpdb;
$table_name = $wpdb->prefix . ‘sepay_transactions’;
// Lấy thông tin transaction
$transaction = $wpdb->get_row($wpdb->prepare(
“SELECT * FROM $table_name WHERE transaction_id = %s AND status = ‘pending'”,
$transaction_id
));
if (!$transaction) {
return false;
}
// Cập nhật status
$wpdb->update(
$table_name,
array(‘status’ => ‘completed’),
array(‘transaction_id’ => $transaction_id)
);
// Gửi email ebook
$this->send_ebook_email($transaction->customer_email, $transaction->customer_name);
// Cập nhật lên Systeme.io
$this->update_systeme_io($transaction->customer_name, $transaction->customer_email);
return true;
}
private function send_ebook_email($email, $name) {
$subject = ‘Ebook của bạn đã sẵn sàng!’;
$message = ”
Chào $name,
Cảm ơn bạn đã mua ebook của chúng tôi.
Link download ebook: [EBOOK_DOWNLOAD_LINK]
Trân trọng,
Your Team
“;
wp_mail($email, $subject, $message);
}
private function update_systeme_io($name, $email) {
$api_key = ‘YOUR_SYSTEME_IO_API_KEY’;
$tag_id = ‘YOUR_TAG_ID’; // TAG_ID được cấu hình sẵn
$data = array(
’email’ => $email,
‘firstName’ => $name,
‘tags’ => array($tag_id)
);
$response = wp_remote_post(‘https://systeme.io/api/contacts’, array(
‘headers’ => array(
‘X-API-Key’ => $api_key,
‘Content-Type’ => ‘application/json’
),
‘body’ => json_encode($data),
‘timeout’ => 30
));
return $response;
}
}
new SepayEbookPlugin();
?>
2. JavaScript cho Frontend (sepay-frontend.js)
jQuery(document).ready(function($) {
$(‘#ebookForm’).on(‘submit’, function(e) {
e.preventDefault();
var customerName = $(‘#customerName’).val();
var customerEmail = $(‘#customerEmail’).val();
if (!customerName || !customerEmail) {
alert(‘Vui lòng điền đầy đủ thông tin’);
return;
}
// Show loading
$(‘button[type=”submit”]’).html(‘<i class=”fas fa-spinner fa-spin”></i> Đang tạo mã QR…’).prop(‘disabled’, true);
$.ajax({
url: sepay_ajax.ajax_url,
method: ‘POST’,
data: {
action: ‘create_sepay_payment’,
customer_name: customerName,
customer_email: customerEmail,
nonce: sepay_ajax.nonce
},
success: function(response) {
if (response.success) {
// Hiển thị QR code
$(‘#qrCode’).html(‘<img src=”‘ + response.data.qr_code + ‘” alt=”QR Code” class=”mx-auto”>’);
$(‘#paymentContent’).text(response.data.content);
$(‘#qrSection’).removeClass(‘hidden’);
// Scroll to QR section
$(‘html, body’).animate({
scrollTop: $(‘#qrSection’).offset().top
}, 500);
} else {
alert(‘Có lỗi xảy ra: ‘ + response.data);
}
},
error: function() {
alert(‘Có lỗi xảy ra, vui lòng thử lại’);
},
complete: function() {
$(‘button[type=”submit”]’).html(‘<i class=”fas fa-qrcode”></i> Tạo mã QR thanh toán’).prop(‘disabled’, false);
}
});
});
});