| @@ -58,12 +58,6 @@ public class WxMicroPayController extends BaseController { | |||
| @Autowired | |||
| private WxCUserBasicInfoService wxCUserBasicInfoService; | |||
| @Autowired | |||
| private WxCUserService wxCUserService; | |||
| @Autowired | |||
| private PosCouponOrderVerifyService posCouponOrderVerifyService; | |||
| @ApiOperation(value = "付款码支付订单", notes = "params:{\"authCode\":\"String\",\"totalFee\":\"支付金额\"},\n" + | |||
| "注意:\n" + | |||
| "提醒1:提交支付请求后微信会同步返回支付结果。当返回结果为“系统错误”时,商户系统等待5秒后调用【查询订单API】,查询支付实际交易结果;当返回结果为“USERPAYING”时,商户系统可设置间隔时间(建议10秒)重新查询支付结果,直到支付成功或超时(建议30秒);\n" + | |||
| @@ -112,7 +106,7 @@ public class WxMicroPayController extends BaseController { | |||
| record.setIp(ipStr); | |||
| try { | |||
| return wxPayOrderService.createMicroPayOrder(user, record, EnumPayWay.PAY_WAY_WECHAT); | |||
| return wxPayOrderService.createMicroPayOrder(user, record, EnumPayWay.PAY_WAY_WECHAT_MA, null); | |||
| } catch (MallinkException e) { | |||
| logger.error("payment wechat, order create error, req 2: " + record.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| @@ -126,11 +120,13 @@ public class WxMicroPayController extends BaseController { | |||
| @PostMapping("order_query") | |||
| public ResultData queryMicopayOrder(@RequestBody Map<String, String> paramMap) { | |||
| logger.info("queryMicopayOrder: " + paramMap.toString()); | |||
| WxMerchantBUser user = getUser(); | |||
| String payOrderIdStr = paramMap.get("payOrderId"); | |||
| String orderIdStr = paramMap.get("orderId"); | |||
| return queryMicopayOrder(payOrderIdStr,orderIdStr,EnumPayWay.PAY_WAY_WECHAT_MA); | |||
| } | |||
| private ResultData queryMicopayOrder(String payOrderIdStr,String orderIdStr,EnumPayWay payWay) { | |||
| WxMerchantBUser user = getUser(); | |||
| if (StringUtils.isBlank(payOrderIdStr)) { | |||
| logger.error("payOrderId不能为空"); | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "payOrderId不能为空"); | |||
| @@ -146,14 +142,14 @@ public class WxMicroPayController extends BaseController { | |||
| payOrderId = Long.valueOf(payOrderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| payOrderId = 0L; | |||
| logger.error("payOrderId参数不正确: " + paramMap.toString()); | |||
| logger.error("payOrderId参数不正确: " + payOrderIdStr); | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL); | |||
| } | |||
| try { | |||
| orderId = Long.valueOf(orderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| orderId = 0L; | |||
| logger.error("orderId参数不正确: " + paramMap.toString()); | |||
| logger.error("orderId参数不正确: " + orderIdStr); | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL); | |||
| } | |||
| @@ -164,7 +160,7 @@ public class WxMicroPayController extends BaseController { | |||
| record.setOrderId(orderId); | |||
| try { | |||
| ResultData resultData = wxPayOrderService.payOrderQuery(user, record); | |||
| ResultData resultData = wxPayOrderService.payOrderQuery(user, record, payWay); | |||
| return resultData; | |||
| } catch (MallinkException e) { | |||
| logger.error("payment wechat, order query error, req 2: " + record.toString() + ", e:" + e.getMessage()); | |||
| @@ -174,6 +170,7 @@ public class WxMicroPayController extends BaseController { | |||
| return new ResultData(ErrorCode.PAY_ORDER_QUERY_ERROR, e.getMessage()); | |||
| } | |||
| } | |||
| @ApiOperation(value = "撤销付款码支付订单", notes = "{\"payOrderId\":\"String\",\"orderId\":\"String\"}") | |||
| @PostMapping("order_reverse") | |||
| @@ -370,12 +367,16 @@ public class WxMicroPayController extends BaseController { | |||
| @PostMapping("pay_order_create_v2") | |||
| public ResultData saveMicopayPayOrder(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |||
| logger.info("saveMicopayPayOrder: " + paramMap.toString()); | |||
| WxMerchantBUser user = getUser(); | |||
| String orderIdStr = paramMap.get("orderId"); | |||
| String couponOrderIdStr = paramMap.get("couponOrderId"); | |||
| String authCode = paramMap.get("authCode"); | |||
| String payPriceStr = paramMap.get("payPrice"); | |||
| return saveMicopayPayOrder(orderIdStr, couponOrderIdStr, authCode, payPriceStr, EnumPayWay.PAY_WAY_WECHAT_MA, request); | |||
| } | |||
| private ResultData saveMicopayPayOrder(String orderIdStr,String couponOrderIdStr,String authCode,String payPriceStr, | |||
| EnumPayWay payWay, HttpServletRequest request) { | |||
| WxMerchantBUser user = getUser(); | |||
| if (StringUtils.isBlank(orderIdStr)) { | |||
| logger.error("orderId不能为空"); | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空"); | |||
| @@ -411,7 +412,7 @@ public class WxMicroPayController extends BaseController { | |||
| orderId = Long.valueOf(orderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| orderId = 0L; | |||
| logger.error("orderId参数不正确: " + paramMap.toString()); | |||
| logger.error("orderId参数不正确: " + orderIdStr); | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL); | |||
| } | |||
| @@ -426,7 +427,7 @@ public class WxMicroPayController extends BaseController { | |||
| couponOrderId = Long.valueOf(couponOrderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| couponOrderId = 0L; | |||
| logger.error("couponOrderId参数不正确: " + paramMap.toString()); | |||
| logger.error("couponOrderId参数不正确: " + couponOrderIdStr); | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL); | |||
| } | |||
| wxCouponOrder = wxCouponOrderService.getById(couponOrderId); | |||
| @@ -471,7 +472,7 @@ public class WxMicroPayController extends BaseController { | |||
| record.setIp(ipStr); | |||
| try { | |||
| return wxPayOrderService.createMicroPayOrder(user, record, EnumPayWay.PAY_WAY_WECHAT); | |||
| return wxPayOrderService.createMicroPayOrder(user, record, payWay, null); | |||
| } catch (MallinkException e) { | |||
| logger.error("payment wechat, order create error, req 2: " + record.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| @@ -496,7 +497,7 @@ public class WxMicroPayController extends BaseController { | |||
| } | |||
| } | |||
| } | |||
| private Boolean checkAuthCode(String authCode) { | |||
| if (authCode.length() == 18) { | |||
| String head = authCode.substring(0, 2); | |||
| @@ -38,7 +38,7 @@ import com.iformall.service.WxCouponService; | |||
| import com.iformall.service.WxOrderService; | |||
| import com.iformall.service.WxPayAccountService; | |||
| import com.iformall.service.WxPayOrderService; | |||
| import com.iformall.service.pay.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| import com.iformall.utils.IPUtil; | |||
| import io.swagger.annotations.Api; | |||
| @@ -68,7 +68,7 @@ public class WxPayBillController extends BaseController { | |||
| try { | |||
| paramMap = WxPayment.xmlToMap(resultxml); | |||
| logger.info("微信支付回调, notify, param: " + paramMap.toString() ); | |||
| String response = wxPayBillService.notify(paramMap, EnumPayWay.PAY_WAY_WEAPP); | |||
| String response = wxPayBillService.notify(paramMap, EnumPayWay.PAY_WAY_WECHAT); | |||
| logger.info("微信支付回调, notify success, req : " + resultxml + ", resp: " + response.toString()); | |||
| return response; | |||
| } catch (BizMessageException e) { | |||
| @@ -120,7 +120,7 @@ public class WxPayBillController extends BaseController { | |||
| xml = IOUtils.toString(request.getInputStream(), Charset.forName("UTF-8")); | |||
| logger.info(xml); | |||
| paramMap = WxPayment.xmlToMap(xml); | |||
| response = wxRefundOrderService.notify(paramMap, EnumPayWay.PAY_WAY_WEAPP); | |||
| response = wxRefundOrderService.notify(paramMap, EnumPayWay.PAY_WAY_WECHAT); | |||
| logger.info("refund wxpay, notify success, req : " + xml + ", resp: " + response.toString()); | |||
| return response; | |||
| } catch (BizMessageException e) { | |||
| @@ -159,7 +159,7 @@ public class WxPayBillController extends BaseController { | |||
| xml = IOUtils.toString(request.getInputStream(), Charset.forName("UTF-8")); | |||
| paramMap = WxPayment.xmlToMap(xml); | |||
| logger.info("share wxpay, notify, param: " + xml ); | |||
| response = wxPayBillService.notify(paramMap, EnumPayWay.PAY_WAY_WEAPP); | |||
| response = wxPayBillService.notify(paramMap, EnumPayWay.PAY_WAY_WECHAT); | |||
| logger.info("share wxpay, notify success, req : " + xml + ", resp: " + response.toString()); | |||
| return response; | |||
| } catch (BizMessageException e) { | |||
| @@ -136,7 +136,7 @@ public class WxPayController extends BaseController { | |||
| FmInsideNotifyRefundSuccessMsg refundSuccessMsg = new FmInsideNotifyRefundSuccessMsg(); | |||
| refundSuccessMsg.setMsgType(EnumMsgRecordType.INSIDE_NOTIFY_REFUND_SUCCESS.getCode()); | |||
| refundSuccessMsg.setDelayTimeLevel(3); | |||
| refundSuccessMsg.setPayWay(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| refundSuccessMsg.setPayWay(EnumPayWay.PAY_WAY_WECHAT.getCode()); | |||
| refundSuccessMsg.setJsonMsg(jsonMsg); | |||
| // response = wxRefundOrderService.notify(paramMap, EnumPayWay.PAY_WAY_WEAPP); | |||
| @@ -180,7 +180,7 @@ public class WxPayController extends BaseController { | |||
| xml = IOUtils.toString(request.getInputStream(), Charset.forName("UTF-8")); | |||
| paramMap = WxPayment.xmlToMap(xml); | |||
| logger.info("share wxpay, notify, param: " + xml ); | |||
| response = wxPayOrderService.notify(paramMap, EnumPayWay.PAY_WAY_WEAPP); | |||
| response = wxPayOrderService.notify(paramMap, EnumPayWay.PAY_WAY_WECHAT); | |||
| logger.info("share wxpay, notify success, req : " + xml + ", resp: " + response.toString()); | |||
| return response; | |||
| } catch (BizMessageException e) { | |||
| @@ -231,7 +231,7 @@ public class WxPayController extends BaseController { | |||
| try { | |||
| paramMap = WxPayment.xmlToMap(resultxml); | |||
| logger.info("微信支付回调, notify, param: " + paramMap.toString() ); | |||
| String response = wxSubsidyService.notify(paramMap, EnumPayWay.PAY_WAY_WEAPP); | |||
| String response = wxSubsidyService.notify(paramMap, EnumPayWay.PAY_WAY_WECHAT); | |||
| logger.info("微信支付回调, notify success, req : " + resultxml + ", resp: " + response.toString()); | |||
| return response; | |||
| } catch (BizMessageException e) { | |||
| @@ -29,6 +29,8 @@ import com.iformall.service.WxOrderService; | |||
| import com.iformall.service.WxPayOrderService; | |||
| import com.iformall.service.WxRefundOrderService; | |||
| import com.iformall.service.helper.WxPayOrderServiceHelper; | |||
| import com.iformall.service.pay.PayServiceFactory; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| import com.iformall.utils.DateUtils; | |||
| @Component | |||
| @@ -74,6 +76,9 @@ public class OrderExpiringSchedule { | |||
| @Autowired | |||
| WxPayOrderService wxPayOrderService; | |||
| @Autowired | |||
| PayServiceFactory payServiceFactory; | |||
| @Scheduled(cron = "0 */5 * * * *?") // 每5分钟检查一次 | |||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | |||
| public void orderExpireSchedule() { | |||
| @@ -180,13 +185,13 @@ public class OrderExpiringSchedule { | |||
| } | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void expiredAndReturnMoney(WxOrder wxOrder,boolean checkRealPay) { | |||
| public void expiredAndReturnMoney(WxOrder wxOrder,boolean checkRealPay) throws MallinkException, Exception { | |||
| orderGroupExpired(wxOrder,checkRealPay,false); | |||
| returnMoney(wxOrder); | |||
| } | |||
| public boolean wxPaySuccess(WxOrder order,boolean autoReHandle) { | |||
| public boolean wxPaySuccess(WxOrder order,boolean autoReHandle) throws MallinkException, Exception { | |||
| WxPayOrder payOrderq = new WxPayOrder(); | |||
| payOrderq.setOrderId(order.getId()); | |||
| List<WxPayOrder> payorderlist = wxPayOrderMapper.findList(payOrderq); | |||
| @@ -220,12 +225,13 @@ public class OrderExpiringSchedule { | |||
| if (null == payAccount) { | |||
| return true; | |||
| } | |||
| Map<String, String> retMap = WxPayOrderServiceHelper.wxOrderPayStatusMap(wxpayOrder, order, appInfo, payAccount); | |||
| int status = WxPayOrderServiceHelper.getPayStatusFromMap(retMap, wxpayOrder.getPayOrderNo()); | |||
| //Map<String, String> retMap = WxPayOrderServiceHelper.wxOrderPayStatusMap(wxpayOrder, order, appInfo, payAccount); | |||
| //int status = WxPayOrderServiceHelper.getPayStatusFromMap(retMap, wxpayOrder.getPayOrderNo()); | |||
| PayQueryAdapterResult payResult = payServiceFactory.getPayAdapterService(wxpayOrder.getPayVendor()).queryPayStatus(wxpayOrder, order, appInfo, payAccount); | |||
| //如果是支付成功,并且需要重试成功流程, | |||
| if (EnumPayStatus.PAY_STATUS_SUCCESS.getCode() == status) { | |||
| if (EnumPayStatus.PAY_STATUS_SUCCESS.getCode().intValue() == payResult.getCode()) { | |||
| if (autoReHandle) { | |||
| wxPayOrderService.handleSuccessOrder(wxpayOrder, order, retMap,true); | |||
| wxPayOrderService.handleSuccessOrder(wxpayOrder, order, payResult,true); | |||
| } | |||
| return true; | |||
| } | |||
| @@ -234,7 +240,7 @@ public class OrderExpiringSchedule { | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void orderExpired(WxOrder order,boolean checkRealPay) { | |||
| public void orderExpired(WxOrder order,boolean checkRealPay) throws MallinkException, Exception { | |||
| if (checkRealPay) { | |||
| if (wxPaySuccess(order,true)) { | |||
| return; | |||
| @@ -257,7 +263,7 @@ public class OrderExpiringSchedule { | |||
| } | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void orderPressExpired(WxOrder order, boolean payExpired,boolean checkRealPay) { | |||
| public void orderPressExpired(WxOrder order, boolean payExpired,boolean checkRealPay) throws MallinkException, Exception { | |||
| if (checkRealPay) { | |||
| if (wxPaySuccess(order,true)) { | |||
| return; | |||
| @@ -283,7 +289,7 @@ public class OrderExpiringSchedule { | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void orderGroupExpired(WxOrder order,boolean checkRealPay,boolean stockBack) { | |||
| public void orderGroupExpired(WxOrder order,boolean checkRealPay,boolean stockBack) throws MallinkException, Exception { | |||
| if (checkRealPay) { | |||
| if (wxPaySuccess(order,true)) { | |||
| return; | |||
| @@ -309,9 +315,11 @@ public class OrderExpiringSchedule { | |||
| /** | |||
| * 回调会再次扣减库存 | |||
| * @param order | |||
| * @throws Exception | |||
| * @throws MallinkException | |||
| */ | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void returnMoney(WxOrder order) { | |||
| public void returnMoney(WxOrder order) throws MallinkException, Exception { | |||
| //支付成功的才退款 | |||
| if (!wxPaySuccess(order,false)) { | |||
| return; | |||
| @@ -5,8 +5,8 @@ package com.iformall.enums; | |||
| */ | |||
| public enum EnumPayMode { | |||
| MCH(0, "普通商户模式"), | |||
| MCH_S(1, "服务商模式"); | |||
| MCH(0, "微信普通商户模式"), | |||
| MCH_S(1, "微信服务商模式"); | |||
| public static EnumPayMode getEnum(Integer code) { | |||
| for (EnumPayMode value : values()) { | |||
| @@ -15,7 +15,8 @@ public enum EnumPayStatus { | |||
| PAY_STATUS_CLOSE_FAIL(6,"关闭支付订单失败"), | |||
| PAY_STATUS_REVERSE(7, "撤销支付订单"), | |||
| PAY_STATUS_REVERSE_FAIL(8,"撤销支付订单失败"), | |||
| PAY_STATUS_REFUND(9,"支付转退款") | |||
| PAY_STATUS_REFUND(9,"支付转退款"), | |||
| PAY_STATUS_ORDER_NOT_EXISTS(10,"支付单不存在") | |||
| ; | |||
| public static EnumPayStatus getEnum(Integer code) { | |||
| @@ -5,8 +5,8 @@ package com.iformall.enums; | |||
| */ | |||
| public enum EnumPayWay { | |||
| PAY_WAY_WEAPP(0, "微信小程序"), | |||
| PAY_WAY_WECHAT(1, "微信支付"), | |||
| PAY_WAY_WECHAT(1, "微信小程序支付"), | |||
| PAY_WAY_WECHAT_MA(12, "微信小程序B端扫码支付"), | |||
| PAY_WAY_WECHAT_WAP(2, "微信H5"), | |||
| PAY_WAY_ALIPAY(3, "支付宝"), | |||
| PAY_WAY_ALIPAY_WAP(4, "支付宝H5"), | |||
| @@ -16,13 +16,13 @@ public enum EnumPayWay { | |||
| public static EnumPayWay getEnum(Integer code) { | |||
| for (EnumPayWay value : values()) { | |||
| if (value.getCode().equals(code)) { | |||
| if (value.getCode().intValue()==code.intValue()) { | |||
| return value; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| private Integer code; | |||
| private String message; | |||
| @@ -12,12 +12,15 @@ import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayShare; | |||
| import com.iformall.enums.EnumPayWay; | |||
| import com.iformall.service.pay.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayAdapterResult; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| public interface WxPayOrderService { | |||
| public void handleSuccessOrder(WxPayOrder oldRecord,WxOrder order,Map<String, String> retMap,boolean isScheduleTask); | |||
| public void handleSuccessOrder(WxPayOrder oldRecord,WxOrder order,PayQueryAdapterResult queryResult,boolean isScheduleTask); | |||
| public WxPayOrder handleWxOrderQuery(WxPayOrder oldRecord,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount,IdWorker idworker,boolean repeatPay); | |||
| @@ -37,13 +40,13 @@ public interface WxPayOrderService { | |||
| * @param payWay | |||
| * @return | |||
| */ | |||
| ResultData createMicroPayOrder(WxMerchantBUser user, WxPayOrder record, EnumPayWay payWay); | |||
| ResultData createMicroPayOrder(WxMerchantBUser user, WxPayOrder record, EnumPayWay payWay,PayExtraParam params); | |||
| /** | |||
| * 微信支付订单查询 | |||
| * 支付订单查询 | |||
| * @param record 支付订单 | |||
| */ | |||
| ResultData payOrderQuery(WxMerchantBUser user, WxPayOrder record); | |||
| ResultData payOrderQuery(WxMerchantBUser user, WxPayOrder record,EnumPayWay payWay); | |||
| /** | |||
| * 微信支付关闭订单 | |||
| @@ -74,15 +77,15 @@ public interface WxPayOrderService { | |||
| * @param transactionId | |||
| * @param from | |||
| */ | |||
| void handleOrderPaySuccess(WxPayOrder record, String transactionId, Integer from); | |||
| void handleOrderPaySuccess(WxPayOrder record, String transactionId); | |||
| /** | |||
| * 刷卡支付成功处理 | |||
| * | |||
| * @param Object 支付方API返回的对象 (查询或者支付 API返回的对象) | |||
| * @param record | |||
| * @param transactionId | |||
| */ | |||
| void handleMicroOrderPaySuccess(WxMerchantBUser user, WxPayOrder record, String transactionId, String openId, String isSubscribe); | |||
| void handleMicroOrderPaySuccess(Object result,String transcationId,WxPayOrder record ,WxMerchantBUser user,WxAppinfo appInfo,PayExtraParam params); | |||
| /** | |||
| * 券支付成功处理 | |||
| @@ -84,6 +84,9 @@ public class WxPayOrderServiceHelper { | |||
| }else { | |||
| String errCode = returnMap.get("err_code"); | |||
| String errCodeDes = returnMap.get("err_code_des"); | |||
| if (errCode.equalsIgnoreCase("ORDERNOTEXIST")) { | |||
| return EnumPayStatus.PAY_STATUS_ORDER_NOT_EXISTS.getCode(); | |||
| } | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "订单查询微信支付状态失败["+errCode+":"+errCodeDes+"]"+payOrderNo); | |||
| } | |||
| } else { | |||
| @@ -92,6 +95,39 @@ public class WxPayOrderServiceHelper { | |||
| } | |||
| } | |||
| public static String getPayStatusMsg(Map<String, String> returnMap,String payOrderNo) { | |||
| String return_code = returnMap.get("return_code"); | |||
| if ("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String trade_state = returnMap.get("trade_state"); | |||
| if ("SUCCESS".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_SUCCESS.getMessage(); | |||
| }else if ("REFUND".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_REFUND.getMessage(); | |||
| }else if ("NOTPAY".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_NOTPAY.getMessage(); | |||
| }else if ("CLOSED".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_CLOSE.getMessage(); | |||
| }else if ("REVOKED".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_REVERSE.getMessage(); | |||
| }else if ("USERPAYING".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_WAIT.getMessage(); | |||
| }else if ("PAYERROR".equals(trade_state)) { | |||
| return EnumPayStatus.PAY_STATUS_FAIL.getMessage(); | |||
| } | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "订单查询微信支付状态非法["+trade_state+"]"+payOrderNo); | |||
| }else { | |||
| String errCode = returnMap.get("err_code"); | |||
| String errCodeDes = returnMap.get("err_code_des"); | |||
| return errCodeDes; | |||
| } | |||
| } else { | |||
| String return_msg = returnMap.get("return_msg"); | |||
| return return_msg; | |||
| } | |||
| } | |||
| public static int wxOrderPayStatus(WxPayOrder record,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount) { | |||
| Map<String, String> returnMap = wxOrderPayStatusMap(record,order,appInfo,payAccount); | |||
| return getPayStatusFromMap(returnMap,record.getPayOrderNo()); | |||
| @@ -18,9 +18,13 @@ import com.iformall.mapper.*; | |||
| import com.iformall.pay.*; | |||
| import com.iformall.service.*; | |||
| import com.iformall.service.helper.WxPayOrderServiceHelper; | |||
| import com.iformall.service.pay.PayAdapterResult; | |||
| import com.iformall.service.pay.PayExtraParam; | |||
| import com.iformall.service.pay.PayServiceFactory; | |||
| import com.iformall.service.pay.entity.PayAdapterResult; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| import com.iformall.service.pay.service.CDrivingPayService; | |||
| import com.iformall.service.pay.service.CPassivePayService; | |||
| import com.iformall.service.pay.service.PayAdapterService; | |||
| import com.iformall.utils.*; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| @@ -91,6 +95,9 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| @Autowired | |||
| PosCouponOrderVerifyMapper posCouponOrderVerifyMapper; | |||
| @Autowired | |||
| PayServiceFactory payServiceFactory; | |||
| JSONObject errorMapQuery = JSON.parseObject("{" + | |||
| "\"ORDERNOTEXIST\":{\"detail\":\"此交易订单号不存在\",\"reason\":\"查询系统中不存在此交易订单号\",\"resolution\":\"该API只能查提交支付交易返回成功的订单,请商户检查需要查询的订单号是否正确\"},\n" + | |||
| @@ -107,13 +114,13 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| //方法必须为public AopContext.currentProxy() | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| @Override | |||
| public void handleSuccessOrder(WxPayOrder oldRecord,WxOrder order,Map<String, String> retMap,boolean isScheduleTask) { | |||
| public void handleSuccessOrder(WxPayOrder oldRecord,WxOrder order,PayQueryAdapterResult queryResult,boolean isScheduleTask) { | |||
| Date curDate = new Date(); | |||
| if (oldRecord.getPayOrderStatus() != EnumPayStatus.PAY_STATUS_SUCCESS.getCode()) { | |||
| //因为此处是处理订单支付成功了但是订单状态为未支付成功的异常状态,库存不应该再扣减 | |||
| //更新wxPayOrder | |||
| oldRecord.setPayOrderStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| String timEndStr = retMap.get("time_end"); | |||
| String timEndStr = queryResult.getEndTimeStr(); | |||
| Date timeEnd = null; | |||
| try { | |||
| timeEnd = Utility.getDateFromString(timEndStr); | |||
| @@ -123,7 +130,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| oldRecord.setPayTimeEnd(timeEnd); | |||
| oldRecord.setPayEndFrom(EnumPayEndFrom.QUERY.getCode()); | |||
| String transactionId = retMap.get("transaction_id"); | |||
| String transactionId = queryResult.getTransactionId(); | |||
| oldRecord.setTransactionId(transactionId); | |||
| oldRecord.setUpdateTime(curDate); | |||
| wxPayOrderMapper.updateById(oldRecord); | |||
| @@ -166,12 +173,11 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| @Override | |||
| public WxPayOrder handleWxOrderQuery(WxPayOrder oldRecord,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount,IdWorker idworker,boolean repeatPay) { | |||
| Map<String, String> retMap = null;; | |||
| PayQueryAdapterResult queryResult = null; | |||
| int wxpaystatus = 0; | |||
| try { | |||
| retMap = (Map<String, String>) PayServiceFactory.getPayAdapterService(oldRecord.getPayFrom()).queryPayStatus(oldRecord, order, appInfo, payAccount); | |||
| wxpaystatus = PayServiceFactory.getPayAdapterService(oldRecord.getPayFrom()).queryPayStatus(retMap, oldRecord.getPayOrderNo()); | |||
| queryResult = payServiceFactory.getPayAdapterService(oldRecord.getPayFrom()).queryPayStatus(oldRecord, order, appInfo, payAccount); | |||
| wxpaystatus = payServiceFactory.getPayAdapterService(oldRecord.getPayFrom()).queryPayStatus(queryResult, oldRecord.getPayOrderNo()); | |||
| } catch (Exception e) { | |||
| logger.error("pay order service handleWxOrderQuery error: " ,e); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR); | |||
| @@ -180,7 +186,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| if (EnumPayStatus.PAY_STATUS_SUCCESS.getCode()==wxpaystatus) { | |||
| //直接调用不会触发事务,spring事务基于AOP代理 | |||
| WxPayOrderServiceImpl proxy = (WxPayOrderServiceImpl) AopContext.currentProxy(); | |||
| proxy.handleSuccessOrder(oldRecord,order,retMap,false); | |||
| proxy.handleSuccessOrder(oldRecord,order,queryResult,false); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_EXIST.getCode(),"该订单已经支付,请勿重复支付!"); | |||
| }else if (EnumPayStatus.PAY_STATUS_REFUND.getCode()==wxpaystatus) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_REFUND.getCode(),"该订单已经退款,无法支付!"); | |||
| @@ -283,44 +289,6 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| isShare = EnumPayShare.YES; | |||
| } | |||
| // // 1.5 检查是否有已支付订单 | |||
| // { | |||
| // WxPayOrder payOrderParams = new WxPayOrder(); | |||
| // payOrderParams.setOrderId(order.getId()); | |||
| // payOrderParams.setPayOrderStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| // QueryWrapper<WxPayOrder> queryWrapper = new QueryWrapper<>(); | |||
| // queryWrapper.setEntity(payOrderParams); | |||
| // int count = wxPayOrderMapper.selectCount(queryWrapper); | |||
| // if (count > 0) { | |||
| // logger.error("已支付>>>" + order); | |||
| // throw new MallinkException(ErrorCode.PAY_ORDER_EXIST); | |||
| // } | |||
| // } | |||
| // | |||
| // /*设置进入分账,待分账时再判断是否存在对应的分账账户 | |||
| // // 1.5 check 分账 receive | |||
| // if (isShare == EnumPayShare.YES) { | |||
| // WxProfitSharingReceiver receiver = new WxProfitSharingReceiver(); | |||
| // receiver.setMerchantId(order.getMerchantId()); | |||
| // List<WxProfitSharingReceiver> merList = wxProfitSharingReceiverMapper.findList(receiver); | |||
| // if (merList.size() > 0) { | |||
| // isShare = EnumPayShare.YES; | |||
| // } else { | |||
| // isShare = EnumPayShare.NO; | |||
| // } | |||
| // } | |||
| // */ | |||
| // | |||
| // //支付中的订单, 都是已经点过支付的订单,要等微信通知更新,不能再操作。 | |||
| // record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| // List<WxPayOrder> list = wxPayOrderMapper.findList(record); | |||
| // if (list.size() > 0) { | |||
| // logger.error("支付状态等待微信同步中>>>" + order); | |||
| // throw new MallinkException(ErrorCode.PAY_ORDER_PAYING.getCode(),"订单支付状态等待微信同步中"); | |||
| // // 3.0 已有支付订单,关闭之前的 | |||
| // //payOrderClose(appInfo, list.get(0)); | |||
| // } | |||
| Date currentDate = new Date(); | |||
| //一个订单只有一条支付记录。 | |||
| WxPayOrder orderq = new WxPayOrder(); | |||
| @@ -400,7 +368,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| logger.debug("wxPayRecord md5 before:"+record.toString()); | |||
| String beforemd5 = HashUtil.md5(record.toString()); | |||
| PayAdapterResult payResult = PayServiceFactory.getPayAdapterService(payWay.getCode()).pay(payAccount, order, record,isShare,appInfo, currentDate, params); | |||
| PayAdapterResult payResult = payServiceFactory.getPayAdapterService(payWay.getCode()).pay(payAccount, order, record,isShare,appInfo, currentDate, params); | |||
| String aftermd5 = HashUtil.md5(record.toString()); | |||
| if (beforemd5.equals(aftermd5)) { | |||
| logger.error("支付方式["+payWay.getCode()+"]支付接口【pay】过程中 WxPayOrder 未做更新.record:"+record.toString()); | |||
| @@ -429,10 +397,13 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| @Override | |||
| public ResultData createMicroPayOrder(WxMerchantBUser user, WxPayOrder record, EnumPayWay payWay) { | |||
| public ResultData createMicroPayOrder(WxMerchantBUser user, WxPayOrder record, EnumPayWay payWay,PayExtraParam params) { | |||
| final IdWorker idworker = IdWorker.get(); | |||
| WxAppinfo appInfo = wxAppinfoService.getCAppInfo(user); | |||
| if (null == params) { | |||
| params = new PayExtraParam("bAppId",user.getAppId()).set("bUserPhone", user.getPhone()); | |||
| } | |||
| EnumPayShare isShare = EnumPayShare.NO; | |||
| @@ -460,7 +431,6 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| List<WxPayOrder> list = wxPayOrderMapper.findList(record); | |||
| String payOrderNo = ""; | |||
| String orderNo = String.valueOf(order.getId()); | |||
| if (list.size() <= 0) { | |||
| // 3. 创建支付订单 | |||
| Long id = idworker.nextId(); | |||
| @@ -475,7 +445,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| // 支付单号 | |||
| record.setPayOrderNo(payOrderNo); | |||
| record.setPayAmount(record.getPayAmount()); | |||
| record.setPayVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setPayVendor(payWay.getCode()); | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| record.setShare(isShare.getCode()); | |||
| if (isShare == EnumPayShare.YES) { | |||
| @@ -504,141 +474,29 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| payOrderNo = String.valueOf(record.getId()); | |||
| } | |||
| if (payAccount.getType() == EnumPayMode.MCH.getCode()) { | |||
| // 刷卡支付 普通商户模式 | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxMicroPayOrderP wxPayOrderP = new WxMicroPayOrderP(); | |||
| wxPayOrderP.setAppid(user.getAppId()); | |||
| wxPayOrderP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderP.setDevice_info(user.getPhone()); | |||
| wxPayOrderP.setNonce_str(noncestr); | |||
| wxPayOrderP.setBody(order.getDetail()); | |||
| wxPayOrderP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderP.setTotal_fee(record.getPayAmount()); | |||
| wxPayOrderP.setSpbill_create_ip(record.getIp()); // 终端IP | |||
| wxPayOrderP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| Map<String, String> payOrderMap = BeanUtils.toStringMap(wxPayOrderP); | |||
| wxPayOrderP.setSign(WxPayment.createSign(payOrderMap, payAccount.getApiKey())); | |||
| String response = WxPay.micropay(BeanUtils.toStringMap(wxPayOrderP)); | |||
| logger.info("pay order, wechat micropay, " + wxPayOrderP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payOrderId", payOrderNo); | |||
| returnMap.put("orderId", orderNo); | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String openId = returnMap.get("openid"); | |||
| String isSubscribe = returnMap.get("is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| record.setTransactionId(transactionId); | |||
| record.setUpdateTime(new Date()); | |||
| handleMicroOrderPaySuccess(user, record, transactionId, openId, isSubscribe); | |||
| return new ResultData(Result.SUCCESS, "刷卡支付成功", returnMap); | |||
| } else { | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if (errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } else { | |||
| String errMsg = returnMap.get("return_msg"); | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } else { | |||
| // 统一下单 // 服务商模式 | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxMicroPayOrderSP wxPayOrderSP = new WxMicroPayOrderSP(); | |||
| wxPayOrderSP.setAppid(appInfo.getParentAppId()); | |||
| wxPayOrderSP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderSP.setSub_appid(appInfo.getAppId()); | |||
| wxPayOrderSP.setSub_mch_id(payAccount.getSubMchId()); | |||
| wxPayOrderSP.setDevice_info(user.getPhone()); | |||
| wxPayOrderSP.setNonce_str(noncestr); | |||
| wxPayOrderSP.setBody(order.getDetail()); | |||
| wxPayOrderSP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderSP.setTotal_fee(record.getPayAmount()); | |||
| wxPayOrderSP.setSpbill_create_ip(record.getIp()); | |||
| wxPayOrderSP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderSP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderSP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| wxPayOrderSP.setSign_type("HMAC-SHA256"); | |||
| wxPayOrderSP.setProfit_sharing(null); | |||
| if (isShare == EnumPayShare.YES) { | |||
| wxPayOrderSP.setProfit_sharing("Y"); | |||
| } | |||
| Map<String, String> payOrderMap = BeanUtils.toStringMap(wxPayOrderSP); | |||
| wxPayOrderSP.setSign(WxPayment.createSignHMAC(payOrderMap, payAccount.getApiKey())); | |||
| String response = WxPay.micropay(BeanUtils.toStringMap(wxPayOrderSP)); | |||
| logger.info("pay order, wechat micropay, " + wxPayOrderSP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payOrderId", payOrderNo); | |||
| returnMap.put("orderId", orderNo); | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String openId = returnMap.get("sub_openid"); | |||
| String isSubscribe = returnMap.get("sub_is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| record.setTransactionId(transactionId); | |||
| record.setUpdateTime(new Date()); | |||
| handleMicroOrderPaySuccess(user, record, transactionId, openId, isSubscribe); | |||
| return new ResultData(Result.SUCCESS, "刷卡支付成功", returnMap); | |||
| } else { | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if (errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } else { | |||
| String errMsg = returnMap.get("return_msg"); | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| logger.debug("wxPayRecord md5 before:"+record.toString()); | |||
| String beforemd5 = HashUtil.md5(record.toString()); | |||
| PayAdapterResult payResult = payServiceFactory.getPayAdapterService(payWay.getCode()).pay(payAccount, order, record,isShare,appInfo, currentDate, params); | |||
| String aftermd5 = HashUtil.md5(record.toString()); | |||
| if (beforemd5.equals(aftermd5)) { | |||
| logger.error("支付方式["+payWay.getCode()+"]支付接口【pay】过程中 WxPayOrder 未做更新.record:"+record.toString()); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(),"支付方式["+payWay.getCode()+"]支付接口【pay】过程中 WxPayOrder 未做更新"); | |||
| } | |||
| //支付失败,更新record,支付成功,则调用 | |||
| if (payResult.isSuccess()) { | |||
| handleMicroOrderPaySuccess(payResult.getData(), payResult.getTransactionId(),record,user, appInfo, params); | |||
| return new ResultData(Result.SUCCESS, "创建支付订单成功", payResult.getData()); | |||
| }else { | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), payResult.getMsg(), payResult.getData()); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| @@ -647,10 +505,10 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| /** | |||
| * 微信订单查询 | |||
| * 支付订单查询 | |||
| */ | |||
| @Override | |||
| public ResultData payOrderQuery(WxMerchantBUser user, WxPayOrder record) { | |||
| public ResultData payOrderQuery(WxMerchantBUser user, WxPayOrder record,EnumPayWay payWay) { | |||
| try { | |||
| if (StringUtils.isBlank(record.getPayOrderNo())) { | |||
| @@ -670,117 +528,61 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| WxAppinfo appInfo = wxAppinfoService.getCAppInfo(record); | |||
| PayAdapterService payService = payServiceFactory.getPayAdapterService(payWay.getCode()); | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId()); | |||
| String response = WxPayOrderServiceHelper.wechatPayOrderQuery(appInfo, record,payAccount); | |||
| logger.info("pay order query, " + record.toString() + ", response: " + response); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String trade_state = returnMap.get("trade_state"); | |||
| //SUCCESS—支付成功 | |||
| //REFUND—转入退款 | |||
| //NOTPAY—未支付 | |||
| //CLOSED—已关闭 | |||
| //REVOKED—已撤销(刷卡支付) | |||
| //USERPAYING--用户支付中 | |||
| //PAYERROR--支付失败(其他原因,如银行返回失败) | |||
| if ("SUCCESS".equals(trade_state)) { | |||
| String openId = returnMap.get("sub_openid"); | |||
| String sub_is_subscribe = returnMap.get("sub_is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| String tradeType = returnMap.get("trade_type"); | |||
| if ("MICROPAY".equals(tradeType)) { | |||
| handleMicroOrderPaySuccess(user, record, transactionId, openId, sub_is_subscribe); | |||
| } else { | |||
| handleOrderPaySuccess(record, transactionId, EnumPayEndFrom.QUERY.getCode()); | |||
| } | |||
| return new ResultData(Result.SUCCESS, "订单支付成功", returnMap); | |||
| } else if ("REFUND".equals(trade_state)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_REFUND.getCode()); | |||
| // handlePayOrderStatusUpdate(record); | |||
| return new ResultData(ErrorCode.PAY_ORDER_REFUND, returnMap); | |||
| } else if ("NOTPAY".equals(trade_state)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_NOTPAY.getCode()); | |||
| //handlePayOrderStatusUpdate(record); | |||
| return new ResultData(ErrorCode.PAY_ORDER_NOT_PAY, returnMap); | |||
| } else if ("CLOSED".equals(trade_state)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_CLOSE.getCode()); | |||
| ///handlePayOrderStatusUpdate(record); | |||
| return new ResultData(ErrorCode.PAY_ORDER_CLOSED, returnMap); | |||
| } else if ("REVOKED".equals(trade_state)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_REVERSE.getCode()); | |||
| // handlePayOrderStatusUpdate(record); | |||
| return new ResultData(ErrorCode.PAY_ORDER_REVERSE, returnMap); | |||
| } else if ("USERPAYING".equals(trade_state)) { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| // handlePayOrderStatusUpdate(record); | |||
| return new ResultData(ErrorCode.PAY_ORDER_PAYING, returnMap); | |||
| } else { | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_FAIL.getCode()); | |||
| // handlePayOrderStatusUpdate(record); | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR, returnMap); | |||
| } | |||
| } else { | |||
| String errCode = returnMap.get("err_code"); | |||
| String errCodeDes = returnMap.get("err_code_des"); | |||
| if (errCode.equalsIgnoreCase("ORDERNOTEXIST")) { | |||
| // 支付订单不存在, 订单失败 | |||
| WxOrder updateOrderForMicroPay = new WxOrder(); | |||
| updateOrderForMicroPay.setId(order.getId()); | |||
| updateOrderForMicroPay.setOrderStatus(EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL.getCode()); | |||
| try { | |||
| wxOrderMapper.updateById(updateOrderForMicroPay); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| } | |||
| } | |||
| String errMsg = returnMap.get("return_msg"); | |||
| if (StringUtils.isNotBlank(errMsg)) { | |||
| record.setFailReason(errMsg); | |||
| } else { | |||
| record.setFailReason(errCodeDes); | |||
| } | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } else { | |||
| String errCode = returnMap.get("err_code"); | |||
| String errCodeDes = returnMap.get("err_code_des"); | |||
| if (errCode.equalsIgnoreCase("ORDERNOTEXIST")) { | |||
| // 支付订单不存在, 订单失败 | |||
| WxOrder updateOrderForMicroPay = new WxOrder(); | |||
| updateOrderForMicroPay.setId(order.getId()); | |||
| updateOrderForMicroPay.setOrderStatus(EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL.getCode()); | |||
| try { | |||
| wxOrderMapper.updateById(updateOrderForMicroPay); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| } | |||
| } | |||
| String errMsg = returnMap.get("return_msg"); | |||
| if (StringUtils.isNotBlank(errMsg)) { | |||
| record.setFailReason(errMsg); | |||
| } else { | |||
| record.setFailReason(errCodeDes); | |||
| } | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| PayQueryAdapterResult apiResult = payService.queryPayStatus(record, order, appInfo, payAccount); | |||
| int status = payServiceFactory.getPayAdapterService(payWay.getCode()).queryPayStatus(apiResult, record.getPayOrderNo()); | |||
| boolean isSuccess = true; | |||
| String msg = ""; | |||
| if (status == EnumPayStatus.PAY_STATUS_REFUND.getCode().intValue()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_REFUND, apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_NOTPAY.getCode().intValue()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_NOT_PAY, apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_CLOSE.getCode().intValue()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_CLOSED, apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_REVERSE.getCode().intValue()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_REVERSE, apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_WAIT.getCode().intValue()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_PAYING, apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_FAIL.getCode().intValue()) { | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR, apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_SUCCESS.getCode().intValue()) { | |||
| //C端主动支付还是 | |||
| if (payService instanceof CDrivingPayService) { | |||
| handleOrderPaySuccess(record, apiResult.getTransactionId()); | |||
| //C端被动支付 | |||
| }else if (payService instanceof CPassivePayService) { | |||
| handleMicroOrderPaySuccess(apiResult.getData(),apiResult.getTransactionId(), record,user,appInfo,null); | |||
| } | |||
| return new ResultData(Result.SUCCESS, "success",apiResult.getData()); | |||
| }else if (status == EnumPayStatus.PAY_STATUS_ORDER_NOT_EXISTS.getCode().intValue()) { | |||
| isSuccess = false; | |||
| // 支付订单不存在, 订单失败 | |||
| WxOrder updateOrderForMicroPay = new WxOrder(); | |||
| updateOrderForMicroPay.setId(order.getId()); | |||
| updateOrderForMicroPay.setOrderStatus(EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL.getCode()); | |||
| try { | |||
| wxOrderMapper.updateById(updateOrderForMicroPay); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| } | |||
| msg = "订单支付渠道支付单不存在。orderId:"+order.getId(); | |||
| }else { | |||
| isSuccess = false; | |||
| msg = apiResult.getMsg(); | |||
| } | |||
| if (!isSuccess) { | |||
| record.setFailReason(msg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay order update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(),msg, apiResult.getData()); | |||
| }else { | |||
| return new ResultData(Result.SUCCESS, "success",apiResult.getData()); | |||
| } | |||
| } catch (MallinkException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| @@ -1113,7 +915,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| String partnerKey = payAccount.getApiKey(); | |||
| try { | |||
| if (payWay == EnumPayWay.PAY_WAY_WEAPP) { | |||
| if (payWay == EnumPayWay.PAY_WAY_WECHAT) { | |||
| boolean signVerified = false; | |||
| if (isNormal) { | |||
| // 普通商户号支付 | |||
| @@ -1171,7 +973,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| payOrder.setPayEndFrom(EnumPayEndFrom.CALLBACK.getCode()); | |||
| // 处理支付成功 | |||
| handleOrderPaySuccess(payOrder, paramMap.get("transaction_id"), EnumPayEndFrom.CALLBACK.getCode()); | |||
| handleOrderPaySuccess(payOrder, paramMap.get("transaction_id")); | |||
| logger.info("notify order, wxpay checksign success, paramMap:{}, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "SUCCESS"); | |||
| @@ -1189,30 +991,31 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| return XmlUtil.getRequestXml(resultMap); | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void handleOrderPaySuccess(WxPayOrder record, String transactionId, Integer from) { // from 0:callback, 1:query | |||
| Date currentDate = new Date(); | |||
| public WxOrder handler(WxPayOrder record, String transactionId,Long cUserId) { | |||
| Date currentDate = new Date(); | |||
| EnumPayStatus payStatus = EnumPayStatus.getEnum(record.getPayOrderStatus()); | |||
| // 判断支付状态 | |||
| if (payStatus == EnumPayStatus.PAY_STATUS_SUCCESS) { | |||
| // 已经是成功状态,只更新transactionId | |||
| try { | |||
| WxPayOrder updateT = new WxPayOrder(); | |||
| updateT.setId(record.getId()); | |||
| updateT.setUpdateTime(currentDate); | |||
| updateT.setTransactionId(transactionId); | |||
| record.setUpdateTime(currentDate); | |||
| record.setTransactionId(transactionId); | |||
| if (null != cUserId) { | |||
| record.setCUserId(cUserId); | |||
| } | |||
| record.setFailReason(""); | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return; | |||
| return null; | |||
| } | |||
| if (payStatus != EnumPayStatus.PAY_STATUS_WAIT) { | |||
| logger.info("pay success handle, payOrder " + record.getPayOrderNo() + | |||
| "is paid success , orderId : " + record.getOrderId()); | |||
| return; | |||
| return null; | |||
| } | |||
| WxOrder order = wxOrderMapper.selectById(record.getOrderId()); | |||
| if (order == null) { | |||
| @@ -1227,6 +1030,9 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| updateOrder.updateTenantInfo(order); | |||
| updateOrder.setOrderId(record.getOrderId()); | |||
| updateOrder.setUpdateTime(currentDate); | |||
| if (null != cUserId) { | |||
| updateOrder.setCUserId(cUserId); | |||
| } | |||
| updateOrder.setPayOrderStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| updateOrder.setTransactionId(transactionId); | |||
| try { | |||
| @@ -1235,7 +1041,17 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| logger.error("支付订单数据库更新失败: " + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "支付订单数据库更新失败: " + e.getMessage()); | |||
| } | |||
| return order; | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void handleOrderPaySuccess(WxPayOrder record, String transactionId) { | |||
| WxPayOrderServiceImpl proxy = (WxPayOrderServiceImpl) AopContext.currentProxy(); | |||
| WxOrder order = proxy.handler(record,transactionId,null); | |||
| if (null == order ) { | |||
| return; | |||
| } | |||
| Long productId = order.getProductId(); | |||
| WxCoupon wxCoupon = wxCouponMapper.selectById(productId); | |||
| if(wxCoupon != null) { | |||
| @@ -1274,83 +1090,24 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| return wxCoupon.getType().equals(EnumCouponType.COUPON_GROUP.getCode()) && wxCoupon.getSalePrice().equals(record.getPayAmount()); | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void handleMicroOrderPaySuccess(WxMerchantBUser user, WxPayOrder record, String transactionId, String openId, String isSubscribe) { | |||
| Date currentDate = new Date(); | |||
| EnumPayStatus payStatus = EnumPayStatus.getEnum(record.getPayOrderStatus()); | |||
| // 判断支付状态 | |||
| if (payStatus == EnumPayStatus.PAY_STATUS_SUCCESS) { | |||
| // 已经是成功状态,只更新transactionId | |||
| try { | |||
| record.setUpdateTime(currentDate); | |||
| record.setTransactionId(transactionId); | |||
| record.setFailReason(""); | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("更新transactionId: " + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return; | |||
| } | |||
| if (payStatus != EnumPayStatus.PAY_STATUS_WAIT) { | |||
| logger.info("pay success handle, payOrder " + record.getPayOrderNo() + | |||
| "is paid success , orderId : " + record.getOrderId()); | |||
| return; | |||
| } | |||
| WxOrder order = wxOrderMapper.selectById(record.getOrderId()); | |||
| if (order == null) { | |||
| logger.error("pay success handle, order " + record.getOrderId() + " not found , payOrderid : " + record.getPayOrderNo()); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND); | |||
| } | |||
| WxAppinfo appinfo = wxAppinfoService.getCAppInfo(order); | |||
| // add c_user, 收银台支付和券支付可能不是同一个人 | |||
| Date curDate = new Date(); | |||
| WxCUser userQ = new WxCUser(); | |||
| userQ.setOpenId(openId); | |||
| userQ.setAppId(appinfo.getAppId()); | |||
| WxCUser user1 = wxCUserMapper.findByOpenId(userQ); | |||
| if (user1 == null) { | |||
| userQ.setCreateDate(curDate); | |||
| userQ.setUpdateDate(curDate); | |||
| if ("N".equalsIgnoreCase(isSubscribe)) { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.NO.getCode()); | |||
| } else { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.YES.getCode()); | |||
| } | |||
| wxCUserService.saveOrUpdate(userQ); | |||
| } else { | |||
| userQ.setId(user1.getId()); | |||
| userQ.setUpdateDate(curDate); | |||
| userQ.setLoginCount(user1.getLoginCount()); | |||
| if ("N".equalsIgnoreCase(isSubscribe)) { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.NO.getCode()); | |||
| } else { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.YES.getCode()); | |||
| } | |||
| wxCUserService.saveOrUpdate(userQ); | |||
| } | |||
| record.setCUserId(userQ.getId()); | |||
| // 修改支付订单状态 | |||
| WxPayOrder updateOrder = new WxPayOrder(); | |||
| updateOrder.setId(record.getId()); | |||
| updateOrder.setOrderId(record.getOrderId()); | |||
| updateOrder.setCUserId(userQ.getId()); | |||
| updateOrder.setUpdateTime(currentDate); | |||
| updateOrder.setPayOrderStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| updateOrder.setTransactionId(transactionId); | |||
| updateOrder.setFailReason(""); | |||
| try { | |||
| wxPayOrderMapper.updateById(updateOrder); | |||
| } catch (Exception e) { | |||
| logger.error("支付订单数据库更新失败: " + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "支付订单数据库更新失败: " + e.getMessage()); | |||
| } | |||
| public void handleMicroOrderPaySuccess(Object result,String transcationId, WxPayOrder record ,WxMerchantBUser user,WxAppinfo appInfo,PayExtraParam params) { | |||
| //B端操作,需要生成cUser | |||
| Long cUserId; | |||
| try { | |||
| cUserId = payServiceFactory.getCPassivePayService(1).createCUserAfterPay(result, appInfo); | |||
| } catch (Exception e1) { | |||
| logger.error("创建C端用户失败: " ,e1); | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "创建C端用户失败: " + e1.getMessage()); | |||
| } | |||
| WxPayOrderServiceImpl proxy = (WxPayOrderServiceImpl) AopContext.currentProxy(); | |||
| WxOrder order = proxy.handler(record,transcationId,cUserId); | |||
| if (null == order) { | |||
| return; | |||
| } | |||
| // B端收银台核销券处理, 收银台支付和券支付可能不是同一个人 | |||
| WxPayOrder payOrderQ = new WxPayOrder(); | |||
| payOrderQ.setOrderId(order.getId()); | |||
| @@ -1361,11 +1118,9 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| if (unPayOrder != null) { | |||
| // 更新券支付状态 | |||
| try { | |||
| WxPayOrder payOrderCoupon = new WxPayOrder(); | |||
| payOrderCoupon.setId(unPayOrder.getId()); | |||
| payOrderCoupon.setOrderId(order.getId()); | |||
| payOrderCoupon.setPayOrderStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| wxPayOrderMapper.updateById(updateOrder); | |||
| unPayOrder.setOrderId(order.getId()); | |||
| unPayOrder.setPayOrderStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| wxPayOrderMapper.updateById(unPayOrder); | |||
| } catch (Exception e) { | |||
| logger.error("支付订单数据库更新失败: " + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "支付订单数据库更新失败: " + e.getMessage()); | |||
| @@ -1385,7 +1140,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| // 修改订单状态 | |||
| order.setCUserId(userQ.getId()); | |||
| order.setCUserId(cUserId); | |||
| try { | |||
| int _count = wxOrderService.microPayOrderSuccess(order); | |||
| if (_count > 1) { | |||
| @@ -1410,7 +1165,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| if (record.getShare().equals(EnumPayShare.NO.getCode())) { | |||
| //结算记录 | |||
| try { | |||
| createMerchantSubsidy(record, order, curDate); | |||
| createMerchantSubsidy(record, order, new Date()); | |||
| } catch (Exception e) { | |||
| logger.error("收银台结算记录失败,{}", e.getMessage()); | |||
| } | |||
| @@ -1572,7 +1327,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| try { | |||
| record.setPayOrderNo(String.valueOf(record.getId())); | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectById(appInfo.getPayId()); | |||
| int payStatus = PayServiceFactory.getPayAdapterService(record.getPayFrom()).queryPayStatusCode(record, order, appInfo, payAccount); | |||
| int payStatus = payServiceFactory.getPayAdapterService(record.getPayVendor()).queryPayStatusCode(record, order, appInfo, payAccount); | |||
| if (payStatus!=EnumPayStatus.PAY_STATUS_SUCCESS.getCode().intValue() && payStatus!=EnumPayStatus.PAY_STATUS_WAIT.getCode().intValue()) { | |||
| // 支付订单未成功,返回异常 | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_FAIL.getCode()); | |||
| @@ -1729,4 +1484,6 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| } | |||
| @@ -511,7 +511,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| record.setRefundVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setRefundVendor(payOrder.getPayVendor()); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.info("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| @@ -600,7 +600,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| record.setRefundVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setRefundVendor(payOrder.getPayVendor()); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.info("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| @@ -821,7 +821,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| record.setRefundVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setRefundVendor(payOrder.getPayVendor()); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.info("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| @@ -910,7 +910,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| record.setRefundVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setRefundVendor(payOrder.getPayVendor()); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.info("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| @@ -1445,7 +1445,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| record.setRefundVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setRefundVendor(payOrder.getPayVendor()); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.info("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| @@ -1527,7 +1527,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| record.setRefundVendor(EnumPayWay.PAY_WAY_WEAPP.getCode()); | |||
| record.setRefundVendor(payOrder.getPayVendor()); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.info("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| @@ -1,21 +0,0 @@ | |||
| package com.iformall.service.pay; | |||
| import java.util.Date; | |||
| import java.util.Map; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayShare; | |||
| public interface PayAdapterService { | |||
| public PayAdapterResult pay(WxPayAccount payAccount,WxOrder order,WxPayOrder record ,EnumPayShare isShare,WxAppinfo appInfo,Date currentDate, PayExtraParam params) throws Exception; | |||
| public Object queryPayStatus(WxPayOrder oldRecord,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount) throws Exception; | |||
| public int queryPayStatus(Object statusObject,String orderOutNo) throws Exception; | |||
| public int queryPayStatusCode(WxPayOrder oldRecord,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount) throws Exception; | |||
| } | |||
| @@ -3,35 +3,75 @@ package com.iformall.service.pay; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayWay; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.pay.impl.WxPayAdapterService; | |||
| import com.iformall.service.WxPayOrderService; | |||
| import com.iformall.service.pay.service.CDrivingPayService; | |||
| import com.iformall.service.pay.service.CPassivePayService; | |||
| import com.iformall.service.pay.service.PayAdapterService; | |||
| import com.iformall.service.pay.service.miniApp.wx.appPay.WxMiniAppPayAdapterService; | |||
| import com.iformall.service.pay.service.miniApp.wx.maPay.WxMaPayAdapterService; | |||
| /** | |||
| * 支付方式工厂 | |||
| * @author alascor | |||
| */ | |||
| @Service | |||
| public class PayServiceFactory { | |||
| private static Map<Integer,PayAdapterService> serviceMap = new ConcurrentHashMap<Integer, PayAdapterService>(); | |||
| private Map<Integer,PayAdapterService> serviceMap = null; | |||
| public static PayAdapterService getPayAdapterService(Integer type) throws MallinkException{ | |||
| if (serviceMap.containsKey(type)) { | |||
| return serviceMap.get(type); | |||
| @Autowired | |||
| WxMiniAppPayAdapterService wxMiniAppPayService; | |||
| @Autowired | |||
| WxMaPayAdapterService wxMiniAppMaPayService; | |||
| @Autowired | |||
| WxPayOrderService wxPayOrderService; | |||
| public PayAdapterService getPayAdapterService(Integer type) throws MallinkException{ | |||
| PayAdapterService service = getServiceMap().get(type); | |||
| if (null == service) { | |||
| throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"payType["+type+"] 支付service未找到"); | |||
| } | |||
| return service; | |||
| } | |||
| public CDrivingPayService getCDrivingPayService(Integer type) throws MallinkException{ | |||
| PayAdapterService service = getServiceMap().get(type); | |||
| if (null == service) { | |||
| throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"payType["+type+"] 支付service未找到"); | |||
| } | |||
| if (service instanceof CDrivingPayService) { | |||
| return (CDrivingPayService) service; | |||
| }else { | |||
| PayAdapterService payService = null; | |||
| if(EnumPayWay.PAY_WAY_WECHAT.getCode().intValue()==type.intValue()) { | |||
| payService = new WxPayAdapterService(); | |||
| }else { | |||
| //other pay | |||
| } | |||
| serviceMap.put(type, payService); | |||
| throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"payType["+type+"] 支付service不是CDrivingPayService"); | |||
| } | |||
| PayAdapterService service = serviceMap.get(type); | |||
| } | |||
| public CPassivePayService getCPassivePayService(Integer type) throws MallinkException{ | |||
| PayAdapterService service = getServiceMap().get(type); | |||
| if (null == service) { | |||
| throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"payType["+type+"] 支付service未找到"); | |||
| } | |||
| return service; | |||
| if (service instanceof CPassivePayService) { | |||
| return (CPassivePayService) service; | |||
| }else { | |||
| throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"payType["+type+"] 支付service不是CPassivePayService"); | |||
| } | |||
| } | |||
| private Map<Integer,PayAdapterService> getServiceMap() { | |||
| if (null != serviceMap) { | |||
| serviceMap = new ConcurrentHashMap<Integer,PayAdapterService>(); | |||
| serviceMap.put(EnumPayWay.PAY_WAY_WECHAT.getCode(), wxMiniAppPayService); | |||
| serviceMap.put(EnumPayWay.PAY_WAY_WECHAT_MA.getCode(), wxMiniAppMaPayService); | |||
| } | |||
| return serviceMap; | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package com.iformall.service.pay; | |||
| package com.iformall.service.pay.entity; | |||
| import java.io.Serializable; | |||
| @@ -8,14 +8,17 @@ public class PayAdapterResult implements Serializable{ | |||
| private String msg; | |||
| private Object data; | |||
| private String transactionId; | |||
| public PayAdapterResult() { | |||
| } | |||
| public PayAdapterResult(boolean isSuccess,String msg,Object data) { | |||
| public PayAdapterResult(boolean isSuccess,String msg,Object data,String transactionId) { | |||
| this.isSuccess = isSuccess; | |||
| this.msg = msg; | |||
| this.data = data; | |||
| this.transactionId = transactionId; | |||
| } | |||
| public boolean isSuccess() { | |||
| @@ -36,4 +39,12 @@ public class PayAdapterResult implements Serializable{ | |||
| public void setData(Object data) { | |||
| this.data = data; | |||
| } | |||
| public String getTransactionId() { | |||
| return transactionId; | |||
| } | |||
| public void setTransactionId(String transactionId) { | |||
| this.transactionId = transactionId; | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package com.iformall.service.pay; | |||
| package com.iformall.service.pay.entity; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| @@ -0,0 +1,30 @@ | |||
| package com.iformall.service.pay.entity; | |||
| import java.io.Serializable; | |||
| import lombok.Data; | |||
| import lombok.ToString; | |||
| @Data | |||
| @ToString | |||
| public class PayQueryAdapterResult implements Serializable{ | |||
| private static final long serialVersionUID = -8271349001408799798L; | |||
| public PayQueryAdapterResult(int code, String msg, Object data,String transactionId,String endTimeStr) { | |||
| this.code = code; | |||
| this.msg = msg; | |||
| this.data = data; | |||
| } | |||
| private int code; | |||
| private String msg; | |||
| private Object data; | |||
| private String transactionId; | |||
| private String endTimeStr; | |||
| } | |||
| @@ -0,0 +1,9 @@ | |||
| package com.iformall.service.pay.service; | |||
| /** | |||
| * C端主动发起的支付 | |||
| * @author alascor | |||
| */ | |||
| public interface CDrivingPayService extends PayAdapterService{ | |||
| } | |||
| @@ -0,0 +1,33 @@ | |||
| package com.iformall.service.pay.service; | |||
| import java.util.Date; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayShare; | |||
| import com.iformall.service.pay.entity.PayAdapterResult; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| /** | |||
| * C端被动发起的支付 | |||
| * @author alascor | |||
| */ | |||
| public interface CPassivePayService extends PayAdapterService{ | |||
| /** | |||
| * 支付成功之后生成用户 | |||
| * @param result 支付API或者查询API 接口返回的对象 | |||
| * @param payAccount | |||
| * @param order | |||
| * @param record | |||
| * @param isShare | |||
| * @param appInfo | |||
| * @param currentDate | |||
| * @param params | |||
| * @throws Exception | |||
| */ | |||
| public Long createCUserAfterPay(Object result,WxAppinfo appInfo) throws Exception; | |||
| } | |||
| @@ -0,0 +1,59 @@ | |||
| package com.iformall.service.pay.service; | |||
| import java.util.Date; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayShare; | |||
| import com.iformall.service.pay.entity.PayAdapterResult; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| public interface PayAdapterService { | |||
| /** | |||
| * 真正支付过程,调用对应端的API调用支付 | |||
| * @param payAccount WxPayAccount | |||
| * @param order WxOrder | |||
| * @param record WxPayOrder | |||
| * @param isShare EnumPayShare | |||
| * @param appInfo C端app | |||
| * @param currentDate Date 当前时间,可能加密串用 | |||
| * @param params PayExtraParam 其他需要的参数 | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| public PayAdapterResult pay(WxPayAccount payAccount,WxOrder order,WxPayOrder record ,EnumPayShare isShare,WxAppinfo appInfo,Date currentDate, PayExtraParam params) throws Exception; | |||
| /** | |||
| * 查询支付结果,调用对应端的API查询支付结果 | |||
| * @param oldRecord WxPayOrder | |||
| * @param order WxOrder | |||
| * @param appInfo C端app | |||
| * @param payAccount WxPayAccount | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| public PayQueryAdapterResult queryPayStatus(WxPayOrder oldRecord,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount) throws Exception; | |||
| /** | |||
| * 根据返回结果对象解析出支付状态 | |||
| * @param statusObject 支付端返回的查询对象 | |||
| * @param orderOutNo 传给第三方支付端的订单唯一标识 | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| public int queryPayStatus(PayQueryAdapterResult statusObject,String orderOutNo) throws Exception; | |||
| /** | |||
| * 查询支付结果,调用对应端的API查询支付结果 | |||
| * @param oldRecord WxPayOrder | |||
| * @param order WxOrder | |||
| * @param appInfo C端app | |||
| * @param payAccount WxPayAccount | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| public int queryPayStatusCode(WxPayOrder oldRecord,WxOrder order,WxAppinfo appInfo,WxPayAccount payAccount) throws Exception; | |||
| } | |||
| @@ -0,0 +1,32 @@ | |||
| package com.iformall.service.pay.service.miniApp.wx; | |||
| import java.util.Map; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.service.helper.WxPayOrderServiceHelper; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| import com.iformall.service.pay.service.PayAdapterService; | |||
| public class BaseWxPayAdapterService { | |||
| protected PayQueryAdapterResult queryPayStatus(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, WxPayAccount payAccount) | |||
| throws Exception { | |||
| Map<String, String> retMap = WxPayOrderServiceHelper.wxOrderPayStatusMap(oldRecord, order, appInfo, payAccount); | |||
| int code = WxPayOrderServiceHelper.getPayStatusFromMap(retMap,oldRecord.getPayOrderNo()); | |||
| String msg = WxPayOrderServiceHelper.getPayStatusMsg(retMap, oldRecord.getPayOrderNo()); | |||
| PayQueryAdapterResult result = new PayQueryAdapterResult(code, msg, retMap,retMap.get("transaction_id"),retMap.get("time_end")); | |||
| return result; | |||
| } | |||
| protected int queryPayStatus(PayQueryAdapterResult statusObject, String orderOutNo) throws Exception { | |||
| return WxPayOrderServiceHelper.getPayStatusFromMap((Map<String, String>) statusObject.getData(),orderOutNo); | |||
| } | |||
| protected int queryPayStatusCode(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, WxPayAccount payAccount) | |||
| throws Exception { | |||
| return WxPayOrderServiceHelper.wxOrderPayStatus(oldRecord, order, appInfo, payAccount); | |||
| } | |||
| } | |||
| @@ -1,9 +1,10 @@ | |||
| package com.iformall.service.pay.impl; | |||
| package com.iformall.service.pay.service.miniApp.wx.appPay; | |||
| import java.util.Date; | |||
| import java.util.Map; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.springframework.stereotype.Service; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| @@ -21,10 +22,11 @@ import com.iformall.pay.WxPay; | |||
| import com.iformall.pay.WxPayOrderP; | |||
| import com.iformall.pay.WxPayOrderSP; | |||
| import com.iformall.pay.WxPayment; | |||
| import com.iformall.service.helper.WxPayOrderServiceHelper; | |||
| import com.iformall.service.pay.PayAdapterResult; | |||
| import com.iformall.service.pay.PayAdapterService; | |||
| import com.iformall.service.pay.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayAdapterResult; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| import com.iformall.service.pay.service.CDrivingPayService; | |||
| import com.iformall.service.pay.service.miniApp.wx.BaseWxPayAdapterService; | |||
| import com.iformall.utils.BeanUtils; | |||
| import com.iformall.utils.MapUtil; | |||
| import com.iformall.utils.Utility; | |||
| @@ -32,7 +34,8 @@ import com.iformall.utils.Utility; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| @Slf4j | |||
| public class WxPayAdapterService implements PayAdapterService{ | |||
| @Service | |||
| public class WxMiniAppPayAdapterService extends BaseWxPayAdapterService implements CDrivingPayService{ | |||
| JSONObject errorMap = JSON.parseObject("{" + | |||
| @@ -232,20 +235,20 @@ public class WxPayAdapterService implements PayAdapterService{ | |||
| } | |||
| @Override | |||
| public Object queryPayStatus(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, WxPayAccount payAccount) | |||
| throws Exception { | |||
| return WxPayOrderServiceHelper.wxOrderPayStatusMap(oldRecord, order, appInfo, payAccount); | |||
| public PayQueryAdapterResult queryPayStatus(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, | |||
| WxPayAccount payAccount) throws Exception { | |||
| return super.queryPayStatus(oldRecord, order, appInfo, payAccount); | |||
| } | |||
| @Override | |||
| public int queryPayStatus(Object statusObject, String orderOutNo) throws Exception { | |||
| return WxPayOrderServiceHelper.getPayStatusFromMap((Map<String, String>) statusObject,orderOutNo); | |||
| public int queryPayStatus(PayQueryAdapterResult statusObject, String orderOutNo) throws Exception { | |||
| return super.queryPayStatus(statusObject, orderOutNo); | |||
| } | |||
| @Override | |||
| public int queryPayStatusCode(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, WxPayAccount payAccount) | |||
| throws Exception { | |||
| return WxPayOrderServiceHelper.wxOrderPayStatus(oldRecord, order, appInfo, payAccount); | |||
| return super.queryPayStatusCode(oldRecord, order, appInfo, payAccount); | |||
| } | |||
| } | |||
| @@ -0,0 +1,249 @@ | |||
| package com.iformall.service.pay.service.miniApp.wx.maPay; | |||
| import java.util.Date; | |||
| import java.util.Map; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayMode; | |||
| import com.iformall.enums.EnumPayShare; | |||
| import com.iformall.enums.EnumUserIsSubscribe; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.WxCUserMapper; | |||
| import com.iformall.pay.WxMicroPayOrderP; | |||
| import com.iformall.pay.WxMicroPayOrderSP; | |||
| import com.iformall.pay.WxPay; | |||
| import com.iformall.pay.WxPayment; | |||
| import com.iformall.service.WxCUserService; | |||
| import com.iformall.service.pay.entity.PayAdapterResult; | |||
| import com.iformall.service.pay.entity.PayExtraParam; | |||
| import com.iformall.service.pay.entity.PayQueryAdapterResult; | |||
| import com.iformall.service.pay.service.CPassivePayService; | |||
| import com.iformall.service.pay.service.miniApp.wx.BaseWxPayAdapterService; | |||
| import com.iformall.utils.BeanUtils; | |||
| import com.iformall.utils.Utility; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| @Slf4j | |||
| @Service | |||
| public class WxMaPayAdapterService extends BaseWxPayAdapterService implements CPassivePayService{ | |||
| private WxMicroPayOrderP generateWxMicroPayOrderP(WxPayAccount payAccount, WxOrder order, WxPayOrder record, EnumPayShare isShare, | |||
| WxAppinfo appInfo, Date currentDate,String bAppId,String bUserPhone) throws Exception { | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxMicroPayOrderP wxPayOrderP = new WxMicroPayOrderP(); | |||
| wxPayOrderP.setAppid(bAppId); | |||
| wxPayOrderP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderP.setDevice_info(bUserPhone); | |||
| wxPayOrderP.setNonce_str(noncestr); | |||
| wxPayOrderP.setBody(order.getDetail()); | |||
| wxPayOrderP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderP.setTotal_fee(record.getPayAmount()); | |||
| wxPayOrderP.setSpbill_create_ip(record.getIp()); // 终端IP | |||
| wxPayOrderP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| Map<String, String> payOrderMap = BeanUtils.toStringMap(wxPayOrderP); | |||
| wxPayOrderP.setSign(WxPayment.createSign(payOrderMap, payAccount.getApiKey())); | |||
| return wxPayOrderP; | |||
| } | |||
| private PayAdapterResult getWxMicroPayOrderPResult(Map<String, String> returnMap,WxPayOrder record,WxPayAccount payAccount) { | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| PayAdapterResult pr = null; | |||
| if ("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String openId = returnMap.get("openid"); | |||
| String isSubscribe = returnMap.get("is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| record.setTransactionId(transactionId); | |||
| record.setUpdateTime(new Date()); | |||
| pr = new PayAdapterResult(true, "success", returnMap,transactionId); | |||
| } else { | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if (errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| pr = new PayAdapterResult(false, errMsg, returnMap,null); | |||
| } | |||
| } else { | |||
| String errMsg = returnMap.get("return_msg"); | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| pr = new PayAdapterResult(false, errMsg, returnMap,null); | |||
| } | |||
| return pr; | |||
| } | |||
| private WxMicroPayOrderSP generateWxMicroPayOrderSP(WxPayAccount payAccount, WxOrder order, WxPayOrder record, EnumPayShare isShare, | |||
| WxAppinfo appInfo, Date currentDate,String bUserPhone) throws Exception { | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxMicroPayOrderSP wxPayOrderSP = new WxMicroPayOrderSP(); | |||
| wxPayOrderSP.setAppid(appInfo.getParentAppId()); | |||
| wxPayOrderSP.setMch_id(payAccount.getMchId()); | |||
| wxPayOrderSP.setSub_appid(appInfo.getAppId()); | |||
| wxPayOrderSP.setSub_mch_id(payAccount.getSubMchId()); | |||
| wxPayOrderSP.setDevice_info(bUserPhone); | |||
| wxPayOrderSP.setNonce_str(noncestr); | |||
| wxPayOrderSP.setBody(order.getDetail()); | |||
| wxPayOrderSP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxPayOrderSP.setTotal_fee(record.getPayAmount()); | |||
| wxPayOrderSP.setSpbill_create_ip(record.getIp()); | |||
| wxPayOrderSP.setAuth_code(record.getAuthCode()); | |||
| wxPayOrderSP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| Date futureDate = new Date(); | |||
| futureDate.setTime(currentDate.getTime() + 15 * 60 * 1000); | |||
| wxPayOrderSP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(futureDate)); // 15分钟后结束 | |||
| wxPayOrderSP.setSign_type("HMAC-SHA256"); | |||
| wxPayOrderSP.setProfit_sharing(null); | |||
| if (isShare == EnumPayShare.YES) { | |||
| wxPayOrderSP.setProfit_sharing("Y"); | |||
| } | |||
| Map<String, String> payOrderMap = BeanUtils.toStringMap(wxPayOrderSP); | |||
| wxPayOrderSP.setSign(WxPayment.createSignHMAC(payOrderMap, payAccount.getApiKey())); | |||
| return wxPayOrderSP; | |||
| } | |||
| private PayAdapterResult getWxMicroPayOrderSPResutl(Map<String, String> returnMap,WxPayOrder record,WxPayAccount payAccount) { | |||
| PayAdapterResult pr = null; | |||
| String return_code = returnMap.get("return_code"); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equalsIgnoreCase(return_code)) { | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String openId = returnMap.get("sub_openid"); | |||
| String isSubscribe = returnMap.get("sub_is_subscribe"); | |||
| String transactionId = returnMap.get("transaction_id"); | |||
| record.setTransactionId(transactionId); | |||
| record.setUpdateTime(new Date()); | |||
| pr = new PayAdapterResult(true, "success", returnMap,transactionId); | |||
| } else { | |||
| String err_code = returnMap.get("err_code"); | |||
| String errMsg = returnMap.get("err_code_des"); | |||
| if (errMsg.length() <= 0) { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| pr = new PayAdapterResult(false, errMsg, returnMap,null); | |||
| } | |||
| } else { | |||
| String errMsg = returnMap.get("return_msg"); | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| pr = new PayAdapterResult(false, errMsg, returnMap,null); | |||
| } | |||
| return pr; | |||
| } | |||
| @Override | |||
| public PayAdapterResult pay(WxPayAccount payAccount, WxOrder order, WxPayOrder record, EnumPayShare isShare, | |||
| WxAppinfo appInfo, Date currentDate, PayExtraParam params) throws Exception { | |||
| if (payAccount.getType() == EnumPayMode.MCH.getCode()) { | |||
| // 扫码支付 普通商户模式 | |||
| String bAppId = (String) params.getValue("bAppId"); | |||
| if (StringUtils.isBlank(bAppId)) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"bAppId 不能为空"); | |||
| } | |||
| String bUserPhone = (String) params.getValue("bUserPhone"); | |||
| if (StringUtils.isBlank(bUserPhone)) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"bUserPhone 不能为空"); | |||
| } | |||
| WxMicroPayOrderP wxPayOrderP = generateWxMicroPayOrderP(payAccount, order, record, isShare, appInfo, currentDate, bAppId, bUserPhone); | |||
| String response = WxPay.micropay(BeanUtils.toStringMap(wxPayOrderP)); | |||
| log.info("pay order, wechat micropay, " + wxPayOrderP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payOrderId", record.getPayOrderNo()); | |||
| returnMap.put("orderId", String.valueOf(order.getId())); | |||
| return getWxMicroPayOrderPResult(returnMap, record, payAccount); | |||
| } else { | |||
| // 统一下单 // 服务商模式 | |||
| String bUserPhone = (String) params.getValue("bUserPhone"); | |||
| if (StringUtils.isBlank(bUserPhone)) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"bUserPhone 不能为空"); | |||
| } | |||
| WxMicroPayOrderSP wxPayOrderSP = generateWxMicroPayOrderSP(payAccount, order, record, isShare, appInfo, currentDate, bUserPhone); | |||
| String response = WxPay.micropay(BeanUtils.toStringMap(wxPayOrderSP)); | |||
| log.info("pay order, wechat micropay, " + wxPayOrderSP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payOrderId", record.getPayOrderNo()); | |||
| returnMap.put("orderId", String.valueOf(order.getId())); | |||
| return getWxMicroPayOrderSPResutl(returnMap, record, payAccount); | |||
| } | |||
| } | |||
| @Autowired | |||
| WxCUserMapper wxCUserMapper; | |||
| @Autowired | |||
| WxCUserService wxCUserService; | |||
| @Override | |||
| public Long createCUserAfterPay(Object result,WxAppinfo appInfo) throws Exception { | |||
| // add c_user, 收银台支付和券支付可能不是同一个人 | |||
| Date curDate = new Date(); | |||
| WxCUser userQ = new WxCUser(); | |||
| Map<String, String> returnMap = (Map<String, String>) result; | |||
| String openId = returnMap.get("sub_openid"); | |||
| String isSubscribe = returnMap.get("sub_is_subscribe"); | |||
| userQ.setOpenId(openId); | |||
| userQ.setAppId(appInfo.getAppId()); | |||
| WxCUser user1 = wxCUserMapper.findByOpenId(userQ); | |||
| if (user1 == null) { | |||
| userQ.setCreateDate(curDate); | |||
| userQ.setUpdateDate(curDate); | |||
| if ("N".equalsIgnoreCase(isSubscribe)) { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.NO.getCode()); | |||
| } else { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.YES.getCode()); | |||
| } | |||
| wxCUserService.saveOrUpdate(userQ); | |||
| } else { | |||
| userQ.setId(user1.getId()); | |||
| userQ.setUpdateDate(curDate); | |||
| userQ.setLoginCount(user1.getLoginCount()); | |||
| if ("N".equalsIgnoreCase(isSubscribe)) { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.NO.getCode()); | |||
| } else { | |||
| userQ.setIsSubscribe(EnumUserIsSubscribe.YES.getCode()); | |||
| } | |||
| wxCUserService.saveOrUpdate(userQ); | |||
| } | |||
| return userQ.getId(); | |||
| } | |||
| @Override | |||
| public PayQueryAdapterResult queryPayStatus(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, | |||
| WxPayAccount payAccount) throws Exception { | |||
| return super.queryPayStatus(oldRecord, order, appInfo, payAccount); | |||
| } | |||
| @Override | |||
| public int queryPayStatus(PayQueryAdapterResult statusObject, String orderOutNo) throws Exception { | |||
| return super.queryPayStatus(statusObject, orderOutNo); | |||
| } | |||
| @Override | |||
| public int queryPayStatusCode(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, WxPayAccount payAccount) | |||
| throws Exception { | |||
| return super.queryPayStatusCode(oldRecord, order, appInfo, payAccount); | |||
| } | |||
| } | |||
| @@ -0,0 +1,11 @@ | |||
| package com.iformall.service.pay.service.payway; | |||
| import com.iformall.service.pay.service.PayAdapterService; | |||
| /** | |||
| * 小程序app支付service | |||
| * @author alascor | |||
| */ | |||
| public abstract class MiniAppPayBaseService implements PayAdapterService{ | |||
| } | |||