PHP code example of gembapay / gembapay-php

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

    

gembapay / gembapay-php example snippets


use GembaPay\GembaPay;

$gembapay = new GembaPay(
    apiKey: 'gembapay_test_your_key',  // test key for development
    webhookSecret: 'your_webhook_secret'
);

// Create a payment
$payment = $gembapay->createPayment(
    orderId: 'ORDER-123',
    amount: 100.00,
    currency: 'EUR'
);

echo $payment['paymentUrl'];
// → https://payment.gembapay.com/checkout/ORDER-123

echo implode(', ', $payment['allowedMethods']);
// → stripe, paypal

$payment = $gembapay->createPayment(
    orderId: 'ORDER-456',
    amount: 49.99,
    currency: 'USD',
    description: 'Premium Plan'
);

// Redirect customer to unified checkout (use paymentUrl as-is — it carries an unguessable token).
// The page follows the customer's browser language (en/bg/es) and device theme;
// to fix them: $payment['paymentUrl'] . '?lang=bg&theme=light'
header('Location: ' . $payment['paymentUrl']);
exit;

$status = $gembapay->getPaymentStatus('ORDER-456');

echo $status['status'];   // 'completed'
echo $status['network'];  // 'stripe' or 'paypal'

// webhook.php
$gembapay = new GembaPay(
    apiKey: $_ENV['GEMBAPAY_API_KEY'],
    webhookSecret: $_ENV['GEMBAPAY_WEBHOOK_SECRET']
);

try {
    $event = $gembapay->parseWebhookFromGlobals();

    if ($event['event'] === 'payment.completed') {
        $orderId = $event['payment']['orderId'];
        $amount = $event['payment']['usdAmount'];
        $method = $event['payment']['network'];

        // Fulfill the order
        fulfillOrder($orderId);
    }

    http_response_code(200);
    echo json_encode(['received' => true]);

} catch (\GembaPay\GembaPayException $e) {
    http_response_code(401);
    echo json_encode(['error' => $e->getMessage()]);
}

$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_GEMBAPAY_SIGNATURE'] ?? '';

if ($gembapay->verifyWebhook($payload, $signature)) {
    $data = json_decode($payload, true);
    // Process event...
}

$transactions = $gembapay->listTransactions();
$stats = $gembapay->getStats();

// config/services.php
'gembapay' => [
    'api_key' => env('GEMBAPAY_API_KEY'),
    'webhook_secret' => env('GEMBAPAY_WEBHOOK_SECRET'),
],

// app/Providers/AppServiceProvider.php
$this->app->singleton(GembaPay::class, function () {
    return new GembaPay(
        apiKey: config('services.gembapay.api_key'),
        webhookSecret: config('services.gembapay.webhook_secret')
    );
});

// In your controller
public function checkout(Request $request, GembaPay $gembapay)
{
    $payment = $gembapay->createPayment(
        orderId: $request->order_id,
        amount: $request->amount,
        currency: $request->currency ?? 'USD'
    );

    return redirect($payment['paymentUrl']);
}

$gembapay = new GembaPay(apiKey: 'gembapay_test_your_key');
var_dump($gembapay->isTestMode()); // true

new GembaPay(
    string $apiKey,               // Required
    ?string $webhookSecret = null, // For webhook verification
    ?string $baseUrl = null,       // Custom API URL
    int $timeout = 30              // Timeout in seconds
)
bash
composer