4
4
import java .io .InputStream ;
5
5
import java .security .PrivateKey ;
6
6
import java .security .cert .Certificate ;
7
+ import java .util .ArrayList ;
7
8
import java .util .Collections ;
8
9
import java .util .Date ;
9
10
import java .util .Enumeration ;
10
11
import java .util .HashMap ;
11
12
import java .util .LinkedHashMap ;
12
13
import java .util .List ;
13
14
import java .util .Map ;
15
+ import java .util .stream .Collectors ;
14
16
15
17
import org .apache .http .HttpEntity ;
16
18
21
23
import com .alibaba .fastjson .JSON ;
22
24
import com .alibaba .fastjson .JSONObject ;
23
25
import com .egzosn .pay .common .api .BasePayService ;
26
+ import com .egzosn .pay .common .api .TransferService ;
24
27
import com .egzosn .pay .common .bean .AssistOrder ;
25
28
import com .egzosn .pay .common .bean .BillType ;
26
-
27
29
import com .egzosn .pay .common .bean .CurType ;
28
30
import com .egzosn .pay .common .bean .MethodType ;
29
31
import com .egzosn .pay .common .bean .NoticeParams ;
50
52
import com .egzosn .pay .common .util .sign .encrypt .RSA2 ;
51
53
import com .egzosn .pay .common .util .str .StringUtils ;
52
54
import com .egzosn .pay .wx .bean .WxPayError ;
53
- import com .egzosn .pay .wx .bean .WxTransferType ;
54
55
import com .egzosn .pay .wx .v3 .bean .WxAccountType ;
55
56
import com .egzosn .pay .wx .v3 .bean .WxBillType ;
56
57
import com .egzosn .pay .wx .v3 .bean .WxTransactionType ;
58
+ import com .egzosn .pay .wx .v3 .bean .WxTransferType ;
57
59
import com .egzosn .pay .wx .v3 .bean .order .Amount ;
58
60
import com .egzosn .pay .wx .v3 .bean .order .RefundAmount ;
59
61
import com .egzosn .pay .wx .v3 .bean .response .Resource ;
60
62
import com .egzosn .pay .wx .v3 .bean .response .WxNoticeParams ;
61
63
import com .egzosn .pay .wx .v3 .bean .response .WxPayMessage ;
62
64
import com .egzosn .pay .wx .v3 .bean .response .WxRefundResult ;
65
+ import com .egzosn .pay .wx .v3 .bean .transfer .TransferDetail ;
63
66
import com .egzosn .pay .wx .v3 .utils .AntCertificationUtil ;
64
67
import com .egzosn .pay .wx .v3 .utils .WxConst ;
65
68
72
75
* date 2021/10/6
73
76
* </pre>
74
77
*/
75
- public class WxPayService extends BasePayService <WxPayConfigStorage > {
78
+ public class WxPayService extends BasePayService <WxPayConfigStorage > implements TransferService {
76
79
77
80
78
81
/**
@@ -91,14 +94,14 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
91
94
*/
92
95
private volatile WxParameterStructure wxParameterStructure ;
93
96
97
+
94
98
/**
95
99
* 创建支付服务
96
100
*
97
101
* @param payConfigStorage 微信对应的支付配置
98
102
*/
99
103
public WxPayService (WxPayConfigStorage payConfigStorage ) {
100
- super (payConfigStorage );
101
-
104
+ this (payConfigStorage , null );
102
105
}
103
106
104
107
/**
@@ -110,6 +113,7 @@ public WxPayService(WxPayConfigStorage payConfigStorage) {
110
113
public WxPayService (WxPayConfigStorage payConfigStorage , HttpConfigStorage configStorage ) {
111
114
super (payConfigStorage , configStorage );
112
115
}
116
+
113
117
{
114
118
initAfter ();
115
119
}
@@ -613,14 +617,100 @@ public Map<String, Object> downloadBill(Date billDate, BillType billType) {
613
617
614
618
615
619
/**
616
- * 转账
620
+ * 发起商家转账, 转账账单电子回单申请受理接口
617
621
*
618
- * @param order 转账订单
622
+ * @param transferOrder 转账订单
619
623
* @return 对应的转账结果
620
624
*/
621
625
@ Override
622
- public Map <String , Object > transfer (TransferOrder order ) {
623
- throw new PayErrorException (new WxPayError ("" , "V3不支持转账" ));
626
+ public Map <String , Object > transfer (TransferOrder transferOrder ) {
627
+ //转账账单电子回单申请受理接口
628
+ if (transferOrder .getTransferType () == WxTransferType .TRANSFER_BILL_RECEIPT ) {
629
+ Map <String , Object > attr = new MapGen <String , Object >(WxConst .OUT_BATCH_NO , transferOrder .getBatchNo ()).getAttr ();
630
+ return getAssistService ().doExecute (attr , transferOrder .getTransferType ());
631
+ }
632
+
633
+ Map <String , Object > parameters = new HashMap <>(12 );
634
+ parameters .put (WxConst .APPID , payConfigStorage .getAppId ());
635
+ parameters .put (WxConst .OUT_BATCH_NO , transferOrder .getBatchNo ());
636
+ OrderParaStructure .loadParameters (parameters , WxConst .BATCH_NAME , transferOrder );
637
+ parameters .put (WxConst .BATCH_REMARK , transferOrder .getRemark ());
638
+ parameters .put (WxConst .TOTAL_AMOUNT , Util .conversionCentAmount (transferOrder .getAmount ()));
639
+ parameters .put (WxConst .TOTAL_NUM , transferOrder .getAttr (WxConst .TOTAL_NUM ));
640
+ Object transferDetailListAttr = transferOrder .getAttr (WxConst .TRANSFER_DETAIL_LIST );
641
+ List <TransferDetail > transferDetails = initTransferDetailListAttr (transferDetailListAttr );
642
+ parameters .put (WxConst .TRANSFER_DETAIL_LIST , transferDetails );
643
+ OrderParaStructure .loadParameters (parameters , WxConst .TRANSFER_SCENE_ID , transferOrder );
644
+ return getAssistService ().doExecute (parameters , transferOrder .getTransferType ());
645
+ }
646
+
647
+ private List <TransferDetail > initTransferDetailListAttr (Object transferDetailListAttr ) {
648
+ List <TransferDetail > transferDetails = null ;
649
+ if (transferDetailListAttr instanceof String ) {
650
+ transferDetails = JSON .parseArray ((String ) transferDetailListAttr , TransferDetail .class );
651
+ }
652
+ else if (null != transferDetailListAttr ) {
653
+ //偷懒的做法
654
+ transferDetails = JSON .parseArray (JSON .toJSONString (transferDetailListAttr ), TransferDetail .class );
655
+ }
656
+ else {
657
+ return null ;
658
+ }
659
+
660
+ String serialNumber = payConfigStorage .getCertEnvironment ().getSerialNumber ();
661
+ Certificate certificate = getAssistService ().getCertificate (serialNumber );
662
+ return transferDetails .stream ()
663
+ .peek (transferDetailListItem -> {
664
+ String userName = transferDetailListItem .getUserName ();
665
+ if (StringUtils .isNotEmpty (userName )) {
666
+ String encryptedUserName = AntCertificationUtil .encryptToString (userName , certificate );
667
+ transferDetailListItem .setUserName (encryptedUserName );
668
+ }
669
+ String userIdCard = transferDetailListItem .getUserIdCard ();
670
+ if (StringUtils .isNotEmpty (userIdCard )) {
671
+ String encryptedUserIdCard = AntCertificationUtil .encryptToString (userIdCard , certificate );
672
+ transferDetailListItem .setUserIdCard (encryptedUserIdCard );
673
+ }
674
+ }).collect (Collectors .toList ());
675
+ }
676
+
677
+ /**
678
+ * 转账查询API
679
+ * 通过批次单号查询批次单 与 通过明细单号查询明细单
680
+ *
681
+ * @param assistOrder 辅助交易订单
682
+ * @return 对应的转账订单
683
+ */
684
+ @ Override
685
+ public Map <String , Object > transferQuery (AssistOrder assistOrder ) {
686
+ Map <String , Object > parameters = new HashMap <>(6 );
687
+ TransactionType transactionType = assistOrder .getTransactionType ();
688
+ List <Object > uriVariables = new ArrayList <>(3 );
689
+
690
+ if (StringUtils .isNotEmpty (assistOrder .getTradeNo ())) {
691
+ uriVariables .add (assistOrder .getTradeNo ());
692
+ String detailId = assistOrder .getAttrForString (WxConst .DETAIL_ID );
693
+ if (StringUtils .isNotEmpty (detailId )) {
694
+ uriVariables .add (detailId );
695
+ }
696
+ }
697
+ else if (StringUtils .isNotEmpty (assistOrder .getOutTradeNo ())) {
698
+ uriVariables .add (assistOrder .getOutTradeNo ());
699
+ String outDetailNo = assistOrder .getAttrForString (WxConst .OUT_DETAIL_NO );
700
+ if (StringUtils .isNotEmpty (outDetailNo )) {
701
+ uriVariables .add (outDetailNo );
702
+ }
703
+ }
704
+
705
+ if (transactionType == com .egzosn .pay .wx .v3 .bean .WxTransferType .QUERY_BATCH_BY_BATCH_ID || transactionType == com .egzosn .pay .wx .v3 .bean .WxTransferType .QUERY_BATCH_BY_OUT_BATCH_NO ) {
706
+ OrderParaStructure .loadParameters (parameters , WxConst .NEED_QUERY_DETAIL , assistOrder );
707
+ OrderParaStructure .loadParameters (parameters , WxConst .OFFSET , assistOrder );
708
+ OrderParaStructure .loadParameters (parameters , WxConst .LIMIT , assistOrder );
709
+ OrderParaStructure .loadParameters (parameters , WxConst .DETAIL_STATUS , assistOrder );
710
+ }
711
+
712
+ String requestBody = JSON .toJSONString (parameters );
713
+ return getAssistService ().doExecute (requestBody , assistOrder .getTransactionType (), uriVariables .toArray ());
624
714
}
625
715
626
716
0 commit comments