PHP code example of cfn / unionpay

1. Go to this page and download the library: Download cfn/unionpay 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/ */

    

cfn / unionpay example snippets



/**
 * User: cfn <[email protected]>
 * Datetime: 2021/8/15 22:34
 * Copyright: php
 */

use unionpay\Factory;

$options = [
    'appid' => '*********', // appid
    'secret' => '*********', // 密钥
    'symmetricKey' => '*********', // 对称密钥
    'merId' => '*********', // 商户编号
    'signCertPath' => '*********', // 签名证书路径pfx结尾
    'signCertPwd' => '*********', // 签名证书密码
    'encryptCertPath' => '*********', // 敏感信息加密证书路径 cer结尾
    'debug' => False // debug模式
];

$app = Factory::miniProgram($options);


$backendToken = $app->backend_token->getToken(false);

print_r($backendToken);

// 输出:
//Array
//(
//    [backendToken] => GLnIGxENS6eDke7hvrZVjQ==
//    [expiresIn] => 7200
//)


$accessToken = $app->access_token->getToken('DD8jVkc1TwaCSohdazL8+w==',false);

print_r($accessToken);

// 输出
//Array
//(
//    [expiresIn] => 3600
//    [unionId] => 77e5ad3128eade6dd1222fbf722a7a1a7becfe9d715ace8841d429d29a87a700
//    [openId] => YbknmZra+VRPee76j+IVFeQHQ0vQ3pAZHVaCw7ovJQk/jTof+GMd6DSDRQAf/gaf
//    [scope] => upapi_mobile
//    [accessToken] => J39i0JZlD8uLxF3uEM6DzynLYJatTaSOmYah1ybVTnYy3dq7lTZzaLI1c/fJbQ+k+0eRdGj9fcx4BFtBbqu5VN/wAbP2aodWmuHQt5vKkMg=
//    [refreshToken] => DACgw7edTz29xCx9lcjwhw==
//)


$frontToken = $app->front_token->getToken();

print_r($frontToken);

// 输出
//Array
//(
//    [expiresIn] => 7200
//    [frontToken] => YO1p4AFJRCy0TLZMu4MNHw==
//)

// 参数解析
//* @param string $code 用户授权或静默授权获取的code和openid必传其一 不是必填项
//* @param string $openId 用户唯一标识如果未传递code请确保已调用accessToken后再调用此接口 不是必填项
//* @param bool $decrypt 是否解密返回 不是必填项

// 第一种方式 通过code直接获取
$mobile = $app->user->mobile("nSuRv/iJQm+6wYE6sqRx8w==");

// 第二种方式 通过openid获取,调用这种方式之前请确保已先调用accessToken方法
$mobile = $app->user->mobile(null,'YbknmZra+VRPee76j+IVFeQHQ0vQ3pAZHVaCw7ovJQk/jTof+GMd6DSDRQAf/gaf', false);


print_r($mobile);

// 输出
//Array
//(
//    [mobile] => Bth5XXdhUQIQLYXOcAreTQ==
//)

$mobile = $app->crypto->decrypt('Bth5XXdhUQIQLYXOcAreTQ==');

var_dump($mobile);

// 输出
//string(11) "1**********"


/**
 * User: cfn <[email protected]>
 * Datetime: 2021/8/15 22:34
 * Copyright: php
 */

use unionpay\Factory;

$options = [
    'appid' => '*********', // appid
    'secret' => '*********', // 密钥
    'symmetricKey' => '*********', // 对称密钥
    'merId' => '*********', // 商户编号
    'signCertPath' => '*********', // 签名证书路径pfx结尾
    'signCertPwd' => '*********', // 签名证书密码
    'encryptCertPath' => '*********', // 敏感信息加密证书路径 cer结尾
    'debug' => False // debug模式
];

$app = Factory::payment($options);

// txnAmt支付金额(分),orderId商户订单号
$data = $app->order->pay(['txnAmt' => 1, 'orderId' => date('YmdHis').rand(1000,9999)]);

// 对返回的结果进行验签
if ($app->signature->validate($data))
{
    // todo 验签成功
}

print_r($data);

// 输出
// Array
// (
//     [bizType] => 000201
//     [txnSubType] => 01
//     [orderId] => **************
//     [txnType] => 01
//     [encoding] => utf-8
//     [version] => 5.1.0
//     [accessType] => 0
//     [txnTime] => **************
//     [respMsg] => 成功[0000000]
//     [merId] => **************
//     [tn] => **************
//     [signMethod] => 01
//     [respCode] => 00
//     [signPubKeyCert] => -----BEGIN CERTIFICATE-----
// MIIEKzCCAxOgAwIBAgIFEpVGRCEwDQYJKoZIhvcNAQEFBQAwITELMAkGA1UEBhMC
// Q04xEjAQBgNVBAoTCUNGQ0EgT0NBMTAeFw0yMDA3MTYwOTM4MzRaFw0yNTA3MTYw
//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6LhtFi01Fnn3qwHe2vCEVBPJlazSsFE61tRCBTTWm8p/zfZKI9wGyir5aYBiPC
// TRPgXaQ4cYqSAh1n98a4ONBy2/StBl+TfKvCIoXARUSp12lOVY/aKg+8Jk4MIvEw
// 8WCL98tTVxXe1nWPlpFDS9y0ivMyfYlWkTb6+0gMrYA2nzrfFGS1KZNRBS7p3Bh5
// tdBPIgSd5gLZpAun8d0C3CcRZhcIof9hmxIc9ieQoWas52oVZDzsaGTo9rsTo9nU
// 3N3BThugW+P/koUnIFRG
// -----END CERTIFICATE-----
//     [signature] => fxLxEKV4GGLpvUnYsaCILUh6YyYI/jgwdeh94dGrT75nwGCOnspmB06cuzNj7G47mIR/TJZ0EEafJjaL2gkanVQMk4RfSMWGc+xcj8IYhdprbqZHyy7tbMCIMCDRlz1QKK2+UXXHs+dYDWHwqp3t4ZXpZ/GkmFNCRuExtzCcdotgzLGAc6PhGCKmL0nKC+ekGB48uLsg3lsmSTO08RUk9G32cOxqcFoVjhDJRjeqnccBo16GEjOT8TiyJOqFiG8Jk+E3ZZcYo1JM1FVRzR7TXuVcxEJmdePM3Akmtxa9MsuHMM0YP8YqPwN9Z9PH72fAplsAFPCAwhQrCeNjX6+f9g==
//)

// 默认自动验签,第二个参数设置为false就不自动验签,可自行验签, 注意验签失败会抛异常
$response = $app->notify->unit(function ($message) use ($app){
    // 自行验签
    $res = $app->crypto->verify($message);
    if ($res) {
        // 验签成功
        echo "验签成功";
    } else {
        // 验签失败
        echo "验签失败";
    }
}, false);

$response->send();

// 默认自动验签,第二个参数设置为false就不自动验签,可自行验签, 注意验签失败会抛异常
$response = $app->notify->unit(function ($message) use ($app){
    // 自行验签
    $res = $app->signature->validate($message);
    if ($res) {
        // 验签成功
        echo "验签成功";
    } else {
        // 验签失败
        echo "验签失败";
    }
}, false);

$response->send();