PHP code example of gatepay / core

1. Go to this page and download the library: Download gatepay/core 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/ */

    

gatepay / core example snippets



// That's it. Seriously.

$gateway = $registry->get('PayPal');
$processor = $gateway->process($transaction, $httpFactory, $httpClient);
// Handle result with clean states, no magic strings
// maybe in async process or webhook handler?
match ($transaction->getState()) {
    TransactionState::SUCCESS => handleSuccess($transaction),
    TransactionState::ERROR   => handleError($transaction),
    TransactionState::PENDING => handlePending($transaction),
    TransactionState::BEGIN   => handleProcessing($transaction),
};

// Guzzle? Sure.
$client = new GuzzleHttp\Client();

// Symfony HTTP Client? No problem.
$client = new Symfony\Component\HttpClient\Psr18Client();

// Your custom client? Go ahead.
$client = new YourAwesomeHttpClient();

// GatePay doesn't care. It just works.
$gateway->process($transaction, $factory, $client);

// Before (every gateway is different)

if ($response['status'] === 'PAID') { }      // Gateway A
if ($response['state'] === 1) { }            // Gateway B  
if ($response['result'] === 'success') { }   // Gateway C
if ($response['code'] === '00') { }          // Gateway D 🤮

// After (GatePay)
// The Transaction object is the single source of truth for your transaction's state
if ($transaction->getState() === TransactionState::SUCCESS) {
    // TransactionResponseInterface
    // ALL You need here:
    // 1. Transaction: The original transaction object with all your parameters
    // 2. Transaction Status: GatePay\Core\Enum\TransactionState
    // 3. PSR Response: Psr\Http\Message\ResponseInterface -> Your gateway's raw response, YOU NEED IT!!
    $transactionResponse = $transaction->getResponse();
}

// That's it. For ALL gateways.
// Note: If the payment gateway developer follow the standard!


// Transaction Object = Owner of the entire transaction lifecycle
$my_transaction = new Transaction(...);

$processor = $gateway->process($my_transaction, $httpFactory, $httpClient);

$my_transaction->getState(); // PENDING, SUCCESS, ERROR
$processor->getTransaction() === $my_transaction; // True, it's the same object
// Debug like a pro, not like a detective

// Create a new gateway in minutes, not days
class MyGateway extends AbstractGateway
{
    protected string $name = "MyGateway";
    protected array $actions = [
        GatewayAction::CHARGE->value => ChargeAction::class,
        GatewayAction::REFUND->value => RefundAction::class,
    ];
}

// That's your gateway. Register and use.
$registry->add(new MyGateway());
// and add the alias if you want
$registry->addAlias('MyGW', MyGateway::class);


declare(strict_types=1);

use GatePay\Core\Enum\GatewayAction;
use GatePay\Core\Enum\TransactionState;
use GatePay\Core\GatewayRegistry;
use GatePay\Core\Transaction;
use GatePay\Core\Utils\ReferenceOrderId;

// the registry centralizes all your gateways, you can also add alias for easier access
$registry = new GatewayRegistry();
// .... any gateway registration here,
// you can also register your gateway in a service provider or bootstrap file, it's up to you

// 1️⃣ Generate unique order ID (k-sortable, prefixed) - always 30 characters, perfect for payment systems
$orderIdGen = new ReferenceOrderId('PYMT');
$orderId = $orderIdGen->generate(); 
// → PYMT-019d43d20eb8-6a5a7925dfb5

// 2️⃣ Create transaction with clear parameters
$transaction = new Transaction(
    transactionId: $orderId,
    action: GatewayAction::CHARGE,
    parameters: [
        'amount' => 100000,
        'currency' => 'IDR',
        'card_number' => '4111111111111111',
    ]
);

// 3️⃣ Setup (use any PSR-18 client & PSR-17 factory)
$httpClient = new GuzzleHttp\Client();
$httpFactory = new GuzzleHttp\Psr7\HttpFactory();

// 4️⃣ Process - one line, any gateway
$gateway = $registry->get('MyGateway');
if (!$gateway->hasAction($transaction->getAction())) {
    // maybe log or throw custom exception,
    // but the point is you don't need to check for null or catch exception just to check if the gateway support the action,
    // you can just do logic here and let the gateway handle it, it's more clean and less error prone
    return;
}

$processor = $gateway->process($transaction, $httpFactory, $httpClient);

// 5️⃣ Handle result - clean, predictable states
match ($transaction->getState()) {
    TransactionState::SUCCESS => fn() => saveSuccess($transaction->getTransactionResultData()),
    TransactionState::ERROR   => fn() => logError($transaction->getError()),
    TransactionState::PENDING => fn() => queueForPolling($transaction),
    TransactionState::BEGIN   => fn() => handleProcessing($transaction),
};

$gen = new ReferenceOrderId('INVX');
$id = $gen->generate(); // INVX-019d43d20eb8-6a5a7925dfb5

$result = XMLParserArray::parse($xmlString);
// Same output format, regardless of available extensions