chengf
2025-08-29 fef2c4d637ca36cd8379b98d1e54d68a7a0536b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.java110.acct.cmd.payment;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.acct.payment.IPaymentBusiness;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.CommunitySettingFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.factory.WechatFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.payment.*;
import com.java110.dto.wechat.SmallWeChatDto;
import com.java110.intf.acct.IPaymentKeyV1InnerServiceSMO;
import com.java110.intf.acct.IPaymentPoolConfigV1InnerServiceSMO;
import com.java110.intf.acct.IPaymentPoolV1InnerServiceSMO;
import com.java110.intf.acct.IPaymentPoolValueV1InnerServiceSMO;
import com.java110.intf.fee.IPayFeeV1InnerServiceSMO;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.cache.UrlCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.PayUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
 
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * native qrcode 支付
 * add by wuxw 2023-06-25
 */
@Java110Cmd(serviceCode = "payment.nativeQrcodePayment")
public class NativeQrcodePaymentCmd extends Cmd {
 
 
    private static final Logger logger = LoggerFactory.getLogger(NativeQrcodePaymentCmd.class);
 
    protected static final String DEFAULT_PAYMENT_ADAPT = "wechatNativeQrcodePaymentFactory";// 默认微信通用支付
 
    @Autowired
    private IPaymentKeyV1InnerServiceSMO paymentKeyV1InnerServiceSMOImpl;
 
    @Autowired
    private IPaymentPoolValueV1InnerServiceSMO paymentPoolValueV1InnerServiceSMOImpl;
 
    @Autowired
    private IPaymentPoolConfigV1InnerServiceSMO paymentPoolConfigV1InnerServiceSMOImpl;
 
    @Autowired
    private IPaymentPoolV1InnerServiceSMO paymentPoolV1InnerServiceSMOImpl;
 
    @Autowired
    private IPayFeeV1InnerServiceSMO payFeeV1InnerServiceSMOImpl;
 
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        Assert.hasKeyAndValue(reqJson, "business", "未包含业务");
        Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区");
 
    }
 
 
 
    private static final String VERSION = "1.0";
    @Value("${fuiou.pay.unified-order-url}")
    public String PAY_UNIFIED_ORDER_URL;
    @Autowired
    private RestTemplate outRestTemplate;
    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
