PHP程序员学习笔记|如何学习PHP

个人总结的PHP学习方法


记录一次微信支付于支付宝支付中关于APP的接口问题

2016-2-23 0phpcom 学习笔记


支付宝支付

APP调用支付宝主要使用RSA加密方式,返回APP的参数需要有提交的参数,以及提交参数的RSA加密算法


		$order_id = IFilter::act(IReq::get('order_id'));
		$user_id = IFilter::act(IReq::get('user_id'));
		//读取订单价格
		$path = IWeb::$app->getBasePath() . 'plugins/key_ali/rsa_private_key.pem';


		$order_id_last = array_pop($order_id_array);

		$total_fee = 1;//最终价格
		$notify_url= "http://dev.3haogou.com".IUrl::creatUrl('app/surePay');

		//支付宝参数
		$ali_parmart = array(
			'partner'=>"2088911916166082",
			'seller_id'=>'3fresh@celife.cn',
			'out_trade_no'=>$order_id,
			'subject'=>'测试的商品',
			'body'=>'cs',
			'total_fee'	 => $total_fee,
			'notify_url' => $notify_url,
			'service'	 => 'mobile.securitypay.pay',
			'payment_type'=>1,
			'_input_charset'=>'utf-8',
			'it_b_pay'=>"30m",
			'return_url'=>"m.alipay.com"
		);
		$ali_parmart_str = '';
		foreach($ali_parmart as $k=>$v){
			$ali_parmart_str .= $k.'="'.$v.'"&';
		}
		$ali_parmart_str = rtrim($ali_parmart_str,"&");
//		$ali_parmart_str = http_build_query($ali_parmart);


		$ali_sing = Common::payInfo($ali_parmart_str,$path);//最终计算的密室 要返回给APP
		$data['ali_sing'] = urlencode($ali_sing);
		$data['ali_info'] = $ali_parmart_str;
		$data['ali_final'] = $data['ali_info']."&sign=\"".$data['ali_sing']."\"&sign_type=\"RSA\"";//app要使用的参数
			/*
	 * @brief 支付宝支付信息
	 * $content string 提交的内容
	 * $rsa_private_key_path string rsa_private_key 路径
	 * **/
	static function payInfo($content,$rsa_private_key_path){

		$public_key= file_get_contents($rsa_private_key_path);
		$pkeyid = openssl_get_privatekey($public_key);

		openssl_sign($content, $sign, $pkeyid);
		openssl_free_key($pkeyid);
		$sign = base64_encode($sign);

		return $sign;
	}



//下面是微信的

APP需要计算2次 第一次需要计算这几个参数 注意 加密需要安装ASCLL排序


        $WxPay = new WxPayApi();
        $WxBase = new WxPayDataBase();
        $nonce_str = $WxPay->getNonceStr();
//        $this->getPrepayId($nonce_str);

        $Wx_Data['appid'] = WxPayConf_pub::APPID_app;
        $Wx_Data['attach'] = "支付测试";
        $Wx_Data['mch_id'] = WxPayConf_pub::MCHID_app;
        $Wx_Data['nonce_str'] = $nonce_str;
        $Wx_Data['body']    = '三号柜支付';
        $Wx_Data['out_trade_no'] = $order_id;
        $Wx_Data['total_fee'] = 1;
        $Wx_Data['spbill_create_ip'] = "8.8.8.8";
        $Wx_Data['notify_url'] = "http://dev.3haogou.com/app/Paywechat";
        $Wx_Data['trade_type'] = "APP";

        ksort($Wx_Data);
        $Wx_Data_str = '';
        foreach ($Wx_Data as $k=>$v) {
            $Wx_Data_str .=$k.'='.$v.'&';
        }
        $Wx_Data_str = rtrim($Wx_Data_str,'&');
        $singA = "$Wx_Data_str&key=".WxPayConf_pub::APPSECRET_app;

        $wxSing = strtoupper(md5($singA));
        $Wx_Data['sign'] = $wxSing;

        $Wx_Data_Xml = "<xml>";
        foreach($Wx_Data as $k=>$v){
            $Wx_Data_Xml .="<$k>$v</$k>";
        }
        $Wx_Data_Xml .= "</xml>";
//echo $Wx_Data_Xml;
        $Wx_Data_Xml = $this->curlSend('https://api.mch.weixin.qq.com/pay/unifiedorder',$Wx_Data_Xml);



上面返回数据


<xml>
    <return_code>
        <![CDATA[SUCCESS]]>
    </return_code>
    <return_msg>
        <![CDATA[OK]]>
    </return_msg>
    <appid>
        <![CDATA[wx7847a62939ab099a]]>
    </appid>
    <mch_id>
        <![CDATA[1315535901]]>
    </mch_id>
    <nonce_str>
        <![CDATA[tgdyebOqXcv17zaB]]>
    </nonce_str>
    <sign>
        <![CDATA[41320C1D6ED27FD9E9FCAEAC194A2E08]]>
    </sign>
    <result_code>
        <![CDATA[SUCCESS]]>
    </result_code>
    <prepay_id>
        <![CDATA[wx20160223132249e37b1f1ecf0437680137]]>
    </prepay_id>
    <trade_type>
        <![CDATA[APP]]>
    </trade_type>
</xml>


这时候获取 prepay_id 重新计算sign


 
$Wx_Data_Xml_Data = $WxBase->FromXml($Wx_Data_Xml);
        $sing_agen = array(
            'appid'=>WxPayConf_pub::APPID_app,
            'noncestr'=>$WxPay->getNonceStr(),
            'package'=>'Sign=WXPay',
            'partnerid'=>WxPayConf_pub::MCHID_app,
            'prepayid'=>$Wx_Data_Xml_Data['prepay_id'],
            'timestamp'=>time()
        );

        $sing_agen_str = '';
        foreach ($sing_agen as $k=>$v) {
            $sing_agen_str .=$k.'='.$v.'&';
        }
        $sing_agen_str = rtrim($sing_agen_str,'&');
        $sing_agen_str = "$sing_agen_str&key=".WxPayConf_pub::APPSECRET_app;
        $wxSing = strtoupper(md5($sing_agen_str));


这个时候返回
      "return_code": "SUCCESS",
      "return_msg": "OK",
      "appid": "wx7847a62939ab099a",
      "sign": "B02F02D0194D3490BFD0082EB776CE85",
      "result_code": "SUCCESS",
      "prepay_id": "wx2016022313251612b2d07d4e0364254776",
      "trade_type": "APP",
      "timestamp": 1456205117,//第二次加密时候使用的时间戳
      "packages": "Sign=WXPay",
      "partnerid": "1315535901",
      "noncestr": "zzk1k7746oatmkts0oluxc5kt6geo7i1"//第二次加密使用的随机数


« 阿里云配置 VPN服务器 | 常用的分词组件»
发表评论:









订阅Rss