PHP code example of rasedi / php-sdk

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

    

rasedi / php-sdk example snippets


use Rasedi\Sdk\PaymentClient;

$privateKey = "your_private_key_content"; // Or getenv('PRIVATE_KEY')
$secretKey = 'your_secret_key';

$client = new PaymentClient(
    privateKey: $privateKey,
    secretKey: $secretKey
);

use Rasedi\Sdk\Interfaces\ICreatePayment;
use Rasedi\Sdk\Enum\Gateway;

$payload = new ICreatePayment(
    amount: '10000', // Amount in smallest unit (e.g., cents/dinars)
    gateways: [Gateway::FIB, Gateway::ZAIN],
    title: 'Order #1234',
    description: 'Payment for digital goods',
    redirectUrl: 'https://your-site.com/return',
    callbackUrl: 'https://your-site.com/webhook',
    collectFeeFromCustomer: true,
    collectCustomerEmail: true,
    collectCustomerPhoneNumber: false
);

try {
    $response = $client->createPayment($payload);
    
    echo "Payment Created:\n";
    echo "Reference: " . $response->body->referenceCode . "\n";
    echo "Redirect URL: " . $response->body->redirectUrl . "\n";
    
    // Redirect user to $response->body->redirectUrl
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

$referenceCode = 'cf002d99-40e0-4dd3-9dd7-19e78333739f';
$details = $client->getPaymentByReferenceCode($referenceCode);

echo "Status: " . $details->body->status->value . "\n"; // e.g., PENDING, SUCCESS, FAILED

$referenceCode = 'cf002d99-40e0-4dd3-9dd7-19e78333739f';
$cancelResponse = $client->cancelPayment($referenceCode);

echo "New Status: " . $cancelResponse->body->status->value . "\n"; // CANCELLED

use Rasedi\Sdk\Interfaces\IVerifyPayload;

// Assuming you receive the payload as a JSON object or array
$payloadData = [
    'keyId' => '...',
    'content' => '...' // JWT-like signed content
];

try {
    // This will fetch public keys automatically if needed and verify the signature
    $verification = $client->verify($payloadData);
    
    // $verification->body contains the decoded payment update info
    $param = $verification->body;
    
    echo "Verified Update for: " . $param['referenceCode'] . "\n";
    echo "New Status: " . $param['status'] . "\n";
    
} catch (\Exception $e) {
    // Signature verification failed
    http_response_code(400);
    echo "Invalid signature";
}
bash
composer