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

// 证书

const ALIPAY_ROOT_CERT = "/appcertAlipay/alipayRootCert.crt"; // 支付宝根证书放置路径

const ALIPAY_CERT_PUBLIC_KEY = "/appcertAlipay/appCertPublicKey_2021003171636173.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) {}

}

})

}

})

}

})

},

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值