PHP code example of ngandu-dev / flexpay

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

    

ngandu-dev / flexpay example snippets




use Ngandu\Flexpay\BaseUrl;
use Ngandu\Flexpay\Client;
use Ngandu\Flexpay\Credential;

$flexpay = new Client(
    credential: new Credential(merchant: 'merchant_code', token: 'authorization_token'),
    baseUrl: BaseUrl::production(),
);

$sandboxFlexpay = new Client(
    credential: new Credential(merchant: 'TESTMERCHANT', token: 'test-token'),
    baseUrl: BaseUrl::sandbox('https://your-flexpay-sandbox.example/api/rest/v1'),
);

use Ngandu\Flexpay\Data\Currency;
use Ngandu\Flexpay\Request\MobileRequest;

$mobile = $flexpay->mobile(new MobileRequest(
    amount: 10,
    reference: 'invoice-123',
    currency: Currency::USD,
    callbackUrl: 'https://merchant.example/callback',
    phone: '243999999999',
));

if (!$mobile->isAccepted()) {
    // Record $mobile->message and $mobile->code->value for reconciliation.
}

use Ngandu\Flexpay\Data\Currency;
use Ngandu\Flexpay\Request\CardRequest;

$card = $flexpay->card(new CardRequest(
    amount: 10,
    reference: 'invoice-124',
    currency: Currency::USD,
    callbackUrl: 'https://merchant.example/callback',
    approveUrl: 'https://merchant.example/approved',
    cancelUrl: 'https://merchant.example/cancelled',
    declineUrl: 'https://merchant.example/declined',
));

if ($card->isAccepted() && $card->url !== null && $card->url !== '') {
    // Use $card->url to continue checkout under your application's redirect policy.
}

use Ngandu\Flexpay\Data\Currency;
use Ngandu\Flexpay\Request\PayoutRequest;

$payout = $flexpay->payout(new PayoutRequest(
    amount: 10,
    reference: 'payout-123',
    currency: Currency::USD,
    callbackUrl: 'https://merchant.example/payout-callback',
    phone: '243999999999',
));

$payout->isAccepted();

if ($mobile->orderNumber === null || trim($mobile->orderNumber) === '') {
    throw new RuntimeException('FlexPay did not return an order number');
}

$check = $flexpay->check($mobile->orderNumber);
$successful = $check->isSuccessful();
$lookupCode = $check->code->value;
$transactionCode = $check->transaction?->status->value;

use Ngandu\Flexpay\Response\CallbackResponse;

$payload = json_decode(file_get_contents('php://input'), true, flags: JSON_THROW_ON_ERROR);
if (!is_array($payload)) {
    throw new InvalidArgumentException('Expected a callback object');
}

$callback = CallbackResponse::fromPayload($payload);
$callback->isSuccessful();
$callback->providerReference; // provider_reference, when supplied