View Javadoc
1   package com.foxinmy.weixin4j.pay;
2   
3   import com.alibaba.fastjson.JSON;
4   import com.foxinmy.weixin4j.pay.payment.mch.MchPayPackage;
5   import com.foxinmy.weixin4j.pay.payment.mch.SceneInfo;
6   import com.foxinmy.weixin4j.pay.payment.mch.SceneInfoApp;
7   import com.foxinmy.weixin4j.pay.payment.mch.SceneInfoStore;
8   import com.foxinmy.weixin4j.pay.type.TradeType;
9   
10  import java.util.Date;
11  
12  /**
13   * MchPayPackage生成器
14   *
15   * 微信支付中很多新增的支付产品或功能不定期的在原来各种支付API中添加参数项,导致MchPayPackage类会不断更新
16   * MchPayPackage构造方法参数会越来越多,不断增加的参数项也不好继续改构造方法。
17   * PayApi中一些特定的支付api(如JSAPI、MACROPAY)等都是直接传入参数,然后在API内构造MchPayPackage,而不是传入MchPayPackage,
18   * 一旦增加新参数就需要改API,影响正在使用SDK的工程,但如果改为直接使用MchPayPackag的createPayRequest方法,
19   * MchPayPackage的构造方式又太难看,开发者需要对着微信文档然后看着构造函数里边一大堆的参数一一匹配
20   * 所以最终有了PayPackageBuilder这个类,通过一些链式的API构造MchPayPackage,既提供最小参数的各种支付构造方法,又能让代码看上去直观一些
21   *
22   * @author kit (kit.li@qq.com)
23   * @date 2020年06月02日
24   */
25  public class PayPackageBuilder {
26      private static final String Y = "Y";
27      private MchPayPackage mchPayPackage;
28  
29      private PayPackageBuilder(){
30      }
31  
32      /**
33       * 使用MchPayPackage初始化
34       *
35       * @param payPackage
36       * @return
37       */
38      public static PayPackageBuilder init(MchPayPackage payPackage){
39          PayPackageBuilder instance = new PayPackageBuilder();
40          instance.mchPayPackage = payPackage;
41          return instance;
42      }
43  
44      /**
45       * 付款码支付/人脸支付
46       *
47       * @param body
48       *          商品描述
49       * @param outTradeNo
50       *          商户订单号
51       * @param totalFee
52       *          支付金额
53       * @param createIp
54       *          终端IP
55       * @param authCode
56       *          用户付款码
57       * @return
58       */
59      public static PayPackageBuilder microPay(String body, String outTradeNo, double totalFee, String createIp,
60                                               String authCode){
61          PayPackageBuilder instance = new PayPackageBuilder();
62          instance.mchPayPackage = new MchPayPackage(body, outTradeNo, totalFee, null, createIp, TradeType.MICROPAY,
63                  null, authCode, null, null);
64          return instance;
65      }
66  
67      /**
68       * JSAPI支付/小程序支付
69       *
70       * @param body
71       *          商品描述
72       * @param outTradeNo
73       *          商户订单号
74       * @param totalFee
75       *          支付金额
76       * @param createIp
77       *          终端IP
78       * @param notifyUrl
79       *          回调通知地址
80       * @param openid
81       *          用户标识
82       * @return
83       */
84      public static PayPackageBuilder jsapiPay(String body, String outTradeNo, double totalFee, String createIp,
85                                               String notifyUrl, String openid){
86          PayPackageBuilder instance = new PayPackageBuilder();
87          instance.mchPayPackage = new MchPayPackage(body, outTradeNo, totalFee, notifyUrl, createIp, TradeType.JSAPI,
88                  openid, null, null, null);
89          return instance;
90      }
91  
92      /**
93       * native支付
94       *
95       * @param body
96       *          商品描述
97       * @param outTradeNo
98       *          商户订单号
99       * @param totalFee
100      *          支付金额
101      * @param createIp
102      *          终端IP
103      * @param notifyUrl
104      *          回调通知地址
105      * @param productId
106      *          产品ID
107      * @return
108      */
109     public static PayPackageBuilder nativePay(String body, String outTradeNo, double totalFee, String createIp,
110                                              String notifyUrl, String productId){
111         PayPackageBuilder instance = new PayPackageBuilder();
112         instance.mchPayPackage = new MchPayPackage(body, outTradeNo, totalFee, notifyUrl, createIp, TradeType.NATIVE,
113                 null, null, productId, null);
114         return instance;
115     }
116 
117     /**
118      * APP支付
119      *
120      * @param body
121      *          商品描述
122      * @param outTradeNo
123      *          商户订单号
124      * @param totalFee
125      *          支付金额
126      * @param createIp
127      *          终端IP
128      * @param notifyUrl
129      *          回调通知地址
130      * @return
131      */
132     public static PayPackageBuilder appPay(String body, String outTradeNo, double totalFee, String createIp, String notifyUrl){
133         PayPackageBuilder instance = new PayPackageBuilder();
134         instance.mchPayPackage = new MchPayPackage(body, outTradeNo, totalFee, notifyUrl, createIp, TradeType.APP,
135                 null, null, null, null);
136         return instance;
137     }
138 
139     /**
140      * H5支付
141      *
142      * @param body
143      *          商品描述
144      * @param outTradeNo
145      *          商户订单号
146      * @param totalFee
147      *          支付金额
148      * @param createIp
149      *          终端IP
150      * @param notifyUrl
151      *          回调通知地址
152      * @param wapUrl
153      *          wap网站URL地址
154      * @param wapName
155      *          wap网站名
156      * @return
157      */
158     public static PayPackageBuilder h5Pay(String body, String outTradeNo, double totalFee, String createIp,
159                                           String notifyUrl, String wapUrl, String wapName){
160         PayPackageBuilder instance = new PayPackageBuilder();
161         instance.mchPayPackage = new MchPayPackage(body, outTradeNo, totalFee, notifyUrl, createIp, TradeType.MWEB,
162                 null, null, null, null);
163         SceneInfoApp app = SceneInfoApp.createWapAPP(wapName, wapUrl);
164         instance.mchPayPackage.setSceneInfo(String.format("{\"h5_info\":\"%s\"}", app.getSceneInfo()));
165         return instance;
166     }
167 
168     public PayPackageBuilder detail(String detail){
169         this.mchPayPackage.setDetail(detail);
170         return this;
171     }
172 
173     public PayPackageBuilder attach(String attach){
174         this.mchPayPackage.setAttach(attach);
175         return this;
176     }
177 
178     public PayPackageBuilder goodsTag(String goodsTag){
179         this.mchPayPackage.setGoodsTag(goodsTag);
180         return this;
181     }
182 
183     public PayPackageBuilder limitPay(){
184         this.mchPayPackage.setLimitPay("no_credit");
185         return this;
186     }
187 
188     public PayPackageBuilder timeStart(Date date){
189         this.mchPayPackage.setTimeStart(date);
190         return this;
191     }
192 
193     public PayPackageBuilder timeStart(String date){
194         this.mchPayPackage.setTimeStart(date);
195         return this;
196     }
197 
198     public PayPackageBuilder timeExpire(Date date){
199         this.mchPayPackage.setTimeExpire(date);
200         return this;
201     }
202 
203     public PayPackageBuilder timeExpire(String date){
204         this.mchPayPackage.setTimeExpire(date);
205         return this;
206     }
207 
208     public PayPackageBuilder receipt(){
209         this.mchPayPackage.setReceipt(Y);
210         return this;
211     }
212 
213     public PayPackageBuilder sceneInfo(SceneInfo info){
214         this.mchPayPackage.setSceneInfo(info.toJson());
215         return this;
216     }
217 
218     public PayPackageBuilder deposit(){
219         this.mchPayPackage.setDeposit(Y);
220         return this;
221     }
222 
223     public PayPackageBuilder profitSharing(){
224         this.mchPayPackage.setProfitSharing(Y);
225         return this;
226     }
227 
228     public PayPackageBuilder subOpenId(String subOpenId){
229         this.mchPayPackage.setSubOpenId(subOpenId);
230         return this;
231     }
232 
233     public PayPackageBuilder totalFee(double totalFee){
234         this.mchPayPackage.setTotalFee(totalFee);
235         return this;
236     }
237 
238     public PayPackageBuilder totalFee(int totalFee){
239         this.mchPayPackage.setTotalFee(totalFee);
240         return this;
241     }
242 
243     public MchPayPackage build(){
244         return this.mchPayPackage;
245     }
246 }