1 package com.foxinmy.weixin4j.pay.payment.mch;
2
3 import com.foxinmy.weixin4j.pay.model.WeixinPayAccount;
4 import com.foxinmy.weixin4j.pay.payment.PayRequest;
5 import com.foxinmy.weixin4j.pay.type.SignType;
6 import com.foxinmy.weixin4j.pay.type.TradeType;
7 import com.foxinmy.weixin4j.util.DigestUtil;
8 import com.foxinmy.weixin4j.util.MapUtil;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class APPPayRequest extends AbstractPayRequest {
26 public APPPayRequest(String prePayId, WeixinPayAccount payAccount) {
27 super(prePayId, payAccount);
28 }
29
30 @Override
31 public TradeType getPaymentType() {
32 return TradeType.APP;
33 }
34
35
36
37
38 @Override
39 public PayRequest toRequestObject() {
40 PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), "Sign=WXPay");
41 payRequest.setPartnerId(getPaymentAccount().getMchId());
42 payRequest.setPrepayId(getPrePayId());
43 Map<String, String> map = new HashMap<String, String>();
44 map.put("appid", payRequest.getAppId());
45
46 map.put("partnerid", payRequest.getPartnerId());
47 map.put("prepayid", payRequest.getPrepayId());
48 map.put("package", payRequest.getPackageInfo());
49 map.put("timestamp", payRequest.getTimeStamp());
50 map.put("noncestr", payRequest.getNonceStr());
51 String sign = DigestUtil.MD5(
52 String.format("%s&key=%s", MapUtil.toJoinString(map, false, true), getPaymentAccount().getPaySignKey()))
53 .toUpperCase();
54 payRequest.setPaySign(sign);
55 payRequest.setSignType(SignType.MD5);
56 return payRequest;
57 }
58
59 @Override
60 public String toRequestString() {
61 PayRequest payRequest = toRequestObject();
62 StringBuilder content = new StringBuilder();
63 content.append("<xml>");
64 content.append(String.format("<appid><![CDATA[%s]]></appid>", payRequest.getAppId()));
65 content.append(String.format("<partnerid><![CDATA[%s]]></partnerid>", payRequest.getPartnerId()));
66 content.append(String.format("<prepayid><![CDATA[%s]]></prepayid>", payRequest.getPrepayId()));
67 content.append(String.format("<package><![CDATA[%s]]></package>", payRequest.getPackageInfo()));
68 content.append(String.format("<noncestr><![CDATA[%s]]></noncestr>", payRequest.getNonceStr()));
69 content.append(String.format("<timestamp><![CDATA[%s]]></timestamp>", payRequest.getTimeStamp()));
70 content.append(String.format("<sign><![CDATA[%s]]></sign>", payRequest.getPaySign()));
71 content.append("</xml>");
72 return content.toString();
73 }
74 }