Shipping address sharing means that after you open a webpage in your browser and enter the address, you can quickly select or add or edit the address without entering it. This address is a user attribute and can be shared on the webpage of each merchant. Native controls are supported to enter the address, and the address data is transmitted to the merchant. This article describes the development process of the shipping address sharing interface under payment.
I. INTRODUCTION
Shipping address sharing means that after you open a webpage in your browser and enter the address, you can quickly select or add or edit the address without entering it. This address is a user attribute and can be shared on the webpage of each merchant. Native controls are supported to enter the address, and the address data is transmitted to the merchant.
Address sharing is implemented based on JavaScript APIs and can only be used in built-in browsers. other browser calls are invalid. At the same time, Version 5.0 is required. we recommend that you use the user agent to determine the current version number and then call the address interface. Take the iPhone version as an example. you can use useragent to obtain the following sample information: "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 indicates the version number installed by the user. the merchant can determine whether the version number is higher than or equal to 5.0.
Address format
Data fields used for address sharing include:
The region corresponds to the third-level geocode of the national standard, for example, "Guangdong province-Guangzhou-Tianhe District", and the corresponding zip code is 510630. Http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html for details
II. OAuth2.0 authorization
Before obtaining the shipping address, you must call the logon authorization interface to obtain the OAuth2.0 Access Token. Therefore, an authorization is required. the confirmation box is not displayed for this authorization.
The essence is that
http://www.fangbei.org/wxpay/js_api_call.php
Jump
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://www.php.cn/
To obtain the code parameter, and obtain the authorized access_token and openid based on the code. this access token will be used for the shipping address sharing interface.
Use string1 as a signature algorithm. both field names and field values use original values without URL escaping. The specific signature algorithm is addrSign = SHA1 (string1 ). The following is an example of generating an addrSign:
appId=wx17ef1eaef46752cburl=http://open.weixin.qq.com/timeStamp=1384841012nonceStr=123456accessToken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh5FRA
I: after sorting key-value pairs in process a, string1 is:
accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh5FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/?code=CODE&state=STATE
Ii: After the signature of Process B, you can obtain:
addrSign=SHA1(accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh1cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh5FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url=http://open.weixin.qq.com/?code=CODE&state=STATE)=ca604c740945587544a9cc25e58dd090f200e6fb
The implementation code is as follows:
6. complete code
Appid = $ appid; $ this-> appsecret = $ appsecret;} // Generate the oau2's 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;} // Generate the 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 );} // Generate the random string function create_noncestr ($ length = 16) {$ chars = "regular"; $ str = ""; for ($ I = 0; $ I <$ length; $ I ++) {$ str. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1);} return $ str ;} // Generate the signature function get_biz_sign ($ bizObj) {// parameter lower case foreach ($ bizObj as $ k = >$ v) {$ bizParameters [strtolower ($ k)] = $ v ;}// lexicographically sorted ksort ($ bizParameters); // The URL Key-value pair is concatenated into a string $ buff = ""; foreach ($ bizParameters as $ k => $ v) {$ buff. = $ k. "= ". $ v. "&" ;}// remove the last redundant & $ buff2 = substr ($ buff, 0, strlen ($ buff)-1 ); // sha1 signature return sha1 ($ buff2);} // HTTP request (HTTP/HTTPS supported, GET/POST supported) 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, expires, TRUE ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output ;}}
Oauth2_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 ['accessstoke N'] = $ access_token; $ signature = $ weixin-> get_biz_sign ($ obj);?>
Get shared shipping address