This article does not say the specific code path 0-1, I will focus on how to solve a few problems
The main problems encountered paid as follows:
1. Get openid
2. unified under a single, get a pre-order number (I got up, full name prepaid transaction session identifier)
3. invoking payment
4. After payment processing
1. Obtain openid very simple, call Taro.login () to get the code, get passed to the back-end openid, this must take back end
2. Unified several issues under orders:
Probably you need so many mandatory parameters:
{ AppID: '', // AppID mch_id: '', // merchant ID nonce_str: '', // random string body: '', // Product brief description out_trade_no: '', // merchant system internal order number, the only total_fee: '', // order total, in minutes spbill_create_ip: '', // your ip, you pass to the back-end notify_url: '', // notification address, micro-channel tone, telling you to pay where trade_type: 'JSAPI' , openid: '' }
① random string
② signature
③XML assembly and analysis
①
export function randomString(len = 32) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; const maxPos = chars.length; let pwd = ''; for (let i = 0; i < len; i++) { pwd += chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; }
②
Prepare a parameter as follows
const params = { appid: '', Mch_id: '' , nonce_str:randomString(32), body: '', out_trade_no: '', total_fee: '', spbill_create_ip: '', notify_url: '', trade_type: 'JSAPI', openid: '' }
signature:
const sign = signFunc(params)
params.sign = sign
Signature function
Export function signFunc (Data) { // 1. sort key dictionary const = sortArr Object.keys (Data) .sort () // 2. turn on the key-URL // const = qsString the stringify (sortObj) the let qsString = '' sortArr.map ((T, index) => { IF (index === 0 ) { qsString = `${t}=${data[t]}` } else { qsString = `${qsString}&${t}=${data[t]}` } }) // 3.拼接string+key const stringSignTemp = qsString + `&key=${key}` // 4.MD5 signature const Sign = MD5 (stringSignTemp) .toUpperCase () return sign }
Note that I comment phrase, is called qs of npm library, do not use it to generate the URL parameter key-value pairs, because
③