Webhooks Programming

Program a simple webhook URL to receive and store transaction information from SePay.


  • This article will guide you to program a simple website to receive and store transactions sent by SePay. The programming language used is PHP and the database is MySQL.
Information

If you are using Laravel, please use the pre-built SePay package here.

Step 1: Create a database and assign permissions.

  • Create a database named webhooks_receiver, MySQL user webhooks_receiver with password EL2vKpfpDLsz.
  • Access the MySQL Command Line and execute the following commands:
MySQL
create database webhooks_receiver; 
create user 'webhooks_receiver'@'localhost' identified by 'EL2vKpfpDLsz'; 
grant all privileges on webhooks_receiver.* to  'webhooks_receiver'@'localhost' identified by 'EL2vKpfpDLsz';
Note

For security reasons, you should change EL2vKpfpDLsz to a different password.

Step 2: Create a table to store transaction information.

  • Execute the table creation command in MySQL Command Line:
MySQL
create database webhooks_receiver;
create user 'webhooks_receiver'@'localhost' identified by 'EL2vKpfpDLsz';
grant all privileges on webhooks_receiver.* to  'webhooks_receiver'@'localhost' identified by 'EL2vKpfpDLsz';

use webhooks_receiver;
CREATE TABLE `tb_transactions` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `gateway` varchar(100) NOT NULL,
    `transaction_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    `account_number` varchar(100) DEFAULT NULL,
    `sub_account` varchar(250) DEFAULT NULL,
    `amount_in` decimal(20,2) NOT NULL DEFAULT 0.00,
    `amount_out` decimal(20,2) NOT NULL DEFAULT 0.00,
    `accumulated` decimal(20,2) NOT NULL DEFAULT 0.00,
    `code` varchar(250) DEFAULT NULL,
    `transaction_content` text DEFAULT NULL,
    `reference_number` varchar(255) DEFAULT NULL,
    `body` text DEFAULT NULL,
    `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb3;

Step 3: Create the PHP file that SePay will call.

  • The content of the receiver.php file is as follows:
Webhook Receiver
<?php
    $servername = 'localhost';
    $username = 'webhooks_receiver';
    $password = 'EL2vKpfpDLsz';
    $dbname = 'webhooks_receiver';

    // Connect to MySQL
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        echo json_encode(['success'=>FALSE, 'message' => 'MySQL connection failed: '. $conn->connect_error]);
        die();
    }

    // Get data from webhook
    $data = json_decode(file_get_contents('php://input'));

    if(!is_object($data)) {
        echo json_encode(['success'=>FALSE, 'message' => 'No data']);
        die();
    }

    // Initialize variables
    $gateway = $data->gateway;
    $transaction_date = $data->transactionDate;
    $account_number = $data->accountNumber;
    $sub_account = $data->subAccount;

    $transfer_type = $data->transferType;
    $transfer_amount = $data->transferAmount;
    $accumulated = $data->accumulated;

    $code = $data->code;
    $transaction_content = $data->content;
    $reference_number = $data->referenceCode;
    $body = $data->description;

    $amount_in = 0;
    $amount_out = 0;

    if($transfer_type == 'in')
        $amount_in = $transfer_amount;
    else if($transfer_type == 'out')
        $amount_out = $transfer_amount;

    $sql = "INSERT INTO tb_transactions (gateway, transaction_date, account_number, sub_account, amount_in, amount_out, accumulated, code, transaction_content, reference_number, body) VALUES ('{$gateway}', '{$transaction_date}', '{$account_number}', '{$sub_account}', '{$amount_in}', '{$amount_out}', '{$accumulated}', '{$code}', '{$transaction_content}', '{$reference_number}', '{$body}')";

    if ($conn->query($sql) === TRUE) {
        echo json_encode(['success'=>TRUE]);
    } else {
        echo json_encode(['success'=>FALSE, 'message' => 'Can not insert record to mysql: ' . $conn->error]);
    }
?>

Step 4: Add a new Webhook in the WebHooks menu.

Step 5: Create a simulated transaction

  • Create a simulated transaction by logging into your Demo account under Transactions → Simulate Transaction. Select the correct bank account associated with the webhook you created.

Step 6: View results

  • After creating the simulated transaction, go to Transactions → Click the Pay icon under the Auto column to view the WebHooks results, or visit WebHooks Log.

Step 7: Verify data

  • Check if the data has been stored in the database using the following queries:
MYSQL
use webhooks_receiver;
select * from tb_transactions \G;
Note

The example above does not authenticate the source call. For security, you should whitelist SePay’s IP addresses or use an authentication method. For API Key, verify that SePay sends the correct API Key in the header.