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


一、使用实例

官方信息:

二、服务端

1.下载SDK

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

根据自身的需要选择SDK包

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

2.业务层

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

<?php

namespace app\common\controller;

use AlipayTradeAppPayRequest;
use AopCertClient;
use think\Controller;

// sdk路径

include_once  dirname(__FILE__) . '../../../../vendor/alipay-sdk-PHP-4.9.2/aop/request/AlipayTradeAppPayRequest.php';
include_once  dirname(__FILE__) . '../../../../vendor/alipay-sdk-PHP-4.9.2/aop/AopCertClient.php';

class Appalipay extends Controller
{
    // 私钥值
    const PRIVATE_KEY    = "根据你使用的证书模式和密钥模式的私钥要注意区分一下哦";
    const SERVERURL      = 'https://openapi.alipay.com/gateway.do';  // 网关地址
    const APP_PAY_ID     = 'appid';                                  // appid
    // 证书
    const ALIPAY_ROOT_CERT            = "/appcertAlipay/alipayRootCert.crt";                     // 支付宝根证书放置路径
    const ALIPAY_CERT_PUBLIC_KEY      = "/appcertAlipay/appCertPublicKey_20***.crt";  // 应用公钥证书放置路径
    const ALIPAY_CERT_PUBLIC_KEY_RSA2 = "/appcertAlipay/alipayCertPublicKey_RSA2.crt";           // 支付宝公钥证书放置路径


    /** 
     * @method  app - 支付
     * @param   string $orderNo     订单号
     * @param   string $totalAmount 订单金额:精确到分
     * @param   string $subject     订单标题 
     */
    public function app_pay()
    {
        $orderNo     = time();
        $totalAmount = '0.01';
        $subject     = '测试商品';
        $notifyUrl   = 'https://**';

        $aop            = new AopCertClient();
        $appCertPath    = dirname(__FILE__) . self::ALIPAY_CERT_PUBLIC_KEY;
        $alipayCertPath = dirname(__FILE__) . self::ALIPAY_CERT_PUBLIC_KEY_RSA2;
        $rootCertPath   = dirname(__FILE__) . self::ALIPAY_ROOT_CERT;


        $aop->gatewayUrl              = 'https://openapi.alipay.com/gateway.do';
        $aop->appId                   = self::APP_PAY_ID;                         // appid 文档顶部定义
        $aop->rsaPrivateKey           = self::PRIVATE_KEY;                        // 私钥 文档顶部定义
        $aop->alipayrsaPublicKey      = $this->getPublicKey($alipayCertPath);
        $aop->apiVersion              = '1.0';
        $aop->signType                = 'RSA2';
        $aop->postCharset             = 'utf-8';
        $aop->format                  = 'json';
        $aop->isCheckAlipayPublicCert = true;                                     //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN               = $this->getCertSN($appCertPath);           //调用getCertSN获取证书序列号
        $aop->alipayRootCertSN        = $this->getRootCertSN($rootCertPath);      //调用getRootCertSN获取支付宝根证书序列号

        $request = new AlipayTradeAppPayRequest();
        $request->setNotifyUrl($notifyUrl);
        $request->setBizContent(json_encode(array(
            'total_amount' => $totalAmount,
            'product_code' => 'QUICK_MSECURITY_PAY',
            'subject'      => $subject,
            'out_trade_no' => $orderNo,
        )));
        $result = $aop->sdkExecute($request);
        return $result;
    }



    /**====================================================================================================================================*/

    /**
     * 从证书中提取公钥
     * @param $cert
     * @return mixed
     */
    public function getPublicKey($certPath)
    {
        $cert       = file_get_contents($certPath);
        $pkey       = openssl_pkey_get_public($cert);
        $keyData    = openssl_pkey_get_details($pkey);
        $public_key = str_replace('-----BEGIN PUBLIC KEY-----', '', $keyData['key']);
        $public_key = trim(str_replace('-----END PUBLIC KEY-----', '', $public_key));
        return $public_key;
    }

    /**
     * 从证书中提取序列号
     * @param $cert
     * @return string
     */
    public function getCertSN($certPath)
    {
        $cert = file_get_contents($certPath);
        $ssl  = openssl_x509_parse($cert);
        $SN   = md5($this->array2string(array_reverse($ssl['issuer'])) . $ssl['serialNumber']);
        return $SN;
    }

