View Javadoc
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   * APP支付
15   *
16   * @className APPPayRequest
17   * @author jinyu(foxinmy@gmail.com)
18   * @date 2015年12月25日
19   * @since JDK 1.6
20   * @see PrePay
21   * @see PayRequest
22   * @see <a href=
23   *      "https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1">APP支付</a>
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       * <font color="red">只做查看之用,请不要尝试作为支付请求</font>
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          // 因为partnerid和prepayid在PayRequest类中是不进行序列化的
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  }