camoo/enkap-oauth

SmobilPay电子商务插件。OAuth库

安装: 60

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:camoo-plugin

2.0.0 2023-07-21 10:58 UTC

This package is auto-updated.

Last update: 2024-09-21 13:21:11 UTC


README

为e-nkap提供的SDK。SmobilPay电子商务

注意 仅使用此分支与PHP 8.1及以上版本

注意 与PHP 8.1以下版本兼容性维护在v1.0分支中

安装

composer require camoo/enkap-oauth

用法

use Enkap\OAuth\Services\OAuthService;

$consumerKey = 'hqBvUfOjdLoP04763L_LDO';
$consumerSecret = 'FwxKTJzN4jE8IYdeCM83';

$service = new OAuthService($key, $secret);

var_dump($service->getAccessToken());

发起支付

use Enkap\OAuth\Services\OAuthService;
use Enkap\OAuth\Model\Order;
use Enkap\OAuth\Services\OrderService;
use Enkap\OAuth\Lib\Helper;

$consumerKey = 'hqBvUfOjdLoP04763L_LDO';
$consumerSecret = 'FwxKTJzN4jE8IYdeCM83';

$orderService = new OrderService($key, $secret);
$order = $orderService->loadModel(Order::class);
$dataData = [
    'merchantReference' => uniqid('', true),
    'email' => 'enkap@mail.tld',
    'customerName' => 'My customer',
    'totalAmount' => 6400,
    'description' => 'Camoo Test Payment',
    'currency' => 'XAF',
    'items' => [
        [
            'itemId' => '1',
            'particulars' => 'soya bien pimenté',
            'unitCost' => 100,
            'quantity' => 50,
            'subTotal' => 5000
        ],
        [
            'itemId' => 2,
            'unitCost' => 700,
            'quantity' => 2,
            'particulars' => 'Bière 33 Export',
            'subTotal' => 1400,
        ]
    ]
];

try {
    $order->fromStringArray($dataData);
    $response = $orderService->place($order);
     // Save references into your Database
     $entity = $this->Payments->newEntity($dataData);
     $entity->set('oder_transaction_id', $response->getOrderTransactionId());
     $this->Payments->save($entity);

     // redirect User to Enkap System
     Helper::redirect($response->getRedirectUrl());
     
} catch (\Throwable $e) {
    var_dump($e->getMessage());
}

获取支付详情

use Enkap\OAuth\Services\PaymentService;
use Enkap\OAuth\Model\Payment;

$consumerKey = 'hqBvUfOjdLoP04763L_LDO';
$consumerSecret = 'FwxKTJzN4jE8IYdeCM83';

$trxId = 'e07355446e0140ea9876a6ba38b155f3';
$paymentService = new PaymentService($key, $secret);
$payment = $paymentService->getByTransactionId($trxId);
// status
var_dump($payment->getPaymentStatus());
// order
var_dump($payment->getOrder());
# OR
$internalTrxId = '61405dc1a38878.58742206';
$paymentService = new PaymentService($key, $secret);
$payment = $paymentService->getByOrderMerchantId($internalTrxId);

// status
var_dump($payment->getPaymentStatus());
// order
var_dump($payment->getOrder());

检查支付状态

use Enkap\OAuth\Services\StatusService;
use Enkap\OAuth\Lib\Helper;
use Enkap\OAuth\Model\Status;

$consumerKey = 'hqBvUfOjdLoP04763L_LDO';
$consumerSecret = 'FwxKTJzN4jE8IYdeCM83';

$trxId = 'e07355446e0140ea9876a6ba38b155f3';
$statusService = new StatusService($key, $secret);
$status = $statusService->getByTransactionId($trxId);
// Update your database
$query = $this->Payments->query()->set(['status' => $status->getCurrent()])->where(['oder_transaction_id' => $trxId]);
if ($status->confirmed()){
    // Payment successfully completed
    // send Item to user/customer
    return;
}

if ($status->failed() || $status->canceled()) {
  // delete that reference from your Database
}

设置回调URL以自动接收支付状态

use Enkap\OAuth\Services\CallbackUrlService;
use Enkap\OAuth\Model\CallbackUrl;

$setup = new CallbackUrlService($key, $secret);
$callBack = $setup->loadModel(CallbackUrl::class);
# The URL where to redirect the user after the payment is completed. It will contain the reference id generated by your system which was provided in the initial order placement request. E-nkap will append your reference id in the path of the URL with the form: https:///action/return/{yourReferenceId}
$callBack->return_url = 'https:///action/return';

# The URL used by E-nkap to instantly notify you about the status of the payment. E-nkap would append your reference Id (generated by your system and provided in the initial order placement request) as path variable and send a PUT with the status of the payment in the body as {"status":"[txStatus]"}, where [txStatus] the payment status.
$callBack->notification_url = 'https:///action/notify'; // this action should accept PUT Request
$setup->set($callBack);

删除订单

use Enkap\OAuth\Services\OrderService;
use Enkap\OAuth\Model\Order;

$consumerKey = 'hqBvUfOjdLoP04763L_LDO';
$consumerSecret = 'FwxKTJzN4jE8IYdeCM83';

$trxId = 'e07355446e0140ea9876a6ba38b155f3';
$orderService = new OrderService($key, $secret);
$orderModel = $orderService->loadModel(Order::class);
$orderModel->order_transaction_id = $trxId;
$result = $orderService->delete($orderModel);
if ($result === true) {
  // order has been deleted
  //...
}