webtoucher/omnipay-platbox

PlatBox driver for the Omnipay payment processing library

1.0.0 2017-07-31 19:30 UTC

This package is auto-updated.

Last update: 2024-11-06 10:28:19 UTC


README

PlatBox payment processing driver for the Omnipay PHP payment processing library.

Latest Stable Version Total Downloads Daily Downloads Latest Unstable Version License

Installation

The preferred way to install this library is through composer.

Either run

$ php composer.phar require webtoucher/omnipay-platbox "*"

or add

"webtoucher/omnipay-platbox": "*"

to the require section of your composer.json file.

Usage

The following gateways are provided by this package:

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

The first step is preparing data and sending to PlatBox.

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

There is the callback request handler.

    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
    }

There is the callback request 'check' 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();
        }
    }

For general usage instructions, please see the main Omnipay repository.

Support

If you are having general issues with Omnipay, we suggest posting on Stack Overflow. Be sure to add the omnipay tag so it can be easily found.

If you want to keep up to date with release anouncements, discuss ideas for the project, or ask more detailed questions, there is also a mailing list which you can subscribe to.

If you believe you have found a bug, please report it using the GitHub issue tracker.