Php 快应用中支付宝支付和回调
程序员文章站
2024-02-18 16:06:34
...
最近在做华为快应用,所以涉及到了各平台的支付功能。
快应用端代码
官方参数
首先看一下快应用提供的参数组装
实际代码
// 支付宝支付
aliPay: function () {
let that = this;
let buy_data = that.buy_list_data[that.default_chose];
let body = buy_data.product;
let detail = buy_data.detail;
let total_fee = buy_data.price * 100;
let month = '';
let timestamp = new Date().getTime();
let sign = that.$app.$def.md5Fun(body + detail + total_fee + timestamp + month + that.reward);
let channel = that.$app.$def.CHANNEL;
let payData = {
"fee": total_fee, "payfee_vip": 0, "detail": detail, "product": body,
"timestamp": timestamp, "reward": that.reward, "month": "", "bid": 0,
"sign": sign, 'channel': channel,
};
that.$app.$def.get_fetch(that.$app.$def.config.extend.aliPay, payData, function (data) {
if (data.status !== "success") {
isOn = 0;
that.$app.$def.showToastFun(data.msg);
return false;
}
let orderData = data.data;
// 执行调用支付宝支付
alipay.pay({
orderInfo: orderData.orderInfo,
callback: function (ret) {
if (parseInt(ret.resultStatus) === 9000) {
that.$app.$def.goUrl('My/Buy/Status?pay=success&orderId=' + orderData.out_trade_no)
} else {
that.$app.$def.goUrl('My/Buy/Status?pay=fail')
}
isOn = 0;
console.log(ret.resultStatus);
}
})
});
}
华为官方文档:https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-alipay
快应用官方文档:https://doc.quickapp.cn/features/service/alipay.html
其实两个文档写的都差不多,没什么区别
后端代码
发起支付
// 支付宝充值
public function aliPay(){
$uid = $this->uid;
if (!$uid){
return_json_data(-99,'请先登录');
}
//310版本以上+cps用户,进私账
$userInfo = $this->userInfo;
$version_path = 'qrxs_alipay_cert_new';
$alipayconf = C('alipay_new');
$notify_url = '/Fastapp/notify/alipay_new';
$total_fee = $total_fee_old = I('fee','intval',0); //订单总金额 单位 分
$total_fee_vip = I('payfee_vip','intval',0); //订单套餐vip总金额 单位 分
$body = I('product','trim,addslashes','购买书币'); //购买描述
$detail = I('detail','trim,addslashes','购买书币'); //购买详情
$sign = I('sign','trim,addslashes',''); //APP与应用服务器之间的数据签名
$timestamp = I('timestamp','trim,addslashes',''); //时间戳
$reward = I('reward','intval',2); //是否是打赏支付 打赏为1 购买为0 2表示普通书币订单 3表示馈赠书币订单
$month = I('month','trim,addslashes',''); //购买月份数
$bid = I('bid','intval',0); //小说bid(表示用户在该小说页购买的书币,在我的页面购买的设为0)
$sign_ = md5($body.$detail.$total_fee.$timestamp.$month.$reward);
if($sign_ != $sign && $_GET['testa'] != 1){
return_json_data(-16,'签名错误');
}
// 写入订单
$orderId = 'kaa'.$this->uid.date("mdHis").rand(2000,8000);
if(!$orderId ){
return_json_data(0,'充值失败'.$ord_id);
}
// 支付宝开始
$total_fee_alip = $total_fee/100;
$biz_content = [
"timeout_express"=>"30m",
"product_code"=>"QUICK_MSECURITY_PAY",
"total_amount"=>"{$total_fee_alip}",
"subject"=>"{$detail}",
"body"=>"{$body}",
"out_trade_no"=>"{$orderId}",
];
$biz_content = json_encode($biz_content);
$private_key_path = (getcwd()).'/'.$version_path . '/rsa_private_key.pem';
$alipayParam = array(
'charset'=>'utf-8',
'biz_content'=>$biz_content,
'method'=>'alipay.trade.app.pay', // 接口名称
'notify_url'=>$this->notify_host.$notify_url, //支付完成通知地址
'app_id'=>$alipayconf['app_id'], //合作者身份id
'sign_type'=>'RSA2',
'version'=>'1.0',
'timestamp'=>date('Y-m-d H:i:s'),
);
$alipayStr = $this->getParamSign($alipayParam,$private_key_path);
return_json_data(1,'ok',array('orderInfo'=>$alipayStr,'out_trade_no'=>$orderId));
}
//支付宝RSA签名
private function rsaSign($data, $private_key_path) {
$priKey = file_get_contents($private_key_path);
$res = openssl_get_privatekey($priKey);
openssl_sign($data, $sign, $res,OPENSSL_ALGO_SHA256);
openssl_free_key($res);
$sign = base64_encode($sign); //base64编码
return $sign;
}
//支付宝参数签名
private function getParamSign($params = array(),$private_key_path=''){
$tempEncode = "";
foreach ($params as $k => $v){
$tempEncode .= $k . '=' . urlencode($v) . '&';
}
$tempEncode = substr($tempEncode, 0, strlen($tempEncode)-1);
if(get_magic_quotes_gpc()){
$tempEncode = stripslashes($tempEncode);
}
// 签名
ksort($params);
$temp = "";
foreach ($params as $k => $v){
$temp .= $k . '=' . $v . '&';
}
$temp = substr($temp, 0, strlen($temp)-1);
if(get_magic_quotes_gpc()){
$temp = stripslashes($temp);
}
$sign = $this->rsaSign($temp,$private_key_path);
return $tempEncode.'&sign='.urlencode($sign);
}
支付回调
//支付宝支付回调接口 3.1.0新账号
public function alipay_new(){
$this->alipay_config = array(
'partner'=>C('alipay_new')['partner'],
'private_key_path'=>(getcwd()).'/qrxs_alipay_cert_new/rsa_private_key.pem',
'ali_public_key_path'=>(getcwd()).'/qrxs_alipay_cert_new/alipay_public_key.pem',
'cacert' => (getcwd()).'/qrxs_alipay_cert_new/cacert.pem',
'sign_type' => '0001',
'input_charset' => 'utf-8',
'transport' => 'http',
'seller_id' => C('alipay_new')['seller_id'],
'notify_url' => C('SITEURL'),
);
require_once LIB_PATH."/Event/alipay/alipay_notify_app.class.php";
//计算得出通知验证结果
$alipayNotify = new \AlipayNotify($this->alipay_config);
$verify_result = $alipayNotify->verifyNotify();
if($verify_result ) {
$out_trade_no = addslashes($_POST['out_trade_no']); //商户订单号
$trade_no = addslashes($_POST['trade_no']); //支付宝交易号
$trade_status = addslashes($_POST['trade_status']);
// 写入你的逻辑代码
echo "success";
} else {
echo "fail";
}
}
推荐阅读
-
PHP华为快应用华为支付和回调处理
-
Php 快应用中支付宝支付和回调
-
php进行支付宝开发中return_url和notify_url的区别分析
-
php 绝对路径和相对路径混合+支付宝回调处理
-
php进行支付宝开发中return_url和notify_url的区别分析,支付宝notifyurl_PHP教程
-
php进行支付宝开发中return_url和notify_url的区别分析,支付宝notifyurl
-
php进行支付宝开发中return_url和notify_url的区别分析
-
php app支付宝回调(异步通知)详解
-
Java中Spring Boot支付宝扫码支付及支付回调的实现代码
-
php进行支付宝开发中return_url和notify_url的区别分析