微信支付开发当前URL未注册的解决方案
流程实现
1. OAuth2.0授权
JSAPI 支付前需要调用 登录授权接口获取到用户的 Openid 。所以需要做一次授权,这次授权是不弹出确认框的。
其实质就是在用户访问http://www.fangbei.org/wxpay/js_api_call.php
时跳转到https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://www.fangbei.org/wxpay/js_api_call.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
以此来获得code参数,并根据code来获得授权access_token及openid
其实现的详细流程可参考 微信公众平台开发(71)OAuth2.0网页授权
在微信支付的Demo中,其代码为
1 //使用jsapi接口<br>
2 $jsApi = new JsApi_pub();<br>
3 <br>
4 //=========步骤1:网页授权获取用户openid============<br>
5 //通过code获得openid<br>
6 if (!isset($_GET['code']))<br>
7 {<br>
8 //触发微信返回code码<br>
9 $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);<br>
10 Header("Location: $url"); <br>
11 }else<br>
12 {<br>
13 //获取code码,以获取openid<br>
14 $code = $_GET['code'];<br>
15 $jsApi->setCode($code);<br>
16 $openid = $jsApi->getOpenId();<br>
17 }
1 //使用jsapi接口ou9dHt0L8qFLI1foP-kj5x1mDWsM
2 $jsApi = new JsApi_pub();
3
4 //=========步骤1:网页授权获取用户openid============
5 //通过code获得openid
6 if (!isset($_GET['code']))$this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID<br>
$this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号<br>
$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip <br>
$this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串<br>
$this->parameters["sign"] = $this->getSign($this->parameters);//签名
7 {//统一支付接口中,trade_type为JSAPI时,openid为必填参数!<br>
$unifiedOrder->setParameter("openid","$openid");//商品描述<br>
$unifiedOrder->setParameter("body","方倍工作室");//商品描述<br>
//自定义订单号,此处仅作举例<br>
$timeStamp = time();<br>
$out_trade_no = WxPayConf_pub::APPID."$timeStamp";<br>
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 <br>
$unifiedOrder->setParameter("total_fee","1");//总金额<br>
$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 <br>
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
8 //触发微信返回code码//非必填参数,商户可根据实际情况选填<br>
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号 <br>
//$unifiedOrder->setParameter("device_info","XXXX");//设备号 <br>
//$unifiedOrder->setParameter("attach","XXXX");//附加数据 <br>
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间<br>
//$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 <br>
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 <br>
//$unifiedOrder->setParameter("openid","XXXX");//用户标识<br>
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
9 $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
10 Header("Location: $url"); <br>
11 }else
12 {
13 //获取code码,以获取openid
14 $code = $_GET['code'];
15 $jsApi->setCode($code);
16 $openid = $jsApi->getOpenId();
17 }
这一步的最终结果就是获得了当前用户的openid
2. 统一支付
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口
统一支付中以下参数从配置中获取,或由类自动生成,不需要用户填写$this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
$this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
$this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
$this->parameters["sign"] = $this->getSign($this->parameters);//签名在JSAPI支付中,另外填写以下参数//统一支付接口中,trade_type为JSAPI时,openid为必填参数!
$unifiedOrder->setParameter("openid","$openid");//商品描述
$unifiedOrder->setParameter("body","方倍工作室");//商品描述
//自定义订单号,此处仅作举例
$timeStamp = time();
$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
$unifiedOrder->setParameter("total_fee","1");//总金额
$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型其他为选填参数//非必填参数,商户可根据实际情况选填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
//$unifiedOrder->setParameter("device_info","XXXX");//设备号
//$unifiedOrder->setParameter("attach","XXXX");//附加数据
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
//$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
//$unifiedOrder->setParameter("openid","XXXX");//用户标识
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID这些参数最终组成了这样的xml数据,
https://api.mch.weixin.qq.com/pay/unifiedorder
https://api.mch.weixin.qq.com/pay/unifiedorder<xml><br>
<return_code><![CDATA[SUCCESS]]></return_code> <br>
<return_msg><![CDATA[OK]]></return_msg> <br>
<appid><![CDATA[wx8888888888888888]]></appid> <br>
<mch_id><![CDATA[10012345]]></mch_id> <br>
<nonce_str><![CDATA[Be8YX7gjCdtCT7cr]]></nonce_str> <br>
<sign><![CDATA[885B6D84635AE6C020EF753A00C8EEDB]]></sign> <br>
<result_code><![CDATA[SUCCESS]]></result_code> <br>
<prepay_id><![CDATA[wx201410272009395522657a690389285100]]></prepay_id> <br>
<trade_type><![CDATA[JSAPI]]></trade_type> <br>
</xml>
将获得返回 如下数据wx201410272009395522657a690389285100
$jsApi->setPrepayId($prepay_id);<br>
$jsApiParameters = $jsApi->getParameters();
{<br>
"appId": "wx8888888888888888",<br>
"timeStamp": "1414411784",<br>
"nonceStr": "gbwr71b5no6q6ne18c8up1u7l7he2y75",<br>
"package": "prepay_id=wx201410272009395522657a690389285100",<br>
"signType": "MD5",<br>
"paySign": "9C6747193720F851EB876299D59F6C7D"<br>
}
其中包含了最重要的预支付ID参数,prepay_id,值为
wx201410272009395522657a690389285100
3、JS API支付
前面的准备工作做好了以后,JS API根据prepay_id生成jsapi支付参数
生成代码如下
//=========步骤3:使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);<br>
$jsApiParameters = $jsApi->getParameters();生成的json数据如下{
"appId": "wx8888888888888888",
"timeStamp": "1414411784",
"nonceStr": "gbwr71b5no6q6ne18c8up1u7l7he2y75",
"package": "prepay_id=wx201410272009395522657a690389285100",
"signType": "MD5",
"paySign": "9C6747193720F851EB876299D59F6C7D"
}在微信浏览器中调试起js接口,代码如下
<xml><code class="prettyprint linenums lang-php"><xml><br>
<appid><![CDATA[wx8888888888888888]]></appid> <br>
<bank_type><![CDATA[CFT]]></bank_type> <br>
<fee_type><![CDATA[CNY]]></fee_type> <br>
<is_subscribe><![CDATA[Y]]></is_subscribe> <br>
<mch_id><![CDATA[10012345]]></mch_id> <br>
<nonce_str><![CDATA[60uf9sh6nmppr9azveb2bn7arhy79izk]]></nonce_str> <br>
<openid><![CDATA[ou9dHt0L8qFLI1foP-kj5x1mDWsM]]></openid> <br>
<out_trade_no><![CDATA[wx88888888888888881414411779]]></out_trade_no> <br>
<result_code><![CDATA[SUCCESS]]></result_code> <br>
<return_code><![CDATA[SUCCESS]]></return_code> <br>
<sign><![CDATA[0C1D7F2534F1473247550A5A138F0CEB]]></sign> <br>
<sub_mch_id><![CDATA[10012345]]></sub_mch_id> <br>
<time_end><![CDATA[20141027200958]]></time_end> <br>
<total_fee>1</total_fee> <br>
<trade_type><![CDATA[JSAPI]]></trade_type> <br>
<transaction_id><![CDATA[1002750185201410270005514026]]></transaction_id> <br>
</xml>
http://www.fangbei.org/wxpay/js_api_call.php
http://www.fangbei.org/wxpay/
http://www.fangbei.org/wxpay/order/id/56.html
http://www.fangbei.org/wxpay/order/id/
http://www.fangbei.org/wxpay/js_api_call.php你就必须填写:<code class="prettyprint linenums lang-php">http://www.fangbei.org/wxpay/假如是:<code class="prettyprint linenums lang-php">http://www.fangbei.org/wxpay/order/id/56.html你就必须写:<code class="prettyprint linenums lang-php">http://www.fangbei.org/wxpay/order/id/看出规律了吧,就是把最后一个反斜杠后面的内容去掉就OK了。如果还有什么问题可以留言问我。 AD:真正免费,域名 虚机 企业邮箱=0元