PHP code example of webtoucher / omnipay-platbox

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

    

webtoucher / omnipay-platbox example snippets


    $gateway = \Omnipay\Omnipay::create('PlatBox');
    $gateway->setMerchantId('[MERCHANT_ID]');
    $gateway->setSecretKey('[SECRET_KEY]');
    $gateway->setProject('[PROJECT]');
    // $gateway->setTestMode(true);

    $request = $gateway->purchase([
        'order_id' => $orderId,
        'amount' => $amount,
        'currency' => 'RUB',
        'account_id' => $userId,
        'phone_number' => $phone,
    ]);
    $response = $request->send();
    $result = $response->isSuccessful();

    try {
        $data = json_decode(file_get_contents('php://input'), true);
    } catch (\Exception $e) {
        $data = [];
    }
    $action = isset($data['action']) ? $data['action'] : null;
    switch ($action) {
        case 'check':
            $request = $gateway->check($data);
            handleCallback($request, $failCallback);
            break;
        case 'pay':
            $request = $gateway->completePurchase($data);
            handleCallback($request, $failCallback, $successCallback);
            break;
        case 'cancel':
            $request = $gateway->completePurchase($data);
            handleCallback($request, $failCallback, $cancelCallback);
            break;
        default:
            // wrong request handler
    }

    function handleCallback($request, $failCallback = null, $completeCallback = null) {
        try {
            $orderId = $request->getOrderId();
            $order = [...]; // find order model by order ID.
            if (!$order) {
                PlatBoxException::throwException(PlatBoxException::CODE_ORDER_NOT_FOUND_OR_BAD_DATA);
            }
            // Check order status
            if ($order->status = [...]) { // already paid
                PlatBoxException::throwException(PlatBoxException::CODE_PAYMENT_ALREADY_COMPLETED);
            }
            if ($order->status = [...]) { // already cancelled
                PlatBoxException::throwException(PlatBoxException::CODE_PAYMENT_ALREADY_CANCELED);
            }
    
            $request->setMerchantOrderId($order->id);
            $request->setMerchantAmount($order->amount);
            $request->setMerchantCurrency($order->currency);
    
            $responseData = $response->getData();
            $response->confirm();
            if (is_callable($successCallback)) {
                call_user_func($successCallback, $response);
            }
        } catch (PlatBoxException $e) {
            if (is_callable($failCallback)) {
                call_user_func($failCallback, $response);
            }
            $request->error($e->getMessage(), $e->getCode());
        } catch (\Exception $e) {
            if (is_callable($failCallback)) {
                call_user_func($failCallback, $response);
            }
            $request->error();
        }
    }

$ php composer.phar