Signing the request
To send a request to call an Alipay API, you must sign the request parameters to create a request URL. Complete the following steps to sign the request parameters:
1、Package the following request parameters: protocol parameters, required parameters, and optional parameters.
Note:
You can use only parameters defined by Alipay, no other parameters allowed.Take Java as an example, you can use the following code to package the parameters.
//package the request parameters
Map sParaTemp = new HashMap();
sParaTemp.put("service", AlipayConfig.service);
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
sParaTemp.put("notify_url", AlipayConfig.notify_url);
sParaTemp.put("return_url", AlipayConfig.return_url);
sParaTemp.put("out_trade_no", out_trade_no);
sParaTemp.put("subject", subject);
sParaTemp.put("total_fee", total_fee);
//sParaTemp.put("rmb_fee", rmb_fee);
sParaTemp.put("body", body);
sParaTemp.put("currency", currency);
sParaTemp.put("product_code", product_code);
//sParaTemp.put("supplier", supplier);
//sParaTemp.put("secondary_merchant_id", secondary_merchant_id);
//sParaTemp.put("secondary_merchant_name", secondary_merchant_name);
//sParaTemp.put("secondary_merchant_industry", secondary_merchant_industry);
//split_fund_info = split_fund_info.replaceAll("\"", "'");
//sParaTemp.put("split_fund_info", split_fund_info);
2、Sort the packaged parameters and generate a pre-sign string.
Combine all array values in the format of key=value, link the combinations with the character "&", and sort in an order of A to Z. You can sort the parameters by using the corresponding function provided in demo code:
- For JAVA, see alipayapi.jsp
- For PHP, see alipayapi.php
- For ASP.NET C#, see default.aspx.cs
For example, a string with sorted parameters for the MD5 sign type is as below:
_input_charset=utf-8&body=test¤cy=USD¬ify_url=http://localhost:8080/create_forex_trade-JAVA-UTF-8-MD5/notify_url.jsp&out_trade_no=test201707180942***&partner=2088101122136***&product_code=NEW_OVERSEAS_SELLER&return_url=http://localhost:8080/create_forex_trade-JAVA-UTF-8-MD5/return_url.jsp&service=create_forex_trade&subject=test123&total_fee=0.01
For example, a string with sorted parameters for the RSA sign type is as below:
_input_charset=utf-8&body=test¤cy=USD¬ify_url=http://9119c9f0.ngrok.io/create_forex_trade-JAVA-UTF-8-RSA_split/notify_url.jsp&out_trade_no=test20170816150***&partner=2088021017666***&product_code=NEW_OVERSEAS_SELLER&return_url=http://9119c9f0.ngrok.io/create_forex_trade-JAVA-UTF-8-RSA_split/notify_url.jsp&service=create_forex_trade&subject=test123&total_fee=0.01
3、Get the value of the sign parameter.
MD5 sign type
If sign_type
= MD5
, complete the following steps to get the value of sign:
- Append the MD5 private key to the string of sorted parameters and generate a new string. To get the MD5 private key, see Preparing keys. For example:
_input_charset=utf-8&body=test¤cy=USD¬ify_url=http://localhost:8080/create_forex_trade-JAVA-UTF-8-MD5/notify_url.jsp&out_trade_no=test201707180942***&partner=2088101122136***&product_code=NEW_OVERSEAS_SELLER&return_url=http://localhost:8080/create_forex_trade-JAVA-UTF-8-MD5/return_url.jsp&service=create_forex_trade&subject=test123&total_fee=0.01fa378880fd8c187391f3070a3a53500f
- Run the MD5 signature function with the new string. You will get the value of sign. For example:
sign=9d17da5e00959d8ab8b611e75301d0e8
RSA2/RSA sign type
If sign_type
= RSA2
or RSA
, complete the following steps to get the value of sign:
- Run the RSA2/RSA signature function with the pre-sign string you get in Step 2 and get the value of sign. For example, a RSA signature:
sign=H6WZp6aQqNkr5j+aclPlSz45L3udPBoXf8KS5CfHKaB1oWdDs8mte5TPy6GInJCpET0I9aSPc/lXqlTkVee64/01wyHumWQriMW7tB/0Rlko6jImL0QysX4y+sonxHs94t7wmUf9zAoAcLGZplnsn4n04u4eprBI+3SIqJDd82k=
- Encode the signature parameter (sign) into a bytecode string by using the Base64 scheme:
H6WZp6aQqNkr5j%2BaclPlSz45L3udPBoXf8KS5CfHKaB1oWdDs8mte5TPy6GInJCpET0I9aSPc%2FlXqlTkVee64%2F01wyHumWQriMW7tB%2F0Rlko6jImL0QysX4y%2BsonxHs94t7wmUf9zAoAcLGZplnsn4n04u4eprBI%2B3SIqJDd82k%3D