1 package com.foxinmy.weixin4j.pay.api;
2
3 import com.alibaba.fastjson.TypeReference;
4 import com.foxinmy.weixin4j.exception.WeixinException;
5 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
6 import com.foxinmy.weixin4j.pay.model.WeixinPayAccount;
7 import com.foxinmy.weixin4j.pay.profitsharing.*;
8 import com.foxinmy.weixin4j.pay.type.ProfitIdType;
9 import com.foxinmy.weixin4j.pay.type.SignType;
10 import com.foxinmy.weixin4j.pay.type.profitsharing.ReturnAccountType;
11 import com.foxinmy.weixin4j.util.RandomUtil;
12 import com.foxinmy.weixin4j.xml.XmlStream;
13
14 import java.util.List;
15
16
17
18
19
20
21
22
23 public class ProfitSharingApi extends MchApi {
24
25 public ProfitSharingApi(WeixinPayAccount weixinAccount) {
26 super(weixinAccount);
27 }
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public ReceiverResult addReceiver(Receiver receiver) throws WeixinException {
42 ReceiverRequest receiverRequest = new ReceiverRequest(receiver);
43 super.declareMerchant(receiverRequest);
44 String url = getRequestUri("profit_sharing_add_receiver_uri");
45 receiverRequest.setSign(weixinSignature.sign(receiverRequest, SignType.HMAC$SHA256));
46 String para = XmlStream.toXML(receiverRequest);
47 WeixinResponse response = weixinExecutor.post(url, para);
48 return response.getAsObject(new TypeReference<ReceiverResult>(){});
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public ReceiverResult removeReceiver(Receiver receiver) throws WeixinException {
64 ReceiverRequest receiverRequest = new ReceiverRequest(receiver);
65 super.declareMerchant(receiverRequest);
66 String url = getRequestUri("profit_sharing_remove_receiver_uri");
67 receiverRequest.setSign(weixinSignature.sign(receiverRequest, SignType.HMAC$SHA256));
68 String para = XmlStream.toXML(receiverRequest);
69 WeixinResponse response = weixinExecutor.post(url, para);
70 return response.getAsObject(new TypeReference<ReceiverResult>(){});
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public ProfitSharingResult profitSharing(String transactionId, String outOrderNo, List<ReceiverProfit> receivers,
89 Boolean multi) throws WeixinException {
90 ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, receivers);
91 super.declareMerchant(request);
92 String url = multi==null || multi.booleanValue()==false ? getRequestUri("profit_sharing_uri") :
93 getRequestUri("multi_profit_sharing_uri");
94 request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
95 String para = XmlStream.toXML(request);
96 WeixinResponse response = getWeixinSSLExecutor().post(url, para);
97 return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 public ProfitSharingResult profitSharingQuery(String transactionId, String outOrderNo) throws WeixinException {
112 ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, null);
113 request.setMchId(weixinAccount.getMchId());
114 request.setNonceStr(RandomUtil.generateString(16));
115 request.setSubMchId(weixinAccount.getSubMchId());
116 String url = getRequestUri("profit_sharing_query_uri");
117 request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
118 String para = XmlStream.toXML(request);
119 WeixinResponse response = weixinExecutor.post(url, para);
120 return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public ProfitSharingResult profitSharingFinish(String transactionId, String outOrderNo, String description)
140 throws WeixinException {
141 ProfitSharingRequest request = new ProfitSharingRequest(transactionId, outOrderNo, null);
142 request.setDescription(description);
143 super.declareMerchant(request);
144 String url = getRequestUri("profit_sharing_finish_uri");
145 request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
146 String para = XmlStream.toXML(request);
147 WeixinResponse response = getWeixinSSLExecutor().post(url, para);
148 return response.getAsObject(new TypeReference<ProfitSharingResult>(){});
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 public ProfitSharingReturnResult profitSharingReturn(ProfitId id, String outReturnNo,
172 ReturnAccountType returnAccountType, String returnAccount,
173 int returnAmount, String description) throws WeixinException{
174 ProfitSharingReturnRequest request;
175 if(id.getIdType()== ProfitIdType.ORDER_ID){
176 request = new ProfitSharingReturnRequest(id.getId(), null, outReturnNo, returnAccountType,
177 returnAccount, returnAmount, description);
178 }else{
179 request = new ProfitSharingReturnRequest(null, id.getId(), outReturnNo, returnAccountType,
180 returnAccount, returnAmount, description);
181 }
182 super.declareMerchant(request);
183 String url = getRequestUri("profit_sharing_return_uri");
184 request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
185 String para = XmlStream.toXML(request);
186 WeixinResponse response = getWeixinSSLExecutor().post(url, para);
187 return response.getAsObject(new TypeReference<ProfitSharingReturnResult>(){});
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public ProfitSharingReturnResult profitSharingReturnQuery(ProfitId id, String outReturnNo) throws WeixinException{
203 ProfitSharingReturnRequest request;
204 if(id.getIdType()== ProfitIdType.ORDER_ID){
205 request = new ProfitSharingReturnRequest(id.getId(), null, outReturnNo);
206 }else{
207 request = new ProfitSharingReturnRequest(null, id.getId(), outReturnNo);
208 }
209 super.declareMerchant(request);
210 String url = getRequestUri("profit_sharing_return_query_uri");
211 request.setSign(weixinSignature.sign(request, SignType.HMAC$SHA256));
212 String para = XmlStream.toXML(request);
213 WeixinResponse response = weixinExecutor.post(url, para);
214 return response.getAsObject(new TypeReference<ProfitSharingReturnResult>(){});
215 }
216 }