    /**
     * 提取根证书序列号
     * @param $cert  根证书
     * @return string|null
     */
    public function getRootCertSN($certPath)
    {
        $cert                        = file_get_contents($certPath);
        // $this->alipayRootCertContent = $cert;
        $array                       = explode("-----END CERTIFICATE-----", $cert);
        $SN                          = null;
        for ($i = 0; $i < count($array) - 1; $i++) {
            $ssl[$i] = openssl_x509_parse($array[$i] . "-----END CERTIFICATE-----");
            if (strpos($ssl[$i]['serialNumber'], '0x') === 0) {
                $ssl[$i]['serialNumber'] = $this->hex2dec($ssl[$i]['serialNumberHex']);
            }
            if ($ssl[$i]['signatureTypeLN'] == "sha1WithRSAEncryption" || $ssl[$i]['signatureTypeLN'] == "sha256WithRSAEncryption") {
                if ($SN == null) {
                    $SN = md5($this->array2string(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
                } else {

                    $SN = $SN . "_" . md5($this->array2string(array_reverse($ssl[$i]['issuer'])) . $ssl[$i]['serialNumber']);
                }
            }
        }
        return $SN;
    }

    /**
     * 0x转高精度数字
     * @param $hex
     * @return int|string
     */
    function hex2dec($hex)
    {
        $dec = 0;
        $len = strlen($hex);
        for ($i = 1; $i <= $len; $i++) {
            $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
        }
        return $dec;
    }

    protected function array2string($array)
    {
        $string = [];
        if ($array && is_array($array)) {
            foreach ($array as $key => $value) {
                $string[] = $key . '=' . $value;
            }
        }
        return implode(',', $string);
    }

}

3.业务层返回实例

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

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

"alipay_root_cert_sn=6***支付宝根路径证书提取的序列号&alipay_sdk=alipay-sdk-php-2020-04-15&app_cert_sn=证书序列号&app_id=2021*****&biz_content=%7B%22total_amount%22%3A%220.01%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%5Cu6d4b%5Cu8bd5%5Cu5546%5Cu54c1%22%2C%22out_trade_no%22%3A1672109672%7D&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=****&sign_type=RSA2&timestamp=2022-12-27+10%3A54%3A32&version=1.0&sign=sign"

推荐使用联调工具:

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

4.回调层

    /**
     * @method app - pay 回调
     */
    public function appPayNotify()
    {
        // 打个日志看看
        $sFileName  = 'alipay.txt';
        $sContent   = date('Y-m-d H:i:s') . '开始回调\r\n' . '\r\n';
        file_put_contents($sFileName, $sContent, FILE_APPEND);


        // 回调参数根据文档上的异步回调通知看看自己需要的参数接收用 POST 接收过来就行

        $notifyTime = $_POST['notify_time'];   // 回调时间
        $notifyType = $_POST['notify_type'];   // 通知类型
        $notifyId   = $_POST['notify_id'];     // 通知检验 ID
        $appId      = $_POST['app_id'];        // 支付宝分配给开发者的应用 APPID
        $authAppId  = $_POST['auth_app_id'];   // 开发者的 app_id,在服务商调用的场景下为授权方的 app_id
        $tradeNo    = $_POST['trade_no'];      // 支付宝交易凭证号
        $orderNo    = $_POST['out_trade_no'];  // 原支付请求的商家订单号


        // 打个日志看看
        $sFileName      = 'alipay.txt';
        $sContent       = date("Y-m-d H:i:s") . '接收到_POST方式回调参数\r\n' . json_encode($orderNo, JSON_UNESCAPED_UNICODE) . '\r\n';
        file_put_contents($sFileName, $sContent, FILE_APPEND);

        // 接收到回调之后输出 success 给支付宝
        echo 'success';
        die;
    }

三、客户端

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

			// 购买执行
			async actionRpBuy() {
				if (this.num == '' || this.num == 0) {
					uni.showToast({
						title: '请选择购买数量',
						icon: 'none'
					});
					return false;
				}
				request({
					url: '/请求地址',
				}).then(res => {
					if (res.code == 1) {
						console.log(res.data);
						uni.requestPayment({
						      provider: 'alipay', //'alipay','wxpay'// manifest.json->APP模块配种->payment配置一下
						      orderInfo:res.data, // 这个参数给后端返回的请求字符串
							  success: res => {
						       uni.navigateTo({
						        url: '../CompletionOfPayment/CompletionOfPayment'
						       })
						
						      },
						      fail: err => {
						       let orderId = this.ids
						       uni.showModal({
						        // title: '提示',
						        title: err,
						        content: '支付失败',
						        success: function(res) {
						         if (res.confirm) {} else if (res.cancel) {}
						        }
						       })
						      }
						     })
					}
				})
			},

总结

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信支付客户端服务端代码可以分别使用不同的编程语言实现,但推荐使用官方提供的 SDK 实现。以下是基于官方 SDK 的客户端服务端代码示例: 客户端代码: ```java IWXAPI api = WXAPIFactory.createWXAPI(this, "wxAPPID"); // 替换为你自己的 APPID PayReq request = new PayReq(); request.appId = "wxAPPID"; // 替换为你自己的 APPID request.partnerId = "商户号"; request.prepayId= "预支付订单号"; request.packageValue = "Sign=WXPay"; request.nonceStr= "随机字符串"; request.timeStamp= "时间戳"; request.sign= "签名"; api.sendReq(request); ``` 服务端代码: ```php require_once "lib/WxPay.Api.php"; require_once "WxPay.NativePay.php"; require_once 'log.php'; $input = new WxPayUnifiedOrder(); $input->SetBody("test"); // 商品描述 $input->SetAttach("test"); // 附加数据 $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis")); // 商户订单号 $input->SetTotal_fee("1"); // 总金额,单位为分 $input->SetTime_start(date("YmdHis")); // 订单生成时间 $input->SetTime_expire(date("YmdHis", time() + 600)); // 订单失效时间 $input->SetGoods_tag("test"); // 商品标记 $input->SetNotify_url("http://www.example.com/wxpay_notify.php"); // 接收微信支付异步通知回调地址 $input->SetTrade_type("NATIVE"); // 交易类型,取值如下:JSAPI,NATIVE,APP,WAP $input->SetProduct_id("test"); // 商品ID $notify = new NativePay(); $result = $notify->GetPayUrl($input); $url = $result["code_url"]; // 二维码链接 // 生成订单后,可以将订单信息存入数据库 ``` 以上代码仅供参考,具体实现还需要结合实际情况进行调整。另外,微信支付的开发过程中,还需要进行签名校验、异步通知处理等相关操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值