PHP app 喚起微信支付 獲取鏈接【項目實例】
阿新 • • 發佈:2018-03-19
PHP App支付 微信支付 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 = "<xml>";
foreach ($_data_pay as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</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);
}
}
PHP app 喚起微信支付 獲取鏈接【項目實例】