Hệ thống bán Ebook với Sepay.vn và Systeme.io

Hệ thống bán Ebook với Sepay.vn

Tích hợp WordPress + Sepay + Systeme.io

Mua Ebook – 2.000 VNĐ

Ebook Premium

Tài liệu học tập chất lượng cao

2.000 VNĐ

WordPress Plugin Code

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); } }); }); });

3. Shortcode để hiển thị form

// Thêm vào functions.php hoặc plugin function sepay_ebook_form_shortcode() { ob_start(); ?> <div id=”sepay-ebook-form”> <form id=”ebookForm”> <div> <label>Họ và Tên:</label> <input type=”text” id=”customerName” required> </div> <div> <label>Email:</label> <input type=”email” id=”customerEmail” required> </div> <button type=”submit”>Mua Ebook – 2.000 VNĐ</button> </form> <div id=”qrSection” style=”display:none;”> <h3>Quét mã QR để thanh toán</h3> <div id=”qrCode”></div> <p>Nội dung: <span id=”paymentContent”></span></p> </div> </div> <script src=”path/to/sepay-frontend.js”></script> <?php return ob_get_clean(); } add_shortcode(‘sepay_ebook_form’, ‘sepay_ebook_form_shortcode’); // Sử dụng: [sepay_ebook_form]

Hướng dẫn cấu hình

Bước 1: Cấu hình Sepay.vn

1. Đăng ký tài khoản tại Sepay.vn

2. Lấy thông tin tài khoản ngân hàng để nhận tiền

3. Cấu hình webhook URL: https://yoursite.com/wp-admin/admin-ajax.php?action=sepay_webhook

4. Cập nhật thông tin trong plugin:

  • • YOUR_ACCOUNT_NUMBER: Số tài khoản ngân hàng
  • • YOUR_ACCOUNT_NAME: Tên chủ tài khoản
  • • BANK_ID: Mã ngân hàng (VCB, TCB, ACB, etc.)

Bước 2: Cấu hình Systeme.io

1. Đăng nhập vào Systeme.io

2. Vào Settings → API để lấy API Key

3. Tạo Tag để gắn cho khách hàng mua ebook

4. Cập nhật trong plugin:

  • • YOUR_SYSTEME_IO_API_KEY: API Key từ Systeme.io
  • • YOUR_TAG_ID: ID của tag đã tạo

Bước 3: Cài đặt Plugin WordPress

1. Tạo file sepay-ebook-plugin.php trong thư mục /wp-content/plugins/sepay-ebook/

2. Kích hoạt plugin trong WordPress Admin

3. Tạo file JavaScript sepay-frontend.js và enqueue vào theme

4. Sử dụng shortcode [sepay_ebook_form] để hiển thị form

5. Cấu hình link download ebook trong hàm send_ebook_email()

Bảo mật và Testing

1. Verify webhook signature từ Sepay (nếu có)

2. Sử dụng HTTPS cho website

3. Test với số tiền nhỏ trước khi ra mắt

4. Backup database thường xuyên

5. Monitor logs để phát hiện lỗi

Database Schema

CREATE TABLE wp_sepay_transactions ( 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’, sepay_data TEXT, 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), INDEX status_idx (status), INDEX email_idx (customer_email) );

Tính năng đã tích hợp

Form nhập thông tin khách hàng

Tên, email với validation

Tích hợp Sepay.vn API

Tạo mã QR thanh toán tự động

Webhook handler

Nhận thông báo thanh toán thành công

Database logging

Lưu trữ tất cả giao dịch

Auto email delivery

Gửi ebook sau khi thanh toán

Systeme.io integration

Tự động thêm contact và tag

WordPress plugin

Dễ dàng cài đặt và sử dụng

Security & Validation

Nonce, sanitization, verification