Skip to content

Commit

Permalink
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 18 additions & 1 deletion lib/transformer.js
Original file line number Diff line number Diff line change
@@ -5,10 +5,19 @@ const hash = require('./hash')
/*eslint-disable-next-line*/
const SECRET = Symbol('SECRET')

/*eslint-disable-next-line*/
const MCHID = Symbol('MCHID')

/**
* An Axios customizaton transform.
*/
class Transformer {
/**
* @property {string} mchid - The merchant ID
*/
static get mchid() { return this[MCHID] }
static set mchid(value) { this[MCHID] = value }

/**
* @property {string} secret - The merchant secret key string
*/
@@ -17,11 +26,19 @@ class Transformer {

/**
* Compose the pre-request data signature
*
* Note here: While the [MCHID] is set, then checking the input data matching with it.
*
* @param {object} data - The API request parameters
* @return {object} - With data signature
*/
static signer(data) {
const {sign_type: type = `MD5`} = data
const {sign_type: type = `MD5`, mch_id: mchid} = data
Transformer.mchid === void 0 || assert.ok(
Transformer.mchid === mchid,
`The data.mch_id(${mchid}) doesn't matched init one(${Transformer.mchid})`
)

Object.assign(data, {sign: hash.sign(type, data, Transformer.secret)})

return data
9 changes: 5 additions & 4 deletions lib/wechatpay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const https = require('https')
const axios = require('axios')
const interceptor = require('./interceptor')
const transformer = require('./transformer')
const Transformer = require('./transformer')
const utils = require('./utils')

/*eslint-disable-next-line*/
@@ -205,7 +205,8 @@ class Wechatpay {
}
const httpsAgent = new https.Agent(tlsOptions)

transformer.secret = wxpaySettings.secret
Transformer.mchid = wxpaySettings.mchid
Transformer.secret = wxpaySettings.secret

return axios.create(utils.extend(axiosConfig, {
httpsAgent,
@@ -214,8 +215,8 @@ class Wechatpay {
'User-Agent': utils.userAgent(),
},
responseType: 'text',
transformRequest: transformer.request,
transformResponse: transformer.response,
transformRequest: Transformer.request,
transformResponse: Transformer.response,
}))
}

0 comments on commit 97e4b53

Please sign in to comment.