<?php
// STEP 0. 账号帐户资料
//更改商户把相关参数后可测试
$APP_ID="" ; //APPID
$APP_SECRET="";//appsecret
//商户号,填写商户对应参数
$MCH_ID="";
//商户API密钥,填写相应参数
$PARTNER_ID="";
//支付结果回调页面
$NOTIFY_URL="";
//STEP 1. 构造一个订单。
$order=array(
"body" => "V3支付测试",
"appid" => $APP_ID,
"device_info" => "APP-001",
"mch_id" => $MCH_ID,
"nonce_str" => mt_rand(),
"notify_url" => $NOTIFY_URL,
"out_trade_no" => time(),
"spbill_create_ip" => "196.168.1.1",
"total_fee" => 1,//这里的最小单位时分,跟支付宝不一样。1就是1分钱。只能是整形。
"trade_type" => "APP"
);
ksort($order);
//STEP 2. 签名
$sign="";
foreach ($order as $key => $value) {
if($value&&$key!="sign"&&$key!="key"){
$sign.=$key."=".$value."&";
}
}
$sign.="key=".$PARTNER_ID;
$sign=strtoupper(md5($sign));
//STEP 3. 请求服务器
$xml="<xml>\n";
foreach ($order as $key => $value) {
$xml.="<".$key.">".$value."</".$key.">\n";
}
$xml.="<sign>".$sign."</sign>\n";
$xml.="</xml>";
// print_R($xml);
// die;
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => 'Content-type: text/xml',
'content' => $xml
),
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://api.mch.weixin.qq.com/pay/unifiedorder', false, $context);
$result = simplexml_load_string($result,null, LIBXML_NOCDATA);
//使用$result->nonce_str和$result->prepay_id。再次签名返回app可以直接打开的链接。
echo "<pre>";
print_R($result);
die;
$input=array(
"noncestr"=>"".$result->nonce_str,
"prepayid"=>"".$result->prepay_id,//上一步请求微信服务器得到nonce_str和prepay_id参数。
"appid"=>$APP_ID,
"package"=>"Sign=WXPay",
"partnerid"=>$MCH_ID,
"timestamp"=>time(),
);
ksort($input);
$sign="";
foreach ($input as $key => $value) {
if($value&&$key!="sign"&&$key!="key"){
$sign.=$key."=".$value."&";
}
}
$sign.="key=".$PARTNER_ID;
echo "<pre>";
print_R($input);
$sign=strtoupper(md5($sign));
Print_R($sign);
die;
$iOSLink=sprintf("weixin://app/%s/pay/?nonceStr=%s&package=Sign%%3DWXPay&partnerId=%s&prepayId=%s&timeStamp=%s&sign=%s&signType=SHA1",$APP_ID,$input["noncestr"],$MCH_ID,$input["prepayid"],$input["timestamp"],$sign);
echo $iOSLink;
//或者在Safari中打开以便测试。
echo "<h1><a href='${iOSLink}'>微信支付</a></h1>";