//        if (!(reqJson.containsKey("communityId"))){
//            reqJson.put("communityId","2025062600070012");
//        }
        PaymentPoolDto paymentPoolDto = new PaymentPoolDto();
        paymentPoolDto.setCommunityId(reqJson.getString("communityId"));
        paymentPoolDto.setPage(1);
        paymentPoolDto.setRow(10);
        List<PaymentPoolDto> paymentPoolDtos = paymentPoolV1InnerServiceSMOImpl.queryPaymentPools(paymentPoolDto);
        List<PaymentPoolDto> collect = paymentPoolDtos.stream().filter(e -> e.getPaymentType().equals("FUIOU")).collect(Collectors.toList());
        PaymentPoolValueDto paymentPoolValueDto = new PaymentPoolValueDto();
        paymentPoolValueDto.setPpId(collect.get(0).getPpId());
        paymentPoolValueDto.setCommunityId(reqJson.getString("communityId"));
        List<PaymentPoolValueDto> values =  paymentPoolValueV1InnerServiceSMOImpl.queryPaymentPoolValues(paymentPoolValueDto);
        Map<String, List<PaymentPoolValueDto>> payMap = values.stream().collect(Collectors.groupingBy(PaymentPoolValueDto::getColumnKey));
        SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
        if(payMap.containsKey("FUIOU_APP_ID")){
            smallWeChatDto.setAppId(payMap.get("FUIOU_APP_ID").get(0).getColumnValue());
        }else{
            smallWeChatDto.setAppId(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId"));
        }
        if(payMap.containsKey("FUIOU_MCHNT_KEY")){
            smallWeChatDto.setAppSecret(payMap.get("FUIOU_MCHNT_KEY").get(0).getColumnValue());
        }else{
            smallWeChatDto.setAppSecret(MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret"));
        }
        if(payMap.containsKey("FUIOU_ORDER_PRE")){
            smallWeChatDto.setOrderPre(payMap.get("FUIOU_ORDER_PRE").get(0).getColumnValue());
        }
        if(payMap.containsKey("FUIOU_MERCHANT_ID")){
            smallWeChatDto.setMchId(payMap.get("FUIOU_MERCHANT_ID").get(0).getColumnValue());
        }else{
            smallWeChatDto.setMchId(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchId"));
        }
        logger.debug(">>>>>>>>>>>>>>>>支付参数报文,{}", reqJson.toJSONString());
        String appId = context.getReqHeaders().get("app-id");
        String userId = context.getReqHeaders().get("user-id");
        reqJson.put("createUserId", userId);
        reqJson.put("storeId", CmdContextUtils.getStoreId(context));
 
        //1.0 查询当前支付的业务
 
        IPaymentBusiness paymentBusiness = ApplicationContextFactory.getBean(reqJson.getString("business"), IPaymentBusiness.class);
 
        if (paymentBusiness == null) {
            throw new CmdException("当前支付业务不支持");
        }
 
        //2.0 相应业务 下单 返回 单号 ,金额,
        PaymentOrderDto paymentOrderDto = paymentBusiness.unified(context, reqJson);
        paymentOrderDto.setAppId(appId);
        paymentOrderDto.setUserId(userId);
        reqJson.put("money", paymentOrderDto.getMoney());
 
        String token = GenerateCodeFactory.getUUID();
 
        // redis 中 保存 请求参数
        CommonCache.setValue("nativeQrcodePayment_" + token, reqJson.toJSONString(), CommonCache.PAY_DEFAULT_EXPIRE_TIME);
        JSONObject result = new JSONObject();
 
        String notifyUrl = UrlCache.getOwnerUrl() + "/app/payment/notify/wechat/"+appId+"/"+reqJson.getString("paymentPoolId");
 
//
//
//        List<PaymentKeyDto> paymentKeyDtos = null;
//
//        PaymentKeyDto paymentKeyDto = new  PaymentKeyDto();
//        paymentKeyDto.setPaymentType("FUIOU");
//        paymentKeyDtos = paymentKeyV1InnerServiceSMOImpl.queryPaymentKeys(paymentKeyDto);
//
 
        String systemName = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.PAY_GOOD_NAME);
        JSONObject paramMap = new JSONObject();
        paramMap.put("version", VERSION);
        paramMap.put("mchnt_cd", smallWeChatDto.getMchId()); // 富友分配给二级商户的商户号
        paramMap.put("random_str", PayUtil.makeUUID(32));
        paramMap.put("order_amt", (int)(paymentOrderDto.getMoney()*100));
        String generatorId = GenerateCodeFactory.getGeneratorId("81");
        paramMap.put("mchnt_order_no", smallWeChatDto.getOrderPre() + generatorId);
        paramMap.put("txn_begin_ts", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT));
        paramMap.put("goods_des", systemName + "测试");
        paramMap.put("term_id", "abcdefgh");
        paramMap.put("term_ip", PayUtil.getLocalIp());
        paramMap.put("notify_url", notifyUrl + "?wId=" + WechatFactory.getWId(smallWeChatDto.getAppId()));
        paramMap.put("trade_type", "WXBXLET");
        paramMap.put("order_type", "WECHAT");
//        paramMap.put("sub_openid", openid);
//        paramMap.put("sub_appid", smallWeChatDto.getAppId());
 
        paramMap.put("sign", createSign(paramMap, smallWeChatDto.getAppSecret()));
 
        logger.debug("调用支付统一下单接口" + paramMap.toJSONString());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        HttpEntity httpEntity = new HttpEntity(paramMap.toJSONString(), headers);
        ResponseEntity<String> responseEntity = outRestTemplate.exchange(PAY_UNIFIED_ORDER_URL
                , HttpMethod.POST, httpEntity, String.class);
        if(JSONObject.parseObject(responseEntity.getBody()).get("result_msg").equals("SUCCESS")){
            result.put("codeUrl", JSONObject.parseObject(responseEntity.getBody()).get("qr_code"));
            ResponseEntity<String> responseEntity2 = ResultVo.createResponseEntity(result);
            logger.debug("调用支付厂家返回,{}", responseEntity2);
            context.setResponseEntity(responseEntity2);
        }else{
            result.put("codeUrl", JSONObject.parseObject(responseEntity.getBody()).get("qr_code"));
            ResponseEntity<String> responseEntity2 = ResultVo.createResponseEntity(responseEntity);
            logger.debug("调用支付厂家返回,{}", responseEntity);
            context.setResponseEntity(responseEntity2);
        }
 
    }
    private String createSign(JSONObject paramMap, String payPassword) {
        String str = paramMap.getString("mchnt_cd") + "|"
                + paramMap.getString("order_type") + "|"
                + paramMap.getString("order_amt") + "|"
                + paramMap.getString("mchnt_order_no") + "|"
                + paramMap.getString("txn_begin_ts") + "|"
                + paramMap.getString("goods_des") + "|"
                + paramMap.getString("term_id") + "|"
                + paramMap.getString("term_ip") + "|"
                + paramMap.getString("notify_url") + "|"
                + paramMap.getString("random_str") + "|"
                + paramMap.getString("version") + "|"
                + payPassword;
        return PayUtil.md5(str);
    }
 
}