Php 快应用中支付宝支付和回调

最近在做华为快应用,所以涉及到了各平台的支付功能。

快应用端代码

官方参数

首先看一下快应用提供的参数组装

在这里插入图片描述

实际代码

 // 支付宝支付

 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";
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值