APP调用支付宝支付

hot3.png

 

  1. 申请支付宝支付,获取appid,开发者私钥,开发者公钥,支付宝公钥
  2. 下载支付宝sdk : https://docs.open.alipay.com/54/106370/
  3. 核心代码,$setNotifyUrl是支付成功时的回调函数地址,url使用pathinfo模式
  • 生成支付宝订单信息 php  
        if(!$appid || !$rsaPrivateKey || !$alipayrsaPublicKey || !$setNotifyUrl){
            return false;
        }
        
        $total_amount = sprintf("%0.2f",floatval($total_amount)/100);
        
        require_once APP_PATH."Lib/ORG/alipay-sdk-PHP/AopSdk.php";
        $aop = new AopClient;
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $appid;
        $aop->rsaPrivateKey = $rsaPrivateKey;//开发者私钥
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA";
        $aop->alipayrsaPublicKey = $alipayrsaPublicKey;//支付宝公钥
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        $request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数
        $out_trade_no = time().rand(1000, 9999);
        $bizcontent = "{\"body\":\"".$body."\","
                . "\"subject\": \"".$subject."\","
                . "\"out_trade_no\": \"".$out_trade_no."\","
                        . "\"timeout_express\": \"30m\","
                                . "\"total_amount\": \"0.01\","
                                        . "\"product_code\":\"QUICK_MSECURITY_PAY\""
                                                . "}";
        $request->setNotifyUrl($setNotifyUrl);
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同,使用的是sdkExecute
        $response = $aop->sdkExecute($request);
     //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
        //$response = htmlspecialchars($response);
        Log::write('alipay_response:'.print_r($response,true),log::INFO);
        //echo $response;exit;//就是orderString 可以直接给客户端请求,无需再做处理。
        
        if($response){
            return $response;
        }
        return false;

$response即是APP调用时所需的支付宝订单信息,需返回到APP

  • 支付宝回调函数
require_once APP_PATH."Lib/ORG/alipay-sdk-PHP/AopSdk.php";
$aop = new AopClient;
$aop->alipayrsaPublicKey = $alipay_public;//支付宝公钥
$flag = $aop->rsaCheckV1($_POST, NULL, "RSA");
 Log::write('支付宝支付校验结果:'.print_r($flag, true).'  '.print_r($_POST, true));

 

 

APP端代码angular.js  

支付宝的cordova插件其实在github上已经有很多了,但是都已经是以前的版本了。在2016年11月的时候支付宝进行了一次更新,支付宝的SDK升级到2.0版本。以前在app中使用支付宝进行支付叫做移动支付,11月之后更名为APP支付。

安装

cordova plugin add cordova-plugin-alipay-v2 --variable APP_ID=[your AppId]

  //支付宝支付
  $scope.alipay = function(payInfo,orderid,payment_type){
       console.log('支付宝支付订单'+payInfo);

//唤起手机支付宝
       cordova.plugins.alipay.payment(payInfo,
                function success(e){
           
                       $scope.order_buy_result(orderid,payment_type);
           
                    console.log(e.resultStatus);
                    console.log(e.memo);
                    

                     var alertPopup = $ionicPopup.alert({
                      title: '友情提示',
                      template: "<p style='text-align: center'>支付完成</p>",
                      okText: '确定', // String (默认: 'OK')。OK按钮的文字。
                      okType: 'button-assertive', // String (默认: 'button-positive')。OK按钮的类型。
                    });
                    alertPopup.then(function(res) {
                        //跳转到支付成功页面
                        $state.go("pay_success", {'orderid':orderid,'chongzhi_type':caozuo}, {reload:true});
                        return false;
                    });
                    

                },function error(e){
                    console.log(e.resultStatus);
                    console.log(e.memo);
                }
        );
           
       
  }
  
  //支付宝/微信支付成功回调验证(APP端根据需求回调验证)
  $scope.order_buy_result = function(orderid,payment_type){
                   //发送请求到服务器
   }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值