本文介紹微信支付下的收貨地址共享介面的開發流程。
微信收貨地址共享,是指用戶在微信瀏覽器內打開網頁,填寫過地址後,後續可以免填寫支持快速選擇,也可增加和編輯。此位址為使用者屬性,可在各商家的網頁中共用使用。支援原生控制填寫位址,地址資料會傳遞到商家。
位址共享是基於微信JavaScript API 實現,只能在微信內建瀏覽器中使用,其他瀏覽器呼叫無效。同時,需要微信5.0 版本才能支持,建議透過user agent 來確定使用者目前的版本號碼後再呼叫位址介面。以iPhone 版本為例,可透過useragent取得以下微信版本範例資訊:"Mozilla/5.0(iphone;CPU iphone OS 5_1_1 like Mac OS X)AppleWebKit/534.46(KHTML,like Geocko) Mobile/9B206MicroMessenger/5.0"其中5.為使用者安裝的微信版本號,商家可以判定版本號碼是否高於或等於5.0。
位址格式
微信位址共享使用的資料欄位包括:
#收貨人姓名
地區,省市區三級
詳細地址
#郵編
其實質就是在使用者存取
http://www.fangbei.org/wxpay/js_api_call.php
#時跳到
##https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://www.php.cn/
以此來獲得code參數,並根據code來獲得授權access_token及openid,這個access token將用於收貨地址共享介面。
其實現的詳細流程可參考 微信公眾平台開發OAuth2.0網頁授權
二、取得隨機字串
#三、產生簽章
這裡要注意的是簽名過程中所有參數名均為小寫字符,例如appId 在排序後字符串則為appid;
對string1作簽名算法,字段名和字段值都採用原始值,不進行URL 轉義。具體簽章演算法為addrSign = SHA1(string1)。這裡給出生成addrSign 的具體範例如下:
appId=wx17ef1eaef46752cb url=http://open.weixin.qq.com/ timeStamp=1384841012 nonceStr=123456 accessToken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh5FRA#i:經過a過程鍵值排序後得到string1 為:
#accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh5FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/?code=CODE&state=STATE
addrSign=SHA1(accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmg h5FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/?code=CODE&state=STATE)=ca604c740945587544a9cc25e58dd090f200e6fb
實作程式碼如下
四、取得收貨地址
#必填 | 說明 | |
---|---|---|
是 | 公眾號碼appID | |
是 | 填入“jsapi_address”,取得編輯位址權限 | |
是 | 簽名方式,目前僅支援SHA1 | |
是 | 簽名,由各參數一起參與簽名產生 | |
是 | 時間戳 | |
是 | 隨機字串 |
调用方法如下
参数返回:
返回值 | 说明 |
---|---|
err_msg | edit_address:ok获取编辑收货地址成功 |
edit_address:fail获取编辑收货地址失败 | |
username | 收货人姓名 |
telNumber | 收货人电话 |
addressPostalCode | 邮编 |
proviceFirstStageName | 国标收货地址第一级地址 |
addressCitySecondStageName | 国标收货地址第二级地址 |
addressCountiesThirdStageName | 国标收货地址第三级地址 |
addressDetailInfo | 详细收货地址信息 |
nationalCode | 收货地址国家码 |
0ab009c1c6d980c0253937acfceafa69appid = $appid; $this->appsecret = $appsecret; } } //生成OAuth2的URL public function oauth2_authorize($redirect_url, $scope, $state = NULL) { $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_url."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect"; return $url; } //生成OAuth2的Access Token public function oauth2_access_token($code) { $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code"; $res = $this->http_request($url); return json_decode($res, true); } //生成随机字符串 function create_noncestr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i 91031963a914a1b7ad87b66e6af1c857 $v){ $bizParameters[strtolower($k)] = $v; } //字典序排序 ksort($bizParameters); //URL键值对拼成字符串 $buff = ""; foreach ($bizParameters as $k => $v){ $buff .= $k."=".$v."&"; } //去掉最后一个多余的& $buff2 = substr($buff, 0, strlen($buff) - 1); //sha1签名 return sha1($buff2); } //HTTP请求(支持HTTP/HTTPS,支持GET/POST) protected function http_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return $output; } }
76249c644544e2723b673f512ec3ca64oauth2_authorize($url, "snsapi_base", "fangbei"); Header("Location: $jumpurl"); }else{ $oauth2_access_token = $weixin->oauth2_access_token($_GET["code"]); $access_token = $oauth2_access_token['access_token']; } $timestamp = strval(time()); $noncestr = $weixin->create_noncestr(); $obj['appId'] = $weixin->appid; $obj['url'] = $url; $obj['timeStamp'] = $timestamp; $obj['noncestr'] = $noncestr; $obj['accesstoken'] = $access_token; $signature = $weixin->get_biz_sign($obj); ?> 76c82f278ac045591c9159d381de2c57 100db36a723c770d327fc0aef2ce13b1 93f0f5c25f18dab9d176bd4f6de5d30e a71ddd814d1e6bfc91cd420de31796d5 b2386ffb911b14667cb8f0f91ea547a7获取共享收货地址6e916e0f7d1e588d4f442bf645aedb2f 5a9b9ad0f88600bf5b6691a4ccfdc587 5e18004130b03fd4aaa01fe61a84ebb1 function callpay() { WeixinJSBridge.invoke('editAddress',{ "appId" : "cc07cc36e588017f83a75f3858bb8b40", "scope" : "jsapi_address", "signType" : "sha1", "addrSign" : "1d3d1c7e9759d351642d39a10497f992", "timeStamp" : "03882a4ce45308c35874272e3b57511d", "nonceStr" : "7b41e26f1ec98d9091c9d3f4ffb4253e", },function(res){ alert(JSON.stringify(res)); document.form1.address1.value = res.proviceFirstStageName; document.form1.address2.value = res.addressCitySecondStageName; document.form1.address3.value = res.addressCountiesThirdStageName; document.form1.detail.value = res.addressDetailInfo; document.form1.national.value = res.nationalCode; document.form1.user.value = res.userName; document.form1.phone.value = res.telNumber; document.form1.postcode.value = res.addressPostalCode; document.form1.errmsg.value = res.err_msg; }); } 2cacc6d41bbb37262a98f745aa00fbf0 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 6d9483dab1de84c6f6bdd44f695e3158 c684e25378608a0aa02c2e4b83206e6f 879b49175114808d868f5fe5e24c4e0b5de06d44a19d3566e996d5b6826f61ad256dedeae30dbd1452c2575fb192a8234b4a09fdb935961d48b7bd6517f5757f f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e结果01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38e0e0f2ed3d0c19f42385c923e27fc6c9701c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e国家码01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38e8792275d6589524c351b58a284fab79301c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e国家01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38ecbb728e5bdd4f595980245b17dd3775e01c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e省01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38e33910a441ee46ebda4a7c3f5cbe59f6301c3ce868d2b3d9bce8da5c1b7e41e5b a34de1251f0d9fe1e645927f19a896e8b4d429308760b6c2d20d6300079ed38e市01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38e4c751d017c1077dfbc998d326a6cc75801c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e详细01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38e43f17b3bca69dd70c6c6331a09dad3ab01c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e收货人01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38e3a8fb09ff3825288ec29e7aa04c405b901c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e电话01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38ea07580e7f67acefbd8d7a09498dbedee01c3ce868d2b3d9bce8da5c1b7e41e5b f70d0ebff76a19226909a4a79919b5e8b4d429308760b6c2d20d6300079ed38e邮编01c3ce868d2b3d9bce8da5c1b7e41e5bb4d429308760b6c2d20d6300079ed38eb5ec44fb979e70a3b1ecf0b7c9d4d23501c3ce868d2b3d9bce8da5c1b7e41e5b f16b1740fad44fb09bfe928bcc527e08 f5a47148e367a6035fd7a2faa965022e dc6dce4a544fdca2df29d5ac0ea9906b 44e8bc14140e4029c4c8663c9eed246f获取收货地址65281c5ac262bf6d81768915a4a77ac0 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
更多微信支付开发收货地址共享接口 相关文章请关注PHP中文网!