| | |
| | | package com.java110.acct.payment.adapt.fuiou; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.java110.acct.payment.IPaymentFactoryAdapt; |
| | | import com.java110.core.context.ICmdDataFlowContext; |
| | | import com.java110.core.factory.CommunitySettingFactory; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.SortedMap; |
| | | import java.util.TreeMap; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | public static final String TRADE_TYPE_JSAPI = "JSAPI"; |
| | | public static final String TRADE_TYPE_MWEB = "MWEB"; |
| | | public static final String TRADE_TYPE_APP = "APP"; |
| | | public static final String TRADE_TYPE_LETPAY = "LETPAY"; |
| | | |
| | | @Value("${fuiou.pay.unified-order-url}") |
| | | public String PAY_UNIFIED_ORDER_URL; |
| | | @Value("${fuiou.pay.wx-order-url}") |
| | | public String PAY_WX_ORDER_URL; |
| | | private static final String VERSION = "1.0"; |
| | | |
| | | |
| | |
| | | |
| | | SmallWeChatDto smallWeChatDto = getSmallWechat(reqJson); |
| | | |
| | | smallWeChatDto.setAppId(reqJson.getString("appId")); |
| | | |
| | | String appId = context.getReqHeaders().get("app-id"); |
| | | String userId = context.getReqHeaders().get("user-id"); |
| | |
| | | |
| | | String openId = reqJson.getString("openId"); |
| | | |
| | | // if(StringUtil.isEmpty(openId)) { |
| | | // //由于现在只有006,所以写死WECHAT,后续如果有多种支付方式则重新设计 |
| | | //// String appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA; |
| | | //// if (AppDto.WECHAT_OWNER_APP_ID.equals(appId)) { |
| | | //// appType = OwnerAppUserDto.APP_TYPE_WECHAT; |
| | | //// } else if (AppDto.WECHAT_MINA_OWNER_APP_ID.equals(appId)) { |
| | | //// appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA; |
| | | //// } else { |
| | | //// appType = OwnerAppUserDto.APP_TYPE_APP; |
| | | //// } |
| | | // String appType = OwnerAppUserDto.APP_TYPE_WECHAT; |
| | | // OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto(); |
| | | // ownerAppUserDto.setUserId(userId); |
| | | // ownerAppUserDto.setAppType(appType); |
| | | // List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto); |
| | | // |
| | | if(StringUtil.isEmpty(openId)) { |
| | | //由于现在只有006,所以写死WECHAT,后续如果有多种支付方式则重新设计 |
| | | // String appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA; |
| | | // if (AppDto.WECHAT_OWNER_APP_ID.equals(appId)) { |
| | | // appType = OwnerAppUserDto.APP_TYPE_WECHAT; |
| | | // } else if (AppDto.WECHAT_MINA_OWNER_APP_ID.equals(appId)) { |
| | | // appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA; |
| | | // } else { |
| | | // appType = OwnerAppUserDto.APP_TYPE_APP; |
| | | // } |
| | | String appType = OwnerAppUserDto.APP_TYPE_WECHAT; |
| | | OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto(); |
| | | ownerAppUserDto.setUserId(userId); |
| | | ownerAppUserDto.setAppType(appType); |
| | | List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserInnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto); |
| | | |
| | | // Assert.listOnlyOne(ownerAppUserDtos, "未找到开放账号信息"); |
| | | // openId = ownerAppUserDtos.get(0).getOpenId(); |
| | | // } |
| | | try{ |
| | | openId = ownerAppUserDtos.get(0).getOpenId(); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | logger.debug("【小程序支付】 统一下单开始, 订单编号=" + paymentOrderDto.getOrderId()); |
| | |
| | | //添加或更新支付记录(参数跟进自己业务需求添加) |
| | | |
| | | JSONObject resMap = null; |
| | | resMap = this.java110UnifieldOrder( |
| | | paymentOrderDto.getName(), |
| | | paymentOrderDto.getOrderId(), |
| | | tradeType, |
| | | payAmount, |
| | | openId, |
| | | smallWeChatDto, |
| | | notifyUrl |
| | | ); |
| | | if (TRADE_TYPE_LETPAY.equals(tradeType)){ |
| | | resMap = this.java110UnifieldOrder(paymentOrderDto.getName(), |
| | | paymentOrderDto.getOrderId(), |
| | | tradeType, |
| | | payAmount, |
| | | openId, |
| | | smallWeChatDto, |
| | | notifyUrl |
| | | ); |
| | | }else{ |
| | | |
| | | resMap = this.java110UnifieldOrder(paymentOrderDto.getName(), |
| | | paymentOrderDto.getOrderId(), |
| | | tradeType, |
| | | payAmount, |
| | | openId, |
| | | smallWeChatDto, |
| | | notifyUrl |
| | | ); |
| | | } |
| | | |
| | | if ("000000".equals(resMap.getString("result_code"))) { |
| | | if (TRADE_TYPE_JSAPI.equals(tradeType)) { |
| | | // resultMap.putAll(JSONObject.toJavaObject(JSONObject.parseObject(resMap.getString("reserved_pay_info")), Map.class)); |
| | | if (TRADE_TYPE_LETPAY.equals(tradeType)) { |
| | | resultMap.putAll(JSONObject.toJavaObject(JSONObject.parseObject(resMap.getString("reserved_pay_info")), Map.class)); |
| | | resultMap.put("sign", resultMap.get("paySign")); |
| | | resultMap.put("timeStamp", resMap.get("sdk_timestamp").toString()); |
| | | resultMap.put("nonceStr", resMap.get("sdk_noncestr").toString()); |
| | | resultMap.put("package", resMap.get("sdk_package").toString()); |
| | | resultMap.put("signType", resMap.get("sdk_signtype").toString()); |
| | | resultMap.put("paySign", resMap.get("sdk_paysign").toString()); |
| | | resultMap.put("payAppId", resMap.get("sdk_appid").toString()); |
| | | } else if (TRADE_TYPE_APP.equals(tradeType)) { |
| | | resultMap.put("appId", smallWeChatDto.getAppId()); |
| | | resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp()); |
| | |
| | | resultMap.put("partnerid", smallWeChatDto.getMchId()); |
| | | resultMap.put("prepayid", resMap.getString("session_id")); |
| | | //resultMap.put("signType", "MD5"); |
| | | resultMap.put("sign", PayUtil.createSign(resultMap, smallWeChatDto.getPayPassword())); |
| | | resultMap.put("sign", PayUtil.createSign(resultMap, smallWeChatDto.getAppSecret())); |
| | | } else if (TRADE_TYPE_NATIVE.equals(tradeType)) { |
| | | resultMap.put("prepayId", resMap.getString("session_id")); |
| | | resultMap.put("codeUrl", resMap.getString("qr_code")); |
| | | } |
| | | |
| | | resultMap.put("qrCode", resMap.getString("qr_code")); |
| | | resultMap.put("code", "0"); |
| | | resultMap.put("msg", "下单成功"); |
| | | resultMap.put("qrCode", resMap.getString("qr_code")); |
| | | logger.info("【小程序支付】统一下单成功,返回参数:" + resultMap); |
| | | } else { |
| | | resultMap.put("code", resMap.getString("result_code")); |
| | |
| | | // String orderPre = CommunitySettingFactory.getValue(smallWeChatDto.getObjId(), "FUIOU_ORDER_PRE"); |
| | | String orderPre = smallWeChatDto.getOrderPre(); |
| | | JSONObject paramMap = new JSONObject(); |
| | | if (tradeType == null){ |
| | | paramMap.put("version", VERSION); |
| | | paramMap.put("mchnt_cd", smallWeChatDto.getMchId()); // 富友分配给二级商户的商户号 |
| | | paramMap.put("random_str", PayUtil.makeUUID(32)); |
| | | paramMap.put("order_amt", PayUtil.moneyToIntegerStr(payAmount)); |
| | | paramMap.put("mchnt_order_no", orderPre + orderNum); |
| | | paramMap.put("txn_begin_ts", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT)); |
| | | paramMap.put("goods_des", "cesdasw"); |
| | | paramMap.put("term_id", "abcdefgh"); |
| | | paramMap.put("term_ip", PayUtil.getLocalIp()); |
| | | paramMap.put("notify_url", notifyUrl + "?wId=" + WechatFactory.getWId(smallWeChatDto.getAppId())); |
| | | paramMap.put("order_type", "WECHAT"); |
| | | paramMap.put("sub_openid", openid); |
| | | paramMap.put("sub_appid", smallWeChatDto.getAppId()); |
| | | paramMap.put("sign", createSignByQrCode(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); |
| | | |
| | | logger.debug("统一下单返回" + responseEntity); |
| | | |
| | | if (responseEntity.getStatusCode() != HttpStatus.OK) { |
| | | throw new IllegalArgumentException("支付失败" + responseEntity.getBody()); |
| | | } |
| | | return JSONObject.parseObject(responseEntity.getBody()); |
| | | } |
| | | paramMap.put("version", VERSION); |
| | | paramMap.put("mchnt_cd", smallWeChatDto.getMchId()); // 富友分配给二级商户的商户号 |
| | | paramMap.put("order_type", "WECHAT"); |
| | | // paramMap.put("order_amt", PayUtil.moneyToIntegerStr(payAmount)); |
| | | paramMap.put("order_amt", (int)(payAmount*100)); |
| | | // paramMap.put("order_amt", 1); |
| | | paramMap.put("random_str", PayUtil.makeUUID(32)); |
| | | paramMap.put("order_amt", PayUtil.moneyToIntegerStr(payAmount)); |
| | | paramMap.put("mchnt_order_no", orderPre + orderNum); |
| | | paramMap.put("txn_begin_ts", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT)); |
| | | paramMap.put("goods_des", feeName); |
| | | paramMap.put("goods_des", "cesdasw"); |
| | | paramMap.put("term_id", "abcdefgh"); |
| | | paramMap.put("term_ip", PayUtil.getLocalIp()); |
| | | paramMap.put("notify_url", notifyUrl + "?wId=" + WechatFactory.getWId(smallWeChatDto.getAppId())); |
| | | paramMap.put("random_str", PayUtil.makeUUID(32)); |
| | | paramMap.put("version", VERSION); |
| | | paramMap.put("trade_type", tradeType); |
| | | // paramMap.put("sub_openid", openid); |
| | | paramMap.put("sub_openid", openid); |
| | | paramMap.put("sub_appid", smallWeChatDto.getAppId()); |
| | | paramMap.put("sign", createSign(paramMap, smallWeChatDto.getAppSecret())); |
| | | |
| | |
| | | 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); |
| | | PAY_WX_ORDER_URL, HttpMethod.POST, httpEntity, String.class); |
| | | |
| | | logger.debug("统一下单返回" + responseEntity); |
| | | |
| | |
| | | String wId = map.get("wId").toString(); |
| | | wId = wId.replace(" ", "+"); |
| | | appId = WechatFactory.getAppId(wId); |
| | | } else if(map.containsKey("appId")) { |
| | | } else { |
| | | appId = map.get("appid").toString(); |
| | | } |
| | | SortedMap<String, String> paramMap = new TreeMap<String, String>(); |
| | |
| | | paramIn.put("communityId",notifyPaymentOrderDto.getCommunityId()); |
| | | SmallWeChatDto smallWeChatDto = getSmallWechat(paramIn); |
| | | |
| | | String sign = createSign(map, smallWeChatDto.getPayPassword()); |
| | | String sign = createSign(map, smallWeChatDto.getAppSecret()); |
| | | |
| | | if (!sign.equals(map.get("sign"))) { |
| | | // throw new IllegalArgumentException("鉴权失败"); |
| | |
| | | |
| | | private SmallWeChatDto getSmallWechat(JSONObject paramIn) { |
| | | |
| | | // SmallWeChatDto smallWeChatDto = new SmallWeChatDto(); |
| | | // smallWeChatDto.setObjId(paramIn.getString("communityId")); |
| | | // smallWeChatDto.setAppId(paramIn.getString("appId")); |
| | | // smallWeChatDto.setPage(1); |
| | | // smallWeChatDto.setRow(1); |
| | | // List<SmallWeChatDto> smallWeChatDtos = smallWechatV1InnerServiceSMOImpl.querySmallWechats(smallWeChatDto); |
| | | |
| | | // if (smallWeChatDtos == null || smallWeChatDtos.size() < 1) { |
| | | SmallWeChatDto smallWeChatDto = new SmallWeChatDto(); |
| | | //由于富有支付方式要从数据库里面取值,所以此处要查询 |
| | | PaymentPoolDto paymentPoolDto = new PaymentPoolDto(); |
| | |
| | | */ |
| | | private String createSign(JSONObject paramMap, String payPassword) { |
| | | String str = paramMap.getString("mchnt_cd") + "|" |
| | | + paramMap.getString("order_type") + "|" |
| | | + paramMap.getString("trade_type") + "|" |
| | | + paramMap.getString("order_amt") + "|" |
| | | + paramMap.getString("mchnt_order_no") + "|" |
| | | + paramMap.getString("txn_begin_ts") + "|" |
| | |
| | | return PayUtil.md5(str); |
| | | } |
| | | |
| | | |
| | | |
| | | private String createSignByQrCode(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); |
| | | } |
| | | } |