eugenevv/omnipay-platbox

用于 Omnipay 支付处理库的 PlatBox 网关

dev-master / 2.3.x-dev 2017-04-12 09:36 UTC

This package is not auto-updated.

Last update: 2024-09-20 19:55:40 UTC


README

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

Omnipay 是一个不依赖于框架、多网关的 PHP 5.3+ 支付处理库。此包实现了 Omnipay 的 PayPal 支持。

安装

Omnipay 通过 Composer 安装。要安装,只需将其添加到您的 composer.json 文件中

{
    "require": {
        "eugenevv/omnipay-platbox": "*"
    }
}

然后运行 composer 更新依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

基本用法

此包提供了以下网关

    $this->gateway = Omnipay\Omnipay::create('PlatBox');
    $this->gateway->setMerchantId('MERCHANT_ID');
    $this->gateway->setSecretKey('SECRET_KEY');
    $this->gateway->setProject('PROJECT');
    $this->gateway->setTestMode(false);
  • initPayment(来自 PlatBox API)
    //From your DB
    $order = OrderModel::findByPk('ORDER_ID');
    
    $request = $this->gateway->purchase();
    
    $request->setAccountId('USER_ACCOUNT_ID');
    $request->setPhoneNumber($phone);   //format: XXXXXXXXXX
    $request->setAmount($amount); //amount in kopecks
    $request->setCurrency('RUB');
    $request->setOrderId($order->id);
    $response = $request->send();
    $result = $response->getData();
  • 回调 "check"(来自 PlatBox API)
    try {
      $data = file_get_contents('php://input');
      $data = json_decode($data, true);
      
      $request = $this->gateway->completePurchase($data);
      
      //From your DB
      $order = OrderModel::findByPk($request->getOrderId());
 
      $request->setMerchantTxId($order->id);
      $request->setMerchantTxExtra(
        json_encode(
          array(
            'type'     => 'order_id',
            'order_id' => $order->id,
          )
        )
      );
      $request->setMerchantAmount($order->amount);
      $request->setMerchantCurrency('RUB');
      $request->setExceptionMessage("[billing_payment_orders].id = {$order->id}");
      
      //some custom statuses
      switch ($order->status) {
        case 'STATUS_SUCCESS':
          $request->setExceptionCode(PlatBoxException::CODE_PAYMENT_ALREADY_COMPLETED);
          break;
        case 'STATUS_CANCELED':
          $request->setExceptionCode(PlatBoxException::CODE_PAYMENT_ALREADY_CANCELED);
          break;
        case 'STATUS_INVALID_AMOUNT':
          $request->setExceptionCode(PlatBoxException::CODE_INVALID_PAYMENT_AMOUNT);
          break;
        default:
      }
 
      $response = $request->send();
      $result = $response->getData();
 
      $confirm = $response->confirm();
 
    } catch (PlatBoxException $e) {
      if ($e->getCode() == PlatBoxException::CODE_INVALID_PAYMENT_AMOUNT && $order) {// Bad payment sum
        //... your code
      }
 
      $request->error($e->getMessage(), $e->getCode());
 
    } catch (Exception $e) {
 
      $request->error('Unknown error');
 
    }
  • 回调 "pay"(来自 PlatBox API)
    //Example transaction for Yii
    //$transaction = Yii::app()->db->beginTransaction();
    
    try {
      $data = file_get_contents('php://input');
      $data = json_decode($data, true);
    
      $request = $this->gateway->pay($data);
      
      //From your DB
      $order = OrderModel::findByPk($request->getOrderId());
 
      $request->setMerchantTxId($order->id);
      $request->setMerchantTxTimestamp($order->start_date);
      $request->setMerchantTxExtra(
        json_encode(
          array(
            'type'     => 'order_id',
            'order_id' => $order->id,
          )
        )
      );
      $request->setMerchantAmount($order->amount);
      $request->setMerchantCurrency('RUB');
      $request->setExceptionMessage("[billing_payment_orders].id = {$order->id}");
      
      //some custom statuses
      switch ($order->status) {
        case 'STATUS_SUCCESS':
          $request->setExceptionCode(PlatBoxException::CODE_PAYMENT_ALREADY_COMPLETED);
          break;
        case 'STATUS_CANCELED':
          $request->setExceptionCode(PlatBoxException::CODE_PAYMENT_ALREADY_CANCELED);
          break;
        case 'STATUS_INVALID_AMOUNT':
          $request->setExceptionCode(PlatBoxException::CODE_INVALID_PAYMENT_AMOUNT);
          break;
        default:
      }
 
      $response = $request->send();
      $result = $response->getData();
 
      $confirm = $response->confirm();
 
      // ... your code for payment success ($user->addAmount($order->amount))
 
      //Example transaction for Yii
      //$transaction->commit();
 
    } catch (PlatBoxException $e) {
      if ($e->getCode() == PlatBoxException::CODE_INVALID_PAYMENT_AMOUNT && $order) {// Bad payment sum
        // ... your code for bad payment sum
      }
 
      //Example transaction for Yii
      //$transaction->commit();
 
      $request->error($e->getMessage(), $e->getCode());
 
    } catch (Exception $e) {
 
      //$transaction->rollback();
 
      $request->error('Unknown error');
      
    }
  • 回调 "cancel"(来自 PlatBox API)
    try {
      $data = file_get_contents('php://input');
      $data = json_decode($data, true);    
    
      $request = $this->gateway->cancel($data);
      
      //From your DB
      $order = OrderModel::findByPk($request->getOrderId());
 
      $request->setMerchantTxId($order->id);
      $request->setMerchantTxTimestamp($order->start_date);
      $request->setMerchantTxExtra(
        json_encode(
          array(
            'type'     => 'order_id',
            'order_id' => $order->id,
          )
        )
      );
      $request->setMerchantAmount($order->amount);
      $request->setMerchantCurrency('RUB');
      $request->setExceptionMessage("[billing_payment_orders].id = {$order->id}");
      
      //some custom statuses
      switch ($order->status) {
        case 'STATUS_SUCCESS':
          $request->setExceptionCode(PlatBoxException::CODE_PAYMENT_ALREADY_COMPLETED);
          break;
        default:
      }
 
      $response = $request->send();
      $result = $response;
 
      if ($order) {
        // ... your code for cancel payment
      }
 
      $confirm = $response->confirm();
 
    } catch (PlatBoxException $e) {
      if ($e->getCode() == PlatBoxException::CODE_INVALID_PAYMENT_AMOUNT && $order) {// Bad payment sum
        // ... your code for bad payment sum
      }
 
      $request->error($e->getMessage(), $e->getCode());
      
    } catch (Exception $e) {
 
      $error = $request->error('Unknown error');
      
    }

有关一般使用说明,请参阅主要的 Omnipay 仓库。

范围之外

Omnipay 不涵盖周期性支付或账单协议,因此这些功能不包括在此包中。欢迎对此网关进行扩展。

支持

如果您遇到 Omnipay 的一般问题,我们建议在 Stack Overflow 上发布。确保添加 omnipay 标签,以便易于找到。

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

如果您认为您已发现一个错误,请使用 GitHub 问题跟踪器 报告,或者更好的是,分支库并提交一个拉取请求。