webtoucher/omnipay-platbox

Omnipay支付处理库的PlatBox驱动程序

1.0.0 2017-07-31 19:30 UTC

This package is auto-updated.

Last update: 2024-09-06 10:10:05 UTC


README

适用于Omnipay PHP支付处理库的PlatBox支付处理驱动程序。

Latest Stable Version Total Downloads Daily Downloads Latest Unstable Version License

安装

安装此库的首选方式是通过 composer

运行以下命令

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

或将以下内容

"webtoucher/omnipay-platbox": "*"

添加到您的 composer.json 文件的 require 部分。

使用方法

此包提供以下网关

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

第一步是准备数据并发送到PlatBox。

    $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
    }

存在回调请求的 'check' 处理程序。

    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();
        }
    }

有关通用使用说明,请参阅主要的 Omnipay 存储库。

支持

如果您在使用Omnipay时遇到一般问题,我们建议您在 Stack Overflow 上发表帖子。确保添加 omnipay 标签,以便易于查找。

如果您想了解发布公告,讨论项目想法或提出更详细的问题,还有一个您可以订阅的 邮件列表

如果您认为您已经发现了一个错误,请使用 GitHub问题跟踪器 报告。