2021-03-04

**

通过PHP引入安卓app微信支付和支付宝支付集成**
**app微信支付:**
1.下载PHP微信支付SDK;
2.组合参数到微信获取支付参数:
 public function WechatApp($data){
 1.引入相关配置文件
        require_once "../extend/WxPay/WxPay.Api.php";
        require_once "../extend/WxPay/WxPay.JsApiPay.php";
 2.组合好对应的参数请求微信支付订单统一下单接口:https://api.mch.weixin.qq.com/pay/unifiedorder,我这里采用curl请求的方式
        $appid='注册app应用id(不是公众号的appid)';
        $mch_id='微信商户号';
        $key='微信商户key';
        $url="回调地址";
        $ip="ip地址";
        $Real_pay=金额·;
        $trade_type='APP';
        $nonce_str = random_number(32);(32位随机字符串)
 $string="appid=".$appid."&body=".$goods_name."&mch_id=".$mch_id."&nonce_str=".$nonce_str."&notify_url=".$url."&out_trade_no=".$arr['o_id']."&spbill_create_ip=".$ip."&total_fee=".$Real_pay."&trade_type=".$trade_type."&key=".$key;
3.对参数进行MD5加密获得sign
        $sign=strtoupper(md5($string));
        $xml='<xml>
                <appid>'.$appid.'</appid>
                <body>'.$goods_name.'</body>
                <mch_id>'.$mch_id.'</mch_id>
                <nonce_str>'.$nonce_str.'</nonce_str>
                <notify_url>'.$url.'</notify_url>
                <out_trade_no>'.$arr['o_id'].'</out_trade_no>
                <spbill_create_ip>'.$ip.'</spbill_create_ip>
                <total_fee>'.$Real_pay.'</total_fee>
                <trade_type>'.$trade_type.'</trade_type>
                <sign>'.$sign.'</sign>
               </xml>';
 4.这边是我自己封装的一个curl请求的类,通过curl请求获得xml格式数据
$XML=(new \app\Index\logic\CurlRequest())->httpBasic(['url'=>'https://api.mch.weixin.qq.com/pay/unifiedorder','params'=>$xml, 'method'=>'POST_XML']);
     $xmlstring = simplexml_load_string($XML, 'SimpleXMLElement', LIBXML_NOCDATA);
     $order = json_decode(json_encode($xmlstring),true);//获得数组类型数据
     5.需要对微信返回的参数进行二次加密再返回给安卓前端进行组合再唤醒支付
     $nonceStr=\WxPayApi::getNonceStr();
      $order['timeStamp'] = time()."";
        //二次签名剩余参数的补充
        $json = array(
            "appid"=>$order['appid'],
            "noncestr"=>$nonceStr,
            "package"=>"Sign=WXPay",
            "partnerid"=>$order['mch_id'],
            "prepayid"=>$order['prepay_id'],
            "timestamp"=>$order['timeStamp'],
        );
        $secondSignArray['result_code']=1;
        $secondSignArray['out_trade_no']=$arr['o_id'];
        $secondSignArray['appid']=$order['appid'];
        $secondSignArray['noncestr']=$nonceStr;
        $secondSignArray['package']="Sign=WXPay";
        $secondSignArray['prepayid']=$order['prepay_id'];
        $secondSignArray['partnerid']=$order['mch_id'];
        $secondSignArray['timestamp']=$order['timeStamp'];
        ksort($json);
        $str = '';
        foreach ($json as $k => $value) {
            $str .= !$str ? $k . '=' . $value : '&' . $k . '=' . $value;
        }
        $str.='&key='.$key;
        $sign = strtoupper(md5($str));
        $secondSignArray['sign']=$sign;
        return json_encode($secondSignArray);
    }
6.最后把返回的服务端参数进行二次加密后返回给安卓ap客户端
注释:在这里PHP主要提供服务端参数,客户端接收参数得需要另外去下载SDK,下载地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1



**支付宝支付:**
支付宝支付相对微信来说比较简单,代码如下:
 public function AppAliPay($param){
        header("Content-type:text/html;charset=utf-8");
   1.引入文件
   require_once("../extend/Alipay/aop/AopClient.php");
  require_once("../extend/Alipay/aop/request/AlipayTradeAppPayRequest.php");
        $aop = new \AopClient();
        //支付宝相关参数
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do"; // 支付宝网关
        $aop->appId = '支付宝appId';
        //私钥
        $aop->rsaPrivateKey = '申请的支付宝私钥';
        $aop->signType = 'RSA2';
        $aop->alipayrsaPublicKey = '支付宝公钥';
        $request = new \AlipayTradeAppPayRequest();
        //异步回调
        $request->setNotifyUrl("支付宝回调地址");
        $data = json_encode([
            "body"=>'项目',
            "out_trade_no" => 订单id,
            "timeout_express"=>"30m",
            "total_amount" => 支付金额,
            "subject"      => "项目名称",
            "product_code"=>"QUICK_MSECURITY_PAY"
        ]);
        $request->setBizContent($data);
        $result = $aop->sdkExecute($request);
        return $result;
    }
上一篇:设计原则--LSP 里氏替换原则


下一篇:小程序直播