PHP 实现支付宝APP 支付 (服务端 + 客户端 + 异步)
一、使用实例
官方信息:
二、服务端
1.下载SDK
根据自身的需要选择SDK包
下载之后放在了 vendor 文件下:
2.业务层
支付宝证书模式下的配置参考跳转:https://mp.csdn.net/mp_blog/creation/success/127964188
-
-
-
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={"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¬ify_url=****&sign_type=RSA2×tamp=2022-12-27 10:54:32&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) {}
-
}
-
})
-
}
-
})
-
}
-
})
-
},
总结
内容尽量写的仔细一点所以文章有点长,感谢阅读
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfcieg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01