小編給大家分享一下微信支付開發(fā)之收貨地址共享接口的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站專注于黎城網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供黎城營銷型網(wǎng)站建設(shè),黎城網(wǎng)站制作、黎城網(wǎng)頁設(shè)計(jì)、黎城網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造黎城網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供黎城網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
微信收貨地址共享,是指用戶在微信瀏覽器內(nèi)打開網(wǎng)頁,填寫過地址后,后續(xù)可以免填寫支持快速選擇,也可增加和編輯。此地址為用戶屬性,可在各商戶的網(wǎng)頁中共享使用。支持原生控件填寫地址,地址數(shù)據(jù)會(huì)傳遞到商戶。
地址共享是基于微信JavaScript API 實(shí)現(xiàn),只能在微信內(nèi)置瀏覽器中使用,其他瀏覽器調(diào)用無效。同時(shí),需要微信5.0 版本才能支持,建議通過user agent 來確定用戶當(dāng)前的版本號(hào)后再調(diào)用地址接口。以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.0 為用戶安裝的微信版本號(hào),商戶可以判定版本號(hào)是否高于或者等于5.0。
地址格式
微信地址共享使用的數(shù)據(jù)字段包括:
收貨人姓名
地區(qū),省市區(qū)三級(jí)
詳細(xì)地址
郵編
聯(lián)系電話
其中,地區(qū)對(duì)應(yīng)是國標(biāo)三級(jí)地區(qū)碼,如“廣東省-廣州市-天河區(qū)”,對(duì)應(yīng)的郵編是是510630。詳情參考鏈接:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html
獲取收貨地址之前前需要調(diào)用 登錄授權(quán)接口獲取到一次OAuth3.0的Access Token 。所以需要做一次授權(quán),這次授權(quán)是不彈出確認(rèn)框的。
其實(shí)質(zhì)就是在用戶訪問
http://www.fangbei.org/wxpay/js_api_call.php
時(shí)跳轉(zhuǎn)到
https://open.weixin.qq.com/connect/oauth3/authorize?appid=wx8888888888888888&redirect_uri=http://www.php.cn/
以此來獲得code參數(shù),并根據(jù)code來獲得授權(quán)access_token及openid,這個(gè)access token將用于收貨地址共享接口。

其實(shí)現(xiàn)的詳細(xì)流程可參考 微信公眾平臺(tái)開發(fā)OAuth3.0網(wǎng)頁授權(quán)
生成隨機(jī)字符串的方法如下

參與addrSign 簽名的字段包括:appId、url(調(diào)用JavaScript API的網(wǎng)頁url)、timestamp、noncestr、accessToken
對(duì)所有待簽名參數(shù)按照字段名的ASCII 碼從小到大排序(字典序)后,使用URL 鍵值對(duì)的格式(即key1=value1&key2=value2…)拼接成字符串string1。
這里需要注意的是簽名過程中所有參數(shù)名均為小寫字符,例如appId 在排序后字符串則為appid;
對(duì)string1作簽名算法,字段名和字段值都采用原始值,不進(jìn)行URL 轉(zhuǎn)義。具體簽名算法為addrSign = SHA1(string1)。這里給出生成addrSign 的具體示例如下:
appId=wx17ef1eaef46752cb url=http://open.weixin.qq.com/ timeStamp=1384841012 nonceStr=123456 accessToken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh2cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh6FRA
i:經(jīng)過a過程鍵值對(duì)排序后得到string1 為:
accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh2cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh6FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/?code=CODE&state=STATE
ii:經(jīng)過b過程簽名后可得到:
addrSign=SHA1(accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh2cEnCHejWQo8s5L3VvsFAdawhFxUuLmg h6FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/?code=CODE&state=STATE)=ca604c740945587544a9cc25e58dd090f200e6fb
實(shí)現(xiàn)代碼如下

編輯并獲取用戶收貨地址editAddress接口,在網(wǎng)頁前端調(diào)用。
參數(shù)列表:
| 參數(shù) | 必填 | 說明 |
|---|---|---|
| appId | 是 | 公眾號(hào)appID |
| scope | 是 | 填寫“jsapi_address”,獲得編輯地址權(quán)限 |
| signType | 是 | 簽名方式,目前僅支持SHA1 |
| addrSign | 是 | 簽名,由各參數(shù)一起參與簽名生成 |
| timeStamp | 是 | 時(shí)間戳 |
| nonceStr | 是 | 隨機(jī)字符串 |
調(diào)用方法如下

