• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PHP 实现支付宝APP 支付 (服务端 + 客户端 + 异步)

武飞扬头像
有只猫吃很多
帮助1


一、使用实例

官方信息:

学新通

二、服务端

1.下载SDK

App 支付服务端 DEMO&SDK | 开放平台

根据自身的需要选择SDK包

学新通

 下载之后放在了 vendor 文件下:

学新通

2.业务层

支付宝证书模式下的配置参考跳转:https://mp.csdn.net/mp_blog/creation/success/127964188

  1.  
    <?php
  2.  
     
  3.  
    namespace app\common\controller;
  4.  
     
  5.  
    use AlipayTradeAppPayRequest;
  6.  
    use AopCertClient;
  7.  
    use think\Controller;
  8.  
     
  9.  
    // sdk路径
  10.  
     
  11.  
    include_once dirname(__FILE__) . '../../../../vendor/alipay-sdk-PHP-4.9.2/aop/request/AlipayTradeAppPayRequest.php';
  12.  
    include_once dirname(__FILE__) . '../../../../vendor/alipay-sdk-PHP-4.9.2/aop/AopCertClient.php';
  13.  
     
  14.  
    class Appalipay extends Controller
  15.  
    {
  16.  
    // 私钥值
  17.  
    const PRIVATE_KEY = "根据你使用的证书模式和密钥模式的私钥要注意区分一下哦";
  18.  
    const SERVERURL = 'https://openapi.alipay.com/gateway.do'; // 网关地址
  19.  
    const APP_PAY_ID = 'appid'; // appid
  20.  
    // 证书
  21.  
    const ALIPAY_ROOT_CERT = "/appcertAlipay/alipayRootCert.crt"; // 支付宝根证书放置路径
  22.  
    const ALIPAY_CERT_PUBLIC_KEY = "/appcertAlipay/appCertPublicKey_20***.crt"; // 应用公钥证书放置路径
  23.  
    const ALIPAY_CERT_PUBLIC_KEY_RSA2 = "/appcertAlipay/alipayCertPublicKey_RSA2.crt"; // 支付宝公钥证书放置路径
  24.  
     
  25.  
     
  26.  
    /**
  27.  
    * @method app - 支付
  28.  
    * @param string $orderNo 订单号
  29.  
    * @param string $totalAmount 订单金额:精确到分
  30.  
    * @param string $subject 订单标题
  31.  
    */
  32.  
    public function app_pay()
  33.  
    {
  34.  
    $orderNo = time();
  35.  
    $totalAmount = '0.01';
  36.  
    $subject = '测试商品';
  37.  
    $notifyUrl = 'https://**';
  38.  
     
  39.  
    $aop = new AopCertClient();
  40.  
    $appCertPath = dirname(__FILE__) . self::ALIPAY_CERT_PUBLIC_KEY;
  41.  
    $alipayCertPath = dirname(__FILE__) . self::ALIPAY_CERT_PUBLIC_KEY_RSA2;
  42.  
    $rootCertPath = dirname(__FILE__) . self::ALIPAY_ROOT_CERT;
  43.  
     
  44.  
     
  45.  
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  46.  
    $aop->appId = self::APP_PAY_ID; // appid 文档顶部定义
  47.  
    $aop->rsaPrivateKey = self::PRIVATE_KEY; // 私钥 文档顶部定义
  48.  
    $aop->alipayrsaPublicKey = $this->getPublicKey($alipayCertPath);
  49.  
    $aop->apiVersion = '1.0';
  50.  
    $aop->signType = 'RSA2';
  51.  
    $aop->postCharset = 'utf-8';
  52.  
    $aop->format = 'json';
  53.  
    $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  54.  
    $aop->appCertSN = $this->getCertSN($appCertPath); //调用getCertSN获取证书序列号
  55.  
    $aop->alipayRootCertSN = $this->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
  56.  
     
  57.  
    $request = new AlipayTradeAppPayRequest();
  58.  
    $request->setNotifyUrl($notifyUrl);
  59.  
    $request->setBizContent(json_encode(array(
  60.  
    'total_amount' => $totalAmount,
  61.  
    'product_code' => 'QUICK_MSECURITY_PAY',
  62.  
    'subject' => $subject,
  63.  
    'out_trade_no' => $orderNo,
  64.  
    )));
  65.  
    $result = $aop->sdkExecute($request);
  66.  
    return $result;
  67.  
    }
  68.  
     
  69.  
     
  70.  
     
  71.  
    /**====================================================================================================================================*/
  72.  
     
  73.  
    /**
  74.  
    * 从证书中提取公钥
  75.  
    * @param $cert
  76.  
    * @return mixed
  77.  
    */
  78.  
    public function getPublicKey($certPath)
  79.  
    {
  80.  
    $cert = file_get_contents($certPath);
  81.  
    $pkey = openssl_pkey_get_public($cert);
  82.  
    $keyData = openssl_pkey_get_details($pkey);
  83.  
    $public_key = str_replace('-----BEGIN PUBLIC KEY-----', '', $keyData['key']);
  84.  
    $public_key = trim(str_replace('-----END PUBLIC KEY-----', '', $public_key));
  85.  
    return $public_key;
  86.  
    }
  87.  
     
  88.  
    /**
  89.  
    * 从证书中提取序列号
  90.  
    * @param $cert
  91.  
    * @return string
  92.  
    */
  93.  
    public function getCertSN($certPath)
  94.  
    {
  95.  
    $cert = file_get_contents($certPath);
  96.  
    $ssl = openssl_x509_parse($cert);
  97.  
    $SN = md5($this->array2string(array_reverse($ssl['issuer'])) . $ssl['serialNumber']);
  98.  
    return $SN;
  99.  
    }
  100.  
     
  101.  
    /**
  102.  
    * 提取根证书序列号
  103.  
    * @param $cert 根证书
  104.  
    * @return string|null
  105.  
    */
  106.  
    public function getRootCertSN($certPath)
  107.  
    {
  108.  
    $cert = file_get_contents($certPath);
  109.  
    // $this->alipayRootCertContent = $cert;
  110.  
    $array = explode("-----END CERTIFICATE-----", $cert);
  111.  
    $SN = null;
  112.  
    for ($i = 0; $i < count($array) - 1; $i ) {
  113.  
    $ssl[$i] = openssl_x509_parse($array[$i] . "-----END CERTIFICATE-----");
  114.  
    if (strpos($ssl[$i]['serialNumber'], '0x') === 0) {
  115.  
    $ssl[$i]['serialNumber'] = $this->hex2dec($ssl[$i]['serialNumberHex']);
  116.  
    }
  117.  
    if ($ssl[$i]['signatureTypeLN'] == "sha1WithRSAEncryption" || $ssl[$i]['signatureTypeLN'] == "sha256WithRSAEncryption") {
  118.  
    if ($SN == null) {
  119.  
    $SN = md5($this->array2string(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
  120.  
    } else {
  121.  
     
  122.  
    $SN = $SN . "_" . md5($this->array2string(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
  123.  
    }
  124.  
    }
  125.  
    }
  126.  
    return $SN;
  127.  
    }
  128.  
     
  129.  
    /**
  130.  
    * 0x转高精度数字
  131.  
    * @param $hex
  132.  
    * @return int|string
  133.  
    */
  134.  
    function hex2dec($hex)
  135.  
    {
  136.  
    $dec = 0;
  137.  
    $len = strlen($hex);
  138.  
    for ($i = 1; $i <= $len; $i ) {
  139.  
    $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
  140.  
    }
  141.  
    return $dec;
  142.  
    }
  143.  
     
  144.  
    protected function array2string($array)
  145.  
    {
  146.  
    $string = [];
  147.  
    if ($array && is_array($array)) {
  148.  
    foreach ($array as $key => $value) {
  149.  
    $string[] = $key . '=' . $value;
  150.  
    }
  151.  
    }
  152.  
    return implode(',', $string);
  153.  
    }
  154.  
     
  155.  
    }
学新通

3.业务层返回实例

返回的和APP支付文档上不一样哦,这里返回的是一串请求字符串,我们给前端客户端去请求支付宝

可以参考官方文档:APP 支付快速接入 | 网页&移动应用

"alipay_root_cert_sn=6***支付宝根路径证书提取的序列号&alipay_sdk=alipay-sdk-php-2020-04-15&app_cert_sn=证书序列号&app_id=2021*****&biz_content={"total_amount":"0.01","product_code":"QUICK_MSECURITY_PAY","subject":"\u6d4b\u8bd5\u5546\u54c1","out_trade_no":1672109672}&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=****&sign_type=RSA2&timestamp=2022-12-27 10:54:32&version=1.0&sign=sign"

推荐使用联调工具:

https://opensupport.alipay.com/support/tools/cloudparse/interface?ant_source=antsupport

学新通

4.回调层

  1.  
    /**
  2.  
    * @method app - pay 回调
  3.  
    */
  4.  
    public function appPayNotify()
  5.  
    {
  6.  
    // 打个日志看看
  7.  
    $sFileName = 'alipay.txt';
  8.  
    $sContent = date('Y-m-d H:i:s') . '开始回调\r\n' . '\r\n';
  9.  
    file_put_contents($sFileName, $sContent, FILE_APPEND);
  10.  
     
  11.  
     
  12.  
    // 回调参数根据文档上的异步回调通知看看自己需要的参数接收用 POST 接收过来就行
  13.  
     
  14.  
    $notifyTime = $_POST['notify_time']; // 回调时间
  15.  
    $notifyType = $_POST['notify_type']; // 通知类型
  16.  
    $notifyId = $_POST['notify_id']; // 通知检验 ID
  17.  
    $appId = $_POST['app_id']; // 支付宝分配给开发者的应用 APPID
  18.  
    $authAppId = $_POST['auth_app_id']; // 开发者的 app_id,在服务商调用的场景下为授权方的 app_id
  19.  
    $tradeNo = $_POST['trade_no']; // 支付宝交易凭证号
  20.  
    $orderNo = $_POST['out_trade_no']; // 原支付请求的商家订单号
  21.  
     
  22.  
     
  23.  
    // 打个日志看看
  24.  
    $sFileName = 'alipay.txt';
  25.  
    $sContent = date("Y-m-d H:i:s") . '接收到_POST方式回调参数\r\n' . json_encode($orderNo, JSON_UNESCAPED_UNICODE) . '\r\n';
  26.  
    file_put_contents($sFileName, $sContent, FILE_APPEND);
  27.  
     
  28.  
    // 接收到回调之后输出 success 给支付宝
  29.  
    echo 'success';
  30.  
    die;
  31.  
    }
学新通

三、客户端

绑定方法之后使用APP 自带的 uni.requestPayment方法来发起

  1.  
    // 购买执行
  2.  
    async actionRpBuy() {
  3.  
    if (this.num == '' || this.num == 0) {
  4.  
    uni.showToast({
  5.  
    title: '请选择购买数量',
  6.  
    icon: 'none'
  7.  
    });
  8.  
    return false;
  9.  
    }
  10.  
    request({
  11.  
    url: '/请求地址',
  12.  
    }).then(res => {
  13.  
    if (res.code == 1) {
  14.  
    console.log(res.data);
  15.  
    uni.requestPayment({
  16.  
    provider: 'alipay', //'alipay','wxpay'// manifest.json->APP模块配种->payment配置一下
  17.  
    orderInfo:res.data, // 这个参数给后端返回的请求字符串
  18.  
    success: res => {
  19.  
    uni.navigateTo({
  20.  
    url: '../CompletionOfPayment/CompletionOfPayment'
  21.  
    })
  22.  
     
  23.  
    },
  24.  
    fail: err => {
  25.  
    let orderId = this.ids
  26.  
    uni.showModal({
  27.  
    // title: '提示',
  28.  
    title: err,
  29.  
    content: '支付失败',
  30.  
    success: function(res) {
  31.  
    if (res.confirm) {} else if (res.cancel) {}
  32.  
    }
  33.  
    })
  34.  
    }
  35.  
    })
  36.  
    }
  37.  
    })
  38.  
    },
学新通

总结

内容尽量写的仔细一点所以文章有点长,感谢阅读

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfcieg
系列文章
更多 icon
同类精品
更多 icon
继续加载