Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of receiving address sharing Interface developed by WeChat Pay

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The editor would like to share with you the example analysis of the receiving address sharing interface developed by WeChat Pay. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. let's learn about it!

one。 Brief introduction

Wechat shipping address sharing means that users open a web page in Wechat browser, and after filling in the address, they can support quick selection without filling in the address, or add and edit it. This address is a user attribute and can be shared on the web pages of merchants. Support the native control to fill in the address, and the address data will be passed to the merchant.

Address sharing is based on Wechat JavaScript API and can only be used in Wechat's built-in browser. Calls from other browsers are invalid. At the same time, Wechat version 5.0 is required to support it. It is recommended to use user agent to determine the user's current version number before calling the address API. Take the iPhone version as an example, you can obtain the following example information of the Wechat version through 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", where 5.0 is the Wechat version number installed by the user, and merchants can determine whether the version number is higher than or equal to 5.0.

Address format

The data fields used by Wechat address sharing include:

Name of consignee

District, province, municipality and autonomous region

Full address

Postal code

Contact number

Among them, the regional correspondence is the national standard level 3 area code, such as "Guangdong Province-Guangzhou-Tianhe District", and the corresponding postcode is 510630. For more information, please see the link: http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html

two。 OAuth3.0 authorization

Before obtaining the shipping address, you need to call the login authorization API to obtain the Access Token of the OAuth3.0. So you need to do an authorization, and this authorization does not pop up a confirmation box.

Its essence is that when users visit

Http://www.fangbei.org/wxpay/js_api_call.php

Jump to the

Https://open.weixin.qq.com/connect/oauth3/authorize?appid=wx8888888888888888&redirect_uri=http://www.php.cn/

Use this to get the code parameter and obtain the authorization access_token and openid according to the code, which will be used for the shipping address sharing interface.

The detailed process of its implementation can refer to the Wechat public platform to develop OAuth3.0 web page authorization.

Second, get random strings

The method to generate a random string is as follows

Third, generate a signature

Fields that participate in addrSign signature include: appId, url (url of the web page that calls JavaScript API), timestamp, noncestr, accessToken

After sorting all the parameters to be signed according to the ASCII code of the field name (lexicographical order), use the format of the URL key-value pair (that is, key1=value1&key2=value2. Concatenated into the string string1

It should be noted here that all parameter names in the signature process are lowercase characters. For example, the string of appId is appid after sorting.

String1 is used as a signature algorithm, the field name and field value are all original values, and there is no URL escape. The specific signature algorithm is addrSign = SHA1 (string1). A specific example of generating addrSign is given here:

AppId=wx17ef1eaef46752cburl= http://open.weixin.qq.com/timeStamp=1384841012nonceStr=123456accessToken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh2cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh6FRA

I: after sorting the key-value pairs in the a process, the string1 is as follows:

Accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh2cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh6FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url= http://open.weixin.qq.com/?code=CODE&state=STATE

Ii: after signing in the b process, you can get:

AddrSign=SHA1 (accesstoken=OezXcEiiBSKSxW0eoylIeBFk1b8VbNtfWALJ5g6aMgZHaqZwK4euEskSn78Qd5pLsfQtuMdgmhajVM5QDm24W8X3tJ18kz5mhmkUcI3RoLm7qGgh2cEnCHejWQo8s5L3VvsFAdawhFxUuLmgh6FRA&appid=wx17ef1eaef46752cb&noncestr=123456×tamp=1384841012&url= http://open.weixin.qq.com/?code=CODE&state=STATE)=ca604c740945587544a9cc25e58dd090f200e6fb

The implementation code is as follows

Fourth, get the receiving address edit and get the user receiving address editAddress interface, which can be called in the front end of the web page.

Parameter list:

Parameter required indicates that appId is the official account appIDscope is filled in "jsapi_address", signType is the signature method to obtain the permission to edit the address, currently only SHA1addrSign is supported, and all parameters participate in signature generation. TimeStamp is a timestamp and nonceStr is a random string.

The calling method is as follows

Parameter returns:

The return value indicates that err_msgedit_address:ok successfully obtained the edit receiving address edit_address:fail failed to obtain the editing receiving address username consignee name telNumber consignee phone addressPostalCode zip code proviceFirstStageName national standard receiving address first level address addressCitySecondStageName national standard receiving address second level address addressCountiesThirdStageName national standard receiving address third level address addressDetailInfo detailed receiving address information nationalCode receiving address country code 5.

6. The complete code obtains the shared shipping address function callpay () {WeixinJSBridge.invoke ('editAddress', {"appId": "," scope ":" jsapi_address "," signType ":" sha1 "). "addrSign": "," timeStamp ":", "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;}) } result country code national province and city detailed consignee telephone postcode

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report