PHP code example of sevaske / payfort-api

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

    

sevaske / payfort-api example snippets


use Sevaske\PayfortApi\Credential;
use Sevaske\PayfortApi\Merchant;
use Sevaske\PayfortApi\Http\Responses\CheckStatusResponse;

// Setup credentials
$credential = new Credential(
    'merchantIdentifier',
    'accessCode',
    'shaRequestPhrase',
    'shaResponsePhrase',
);

// Use any PSR-18 compatible HTTP client
$httpClient = new \GuzzleHttp\Client(); // or any other PSR-18 client

// Create merchant instance
$merchant = new Merchant('sandbox', $httpClient, $credential);

// Simple status check
$response = $merchant->api()->checkStatus('12345'); // CheckStatusResponse

$merchant->api()->checkStatus(); // Check the status of a transaction
$merchant->api()->recurring(); // Purchase
$merchant->api()->voidAuthorization(); // Void an authorization
$merchant->api()->refund(); // Process a refund
$merchant->api()->createToken(); // Create a new payment token
$merchant->api()->updateToken(); // Update an existing payment token

$response = $merchant->api()->checkStatus('12345', callback: function (
    CheckStatusResponse $response,
    array $request
) {
    $request;                      // Raw request data
    $response->jsonSerialize();    // Parsed response as array
    $response->authorizedAmount(); // ?string
    $response->capturedAmount();   // ?string
    $response->refundedAmount();   // ?string
    
    return $response;
});

$response = $merchant->api()->request([
    'command' => 'CUSTOM',
]); // Returns generic Response

use Sevaske\PayfortApi\Exceptions\PayfortException;
use Sevaske\PayfortApi\Exceptions\PayfortRequestException;
use Sevaske\PayfortApi\Exceptions\PayfortResponseException;
use Sevaske\PayfortApi\Exceptions\PayfortSignatureException;

try {
    $response = $merchant->api()->refund('12345', 1000, 'SAR'); // RefundResponse
} catch (PayfortRequestException $e) {
    // Request could not be sent
} catch (PayfortResponseException $e) {
    // Received an invalid response
} catch (PayfortSignatureException $e) {
    // Signature mismatch
}

// or "catch-all" exception handling
try {
    $response = $merchant->api()->refund('12345', 1000, 'SAR');
} catch (PayfortException $e) {
    $context = $e->context();
}

$merchant->redirection()->...; // Example of future functionality