PHPapp唤起微信支付获取链接【项目实例】
public function weixin_params_data(){ $data = $this->ApiData(); //获取接口访问的数据 //$_type = $post_data['type']; if(!$data['out_trade_no']){ systemmess('0','参数错误'); } $WxPayConfig = array( 'APPID'=>'wxc123456789', 'MCHID'=>'123456', 'KEY'=>'KEY', 'APPSECRET'=>'APPSECRET' ); $out_trade_no = $data['out_trade_no']; //订单号 //订单名称,必填 $proName ='app微信支付'; //付款金额,必填 $total_amount = 1*100; //一分 *100 //商品描述,可空 $body = $oid_body;//trim($_POST['WIDbody']); $return_url = 'http://'.$_SERVER['HTTP_HOST'].''.U('Appreturn/notify_weixin'); // 异步链接 $_data_pay['appid'] = $WxPayConfig['APPID']; //应用ID $_data_pay['mch_id'] = $WxPayConfig['MCHID']; //商户号 $_data_pay['nonce_str'] = md5(rand(100000,999999)); //随机字符串 $_data_pay['sign_type'] = 'MD5'; //签名类型 $_data_pay['body'] = ''.$body; //商品描述 $_data_pay['attach'] = $_type; //附加数据 $_data_pay['out_trade_no'] = $out_trade_no; //商户订单号 $_data_pay['fee_type'] = 'CNY'; //货币类型 $_data_pay['total_fee'] = $total_amount; //总金额 $_data_pay['spbill_create_ip'] = $data['ip']; //终端IP $_data_pay['notify_url'] = $return_url; //通知地址 $_data_pay['trade_type'] = 'APP'; //交易类型 $_data_pay['sign'] = $this->get_weixin_sign($_data_pay,$WxPayConfig['KEY']); //签名 $xml = ""; foreach ($_data_pay as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."".$key.">"; }else{ $xml.="<".$key.">".$key.">"; } } $xml.=" "; // $json['xml'] = $xml; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 //设置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); // $json['data'] = $data; $_data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true); $prepay_id = $_data['prepay_id']; if(!$prepay_id){ //json_echo(1,"系统错误");exit; $json['msg']='1'; $json['state']='2'; $json['_data_pay']=$_data_pay; $json['data']=$data; $json['error']=$_data['return_code'].$_data['return_msg']; echo json_encode($json);exit; } } else { $error = curl_errno($ch); curl_close($ch); $json['error'] = $error; //json_echo(1,"ok",$json);exit; } // $json_data['msg'] = '1'; // $json_data['state'] = '1'; $json_data['appid'] = $WxPayConfig['APPID']; $json_data['partnerid'] = $WxPayConfig['MCHID']; //商户号 $json_data['prepayid'] = $prepay_id; $json_data['package'] = 'Sign=WXPay'; $json_data['noncestr'] = $_data_pay['nonce_str']; $json_data['timestamp'] = time(); $json_data['sign'] = $this->get_weixin_sign($json_data,$WxPayConfig['KEY']); $_json['msg']= '1'; $_json['state']= '1'; $_json['data']= $json_data; echo json_encode($_json);exit; } // 微信app支付 sign private function get_weixin_sign($data,$key){ ksort($data); $buff = ""; foreach ($data as $k => $v){ if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&") . "&key=".$key; $string = md5($buff); //签名步骤四:所有字符转为大写 $result = strtoupper($string); return $result; } //异步通知地址 public function notify_weixin(){ $xml = $GLOBALS['HTTP_RAW_POST_DATA']; //将XML转为array //禁止引用外部xml实体 libxml_disable_entity_loader(true); $return_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); M('log')->add(array('title'=>'微信','content'=>json_encode($return_data))); if($return_data['return_code'] === "SUCCESS" && $return_data['result_code'] === "SUCCESS"){ $out_trade_no = $return_data['out_trade_no']; $type = substr($out_trade_no,0,1); //订单操作 //$this->edit_order_info($return_data['attach'],$return_data['out_trade_no'],$return_data['transaction_id'],$return_data['time_end'],($return_data['total_fee']/100),'wxpay',$return_data); } }
网站栏目:PHPapp唤起微信支付获取链接【项目实例】
本文来源:http://myzitong.com/article/giipdc.html