支付宝支付,服务器如何生成支付订单

2016-10-19  本文已影响0人  贼噶人

## 从私钥文件中将私钥文件获取出来

```java

BufferedReader mBufferedReader = null;

try {

FileReader mFileReader = new FileReader(new File(

"rsa_private_key_pkcs82.pem"));

mBufferedReader = new BufferedReader(mFileReader);

StringBuilder sb = new StringBuilder();

String line = "";

while (null != (line = mBufferedReader.readLine())) {

if (!line.startsWith("-----")) {

sb.append(line);

}

}

privateKey = sb.toString();

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (null != mBufferedReader) {

try {

mBufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

```

2、生成订单信息

```java

MaporderInfoM = new HashMap<>();

orderInfoM.put("app_id", "2016092601974191");

orderInfoM.put("charset", "utf-8");

orderInfoM.put("format", "json");

orderInfoM.put("method", "alipay.trade.app.pay");

orderInfoM.put("notify_url", "http://www.mytian.com.cn/myt_pay/payAction_alipayNotify.do");

orderInfoM.put("sign_type", "RSA");

orderInfoM.put("timestamp", "2016-10-19 10:29:10");

orderInfoM.put("version", "1.0");

MapbizContent = new LinkedHashMap<>();

bizContent.put("timeout_express", "30m");

bizContent.put("seller_id", "xiaosong.xu@mytian.com.cn");

bizContent.put("product_code", "QUICK_MSECURITY_PAY");

bizContent.put("total_amount", "0.01");

bizContent.put("subject", "进阶套餐54元");

bizContent.put("body", "原价120(4.5折)6000麦粒");

bizContent.put("out_trade_no", "2961476844150427");

orderInfoM.put("biz_content", JSONObject.toJSONString(bizContent));

```

3、将订单信息用ali SDK 拼接成待签名字符串

```java

String content = AlipaySignature.getSignContent(orderInfoM);

app_id=2016092601974191&biz_content={"timeout_express":"30m","seller_id":"xiaosong.xu@mytian.com.cn","product_code":"QUICK_MSECURITY_PAY","total_amount":"0.01","subject":"进阶套餐54元","body":"原价120(4.5折)6000麦粒","out_trade_no":"2961476844150427"}&charset=utf-8&format=json&method=alipay.trade.app.pay¬ify_url=http://www.mytian.com.cn/myt_pay/payAction_alipayNotify.do&sign_type=RSA×tamp=2016-10-19 10:29:10&version=1.0

```

进行签名

sign=Cxn8TH6OqgRruFNPzU1YUUnPcYdFn2KFsSHRDipOd2oKXVL3GS6vUD44uWCVttORA62l/YEUEeZAdavlimx6slxCgSay3x1pvKKrwTrPE1RYfFEsjETvaiaJZGz5AQRhPvP5Oqw9SBky3nV2CpKrY6JFJ2kS1w7jfJPeYDxbNXU=

将所有的values新URLEncoder转义

```java

getSignContent(orderInfoM)+"&sign="+URLEncoder.encode(sign, "UTF-8")

public static String getSignContent(MapsortedParams) {        StringBuffer content = new StringBuffer();        Listkeys = new ArrayList(sortedParams.keySet());

Collections.sort(keys);

int index = 0;

for (int i = 0; i < keys.size(); ++i) {

String key = (String) keys.get(i);

String value = (String) sortedParams.get(key);

if (StringUtils.areNotEmpty(new String[] { key, value })) {

try {

content.append(((index == 0) ? "" : "&") + key + "=" + URLEncoder.encode(value, "UTF-8"));

} catch (UnsupportedEncodingException e) {

content.append(((index == 0) ? "" : "&") + key + "=" + value);

}

++index;

}

}

return content.toString();

}

```

得到最终的字符串,响应请求

app_id=2016092601974191&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22xiaosong.xu%40mytian.com.cn%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%22%E8%BF%9B%E9%98%B6%E5%A5%97%E9%A4%9054%E5%85%83%22%2C%22body%22%3A%22%E5%8E%9F%E4%BB%B7120%EF%BC%884.5%E6%8A%98%EF%BC%896000%E9%BA%A6%E7%B2%92%22%2C%22out_trade_no%22%3A%222961476844150427%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Fwww.mytian.com.cn%2Fmyt_pay%2FpayAction_alipayNotify.do&sign_type=RSA×tamp=2016-10-19+10%3A29%3A10&version=1.0&sign=Cxn8TH6OqgRruFNPzU1YUUnPcYdFn2KFsSHRDipOd2oKXVL3GS6vUD44uWCVttORA62l%2FYEUEeZAdavlimx6slxCgSay3x1pvKKrwTrPE1RYfFEsjETvaiaJZGz5AQRhPvP5Oqw9SBky3nV2CpKrY6JFJ2kS1w7jfJPeYDxbNXU%3D

上一篇下一篇

猜你喜欢

热点阅读