PHP code example of sepayvn / laravel-sepay

1. Go to this page and download the library: Download sepayvn/laravel-sepay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sepayvn / laravel-sepay example snippets


return [
    'webhook_token' => env('SEPAY_WEBHOOK_TOKEN'),
    'pattern' => env('SEPAY_MATCH_PATTERN', 'SE'),
];



namespace App\Listeners;

use App\Models\User;
use SePay\SePay\Events\SePayWebhookEvent;
use SePay\SePay\Notifications\SePayTopUpSuccessNotification;

class SePayWebhookListener
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(SePayWebhookEvent $event): void
    {
        // Xử lý tiền vào tài khoản
        if ($event->sePayWebhookData->transferType === 'in') {
            // Trường hợp $info là user id
            $user = User::query()->where('id', $event->info)->first();
            if ($user instanceof User) {
                $user->notify(new SePayTopUpSuccessNotification($event->sePayWebhookData));
            }
        } else {
            // Xử lý tiền ra tài khoản
        }
    }
}

        protected $listen = [
            ...
            \SePay\SePay\Events\SePayWebhookEvent::class => [
                \App\Listeners\SePayWebhookListener::class,
            ],
        ];
    

    public function boot(): void
    {
        \Illuminate\Support\Facades\Event::listen(
            \SePay\SePay\Events\SePayWebhookEvent::class,
            \App\Listeners\SePayWebhookListener::class,
        );
    }
    
bash
php artisan vendor:publish --tag="sepay-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="sepay-config"
bash
php artisan vendor:publish --tag="sepay-views"
bash
php artisan make:listener SePayWebhookListener
bash
curl --location 'https://domain.com/api/sepay/webhook' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer Apikey đây_là_khóa_bí_mật' \
--data '{
    "gateway": "MBBank",
    "transactionDate": "2024-05-25 21:11:02",
    "accountNumber": "0359123456",
    "subAccount": null,
    "code": null,
    "content": "Thanh toan QR SE123456",
    "transferType": "out",
    "description": "Thanh toan QR SE123456",
    "transferAmount": 1700000,
    "referenceCode": "FT123456789",
    "accumulated": 0,
    "id": 123456
}'