View Javadoc
1   package com.foxinmy.weixin4j.payment.mch;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import com.foxinmy.weixin4j.model.WeixinPayAccount;
7   import com.foxinmy.weixin4j.payment.PayRequest;
8   import com.foxinmy.weixin4j.type.SignType;
9   import com.foxinmy.weixin4j.type.TradeType;
10  import com.foxinmy.weixin4j.util.DigestUtil;
11  import com.foxinmy.weixin4j.util.MapUtil;
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 com.foxinmy.weixin4j.payment.mch.PrePay
21   * @see com.foxinmy.weixin4j.payment.PayRequest
22   * @see <a href=
23   *      "https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1">APP支付</a>
24   * @deprecated 迁移到子模块weixin4j-pay
25   */
26  @Deprecated
27  public class APPPayRequest extends AbstractPayRequest {
28      public APPPayRequest(String prePayId, WeixinPayAccount payAccount) {
29          super(prePayId, payAccount);
30      }
31  
32      @Override
33      public TradeType getPaymentType() {
34          return TradeType.APP;
35      }
36  
37      /**
38       * <font color="red">只做查看之用,请不要尝试作为支付请求</font>
39       */
40      @Override
41      public PayRequest toRequestObject() {
42          PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), "Sign=WXPay");
43          payRequest.setPartnerId(getPaymentAccount().getMchId());
44          payRequest.setPrepayId(getPrePayId());
45          Map<String, String> map = new HashMap<String, String>();
46          map.put("appid", payRequest.getAppId());
47          // 因为partnerid和prepayid在PayRequest类中是不进行序列化的
48          map.put("partnerid", payRequest.getPartnerId());
49          map.put("prepayid", payRequest.getPrepayId());
50          map.put("package", payRequest.getPackageInfo());
51          map.put("timestamp", payRequest.getTimeStamp());
52          map.put("noncestr", payRequest.getNonceStr());
53          String sign = DigestUtil.MD5(
54                  String.format("%s&key=%s", MapUtil.toJoinString(map, false, true), getPaymentAccount().getPaySignKey()))
55                  .toUpperCase();
56          payRequest.setPaySign(sign);
57          payRequest.setSignType(SignType.MD5);
58          return payRequest;
59      }
60  
61      @Override
62      public String toRequestString() {
63          PayRequest payRequest = toRequestObject();
64          StringBuilder content = new StringBuilder();
65          content.append("<xml>");
66          content.append(String.format("<appid><![CDATA[%s]]></appid>", payRequest.getAppId()));
67          content.append(String.format("<partnerid><![CDATA[%s]]></partnerid>", payRequest.getPartnerId()));
68          content.append(String.format("<prepayid><![CDATA[%s]]></prepayid>", payRequest.getPrepayId()));
69          content.append(String.format("<package><![CDATA[%s]]></package>", payRequest.getPackageInfo()));
70          content.append(String.format("<noncestr><![CDATA[%s]]></noncestr>", payRequest.getNonceStr()));
71          content.append(String.format("<timestamp><![CDATA[%s]]></timestamp>", payRequest.getTimeStamp()));
72          content.append(String.format("<sign><![CDATA[%s]]></sign>", payRequest.getPaySign()));
73          content.append("</xml>");
74          return content.toString();
75      }
76  }