參數(shù)返回:
| 返回值 | 說明 |
|---|---|
| err_msg | edit_address:ok獲取編輯收貨地址成功 |
| edit_address:fail獲取編輯收貨地址失敗 | |
| username | 收貨人姓名 |
| telNumber | 收貨人電話 |
| addressPostalCode | 郵編 |
| proviceFirstStageName | 國標(biāo)收貨地址第一級(jí)地址 |
| addressCitySecondStageName | 國標(biāo)收貨地址第二級(jí)地址 |
| addressCountiesThirdStageName | 國標(biāo)收貨地址第三級(jí)地址 |
| addressDetailInfo | 詳細(xì)收貨地址信息 |
| nationalCode | 收貨地址國家碼 |
.jpg)
<?php
/*
方倍工作室 http://www.fangbei.org/
CopyRight 2014 All Rights Reserved
*/
define('APPID', "wx—b7559b828e3c13e");
define('APPSECRET', "2b21b42d0c497de9a691a6bb5048a601");
class class_weixin
{
var $appid = APPID;
var $appsecret = APPSECRET;
//構(gòu)造函數(shù),獲取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
}
//生成OAuth3的URL
public function oauth3_authorize($redirect_url, $scope, $state = NULL)
{
$url = "https://open.weixin.qq.com/connect/oauth3/authorize?appid=".$this->appid."&redirect_uri=".$redirect_url."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
return $url;
}
//生成OAuth3的Access Token
public function oauth3_access_token($code)
{
$url = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
$res = $this->http_request($url);
return json_decode($res, true);
}
//生成隨機(jī)字符串
function create_noncestr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++ ){
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
//生成簽名
function get_biz_sign($bizObj)
{
//參數(shù)小寫
foreach ($bizObj as $k => $v){
$bizParameters[strtolower($k)] = $v;
}
//字典序排序
ksort($bizParameters);
//URL鍵值對(duì)拼成字符串
$buff = "";
foreach ($bizParameters as $k => $v){
$buff .= $k."=".$v."&";
}
//去掉最后一個(gè)多余的&
$buff2 = substr($buff, 0, strlen($buff) - 1);
//sha1簽名
return sha1($buff2);
}
//HTTP請(qǐng)求(支持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;
}
}<?php
require_once('wxaddr.class.php');
$weixin = new class_weixin();
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if (!isset($_GET["code"])){
$jumpurl = $weixin->oauth3_authorize($url, "snsapi_base", "fangbei");
Header("Location: $jumpurl");
}else{
$oauth3_access_token = $weixin->oauth3_access_token($_GET["code"]);
$access_token = $oauth3_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);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>獲取共享收貨地址</title>
<meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
<script language="javascript">
function callpay()
{
WeixinJSBridge.invoke('editAddress',{
"appId" : "<?php echo $obj['appId'];?>",
"scope" : "jsapi_address",
"signType" : "sha1",
"addrSign" : "<?php echo $signature;?>",
"timeStamp" : "<?php echo $timestamp;?>",
"nonceStr" : "<?php echo $noncestr;?>",
},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;
});
}
</script>
</head>
<body>
<form name="form1" target="_blank">
<table border="1">
<colgroup><col width="20%"><col width="80%"></colgroup>
<TR><th>結(jié)果</th><th><INPUT value="" name="errmsg" id="9"></th>
<TR><th>國家碼</th><th><INPUT value="" name="national" id="6"></th>
<TR><th>國家</th><th><INPUT value="" name="address3" id="3"></th>
<TR><th>省</th><th><INPUT value="" name="address1" id="1"></th>
<tr><th>市</th><th><INPUT value="" name="address2" id="2"></th>
<TR><th>詳細(xì)</th><th><INPUT value="" name="detail" id="4"></th>
<TR><th>收貨人</th><th><INPUT value="" name="user" id="7"></th>
<TR><th>電話</th><th><INPUT value="" name="phone" id="5"></th>
<TR><th>郵編</th><th><INPUT value="" name="postcode" id="8"></th>
</table>
</form>
<div>
<button type="button" onclick="callpay()">獲取收貨地址</button>
</div>
</body>
</html>以上是“微信支付開發(fā)之收貨地址共享接口的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:微信支付開發(fā)之收貨地址共享接口的示例分析
分享路徑:http://www.chinadenli.net/article40/gegjho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、企業(yè)建站、外貿(mào)建站、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)