PHP SDK
Official PHP SDK for SePay Payment Gateway. Easily integrate payments, bank transfers, VietQR and recurring payments.
PHP 7.4 or higher, ext-json, ext-curl, Guzzle HTTP client
Installation
composer require sepay/sepay-pg
Initialize Client
use SePay\SePayClient;use SePay\Builders\CheckoutBuilder;// Initialize client$sepay = new SePayClient('SP-TEST-XXXXXXX','spsk_live_xxxxxxxxxxxo99PoE7RsBpss3EFH5nV',SePayClient::ENVIRONMENT_SANDBOX, // or ENVIRONMENT_PRODUCTION[]);
Parameter explanation
Example of other configurations:
['timeout' => 60, // Request timeout (in seconds)'retry_attempts' => 5, // Number of retries when request fails'retry_delay' => 2000, // Delay between retries (in milliseconds)'debug' => true, // Enable debug mode (detailed logging)'user_agent' => 'MyApp/1.0 SePay-PHP-SDK/1.0.0', // Application identifier string sent in request'logger' => $customLogger, // PSR-3 compatible logger];
Initialize Payment Form Object (One-time Payment Order)
$checkoutData = CheckoutBuilder::make()->currency('VND')->orderAmount(100000) // 100,000 VND->operation('PURCHASE')->orderDescription('Test payment')->orderInvoiceNumber('INV_001')->successUrl('https://yoursite.com/success')->build();// Create form fields with signature$formFields = $sepay->checkout()->generateFormFields($checkoutData);
Attribute explanation
Create payment processing form
If you build your own HTML form and signature generation function not following the sample code, you must ensure the field order matches the parameter list above so the signing process matches exactly with SePay; swapping field positions may cause invalid signature and SePay will consider the request invalid
<form method="POST" action="https://pay-sandbox.sepay.vn/checkout/init"><?php foreach ($formFields as $name => $value): ?><input type="hidden" name="<?= htmlspecialchars($name) ?>" value="<?= htmlspecialchars($value) ?>"><?php endforeach; ?><button type="submit">Pay now</button></form>
Enable debug mode: $sepay->enableDebugMode(); - Configure retry behavior: $sepay->setRetryAttempts(3)->setRetryDelay(1000);
API
SDK provides methods to call Open API for SePay payment gateway.
$orders = $sepay->orders()->list(['per_page' => 10,'order_status' => 'CAPTURED','from_created_at' => '2025-01-01','to_created_at' => '2025-12-31',]);
$order = $sepay->orders()->retrieve('ORDER_INVOICE_NUMBER');
$result = $sepay->orders()->voidTransaction('ORDER_INVOICE_NUMBER');
Orders are created when customers complete payment, not directly through API.
Error Handling
The SDK has different exception types for different errors
use SePay\Exceptions\AuthenticationException;use SePay\Exceptions\ValidationException;use SePay\Exceptions\NotFoundException;use SePay\Exceptions\RateLimitException;use SePay\Exceptions\ServerException;try {$order = $sepay->orders()->retrieve('ORDER_INVOICE_NUMBER');} catch (AuthenticationException $e) {// Invalid credentials or signatureecho "Authentication failed: " . $e->getMessage();} catch (ValidationException $e) {// Invalid request dataecho "Validation error: " . $e->getMessage();// Get field-specific errorsif ($e->hasFieldError('amount')) {$errors = $e->getFieldErrors('amount');echo "Amount errors: " . implode(', ', $errors);}} catch (NotFoundException $e) {// Resource not foundecho "Not found: " . $e->getMessage();} catch (RateLimitException $e) {// Rate limit exceededecho "Rate limited. Retry after: " . $e->getRetryAfter() . " seconds";} catch (ServerException $e) {// Server error (5xx)echo "Server error: " . $e->getMessage();}
Testing
# Run all tests composer test # Run tests with coverage report composer test-coverage # Static code analysis composer phpstan # Fix code style composer cs-fix
See detailed installation and usage guide at GitHub Repository