1
1
const assert = require ( 'assert' ) ;
2
+ const https = require ( 'https' ) ;
2
3
const { XMLParser, XMLBuilder } = require ( 'fast-xml-parser' ) ;
3
4
const utils = require ( './utils' ) ;
4
5
const Hash = require ( './hash' ) ;
@@ -117,19 +118,19 @@ class Transformer {
117
118
const key = this [ SECRET ] ;
118
119
const mch = this [ MCHID ] ;
119
120
120
- /**
121
- * @param {object } data - The API request parameters
122
- * @return {object } - With data signature
123
- */
124
121
return function signer ( data ) {
122
+ if ( this . security && utils . isObject ( this . merchant ) && utils . isString ( this . baseURL ) && this . baseURL . startsWith ( 'https://' ) ) {
123
+ Reflect . set ( this , 'httpsAgent' , new https . Agent ( this . merchant ) ) ;
124
+ }
125
+ Reflect . deleteProperty ( this , 'merchant' ) ;
126
+
125
127
const methodIsGet = isGetRequest ( this ) ;
126
128
if ( ! methodIsGet && data === undefined ) { throw new TypeError ( 'The payload(data) must be an object or Buffer or Stream.' ) ; }
127
129
if ( ! methodIsGet && ! utils . isObject ( data ) ) { return data ; }
128
130
129
131
const inter = methodIsGet ? new URLSearchParams ( this . params ) : new Map ( Object . entries ( data ) ) ;
130
- const type = inter . get ( 'sign_type' ) || Hash . ALGO_MD5 ;
131
- const mchid = inter . get ( 'mch_id' ) || inter . get ( 'mchid' ) || inter . get ( 'combine_mch_id' ) ;
132
132
133
+ const mchid = inter . get ( 'mch_id' ) || inter . get ( 'mchid' ) || inter . get ( 'combine_mch_id' ) ;
133
134
if ( mch ) {
134
135
assert . ok ( mch === mchid , `The ${ methodIsGet ? 'params' : 'data' } 's merchant ID(${ mchid } ) doesn't matched the init one(${ mch } )` ) ;
135
136
}
@@ -138,7 +139,10 @@ class Transformer {
138
139
inter . set ( 'nonce_str' , Formatter . nonce ( ) ) ;
139
140
}
140
141
141
- inter . set ( 'sign' , Hash . sign ( type , Object . fromEntries ( inter . entries ( ) ) , key ) ) ;
142
+ const type = inter . get ( 'sign_type' ) ;
143
+ inter . set ( 'sign' , Hash . sign (
144
+ [ Hash . ALGO_MD5 , Hash . ALGO_HMAC_SHA256 ] . includes ( type ) ? type : Hash . ALGO_MD5 , Object . fromEntries ( inter . entries ( ) ) , key ,
145
+ ) ) ;
142
146
143
147
return methodIsGet ? data : Object . fromEntries ( inter . entries ( ) ) ;
144
148
} ;
0 commit comments