Rumah > pembangunan bahagian belakang > tutorial php > php如何实现支付宝的APP支付功能(代码)

php如何实现支付宝的APP支付功能(代码)

不言
Lepaskan: 2023-04-03 12:46:02
asal
2494 orang telah melayarinya

本篇文章给大家分享的内容是关于php如何实现支付宝的APP支付功能(代码),内容很详细,有需要的朋友可以参考一下,希望可以帮助到大家。

支付宝网页支付

1.支付宝开放平台添加应用,获得appid,并签约。

2.在支付宝开放品台设置如下:

3.配置支付宝的应用公钥。(根据支付宝的文档)

4.在开放平台下载官方sdk demo。

5.代码:

 //支付宝
  include_once VENDOR_PATH . 'Alipay/aop/AopClient.php';
  include_once VENDOR_PATH . 'Alipay/aop/request/AlipayTradeAppPayRequest.php';
  $notify_url='https://www.www.com/app/pay/AlipayStep3Notify';
  $config = array(
   'appid' =>$this->appid,//
   'rsaPrivateKey' =>$this->rsaPrivateKey,//开发者私钥私钥
   'alipayrsaPublicKey'=>$this->alipayrsaPublicKey,//支付宝公钥
   'charset'=>strtolower('utf-8'),//编码
   'notify_url' =>$notify_url,//回调地址(支付宝支付成功后回调修改订单状态的地址)
   'payment_type' =>1,//(固定值)
   'seller_id' =>'',//收款商家账号
   'charset' => 'utf-8',//编码
   'sign_type' => 'RSA2',//签名方式
   'timestamp' =>date("Y-m-d H:i:s"),
   'version' =>"1.0",//固定值
   'url'  => 'https://openapi.alipay.com/gateway.do',//固定值
   'method' => 'alipay.trade.app.pay',//固定值
  );
   
     $aop = new \AopClient();
     $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
     $aop->appId = $config['appid'];
     $aop->rsaPrivateKey = $config['rsaPrivateKey'];
     $aop->format = "json";
     $aop->charset = "UTF-8";
     $aop->signType = "RSA2";
     $aop->alipayrsaPublicKey=$config['alipayrsaPublicKey'];
    //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
     $request = new \AlipayTradeAppPayRequest();
    //SDK已经封装掉了公共参数,这里只需要传入业务参数
     
     $bizcontent = json_encode([
       'body'=>'**',
       'subject'=>$subject,
       'out_trade_no'=> $order_sn,//此订单号为商户唯一订单号
       'total_amount'=>$totalprice,//保留两位小数
       'product_code'=>'QUICK_MSECURITY_PAY'
     ]);
     $request->setNotifyUrl($config['notify_url']);
     $request->setBizContent($bizcontent);
    //这里和普通的接口调用不同,使用的是sdkExecute
     $response = $aop->sdkExecute($request);
    //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
    $datas=$response;//就是orderString 可以直接给客户端请求,无需再做处理。
    $this->arr['code']=0;
    $this->arr['msg']=$order_sn;
    $this->arr['info']=$datas;
    echo json_encode($this->arr);exit;
Salin selepas log masuk

6.支付回调notify_url。

include_once VENDOR_PATH . 'Alipay/aop/AopClient.php';
   $aop = new \AopClient();
   $config['alipayrsaPublicKey']=$this->$alipayrsaPublicKey;//公钥
   $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
   //此处验签方式必须与下单时的签名方式一致
   $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
   //验签通过后再实现业务逻辑,比如修改订单表中的支付状态。
   /**
   ①验签通过后核实如下参数out_trade_no、total_amount、seller_id
   ②修改订单表
    **/
   $out_trade_no = I('post.out_trade_no'); //商户订单号
Salin selepas log masuk

之后对数据库对应的数据进行修改。

7.订单查询接口:

include_once VENDOR_PATH . 'Alipay/aop/SignData.php';
  include_once VENDOR_PATH . 'Alipay/aop/AopClient.php';
  include_once VENDOR_PATH . 'Alipay/aop/request/AlipayTradeQueryRequest.php';
  $config = array(
   'appid' =>$this->appid,//
   'rsaPrivateKey' =>$this->rsaPrivateKey,//开发者私钥私钥
   'alipayrsaPublicKey'=>$this->alipayrsaPublicKey,//支付宝公钥
   'charset'=>strtolower('utf-8'),//编码
   'notify_url' =>'',//回调地址(支付宝支付成功后回调修改订单状态的地址)
   'payment_type' =>1,//(固定值)
   'seller_id' =>'',//收款商家账号
   'charset' => 'utf-8',//编码
   'sign_type' => 'RSA',//签名方式
   'timestamp' =>date("Y-m-d H:i:s"),
   'version' =>"1.0",//固定值
   'url'  => 'https://openapi.alipay.com/gateway.do',//固定值
   'method' => 'alipay.trade.query',//固定值
  );
   
     $aop = new \AopClient();
     $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
     $aop->appId = $config['appid'];
     $aop->rsaPrivateKey = $config['rsaPrivateKey'];
     $aop->format = "json";
     $aop->charset = "UTF-8";
     $aop->signType = "RSA2";
     $aop->method = $config['method'];
     $aop->apiVersion = '1.0';
     $aop->alipayrsaPublicKey=$config['alipayrsaPublicKey'];
    //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.query
     $request = new \AlipayTradeQueryRequest();
 
     $bizcontent = json_encode([
       'out_trade_no'=>$order_sn,
       'trade_no'=>''
     ]);
     
     $request->setBizContent($bizcontent);
    
     $response = $aop->execute($request);
 
     $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
     $resultCode = $response->$responseNode->code;
     if(!empty($resultCode)&&$resultCode == 10000){
  
       $this->arr['code']=0;
       $this->arr['msg']='success';
       echo json_encode($this->arr);exit;
    
     } else {
       $this->arr['code']=100001;
       $this->arr['msg']='未查询到订单信息';
        echo json_encode($this->arr);exit;
     
 }
Salin selepas log masuk

相关推荐:

php支付宝接口用法分析,php支付宝用法

thinkphp 支付宝,web扫码支付

Atas ialah kandungan terperinci php如何实现支付宝的APP支付功能(代码). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan