Digital signatures can ensure the reliability and anti-repudiation of the API data transmitted. When Merchant’s backend system sends API request to the payment system, the request must be signed to identify who send the request and ensure that the request is not tampered in data transmission.
There are two steps to create digital signature: 1) generating pre-sign string 2) signing this request by using pre-sign string and key with signature alogrithm。
For both API request and response, the pre-sign string should be generated according to the following step:
The correct sorting order of the data is:
{
"attach" : "It is the description of the product.",
"body" : "測試產品",
"mch_create_ip" : "127.0.0.1",
"mch_id" : "10085200000000",
"nonce_str" : "54fa7f8e1006aecb8b58ef6059abb09c",
"notify_url" : "http://www.ionline.com.hk/notify_callback",
"out_trade_no" : "TRDNO150271173415530",
"service" : "ionline.pay.weixin.native.intl",
"sign_type" : "MD5",
"total_fee" : "10"
}
The correct pre-sign string is:
attach=It is the description of the product.&body=測試產品&mch_create_ip=127.0.0.1&mch_id=10085200000000&nonce_str=54fa7f8e1006aecb8b58ef6059abb09c¬ify_url=http://www.ionline.com.hk/notify_callback&out_trade_no=TRDNO150271173415530&service=ionline.pay.weixin.native.intl&sign_type=MD5&total_fee=10
Currently, only MD5 is supported.
sign=MD5(PRE-SIGN_STRING&key=API_KEY).toUpperCase()
Example:
Suppose the request parameters are
{
"attach" : "It is the description of the product.",
"body" : "測試產品",
"mch_create_ip" : "127.0.0.1",
"mch_id" : "10085200000000",
"nonce_str" : "54fa7f8e1006aecb8b58ef6059abb09c",
"notify_url" : "http://www.ionline.com.hk/notify_callback",
"out_trade_no" : "TRDNO150271173415530",
"service" : "ionline.pay.weixin.native.intl",
"sign_type" : "MD5",
"total_fee" : "10"
}
And the API_KEY is "902d9aa50087b9fbc7898b926c2cd9f0"
- The pre-sign string "string1" is:
attach=It is the description of the product.&body=測試產品&mch_create_ip=127.0.0.1&mch_id=10085200000000&nonce_str=54fa7f8e1006aecb8b58ef6059abb09c¬ify_url=http://www.ionline.com.hk/notify_callback&out_trade_no=TRDNO150271173415530&service=ionline.pay.weixin.native.intl&sign_type=MD5&total_fee=10
- Append &key=API_KEY to "string1" and perform MD5 arithmetic to get "sign" value:
sign
=MD5(string1&key=902d9aa50087b9fbc7898b926c2cd9f0).toUpperCase()
=MD5(attach=It is the description of the product.&body=測試產品&mch_create_ip=127.0.0.1&mch_id=10085200000000&nonce_str=54fa7f8e1006aecb8b58ef6059abb09c¬ify_url=http://www.ionline.com.hk/notify_callback&out_trade_no=TRDNO150271173415530&service=ionline.pay.weixin.native.intl&sign_type=MD5&total_fee=10&key=902d9aa50087b9fbc7898b926c2cd9f0).toUpperCase()
="6C3441C872CEEC1ACF7AB1E69D1C2C76"
After receiving the data of the JSON response or XML payment result notification from the payment system, merchant’s backend should perform the following steps to verify signature.