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
14
15
16
17
18
19
20
21
22
23
24
25 public class PayPackageBuilder {
26 private static final String Y = "Y";
27 private MchPayPackage mchPayPackage;
28
29 private PayPackageBuilder(){
30 }
31
32
33
34
35
36
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
48
49
50
51
52
53
54
55
56
57
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
119
120
121
122
123
124
125
126
127
128
129
130
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 }