签名请求并创建请求URL
为调用支付宝接口,您必须对请求的参数进行签名,并创建请求URL。
参数签名
1、将协议参数,必传参数及可选参数打包:
注意:
仅支付宝定义的参数可以被使用,其他参数不允许被使用。用Java语言作为示例代码,您可以通过以下代码进行参数打包:
//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、对打包好的参数进行排序,生成待签名字符串:把打包好的参数按照key=value的形式组合成字符串,按字典顺序排列。您可以使用以下示例代码中提供的相应功能模块完成参数的排序:
- JAVA: alipayapi.jsp
- PHP: alipayapi.php
- ASP.NET C#: default.aspx.cs
MD5签名类型的参数排序字符串示例如下:_input_charset=utf-8&body=test¤cy=USD¬ify_url=http://localhost:8080/create_forex_trade_wap-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_wap-JAVA-UTF-8-MD5/return_url.jsp&service=create_forex_trade_wap&subject=test123&total_fee=0.01
RSA2/RSA签名类型的参数排序字符串示例如下(以下示例为RSA示例):
_input_charset=utf-8&body=test¤cy=USD¬ify_url=http://9119c9f0.ngrok.io/create_forex_trade_wap-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_wap-JAVA-UTF-8-RSA_split/notify_url.jsp&service=create_forex_trade_wap&subject=test123&total_fee=0.01
3、获取参数sign的值:
MD5 签名类型
如果您使用MD5签名类型,请完成以下步骤来获取参数sign的值:
- 把MD5私钥拼接到已排序的参数字符串之后产生一个新的字符串。获取MD5私钥的方式请参考:秘钥准备。新生产的字符串示例如下:
_input_charset=utf-8&body=test¤cy=USD¬ify_url=http://localhost:8080/create_forex_trade_wap-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_wap-JAVA-UTF-8-MD5/return_url.jsp&service=create_forex_trade_wap&subject=test123&total_fee=0.01fa378880fd8c187391f3070a3a53500f
- 调用MD5算法生成sign值。Sign值示例:
sign=9d17da5e00959d8ab8b611e75301d0e8
RSA2/RSA 签名类型 (SHA1, SHA256)
如果您使用RSA2/RSA签名类型,请完成以下步骤来获取参数sign的值:
- 将待签名字符串和私钥带入SHA1或SHA256算法中得出sign值。RSA的Sign值示例:
sign=H6WZp6aQqNkr5j+aclPlSz45L3udPBoXf8KS5CfHKaB1oWdDs8mte5TPy6GInJCpET0I9aSPc/lXqlTkVee64/01wyHumWQriMW7tB/0Rlko6jImL0QysX4y+sonxHs94t7wmUf9zAoAcLGZplnsn4n04u4eprBI+3SIqJDd82k=
- 将签名参数使用base64解码为字节码串。字节码串示例:
H6WZp6aQqNkr5j%2BaclPlSz45L3udPBoXf8KS5CfHKaB1oWdDs8mte5TPy6GInJCpET0I9aSPc%2FlXqlTkVee64%2F01wyHumWQriMW7tB%2F0Rlko6jImL0QysX4y%2BsonxHs94t7wmUf9zAoAcLGZplnsn4n04u4eprBI%2B3SIqJDd82k%3D
创建请求URL
将网关信息,参数及参数值,签名类型信息及sign值用&符号拼接成URL链接,即请求URL。
例如,MD5类型的请求URL:
https://mapi.alipaydev.com/gateway.do?_input_charset=UTF-8&service=create_forex_trade_wap&partner=2088101122136***&out_trade_no=test201707180942***&subject=test123&body=test¤cy=USD&total_fee=0.01&product_code=NEW_OVERSEAS_SELLER¬ify_url=http://localhost:8080/create_forex_trade_wap-JAVA-UTF-8-MD5/notify_url.jsp&return_url=http://localhost:8080/create_forex_trade_wap-JAVA-UTF-8-MD5/return_url.jsp&sign_type=MD5&sign=9d17da5e00959d8ab8b611e75301d0e8
例如,RSA2/RSA类型的请求URL(以下示例为RSA示例):
https://mapi.alipaydev.com/gateway.do?_input_charset=UTF-8&service=create_forex_trade_wap&partner=2088101122136***&out_trade_no=test201707180942***&subject=test123&body=test¤cy=USD&total_fee=0.01&product_code=NEW_OVERSEAS_SELLER¬ify_url=http://localhost:8080/create_forex_trade_wap-JAVA-UTF-8-RSA/notify_url.jsp&return_url=http://localhost:8080/create_forex_trade_wap-JAVA-UTF-8-RSA/return_url.jsp&sign_type=RSA&sign=H6WZp6aQqNkr5j%2BaclPlSz45L3udPBoXf8KS5CfHKaB1oWdDs8mte5TPy6GInJCpET0I9aSPc%2FlXqlTkVee64%2F01wyHumWQriMW7tB%2F0Rlko6jImL0QysX4y%2BsonxHs94t7wmUf9zAoAcLGZplnsn4n04u4eprBI%2B3SIqJDd82k%3D
注意:
沙箱环境网关信息:https://mapi.alipaydev.com/gateway.do?
生成环境网关信息:https://intlmapi.alipay.com/gateway.do?