netoxygen/proxypay

ProxyPay 库。提供生成 SHA 签名和检查 ProxyPay 响应的方法。

1.9.1 2022-10-24 16:05 UTC

This package is auto-updated.

Last update: 2024-09-24 19:59:37 UTC


README

Latest Stable Version Build Status Coverage Status

ProxyPay 库

提供生成 SHA 签名和检查 ProxyPay 响应的方法。

完整文档可在此处找到 here

要求

  • PHP 5.5+

安装

该库遵循 PSR-4 规范,最简单的安装方法是使用 composer

 composer require netoxygen/proxypay

描述

请求和响应签名

你的服务器与 ProxyPay 之间的所有通信都使用 HMAC SHA256 进行签名。

密钥

你已经获得了两个密钥

  • key_in:用于签名你的请求
  • key_out:用于检查 ProxyPay 响应签名

用法

为你的请求签名

你必须在请求中添加一个名为 sha_sign(或 SHA_SIGN)的参数。最简单签名请求的方式是使用 PaymentRequest 类。

use Netoxygen\ProxyPay\PaymentRequest;

/**
  * We assume that $_POST contains all your request parameters:
  * - seller
  * - amount
  * - description
  * - success_url
  * - error_url
  * - cancel_url
  *
  * All the other parameters will be filtered.
  */
$request = new PaymentRequest($_POST, 'my_key_in');

// You can loop over all your parameters in the $request
foreach ($request->get_parameters() as $key => $value) {
    echo $key . '=' . $value;
}

// You can retrieve the SHA_SIGN
$sha_sign = $request->compose_sha_sign();

检查 ProxyPay 响应签名

在处理过程的最后,你将收到来自 ProxyPay 的一个签名 GET 回调。为了确保请求被正确签名,只需使用 Paymentresponse 类。

use Netoxygen\ProxyPay\PaymentResponse;

$response = new PaymentResponse($_GET, 'my_key_out');
if ($response->is_valid()) {
    // $response signature is verified, now check the transaction status
    $params         = $response->get_parameters();
    $payment_status = $params['STATUS'];
    switch($payment_status) {
        case 'SUCCESS':
            // Complete the transaction on your side
            break;
  
        case 'CANCEL':
            // The transaction has been cancelled by the user
            break;
  
        case 'ERROR':   /* FALLTHROUGH */
        default:
            // An error occured
            break;
} else {
    // Bad request: throw away
}