PHP code example of oephpopen / allinpay-syb

1. Go to this page and download the library: Download oephpopen/allinpay-syb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oephpopen / allinpay-syb example snippets


[
    'ret'  => 1,        // 1 = 成功,0 = 失败
    'msg'  => 'ok',     // 对外提示语
    'data' => [...],    // 成功 = 网关返回字段;失败 = ['errcode' => 内部错误码, 'errmsg' => 内部详因]
]

use OephpOpen\AllinpaySyb\Config;
use OephpOpen\AllinpaySyb\Environment;
use OephpOpen\AllinpaySyb\SignType;

$config = (new Config([
    'cusid'                => 'CUSID',       // 商户号
    'appid'                => 'APPID',              // 平台分配 appid
    'orgid'                => '',                      // 集团/代理商商户号(单商户模式留空)
    'sign_type'            => SignType::RSA,           // RSA | RSA2 | SM2
    'merchant_private_key' => '-----BEGIN PRIVATE KEY-----\n...',  // 商户私钥 PEM
    'allinpay_public_key'  => '-----BEGIN PUBLIC KEY-----\n...',   // 通联平台公钥 PEM(验签)
    'sm4_key'              => '',                      // SM4 密钥(32 位 hex,敏感字段加密用)
    'env'                  => Environment::SANDBOX,    // sandbox | production
    'notify_url'           => 'https://example.com/notify.php/allinpay/paycall',
]));

use OephpOpen\AllinpaySyb\AllinpayClient;

$client = new AllinpayClient($config); // 可选:new AllinpayClient($config, $myHttpClient)

// 微信小程序支付(W06)
$result = $client->pay([
    'reqsn'      => 'M202608240001',   // 商户唯一单号
    'trxamt'     => 100,               // 金额:分
    'paytype'    => 'W06',
    'body'       => '会员服务',
    'acct'       => $wxOpenid,         // 小程序 openid
    'sub_appid'  => 'wx1234567890',    // 微信小程序 appid
    'notify_url' => 'https://example.com/notify.php/allinpay/paycall',
]);

if ($result['ret'] !== 1) {
    // 失败:$result['msg'] 对外提示;$result['data']['errcode'] / ['errmsg'] 内部排查
    return $result;
}
$payInfo = $result['data']['payinfo'] ?? '';   // 小程序调起支付参数(JSON 字符串)
$trxid   = $result['data']['trxid'] ?? '';

$result = $client->onepay([
    'reqsn'      => 'M202608240002',
    'trxamt'     => 100,
    'body'       => '会员服务',
    'front_url'  => 'https://example.com/ret',          // 同步跳转(https、无参数)
    'notify_url' => 'https://example.com/notify.php/allinpay/paycall',
]);
if ($result['ret'] !== 1) {
    return $result;
}
// 网关重定向:跳转 http_build_query($result['data']) 或按需拼接支付链接

$result = $client->query(['reqsn' => 'M202608240001']);          // 或 ['trxid' => $trxid]
$result = $client->refund([
    'reqsn'    => 'R202608240001',   // 退款单号(商户唯一)
    'trxamt'   => 100,
    'oldreqsn' => 'M202608240001',   // 原单号(与 oldtrxid 二选一)
]);
$result = $client->cancel(['reqsn' => 'C202608240001', 'trxamt' => 100, 'oldreqsn' => 'M202608240001']); // 当日全额
$result = $client->close(['oldreqsn' => 'M202608240001']);  // 关未支付单

$status = $result['data']['trxstatus'] ?? '';
// '0000'        -> 交易成功(已支付)
// '2000'/'2008' -> 交易处理中
// 其它          -> 交易失败

// $raw = file_get_contents('php://input'); // 表单编码的原始 body
$params = []; parse_str($raw, $params);

$result = $client->handleNotify($params);   // 验签失败:ret=0、errcode=VERIFY_FAILED
if ($result['ret'] !== 1) {
    echo 'fail';    // 返回非 success,通联会重发
    return;
}

$orderNo = $result['data']['cusorderid'];   // 下单时的 reqsn
$trxid   = $result['data']['trxid'];
$amt     = $result['data']['trxamt'];       // 分
// ... 幂等更新本地订单(先判断 trxstatus=0000)...

echo 'success';  // 必须返回 success,否则通联重发(15s/15s/5m/... 共 8 次)

use OephpOpen\AllinpaySyb\Crypto\Sm4Cipher;

$name = Sm4Cipher::encrypt('张三李四', $config->getSm4Key());
// => 3IUFstZ1CNyG1D/nRprv/A==(官方测试向量一致)
$plain = Sm4Cipher::decrypt($name, $config->getSm4Key());