| @@ -38,6 +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.utils.IPUtil; | |||
| import io.swagger.annotations.Api; | |||
| @@ -66,7 +67,13 @@ public class WxPayOrderController extends BaseController { | |||
| @Autowired | |||
| private WxCouponService wxCouponService; | |||
| /** | |||
| * 微信支付 | |||
| * @param paramMap | |||
| * @param request | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| //@ApiOperation(value = "发起微信小程序支付订单", notes = "{\"orderId\":\"string\"}") | |||
| @RequestMapping(value = "/create", method = RequestMethod.POST) | |||
| public ResultData _create(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws Exception { | |||
| @@ -81,19 +88,23 @@ public class WxPayOrderController extends BaseController { | |||
| if(!user.isBasicInfo()) { | |||
| return new ResultData(ErrorCode.USER_IS_NOT_MEMBER); | |||
| } | |||
| WxCUserBasicInfo wxCUserBasicInfo = wxCUserBasicInfoService.getById(user.getUserId()); | |||
| return _create(orderIdStr,user.getId(),EnumPayWay.PAY_WAY_WECHAT,user.getAppId(),new PayExtraParam("openId",user.getOpenId()),request); | |||
| } | |||
| private ResultData _create(String orderIdStr,Long userId, EnumPayWay payWay,String appId,PayExtraParam parm,HttpServletRequest request) { | |||
| WxCUserBasicInfo wxCUserBasicInfo = wxCUserBasicInfoService.getById(userId); | |||
| if ( null == wxCUserBasicInfo) { | |||
| logger.error("会员用户不存在."+user.getUserId()); | |||
| return new ResultData(ErrorCode.USER_IS_EMPTY.getCode(),"会员用户不存在."+user.getUserId()); | |||
| logger.error("会员用户不存在."+userId); | |||
| return new ResultData(ErrorCode.USER_IS_EMPTY.getCode(),"会员用户不存在."+userId); | |||
| } | |||
| if (null != wxCUserBasicInfo && EnumCreditLockedStatus.CLOSE.getCode().equals(wxCUserBasicInfo.getStatus())) { | |||
| return new ResultData(ErrorCode.MEMBER_IS_LOCKED); | |||
| } | |||
| WxAppinfo appInfo = getAppInfo(user.getAppId()); | |||
| WxAppinfo appInfo = getAppInfo(appId); | |||
| if(appInfo == null) { | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| Long orderId = 0L; | |||
| try { | |||
| orderId = Long.valueOf(orderIdStr); | |||
| @@ -104,7 +115,7 @@ public class WxPayOrderController extends BaseController { | |||
| record.setOrderId(orderId); | |||
| try { | |||
| record.setIp(IPUtil.getIpAddr(request)); | |||
| return wxPayOrderService.createPayOrder(appInfo, user, record, EnumPayWay.PAY_WAY_WECHAT); | |||
| return wxPayOrderService.createPayOrder(appInfo, wxCUserBasicInfo, record, payWay,parm); | |||
| } catch (MallinkException e) { | |||
| logger.error("payment wechat, order create error, req 2: " + record.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| @@ -13,6 +13,7 @@ import com.iformall.domain.po.WxOrder; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayOrder; | |||
| import com.iformall.enums.EnumPayWay; | |||
| import com.iformall.service.pay.PayExtraParam; | |||
| public interface WxPayOrderService { | |||
| @@ -27,7 +28,7 @@ public interface WxPayOrderService { | |||
| * @param payWay | |||
| * @return | |||
| */ | |||
| ResultData createPayOrder(WxAppinfo appInfo, WxCUserBasicInfo user, WxPayOrder record, EnumPayWay payWay); | |||
| ResultData createPayOrder(WxAppinfo appInfo, WxCUserBasicInfo user, WxPayOrder record, EnumPayWay payWay,PayExtraParam params); | |||
| /** | |||
| * 刷卡支付订单 | |||
| @@ -19,6 +19,7 @@ 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.utils.*; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| @@ -165,12 +166,17 @@ 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 = WxPayOrderServiceHelper.wxOrderPayStatusMap(oldRecord, order, appInfo, payAccount); | |||
| int wxpaystatus = WxPayOrderServiceHelper.getPayStatusFromMap(retMap, oldRecord.getPayOrderNo()); | |||
| //Map<String, String> retMap = new HashMap<String, String>(); | |||
| //retMap.put("time_end","2020-6-18 10:48:20"); | |||
| //retMap.put("transaction_id","asdasdasdasdasd"); | |||
| //int wxpaystatus = EnumPayStatus.PAY_STATUS_SUCCESS.getCode(); | |||
| Map<String, String> retMap = 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()); | |||
| } catch (Exception e) { | |||
| logger.error("pay order service handleWxOrderQuery error: " ,e); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR); | |||
| } | |||
| if (EnumPayStatus.PAY_STATUS_SUCCESS.getCode()==wxpaystatus) { | |||
| //直接调用不会触发事务,spring事务基于AOP代理 | |||
| WxPayOrderServiceImpl proxy = (WxPayOrderServiceImpl) AopContext.currentProxy(); | |||
| @@ -203,7 +209,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| @Override | |||
| public ResultData createPayOrder(WxAppinfo appInfo, WxCUserBasicInfo user, WxPayOrder record, EnumPayWay payWay) { | |||
| public ResultData createPayOrder(WxAppinfo appInfo, WxCUserBasicInfo user, WxPayOrder record, EnumPayWay payWay,PayExtraParam params) { | |||
| IdWorker idworker = IdWorker.get(); | |||
| EnumPayShare isShare = EnumPayShare.NO; | |||
| @@ -392,7 +398,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| } | |||
| PayAdapterResult payResult = PayServiceFactory.getPayAdapterService(payWay.getCode()).pay(payAccount, order, record, params); | |||
| PayAdapterResult payResult = PayServiceFactory.getPayAdapterService(payWay.getCode()).pay(payAccount, order, record,isShare,appInfo, currentDate, params); | |||
| //成功或者失败都需要更新record | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| @@ -402,9 +408,9 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| if (payResult.isSuccess()) { | |||
| new ResultData(Result.SUCCESS, "创建支付订单成功", payResult.getData()); | |||
| return new ResultData(Result.SUCCESS, "创建支付订单成功", payResult.getData()); | |||
| }else { | |||
| new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), payResult.getMsg(), payResult.getData()); | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), payResult.getMsg(), payResult.getData()); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| logger.error("createwxpayorder error:",e); | |||
| @@ -1559,36 +1565,47 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| try { | |||
| record.setPayOrderNo(String.valueOf(record.getId())); | |||
| 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 result_code = returnMap.get("result_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)) { | |||
| // 查询支付订单 -- 已支付 | |||
| // 继续更改状态 | |||
| } else if ("USERPAYING".equals(trade_state)) { | |||
| // 继续检查状态 | |||
| } else { | |||
| // 支付订单未成功,返回异常 | |||
| record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_FAIL.getCode()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("支付订单失败状态更新异常: " + e.getMessage()); | |||
| } | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "支付订单未成功"); | |||
| } | |||
| int payStatus = PayServiceFactory.getPayAdapterService(record.getPayFrom()).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()); | |||
| try { | |||
| wxPayOrderMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("支付订单失败状态更新异常: " + e.getMessage()); | |||
| } | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "支付订单未成功"); | |||
| } | |||
| // String response = WxPayOrderServiceHelper.wechatPayOrderQuery(appInfo, record,payAccount); | |||
| // logger.info("pay order query, " + record.toString() + ", response: " + response); | |||
| // Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| // String result_code = returnMap.get("result_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)) { | |||
| // // 查询支付订单 -- 已支付 | |||
| // // 继续更改状态 | |||
| // } else if ("USERPAYING".equals(trade_state)) { | |||
| // // 继续检查状态 | |||
| // } else { | |||
| // // 支付订单未成功,返回异常 | |||
| // record.setPayOrderStatus(EnumPayStatus.PAY_STATUS_FAIL.getCode()); | |||
| // try { | |||
| // wxPayOrderMapper.updateById(record); | |||
| // } catch (Exception e) { | |||
| // logger.error("支付订单失败状态更新异常: " + e.getMessage()); | |||
| // } | |||
| // throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "支付订单未成功"); | |||
| // } | |||
| // } | |||
| } catch (MallinkException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (RuntimeException e) { | |||
| @@ -7,6 +7,17 @@ public class PayAdapterResult implements Serializable{ | |||
| private boolean isSuccess; | |||
| private String msg; | |||
| private Object data; | |||
| public PayAdapterResult() { | |||
| } | |||
| public PayAdapterResult(boolean isSuccess,String msg,Object data) { | |||
| this.isSuccess = isSuccess; | |||
| this.msg = msg; | |||
| this.data = data; | |||
| } | |||
| public boolean isSuccess() { | |||
| return isSuccess; | |||
| } | |||
| @@ -1,5 +1,8 @@ | |||
| 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; | |||
| @@ -8,5 +11,11 @@ import com.iformall.enums.EnumPayShare; | |||
| public interface PayAdapterService { | |||
| public PayAdapterResult pay(WxPayAccount payAccount,WxOrder order,WxPayOrder record ,EnumPayShare isShare,WxAppinfo appInfo, Object... params) throws Exception; | |||
| 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; | |||
| } | |||
| @@ -0,0 +1,26 @@ | |||
| package com.iformall.service.pay; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| public class PayExtraParam { | |||
| private Map map = new HashMap(); | |||
| public PayExtraParam(Object key,Object value) { | |||
| map.put(key, value); | |||
| } | |||
| public Object getValue(String key) { | |||
| if (map.containsKey(key)) { | |||
| return map.get(key); | |||
| } | |||
| return null; | |||
| } | |||
| public PayExtraParam set(Object key,Object value) { | |||
| this.map.put(key, value); | |||
| return this; | |||
| } | |||
| } | |||
| @@ -3,6 +3,8 @@ package com.iformall.service.pay.impl; | |||
| import java.util.Date; | |||
| import java.util.Map; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.iformall.common.ErrorCode; | |||
| @@ -19,8 +21,10 @@ 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.utils.BeanUtils; | |||
| import com.iformall.utils.MapUtil; | |||
| import com.iformall.utils.Utility; | |||
| @@ -201,14 +205,17 @@ public class WxPayAdapterService implements PayAdapterService{ | |||
| @Override | |||
| public PayAdapterResult pay(WxPayAccount payAccount, WxOrder order, WxPayOrder record,EnumPayShare isShare,WxAppinfo appInfo, Object... params) throws Exception { | |||
| String openId = (String) params[0]; | |||
| String appId = (String) params[1]; | |||
| Date currentDate = (Date) params[2]; | |||
| public PayAdapterResult pay(WxPayAccount payAccount, WxOrder order, WxPayOrder record,EnumPayShare isShare,WxAppinfo appInfo,Date currentDate, PayExtraParam params) throws Exception { | |||
| if (null == params ) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"openId为空"); | |||
| } | |||
| String openId = (String) params.getValue("openId"); | |||
| if (StringUtils.isBlank(openId) ) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"openId为空"); | |||
| } | |||
| //普通商户模式 | |||
| if (payAccount.getType() == EnumPayMode.MCH.getCode()) { | |||
| WxPayOrderP wxPayOrderP = generateWxPayOrderP(payAccount,order,record,openId,appId,currentDate); | |||
| WxPayOrderP wxPayOrderP = generateWxPayOrderP(payAccount,order,record,openId,appInfo.getAppId(),currentDate); | |||
| String response = WxPay.pushOrder(BeanUtils.toStringMap(wxPayOrderP)); | |||
| log.info("pay order, wechat pushOrder, " + wxPayOrderP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| @@ -216,7 +223,7 @@ public class WxPayAdapterService implements PayAdapterService{ | |||
| } else { | |||
| // 统一下单 // 服务商模式 | |||
| WxPayOrderSP wxPayOrderSP = generateWxPayOrderSP(payAccount,order,record,appInfo,openId,appId,currentDate,isShare); | |||
| WxPayOrderSP wxPayOrderSP = generateWxPayOrderSP(payAccount,order,record,appInfo,openId,appInfo.getAppId(),currentDate,isShare); | |||
| String response = WxPay.pushOrder(BeanUtils.toStringMap(wxPayOrderSP)); | |||
| log.info("pay order, wechat pushOrder, " + wxPayOrderSP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| @@ -224,4 +231,21 @@ 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); | |||
| } | |||
| @Override | |||
| public int queryPayStatus(Object statusObject, String orderOutNo) throws Exception { | |||
| return WxPayOrderServiceHelper.getPayStatusFromMap((Map<String, String>) statusObject,orderOutNo); | |||
| } | |||
| @Override | |||
| public int queryPayStatusCode(WxPayOrder oldRecord, WxOrder order, WxAppinfo appInfo, WxPayAccount payAccount) | |||
| throws Exception { | |||
| return WxPayOrderServiceHelper.wxOrderPayStatus(oldRecord, order, appInfo, payAccount); | |||
| } | |||
| } | |||