| @@ -814,76 +814,6 @@ public class WxProjectConfigController extends BaseController { | |||
| } | |||
| } | |||
| @ApiOperation("获取抖音支付2.0 回调接口配置") | |||
| @GetMapping("/ttcallback/query/settings") | |||
| @SystemControllerLog(description = "") | |||
| @TenantIgnore | |||
| public ResultData ttcallbackQuerySettings(String appid) { | |||
| logger.debug("[" + getIpAddr() + "] WxProjectConfigController::ttcallbackQuerySettings"); | |||
| try { | |||
| WxAppinfo appinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appid); | |||
| if(appinfo == null){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到对应的小程序"); | |||
| } | |||
| if(!EnumAppPlat.TOUTIAO.getCode().equals(appinfo.getPlat()) | |||
| || !EnumAppType.C.getCode().equals(appinfo.getType())){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"该小程序不支持"); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appinfo.getPayId()); | |||
| if(payAccount == null){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到对应的支付数据"); | |||
| } | |||
| TtPayService ttPayService = maUtil.getTtPayService(appinfo, payAccount); | |||
| CallBackSettingsRequest callBackSettingsRequest = ttPayService.querySettings(); | |||
| return new ResultData(callBackSettingsRequest); | |||
| }catch (Exception e){ | |||
| logger.error(e.getMessage(),e); | |||
| return new ResultData(ErrorCode.SYS_SERVER_ERROR); | |||
| } | |||
| } | |||
| @ApiOperation("配置抖音支付2.0 回调接口") | |||
| @PostMapping("/ttcallback/settings") | |||
| @SystemControllerLog(description = "商场集团-更新") | |||
| @TenantIgnore | |||
| public ResultData ttcallbackSettings(@RequestBody Map<String, String> map) { | |||
| logger.debug("[" + getIpAddr() + "] WxProjectConfigController::ttcallbackSettings"); | |||
| String appid = map.get("appid"); | |||
| String create_order_callback = map.get("createOrderCallback"); | |||
| String refund_callback = map.get("refundCallback"); | |||
| String delivery_qrcode_redirect = map.get("deliveryQrcodeRedirect"); | |||
| if(StringUtils.isBlank(appid) || StringUtils.isBlank(create_order_callback) | |||
| || StringUtils.isBlank(refund_callback)){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL); | |||
| } | |||
| try { | |||
| WxAppinfo appinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appid); | |||
| if(appinfo == null){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到对应的小程序"); | |||
| } | |||
| if(!EnumAppPlat.TOUTIAO.getCode().equals(appinfo.getPlat()) | |||
| || !EnumAppType.C.getCode().equals(appinfo.getType())){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"该小程序不支持"); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appinfo.getPayId()); | |||
| if(payAccount == null){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到对应的支付数据"); | |||
| } | |||
| TtPayService ttPayService = maUtil.getTtPayService(appinfo, payAccount); | |||
| CallBackSettingsRequest request = new CallBackSettingsRequest(); | |||
| request.setCreateOrderCallback(create_order_callback); | |||
| request.setRefundCallback(refund_callback); | |||
| request.setDeliveryQrcodeRedirect(delivery_qrcode_redirect); | |||
| boolean b = ttPayService.callbackSettings(request); | |||
| if(b){ | |||
| return new ResultData(); | |||
| }else{ | |||
| return new ResultData(ErrorCode.SYS_SERVER_ERROR); | |||
| } | |||
| }catch (Exception e){ | |||
| logger.error(e.getMessage(),e); | |||
| return new ResultData(ErrorCode.SYS_SERVER_ERROR); | |||
| } | |||
| } | |||
| } | |||
| @@ -58,7 +58,8 @@ public class CallbackPayController extends BaseController { | |||
| @AuthIgnore | |||
| @ApiOperation("支付回调") | |||
| @PostMapping(value = "{projectType}/pay/v3") | |||
| public void _payV3Notify(@PathVariable Integer projectType,@RequestBody Map<String, Object> paranMap, HttpServletResponse response) { | |||
| public void _payV3Notify(@PathVariable Integer projectType, @RequestBody Map<String, Object> paranMap, | |||
| HttpServletRequest request, HttpServletResponse response) { | |||
| logger.debug("[" + getIpAddr() + "] CallbackPayController::_payV3Notify"); | |||
| logger.info("微信支付回调结果通知{}"+JSONObject.toJSONString(paranMap)); | |||
| /** | |||
| @@ -113,8 +114,9 @@ public class CallbackPayController extends BaseController { | |||
| @AuthIgnore | |||
| @ApiOperation("支付回调") | |||
| @PostMapping(value = "/ttNotify") | |||
| public Map<String,Object> _ttNotify(@RequestBody Map<String, Object> paranMap, HttpServletRequest request, HttpServletResponse response) { | |||
| @PostMapping(value = "{projectType}/ttNotify") | |||
| public Map<String,Object> _ttNotify(@PathVariable Integer projectType, @RequestBody Map<String, Object> paranMap, | |||
| HttpServletRequest request, HttpServletResponse response) { | |||
| logger.debug("[" + getIpAddr() + "] CallbackPayController::_ttNotify"); | |||
| Map<String,Object> resultMap = new HashMap<>(); | |||
| logger.info("抖音支付回调结果通知{}"+JSONObject.toJSONString(paranMap)); | |||
| @@ -126,8 +128,9 @@ public class CallbackPayController extends BaseController { | |||
| String type = (String) paranMap.get("type"); | |||
| Map<String, Object> pMap = JSONObject.parseObject(msg, Map.class); | |||
| String appid = (String) pMap.get("appid"); | |||
| WxAppinfo appInfo = wxAppinfoService.getOnlyByAppIdFromRedis(appid); | |||
| // String appid = (String) pMap.get("appid"); | |||
| // WxAppinfo appInfo = wxAppinfoService.getOnlyByAppIdFromRedis(appid); | |||
| WxAppinfo appInfo = wxAppinfoService.getProjectCAppInfoFromRedis(projectType, EnumAppPlat.TOUTIAO.getCode()); | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appInfo.getPayId()); | |||
| if("payment".equals(type)){ | |||
| String out_order_no = (String)pMap.get("cp_orderno"); | |||
| @@ -151,8 +154,9 @@ public class CallbackPayController extends BaseController { | |||
| @AuthIgnore | |||
| @ApiOperation("支付回调") | |||
| @PostMapping(value = "/aliNotify") | |||
| public String _aliNotify(HttpServletRequest request, HttpServletResponse response) { | |||
| @PostMapping(value = "{projectType}/aliNotify") | |||
| public String _aliNotify(@PathVariable Integer projectType, | |||
| HttpServletRequest request, HttpServletResponse response) { | |||
| logger.debug("[" + getIpAddr() + "] CallbackPayController::_aliNotify"); | |||
| String body = ((BodyReaderHttpServletRequestWrapper) request).getBody(); | |||
| @@ -168,8 +172,9 @@ public class CallbackPayController extends BaseController { | |||
| Map<String, String> paranMap = handBody(body); | |||
| logger.info("ali支付回调结果通知 paranMap{}"+JSONObject.toJSONString(paranMap)); | |||
| String app_id = paranMap.get("app_id"); | |||
| WxAppinfo appInfo = wxAppinfoService.getOnlyByAppIdFromRedis(app_id); | |||
| // String app_id = paranMap.get("app_id"); | |||
| // WxAppinfo appInfo = wxAppinfoService.getOnlyByAppIdFromRedis(app_id); | |||
| WxAppinfo appInfo = wxAppinfoService.getProjectCAppInfoFromRedis(projectType, EnumAppPlat.ALI.getCode()); | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appInfo.getPayId()); | |||
| String out_trade_no = paranMap.get("out_trade_no"); | |||
| @@ -89,22 +89,17 @@ public class MiniAppUserController extends BaseController { | |||
| public ResultData login(@RequestBody Map<String, String> map) { | |||
| String ipaddress = getIpAddr(); | |||
| logger.debug("[" + ipaddress + "] MiniAppUserController::login"); | |||
| logger.info("login >>>>>>>>>>>>>>>>>"+map.toString()); | |||
| Map resultMap = new HashMap(); | |||
| String project = map.get("projectType"); | |||
| String appId = map.get("appId"); | |||
| String code = map.get("code"); | |||
| //登录凭证不能为空 | |||
| if (StringUtils.isBlank(appId)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空"); | |||
| } | |||
| if (StringUtils.isBlank(code)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空"); | |||
| } | |||
| WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId); | |||
| WxAppinfo wxAppinfo = getAppInfo(appId,project); | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| @@ -176,21 +171,19 @@ public class MiniAppUserController extends BaseController { | |||
| public ResultData loginPhone(@RequestBody Map<String, String> map) { | |||
| String ipaddress = getIpAddr(); | |||
| logger.debug("[" + ipaddress + "] MiniAppUserController::loginPhone"); | |||
| logger.info("loginPhone >>>>>>>>>>>>>>>>>"+map.toString()); | |||
| String project = map.get("projectType"); | |||
| String appId = map.get("appId"); | |||
| String openId = map.get("openId"); | |||
| String encryptedData = map.get("encryptedData"); | |||
| String iv = map.get("iv"); | |||
| if(StringUtils.isBlank(appId)){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId 不能为空"); | |||
| } | |||
| if(StringUtils.isBlank(openId)){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空"); | |||
| } | |||
| WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId); | |||
| WxAppinfo wxAppinfo = getAppInfo(appId,project); | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| @@ -252,23 +245,20 @@ public class MiniAppUserController extends BaseController { | |||
| public ResultData loginPhoneCode(@RequestBody Map<String, String> map, HttpServletResponse response) { | |||
| String ipaddress = getIpAddr(); | |||
| logger.debug("[" + ipaddress + "] MiniAppUserController::loginPhoneCode"); | |||
| logger.info("loginPhoneCode >>>>>>>>>>>>>>>>>"+map.toString()); | |||
| // String phone,String code,String pwd | |||
| String project = map.get("projectType"); | |||
| String appId = map.get("appId"); | |||
| String openId = map.get("openId"); | |||
| String phone = map.get("phone"); | |||
| String code = map.get("code"); | |||
| if(StringUtils.isBlank(appId)){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId 不能为空"); | |||
| } | |||
| if(StringUtils.isBlank(openId)){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空"); | |||
| } | |||
| WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId); | |||
| WxAppinfo wxAppinfo = getAppInfo(appId,project); | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| @@ -334,15 +324,13 @@ public class MiniAppUserController extends BaseController { | |||
| @PostMapping("/getUserQrcode") | |||
| @ApiOperation(value = "获取二维码", notes = "{\"encryptedData\":\"string\",\"iv\":\"string\"}") | |||
| public ResultData getUserQrcode(@RequestBody Map<String, String> map) { | |||
| logger.info(map.toString()); | |||
| logger.info("getUserQrcode >>>>>>>>>>>>>>>>>"+map.toString()); | |||
| String project = map.get("projectType"); | |||
| String appId = map.get("appId"); | |||
| String img = map.get("img"); | |||
| if(StringUtils.isBlank(appId)){ | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId 不能为空"); | |||
| } | |||
| WxAppinfo wxAppinfo = wxAppinfoService.getOnlyByAppIdFromRedis(appId); | |||
| WxAppinfo wxAppinfo = getAppInfo(appId,project); | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| @@ -369,4 +357,21 @@ public class MiniAppUserController extends BaseController { | |||
| } | |||
| } | |||
| private WxAppinfo getAppInfo(String appId,String project){ | |||
| if (StringUtils.isBlank(appId)) { | |||
| return null; | |||
| } | |||
| Integer projectType = null; | |||
| if(StringUtils.isNotBlank(project)){ | |||
| try{ | |||
| projectType = Integer.parseInt(project); | |||
| }catch(Exception e){} | |||
| } | |||
| //兼容处理 | |||
| if(projectType == null){ | |||
| projectType = EnumProject.PROJECT_5.getCode(); | |||
| } | |||
| return wxAppinfoService.getProjectCAppInfo(appId,projectType); | |||
| } | |||
| } | |||
| @@ -51,8 +51,6 @@ public class MqBaseConsumer { | |||
| //@Autowired | |||
| //private FmInsideNotifyPaySuccessMsgServiceImpl fmInsideNotifyPaySuccessMsgService; | |||
| @Autowired | |||
| private FmInsideNotifyRefundSuccessMsgServiceImpl fmInsideNotifyRefundSuccessMsgService; | |||
| //@Autowired | |||
| //UpdateCouponStockMsgServiceImpl updateCouponStockMsgService; | |||
| @@ -160,8 +158,6 @@ public class MqBaseConsumer { | |||
| //} | |||
| else if(EnumMsgRecordType.INSIDE_NOTIFY_REFUND_SUCCESS.getCode().equals(baseMsg.getMsgType())) { | |||
| // 内部消息 - 微信退款通知 | |||
| FmInsideNotifyRefundSuccessMsg msg = (FmInsideNotifyRefundSuccessMsg)JsonUtil.readValue(message,FmInsideNotifyRefundSuccessMsg.class); | |||
| fmInsideNotifyRefundSuccessMsgService.send(msg); | |||
| } | |||
| else if(EnumMsgRecordType.CASH_OUT.getCode().equals(baseMsg.getMsgType())) { | |||
| // 内部消息 - 商户提现通知 | |||
| @@ -77,22 +77,6 @@ public class WxPayAccount extends TenantEntity { | |||
| @io.swagger.annotations.ApiModelProperty(value="销售提点",name="sellRate") | |||
| private Integer sellRate; | |||
| public String getPayNotifyV3Url(Integer projectType) { | |||
| if(projectType == null){ | |||
| return notifyUrl + "/pay/v3"; | |||
| } | |||
| return notifyUrl + "/" + projectType + "/pay/v3"; | |||
| } | |||
| public String getPayNotifyUrl() { | |||
| return notifyUrl + "/pay"; | |||
| } | |||
| public String getRefundNotifyV3Url() { | |||
| return notifyUrl + "/refund/v3"; | |||
| } | |||
| public String getRefundNotifyUrl() { | |||
| return notifyUrl + "/refund"; | |||
| } | |||
| @io.swagger.annotations.ApiModelProperty(value="微信支付分serviceId",name="serviceId") | |||
| private String serviceId; | |||
| @io.swagger.annotations.ApiModelProperty(value="微信支付分回调地址",name="payScoreNotifyUrl") | |||
| @@ -52,15 +52,6 @@ public interface WxAppinfoService { | |||
| */ | |||
| WxAppinfo getById(Long id); | |||
| /** | |||
| * 多个存在获取集团的。 | |||
| * | |||
| * @param appId | |||
| * @return | |||
| */ | |||
| WxAppinfo getOnlyByAppId(String appId); | |||
| WxAppinfo getByAppId(String appId,String tenantId); | |||
| /** | |||
| * 获取c端小程序信息 | |||
| * @return | |||
| @@ -85,14 +76,15 @@ public interface WxAppinfoService { | |||
| */ | |||
| void deleteById(Long id); | |||
| WxAppinfo getOnlyByAppIdFromRedis(String appId); | |||
| WxAppinfo getByAppIdFromRedis(String appId,String tenantId); | |||
| WxAppinfo getByIdFromRedis(Long id); | |||
| WxAppinfo getCAppInfoFromRedis(String tenantId, EnumAppPlat appPlat); | |||
| WxAppinfo getProjectCAppInfo(Integer projectType, Integer plat); | |||
| WxAppinfo getProjectCAppInfoFromRedis(Integer projectType, Integer plat); | |||
| WxAppinfo getProjectCAppInfo(String appId, Integer projectType); | |||
| WxAppinfo getProjectCAppInfoFromRedis(String appId, Integer projectType); | |||
| } | |||
| @@ -1,70 +0,0 @@ | |||
| package com.iformall.service; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxMerchant; | |||
| import com.iformall.domain.po.WxPayBill; | |||
| import com.iformall.enums.EnumPayWay; | |||
| import java.util.Map; | |||
| /** | |||
| * @author gongbiao | |||
| */ | |||
| public interface WxPayBillService { | |||
| /** | |||
| * 创建支付订单 | |||
| * | |||
| * @param record 支付订单请求 | |||
| * @param payWay | |||
| * @return | |||
| */ | |||
| ResultData createPayBill(WxMerchant merchant,Integer billTypeValue,WxAppinfo appInfo, WxPayBill record, EnumPayWay payWay); | |||
| /** | |||
| * 微信支付订单查询 | |||
| * @param appInfo 支付订单Appinfo | |||
| * @param record 支付订单 | |||
| */ | |||
| ResultData payBillQuery(WxAppinfo appInfo, WxPayBill record); | |||
| /** | |||
| * 微信支付关闭订单 | |||
| * @param appInfo 支付订单Appinfo | |||
| * @param record 支付订单 | |||
| */ | |||
| ResultData payBillClose(WxAppinfo appInfo, WxPayBill record); | |||
| /** | |||
| * 异步通知 | |||
| * | |||
| * @param paramMap 异步通知参数 | |||
| * @param payWay 支付方式 | |||
| * @return | |||
| */ | |||
| String notify(String tenantId,Map<String, String> paramMap, EnumPayWay payWay); | |||
| /** | |||
| * 支付成功处理 | |||
| * | |||
| * @param record | |||
| * @param transactionId | |||
| */ | |||
| void handleBillPaySuccess(WxPayBill record, String transactionId); | |||
| /** | |||
| * 支付状态处理 | |||
| * | |||
| * @param record | |||
| */ | |||
| void handlePayBillStatusUpdate(WxPayBill record); | |||
| WxPayBill getById(Long payBillId); | |||
| void updatePayBillStatus(WxPayBill payBill); | |||
| WxPayBill queryPayBill(WxPayBill payBill); | |||
| } | |||
| @@ -41,13 +41,6 @@ public interface WxRefundOrderService { | |||
| * @return | |||
| */ | |||
| ResultData queryRefundOrder(WxAppinfo appInfo, WxRefundOrder record); | |||
| /** | |||
| * 异步通知 | |||
| * @param paramMap 异步通知参数 | |||
| * @param payWay 支付方式 | |||
| * @return | |||
| */ | |||
| String notify(Map<String, String> paramMap, EnumPayWay payWay,EnumPayVersion payVersion); | |||
| /** | |||
| * 处理退款成功 | |||
| @@ -15,6 +15,7 @@ import com.iformall.service.WxMallService; | |||
| import com.iformall.utils.Constant; | |||
| import com.iformall.utils.RedisCacheUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -93,30 +94,6 @@ public class WxAppinfoServiceImpl implements WxAppinfoService { | |||
| } | |||
| return null; | |||
| } | |||
| @Override | |||
| public WxAppinfo getOnlyByAppId(String appId) { | |||
| WxAppinfo appinfoQ = new WxAppinfo(); | |||
| appinfoQ.setAppId(appId); | |||
| List<WxAppinfo> list = wxAppinfoMapper.findList(appinfoQ); | |||
| if(list.size() == 1){ | |||
| return list.get(0); | |||
| }else if(list.size() > 0){ | |||
| for (WxAppinfo appinfo:list) { | |||
| TenantEntity tenantEntity = new TenantEntity(); | |||
| tenantEntity.setTenantId(appinfo.getTenantId()); | |||
| if(wxMallService.isgroupSupport(tenantEntity)){ | |||
| return appinfo; | |||
| } | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| @Override | |||
| public WxAppinfo getByAppId(String appId, String tenantId) { | |||
| return wxAppinfoMapper.findByAppId(appId,tenantId); | |||
| } | |||
| @Override | |||
| public void wxAppinfoInit(String tenantId, Long payId, Long payBillId, WxProjectConfig wxProjectConfig) { | |||
| @@ -174,48 +151,6 @@ public class WxAppinfoServiceImpl implements WxAppinfoService { | |||
| wxAppinfoMapper.deleteById(id); | |||
| } | |||
| @Override | |||
| public WxAppinfo getOnlyByAppIdFromRedis(String appId) { | |||
| WxAppinfo record = null; | |||
| String key = Constant.appinfoPrev + "only" + ":" + appId; | |||
| record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class); | |||
| if (null == record) { | |||
| record = this.getOnlyByAppId(appId); | |||
| if(record != null){ | |||
| RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7); | |||
| } | |||
| } | |||
| return record; | |||
| } | |||
| @Override | |||
| public WxAppinfo getByAppIdFromRedis(String appId,String tenantId) { | |||
| WxAppinfo record = null; | |||
| String key = Constant.appinfoPrev + tenantId + ":" + appId; | |||
| record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class); | |||
| if (null == record) { | |||
| record = this.getByAppId(appId,tenantId); | |||
| if(record != null){ | |||
| RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7); | |||
| } | |||
| } | |||
| return record; | |||
| } | |||
| @Override | |||
| public WxAppinfo getByIdFromRedis(Long id) { | |||
| WxAppinfo record = null; | |||
| String key = Constant.appinfoPrev + id; | |||
| record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class); | |||
| if (null == record) { | |||
| record = this.getById(id); | |||
| if(record != null){ | |||
| RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7); | |||
| } | |||
| } | |||
| return record; | |||
| } | |||
| @Override | |||
| public WxAppinfo getCAppInfoFromRedis(String tenantId, EnumAppPlat plat) { | |||
| WxAppinfo record = null; | |||
| @@ -267,19 +202,48 @@ public class WxAppinfoServiceImpl implements WxAppinfoService { | |||
| return record; | |||
| } | |||
| @Override | |||
| public WxAppinfo getProjectCAppInfo(String appId, Integer projectType) { | |||
| if(StringUtils.isBlank(appId) || projectType == null){ | |||
| return null; | |||
| } | |||
| WxAppinfo appinfoQ = new WxAppinfo(); | |||
| appinfoQ.setProjectType(projectType); | |||
| appinfoQ.setAppId(appId); | |||
| appinfoQ.setType(EnumAppType.C.getCode()); | |||
| WxAppinfo appInfo = wxAppinfoMapper.selectOne(new QueryWrapper<>(appinfoQ)); | |||
| return appInfo; | |||
| } | |||
| @Override | |||
| public WxAppinfo getProjectCAppInfoFromRedis(String appId, Integer projectType) { | |||
| if(StringUtils.isBlank(appId) || projectType == null){ | |||
| return null; | |||
| } | |||
| WxAppinfo record = null; | |||
| String key = Constant.appinfoPrev + appId + "-" + projectType | |||
| + "-" + EnumAppType.C.getCode(); | |||
| record = RedisCacheUtils.getCacheObject(wxAppinfoRedisTemplate, key, WxAppinfo.class); | |||
| if (null == record) { | |||
| record = this.getProjectCAppInfo(appId, projectType); | |||
| if(record != null){ | |||
| RedisCacheUtils.cache(wxAppinfoRedisTemplate, key, record, 3600*24*7); | |||
| } | |||
| } | |||
| return record; | |||
| } | |||
| private void deleteRedis(Long id){ | |||
| WxAppinfo record = this.getById(id); | |||
| if(record != null){ | |||
| String key1 = Constant.appinfoPrev + record.getAppId(); | |||
| String key1 = Constant.appinfoPrev + record.getId(); | |||
| RedisCacheUtils.removeCache(wxAppinfoRedisTemplate, key1); | |||
| String key2 = Constant.appinfoPrev + record.getId(); | |||
| String key2 = Constant.appinfoPrev + record.getProjectType() + "-" + record.getPlat() | |||
| + "-" + EnumAppType.C.getCode(); | |||
| RedisCacheUtils.removeCache(wxAppinfoRedisTemplate, key2); | |||
| String key3 = Constant.appinfoPrev + record.getTenantId() | |||
| + "-" + EnumAppPlat.getByCode(record.getPlat()) + "-" + record.getType(); | |||
| String key3 = Constant.appinfoPrev + record.getAppId() + "-" + record.getProjectType() | |||
| + "-" + EnumAppType.C.getCode(); | |||
| RedisCacheUtils.removeCache(wxAppinfoRedisTemplate, key3); | |||
| String key4 = Constant.appinfoPrev + record.getTenantId() | |||
| + "-" + null + "-" + record.getType(); | |||
| RedisCacheUtils.removeCache(wxAppinfoRedisTemplate, key4); | |||
| } | |||
| } | |||
| @@ -1,882 +0,0 @@ | |||
| package com.iformall.service.impl; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.common.Result; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxBillSettle; | |||
| import com.iformall.domain.po.WxMerchant; | |||
| import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.domain.po.WxPayAccount; | |||
| import com.iformall.domain.po.WxPayAccountBill; | |||
| import com.iformall.domain.po.WxPayBill; | |||
| import com.iformall.domain.vo.WxBillAll; | |||
| import com.iformall.enums.*; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.*; | |||
| import com.iformall.pay.*; | |||
| import com.iformall.service.*; | |||
| import com.iformall.utils.*; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.context.annotation.Lazy; | |||
| import org.springframework.stereotype.Service; | |||
| import org.springframework.transaction.annotation.Propagation; | |||
| import org.springframework.transaction.annotation.Transactional; | |||
| import java.text.ParseException; | |||
| import java.util.*; | |||
| /** | |||
| * @author gongbiao | |||
| */ | |||
| @Service | |||
| public class WxPayBillServiceImpl implements WxPayBillService { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| WxAppinfoService wxAppinfoService; | |||
| @Autowired | |||
| WxPayAccountBillMapper wxPayAccountBillMapper; | |||
| @Autowired | |||
| WxPayAccountService wxPayAccountService; | |||
| @Autowired | |||
| WxPayBillMapper wxPayBillMapper; | |||
| @Autowired | |||
| WxBillAllMapper wxBillAllMapper; | |||
| @Lazy | |||
| @Autowired | |||
| WxBillAllService wxBillAllService; | |||
| @Autowired | |||
| WxMerchantBUserMapper wxMerchantBUserMapper; | |||
| @Lazy | |||
| @Autowired | |||
| private WxBillSettleService wxBillSettleService; | |||
| @Lazy | |||
| @Autowired | |||
| WxMerchantBUserService wxMerchantBUserService; | |||
| @Lazy | |||
| @Autowired | |||
| WxMerchantService wxMerchantService; | |||
| JSONObject errorMap = JSON.parseObject("{" + | |||
| "\"NOAUTH\":{\"detail\":\"商户无此接口权限\",\"reason\":\"商户未开通此接口权限\",\"resolution\":\"请商户前往申请此接口权限\"}," + | |||
| "\"NOTENOUGH\":{\"detail\":\"余额不足\",\"reason\":\"用户帐号余额不足\",\"resolution\":\"用户帐号余额不足,请用户充值或更换支付卡后再支付\"}," + | |||
| "\"ORDERPAID\":{\"detail\":\"商户订单已支付\",\"reason\":\"商户订单已支付,无需重复操作\",\"resolution\":\"商户订单已支付,无需更多操作\"}," + | |||
| "\"ORDERCLOSED\":{\"detail\":\"订单已关闭\",\"reason\":\"当前订单已关闭,无法支付\",\"resolution\":\"当前订单已关闭,请重新下单\"}," + | |||
| "\"SYSTEMERROR\":{\"detail\":\"系统错误\t\",\"reason\":\"系统超时\",\"resolution\":\"系统异常,请用相同参数重新调用\"}," + | |||
| "\"APPID_NOT_EXIST\":{\"detail\":\"APPID不存在\",\"reason\":\"参数中缺少APPID\",\"resolution\":\"请检查APPID是否正确\"}," + | |||
| "\"MCHID_NOT_EXIST\":{\"detail\":\"MCHID不存在\",\"reason\":\"参数中缺少MCHID\",\"resolution\":\"请检查MCHID是否正确\"}," + | |||
| "\"APPID_MCHID_NOT_MATCH\":{\"detail\":\"appid和mch_id不匹配\",\"reason\":\"appid和mch_id不匹配\",\"resolution\":\"请确认appid和mch_id是否匹配\"}," + | |||
| "\"LACK_PARAMS\":{\"detail\":\"缺少参数\t\",\"reason\":\"缺少必要的请求参数\",\"resolution\":\"请检查参数是否齐全\"}," + | |||
| "\"OUT_TRADE_NO_USED\":{\"detail\":\"商户订单号重复\",\"reason\":\"同一笔交易不能多次提交\",\"resolution\":\"请核实商户订单号是否重复提交\"}," + | |||
| "\"SIGNERROR\":{\"detail\":\"签名错误\",\"reason\":\"参数签名结果不正确\",\"resolution\":\"请检查签名参数和方法是否都符合签名算法要求\"}," + | |||
| "\"XML_FORMAT_ERROR\":{\"detail\":\"XML格式错误\t\",\"reason\":\"XML格式错误\",\"resolution\":\"请检查XML参数格式是否正确\"}," + | |||
| "\"REQUIRE_POST_METHOD\":{\"detail\":\"请使用post方法\",\"reason\":\"未使用post传递参数\",\"resolution\":\"请检查请求参数是否通过post方法提交\"}," + | |||
| "\"POST_DATA_EMPTY\":{\"detail\":\"post数据为空\",\"reason\":\"post数据不能为空\",\"resolution\":\"请检查post数据是否为空\"}," + | |||
| "\"NOT_UTF8\":{\"detail\":\"编码格式错误\",\"reason\":\"未使用指定编码格式\",\"resolution\":\"请使用UTF-8编码格式\"}}"); | |||
| JSONObject errorMapQuery = JSON.parseObject("{" + | |||
| "\"ORDERNOTEXIST\":{\"detail\":\"此交易订单号不存在\",\"reason\":\"查询系统中不存在此交易订单号\",\"resolution\":\"该API只能查提交支付交易返回成功的订单,请商户检查需要查询的订单号是否正确\"},\n" + | |||
| "\"SYSTEMERROR\":{\"detail\":\"系统错误\t\",\"reason\":\"后台系统返回错误\",\"resolution\":\"系统异常,请再调用发起查询\"}}"); | |||
| JSONObject errorMapClose = JSON.parseObject("{" + | |||
| "\"ORDERPAID\":{\"detail\":\"订单已支付\",\"reason\":\"订单已支付,不能发起关单\",\"resolution\":\"订单已支付,不能发起关单,请当作已支付的正常交易\"}," + | |||
| "\"SYSTEMERROR\":{\"detail\":\"系统错误\",\"reason\":\"系统错误\",\"resolution\":\"系统异常,请重新调用该API\"}," + | |||
| "\"ORDERCLOSED\":{\"detail\":\"订单已关闭\",\"reason\":\"订单已关闭,无法重复关闭\",\"resolution\":\"订单已关闭,无需继续调用\"}," + | |||
| "\"SIGNERROR\":{\"detail\":\"签名错误\",\"reason\":\"参数签名结果不正确\",\"resolution\":\"请检查签名参数和方法是否都符合签名算法要求\"}," + | |||
| "\"REQUIRE_POST_METHOD\":{\"detail\":\"请使用post方法\",\"reason\":\"未使用post传递参数\",\"resolution\":\"请检查请求参数是否通过post方法提交\"}," + | |||
| "\"XML_FORMAT_ERROR\":{\"detail\":\"XML格式错误\t\",\"reason\":\"XML格式错误\",\"reason\":\"请检查XML参数格式是否正确\"}}"); | |||
| @Override | |||
| public ResultData createPayBill(WxMerchant merchant,Integer billTypeValue,WxAppinfo appInfo, WxPayBill record, EnumPayWay payWay) { | |||
| final IdWorker idworker = IdWorker.get(); | |||
| EnumPayShare isShare = EnumPayShare.NO; | |||
| try { | |||
| //查询账单是否存在 | |||
| WxBillAll wxBillAll = new WxBillAll(); | |||
| wxBillAll.updateTenantInfo(record); | |||
| wxBillAll.setId(record.getBillId()); | |||
| wxBillAll.setBillTypeValue(billTypeValue); | |||
| List<Map<String, Object>> bills = wxBillAllService.listBill(wxBillAll,merchant,true,false,false,false); | |||
| if (bills.size()==0) { | |||
| logger.error("pay bill, bill not allow, repaymentReq: " + JSONObject.toJSONString(record) + ", payWay: " + payWay.toString()); | |||
| throw new MallinkException(ErrorCode.BILL_ROUTINE_IS_NOT_FOUND); | |||
| } | |||
| //获取账单数据 | |||
| Map<String, Object> bill = bills.get(0); | |||
| Long owe = (Long) bill.get("owe"); | |||
| if (owe.longValue() <= 0) { | |||
| logger.info("账单欠缴为0"); | |||
| return new ResultData(ErrorCode.BILL_OWE_ZERO); | |||
| } | |||
| //获取支付账户信息 | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appInfo.getPayId()); | |||
| Date currentDate = new Date(); | |||
| String payBillNo; | |||
| WxPayBill billParam= new WxPayBill(); | |||
| billParam.setBillId(record.getBillId()); | |||
| List<WxPayBill> list = wxPayBillMapper.findList(billParam); | |||
| //数据库不存在已有订单,新建订单 | |||
| if (list.size() <= 0) { | |||
| // 创建支付订单 | |||
| Long id = idworker.nextId(); | |||
| payBillNo = id+DateUtils.getSystemTime("yyMMddHHmmss"); | |||
| record.setId(id); | |||
| record.updateTenantInfo(record); | |||
| record.setbUserId(record.getbUserId()); | |||
| record.setCreateTime(currentDate); | |||
| record.setUpdateTime(currentDate); | |||
| record.setPayTimeStart(currentDate); | |||
| long endtime = currentDate.getTime() + 120 * 60 * 1000; | |||
| record.setPayTimeEnd(new Date(endtime)); | |||
| // 支付单号 | |||
| record.setPayBillNo(payBillNo); | |||
| record.setPayAmount((Long) bill.get("owe")); | |||
| record.setPayVendor(payWay.getCode()); | |||
| record.setPayBillStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| record.setShare(isShare.getCode()); | |||
| record.setBillTypeValue(billTypeValue); | |||
| int sqlRow = wxPayBillMapper.insert(record); | |||
| if (sqlRow != 1) { | |||
| logger.error("pay bill insert error: " + JSONObject.toJSONString(record)); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| logger.info("创建支付订单:"+record.toString()); | |||
| } else { | |||
| //数据库存在订单信息,更新支付中状态,返回之前的预支付信息 | |||
| WxPayBill wxPayBill = list.get(0); | |||
| wxPayBill.setShare(isShare.getCode()); | |||
| //更新订单状态为支付中 | |||
| wxPayBill.setPayBillStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| wxPayBill.setUpdateTime(currentDate); | |||
| int sqlRow = wxPayBillMapper.updateById(wxPayBill); | |||
| if (sqlRow != 1) { | |||
| logger.error("pay bill update error: " + wxPayBill.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| //数据库中openId与再次传来的openId不相同那么关闭此订单再次生成新的订单 | |||
| if(!wxPayBill.getOpenId().equals(record.getOpenId())){ | |||
| String openId = record.getOpenId(); | |||
| //关闭订单 | |||
| logger.info("openId不同时关闭之前的订单:"+record.toString()); | |||
| ResultData resultData = payBillClose(appInfo, wxPayBill); | |||
| if(resultData.code!=Result.SUCCESS){ | |||
| return new ResultData(ErrorCode.ORDER_IS_FAIL); | |||
| } | |||
| record = wxPayBillMapper.selectById(wxPayBill.getId()); | |||
| record.setOpenId(openId); | |||
| //创建新的订单号 | |||
| payBillNo = record.getId()+DateUtils.getSystemTime("yyMMddHHmmss"); | |||
| record.setPayTimeStart(currentDate); | |||
| long endtime = currentDate.getTime() + 120 * 60 * 1000; | |||
| record.setPayTimeEnd(new Date(endtime)); | |||
| record.setPayBillNo(payBillNo); | |||
| record.setPayAmount((Long) bill.get("owe")); | |||
| record.setUpdateTime(currentDate); | |||
| sqlRow = wxPayBillMapper.updateById(record); | |||
| if (sqlRow != 1) { | |||
| logger.error("pay bill update error: " + JSONObject.toJSONString(record)); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| logger.info("新的订单数据:"+JSONObject.toJSONString(record)); | |||
| }else{ | |||
| logger.info("数据库存在订单信息,更新支付中状态,返回之前的预支付信息:"+JSONObject.toJSONString(wxPayBill)); | |||
| if (!owe.equals(wxPayBill.getPayAmount())) { | |||
| logger.info("欠缴费用与之前不同发起新的订单:" + wxPayBill.toString()); | |||
| ResultData resultData = payBillClose(appInfo, wxPayBill); | |||
| if (resultData.code != Result.SUCCESS) { | |||
| return new ResultData(ErrorCode.ORDER_IS_FAIL); | |||
| } | |||
| record = wxPayBillMapper.selectById(wxPayBill.getId()); | |||
| //创建新的订单号 | |||
| payBillNo = record.getId() + DateUtils.getSystemTime("yyMMddHHmmss"); | |||
| record.setPayTimeStart(currentDate); | |||
| long endtime = currentDate.getTime() + 120 * 60 * 1000; | |||
| record.setPayTimeEnd(new Date(endtime)); | |||
| record.setPayBillNo(payBillNo); | |||
| record.setPayAmount((Long) bill.get("owe")); | |||
| record.setUpdateTime(currentDate); | |||
| sqlRow = wxPayBillMapper.updateById(record); | |||
| if (sqlRow != 1) { | |||
| logger.error("pay bill update error: " + JSONObject.toJSONString(record)); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| logger.info("新的订单数据:" + JSONObject.toJSONString(record)); | |||
| } else if (wxPayBill.getPayTimeEnd().after(new Date())) { | |||
| //订单在120分钟之内没有失效,延用之前预支付订单号 | |||
| String noncestr = Utility.generate32UUID(); | |||
| String timestamp = String.valueOf(Utility.getCurrentTimeStamp()); | |||
| Map<String, String> sighMap = MapUtil.getOrderMap(); | |||
| sighMap.put("appId", appInfo.getAppId()); | |||
| sighMap.put("timeStamp", timestamp); | |||
| sighMap.put("nonceStr", noncestr); | |||
| sighMap.put("package", "prepay_id=" + wxPayBill.getPrepayId()); | |||
| sighMap.put("signType", "HMAC-SHA256"); | |||
| String signAgent = WxPayment.createSignHMAC(sighMap, payAccount.getApiKey()); | |||
| Map<String,String> returnMap=new HashMap<>(); | |||
| returnMap.put("timeStamp", timestamp); | |||
| returnMap.put("nonceStr", noncestr); | |||
| returnMap.put("package", "prepay_id=" + wxPayBill.getPrepayId()); | |||
| returnMap.put("paySign", signAgent); | |||
| returnMap.put("signType", "HMAC-SHA256"); | |||
| logger.info("订单在120分钟之内没有失效,延用之前预支付订单号:"+JSONObject.toJSONString(wxPayBill)); | |||
| return new ResultData(Result.SUCCESS, "创建支付订单成功", returnMap); | |||
| } else { | |||
| //超过时间关闭订单 | |||
| logger.info("超过时间关闭订单:" + wxPayBill.toString()); | |||
| ResultData resultData = payBillClose(appInfo, wxPayBill); | |||
| if(resultData.code!=Result.SUCCESS){ | |||
| return new ResultData(ErrorCode.ORDER_IS_FAIL); | |||
| } | |||
| record = wxPayBillMapper.selectById(wxPayBill.getId()); | |||
| //创建新的订单号 | |||
| payBillNo = record.getId()+DateUtils.getSystemTime("yyMMddHHmmss"); | |||
| record.setPayTimeStart(currentDate); | |||
| long endtime = currentDate.getTime() + 120 * 60 * 1000; | |||
| record.setPayTimeEnd(new Date(endtime)); | |||
| record.setPayBillNo(payBillNo); | |||
| record.setPayAmount((Long) bill.get("owe")); | |||
| record.setUpdateTime(currentDate); | |||
| sqlRow = wxPayBillMapper.updateById(record); | |||
| if (sqlRow != 1) { | |||
| logger.error("pay bill update error: " + JSONObject.toJSONString(record)); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| logger.info("新的订单数据:"+JSONObject.toJSONString(record)); | |||
| } | |||
| } | |||
| } | |||
| // 统一下单 // 服务商模式 | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxPayOrderSP wxPayBillSP = new WxPayOrderSP(); | |||
| wxPayBillSP.setSub_openid(record.getOpenId()); | |||
| wxPayBillSP.setAppid(appInfo.getParentAppId()); | |||
| wxPayBillSP.setMch_id(payAccount.getMchId()); | |||
| wxPayBillSP.setSub_appid(appInfo.getAppId()); | |||
| wxPayBillSP.setSub_mch_id(payAccount.getSubMchId()); | |||
| wxPayBillSP.setNonce_str(noncestr); | |||
| wxPayBillSP.setBody(bill.get("billType").toString()); | |||
| wxPayBillSP.setOut_trade_no(record.getPayBillNo()); | |||
| wxPayBillSP.setTotal_fee(bill.get("owe").toString()); | |||
| // 终端IP | |||
| wxPayBillSP.setSpbill_create_ip(record.getIp()); | |||
| wxPayBillSP.setNotify_url(payAccount.getPayNotifyUrl()); | |||
| // 终端类型 | |||
| wxPayBillSP.setTrade_type(WxPay.TradeType.JSAPI.name()); | |||
| // 订单ID | |||
| wxPayBillSP.setProduct_id(String.valueOf(bill.get("id").toString())); | |||
| wxPayBillSP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| wxPayBillSP.setTime_expire(Utility.getDataFormatStringYYYYMMDDHHmmss(record.getPayTimeEnd())); | |||
| wxPayBillSP.setSign_type("HMAC-SHA256"); | |||
| wxPayBillSP.setProfit_sharing(null); | |||
| if (isShare == EnumPayShare.YES) { | |||
| wxPayBillSP.setProfit_sharing("Y"); | |||
| } | |||
| Map<String, String> payBillMap = BeanUtils.toStringMap(wxPayBillSP); | |||
| wxPayBillSP.setSign(WxPayment.createSignHMAC(payBillMap, payAccount.getApiKey())); | |||
| String response = WxPay.pushOrder(BeanUtils.toStringMap(wxPayBillSP)); | |||
| logger.info("pay bill, wechat pushBill, " + wxPayBillSP.toString() + ", response: " + response); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| returnMap.put("payBillId", record.getId().toString()); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equals(result_code)) { | |||
| String prepay_id = returnMap.get("prepay_id"); | |||
| // update payBill with prepay_id | |||
| record.setPrepayId(prepay_id); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayBillMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay bill update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| String timestamp = String.valueOf(Utility.getCurrentTimeStamp()); | |||
| Map<String, String> sighMap = MapUtil.getOrderMap(); | |||
| sighMap.put("appId", appInfo.getAppId()); | |||
| sighMap.put("timeStamp", timestamp); | |||
| sighMap.put("nonceStr", noncestr); | |||
| sighMap.put("package", "prepay_id=" + prepay_id); | |||
| sighMap.put("signType", "HMAC-SHA256"); | |||
| String signAgent = WxPayment.createSignHMAC(sighMap, payAccount.getApiKey()); | |||
| returnMap.put("timeStamp", timestamp); | |||
| returnMap.put("nonceStr", noncestr); | |||
| returnMap.put("package", "prepay_id=" + prepay_id); | |||
| returnMap.put("paySign", signAgent); | |||
| returnMap.put("signType", "HMAC-SHA256"); | |||
| logger.info("back to UI: " + returnMap.toString()); | |||
| return new ResultData(Result.SUCCESS, "创建支付订单成功", returnMap); | |||
| } | |||
| return updatePayBill(record, returnMap, result_code); | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| private ResultData updatePayBill(WxPayBill record, Map<String, String> returnMap, String result_code) { | |||
| String errMsg = ""; | |||
| JSONObject errObj = errorMap.getJSONObject(result_code); | |||
| if (errObj != null) { | |||
| errMsg = errObj.toJSONString(); | |||
| record.setFailReason(errMsg); | |||
| } else { | |||
| errMsg = returnMap.get("return_msg"); | |||
| record.setFailReason(errMsg); | |||
| } | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayBillMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay bill update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| private String wechatPayBillQuery(WxAppinfo appInfo, WxPayBill record) { | |||
| // get payAccount | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appInfo.getPayId()); | |||
| if (payAccount.getType().equals(EnumPayMode.MCH.getCode())) { | |||
| // 普通商户号模式 | |||
| WxPayOrderQ payBillQ = new WxPayOrderQ(); | |||
| String noncestr = Utility.generate32UUID(); | |||
| payBillQ.setAppid(appInfo.getAppId()); | |||
| payBillQ.setMch_id(payAccount.getMchId()); | |||
| payBillQ.setNonce_str(noncestr); | |||
| payBillQ.setOut_trade_no(record.getPayBillNo()); | |||
| try { | |||
| Map map = BeanUtils.toStringMap(payBillQ); | |||
| payBillQ.setSign(WxPayment.createSign(map, payAccount.getApiKey())); | |||
| map = BeanUtils.toStringMap(payBillQ); | |||
| String response = WxPay.orderQuery(map); | |||
| return response; | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } else { | |||
| // 服务商模式 | |||
| WxPayOrderSQ payBillSQ = new WxPayOrderSQ(); | |||
| String noncestr = Utility.generate32UUID(); | |||
| payBillSQ.setAppid(appInfo.getParentAppId()); | |||
| payBillSQ.setSub_appid(appInfo.getAppId()); | |||
| payBillSQ.setMch_id(payAccount.getMchId()); | |||
| payBillSQ.setSub_mch_id(payAccount.getSubMchId()); | |||
| payBillSQ.setNonce_str(noncestr); | |||
| payBillSQ.setOut_trade_no(record.getPayBillNo()); | |||
| payBillSQ.setSign_type("HMAC-SHA256"); | |||
| try { | |||
| Map map = BeanUtils.toStringMap(payBillSQ); | |||
| payBillSQ.setSign(WxPayment.createSignHMAC(map, payAccount.getApiKey())); | |||
| map = BeanUtils.toStringMap(payBillSQ); | |||
| String response = WxPay.orderQuery(map); | |||
| return response; | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| /** | |||
| * 微信订单查询 | |||
| */ | |||
| @Override | |||
| public ResultData payBillQuery(WxAppinfo appInfo, WxPayBill record) { | |||
| try { | |||
| if (StringUtils.isBlank(record.getPayBillNo())) { | |||
| record.setPayBillNo(record.getPayBillNo()); | |||
| } | |||
| String response = wechatPayBillQuery(appInfo, record); | |||
| logger.info("pay bill 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)) { | |||
| record.setPayBillStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| handlePayBillStatusUpdate(record); | |||
| } else if ("USERPAYING".equals(trade_state)) { | |||
| record.setPayBillStatus(EnumPayStatus.PAY_STATUS_WAIT.getCode()); | |||
| handlePayBillStatusUpdate(record); | |||
| } else { | |||
| record.setPayBillStatus(EnumPayStatus.PAY_STATUS_FAIL.getCode()); | |||
| handlePayBillStatusUpdate(record); | |||
| } | |||
| return new ResultData(Result.SUCCESS, "订单查询成功", returnMap); | |||
| } else { | |||
| return updatePayBill(record, returnMap, result_code); | |||
| } | |||
| } catch (MallinkException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| private String wechatPayBillClose(WxAppinfo appInfo, WxPayBill record) { | |||
| // get payAccount | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appInfo.getPayId()); | |||
| if (payAccount.getType().equals(EnumPayMode.MCH.getCode())) { | |||
| // 普通商户号模式 | |||
| WxPayOrderQ payBillC = new WxPayOrderQ(); | |||
| String noncestr = Utility.generate32UUID(); | |||
| payBillC.setAppid(appInfo.getAppId()); | |||
| payBillC.setMch_id(payAccount.getMchId()); | |||
| payBillC.setNonce_str(noncestr); | |||
| payBillC.setOut_trade_no(record.getPayBillNo()); | |||
| return closeOrderNormal(payAccount, payBillC); | |||
| } else { | |||
| // 服务商模式 | |||
| WxPayOrderSQ payBillSC = new WxPayOrderSQ(); | |||
| String noncestr = Utility.generate32UUID(); | |||
| payBillSC.setAppid(appInfo.getParentAppId()); | |||
| payBillSC.setSub_appid(appInfo.getAppId()); | |||
| payBillSC.setMch_id(payAccount.getMchId()); | |||
| payBillSC.setSub_mch_id(payAccount.getSubMchId()); | |||
| payBillSC.setNonce_str(noncestr); | |||
| payBillSC.setOut_trade_no(record.getPayBillNo()); | |||
| payBillSC.setSign_type("HMAC-SHA256"); | |||
| return closeOrderServer(payAccount, payBillSC); | |||
| } | |||
| } | |||
| private String closeOrderServer(WxPayAccount payAccount, WxPayOrderSQ payBillSC) { | |||
| try { | |||
| Map map = BeanUtils.toStringMap(payBillSC); | |||
| payBillSC.setSign(WxPayment.createSignHMAC(map, payAccount.getApiKey())); | |||
| map = BeanUtils.toStringMap(payBillSC); | |||
| String response = WxPay.closeOrder(map); | |||
| return response; | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| private String closeOrderNormal(WxPayAccount payAccount, WxPayOrderQ payBillC) { | |||
| try { | |||
| Map map = BeanUtils.toStringMap(payBillC); | |||
| payBillC.setSign(WxPayment.createSign(map, payAccount.getApiKey())); | |||
| map = BeanUtils.toStringMap(payBillC); | |||
| String response = WxPay.closeOrder(map); | |||
| return response; | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| /** | |||
| * 微信关闭支付订单 | |||
| */ | |||
| @Override | |||
| public ResultData payBillClose(WxAppinfo appInfo, WxPayBill record) { | |||
| try { | |||
| //关闭之前先查询 | |||
| ResultData resultData = payBillQuery(appInfo, record); | |||
| if(resultData.code == Result.SUCCESS) { | |||
| Map<String, String> map = (Map) resultData.data; | |||
| if("SUCCESS".equals(map.get("result_code")) && "SUCCESS".equals(map.get("trade_state"))){ | |||
| return new ResultData(ErrorCode.ORDER_HAD_PAY); | |||
| } | |||
| } | |||
| String response = wechatPayBillClose(appInfo, record); | |||
| logger.info("pay bill close, " + record.toString() + ", response: " + response); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| String result_code = returnMap.get("result_code"); | |||
| if ("SUCCESS".equals(result_code)) { | |||
| try { | |||
| record.setPayBillStatus(EnumPayStatus.PAY_STATUS_CANCEL.getCode()); | |||
| wxPayBillMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay bill update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(Result.SUCCESS, "订单关闭成功", returnMap); | |||
| } else { | |||
| String errMsg = ""; | |||
| JSONObject errObj = errorMapClose.getJSONObject(result_code); | |||
| if (errObj != null) { | |||
| errMsg = errObj.toJSONString(); | |||
| } else { | |||
| errMsg = returnMap.get("return_msg"); | |||
| } | |||
| record.setFailReason(errMsg); | |||
| record.setUpdateTime(new Date()); | |||
| try { | |||
| wxPayBillMapper.updateById(record); | |||
| } catch (Exception e) { | |||
| logger.error("pay bill update error: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errMsg, returnMap); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| /** | |||
| * 提供微信支付回调调用 | |||
| * | |||
| * @param paramMap 异步通知参数 | |||
| * @param payWay 支付方式 | |||
| * @return | |||
| */ | |||
| @Override | |||
| public String notify(String tenantId,Map<String, String> paramMap, EnumPayWay payWay) { | |||
| // how to get wechatAppId, wechatMchId, partnerKey | |||
| logger.info("-------------"); | |||
| logger.info("微信回调返回值:" + JSONObject.toJSONString(paramMap)); | |||
| logger.info("-------------"); | |||
| String appId = paramMap.get("appid"); | |||
| String subAppId = paramMap.get("sub_appid"); | |||
| String mchId = paramMap.get("mch_id"); | |||
| String subMchId = paramMap.get("sub_mch_id"); | |||
| String attach = paramMap.get("attach"); | |||
| WxAppinfo appinfo; | |||
| boolean isNormal = true; | |||
| if (StringUtils.isBlank(subAppId) && StringUtils.isBlank(subMchId)) { | |||
| // 普通商户号 | |||
| appinfo = wxAppinfoService.getByAppIdFromRedis(appId,tenantId); | |||
| if (appinfo == null) { | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| isNormal = true; | |||
| } else { | |||
| // 服务号 现在用hmac-sha256 | |||
| appinfo = wxAppinfoService.getByAppIdFromRedis(subAppId,tenantId); | |||
| if (appinfo == null) { | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| isNormal = false; | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appinfo.getPayId()); | |||
| if (payAccount == null) { | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_FOUND); | |||
| } | |||
| String partnerKey = payAccount.getApiKey(); | |||
| try { | |||
| if (payWay.getType() == EnumPayWay.EnumPayWayType.WX_MINIPAY) { | |||
| boolean signVerified = false; | |||
| if (isNormal) { | |||
| // 普通商户号支付 | |||
| signVerified = WxPayment.verifyNotify(paramMap, partnerKey); | |||
| if (!signVerified) { | |||
| logger.warn("notify bill, wxpay checksign error, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_NOTIFY_CHECK_SIGN_ERROR); | |||
| } | |||
| } else { | |||
| // 服务号 现在用hmac-sha256 | |||
| signVerified = WxPayment.verifyNotifyHMAC(paramMap, partnerKey); | |||
| if (!signVerified) { | |||
| logger.warn("notify bill, wxpay checksign error, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_NOTIFY_CHECK_SIGN_ERROR); | |||
| } | |||
| } | |||
| if (!"SUCCESS".equals(paramMap.get("return_code"))) { | |||
| logger.warn("notify bill, wxpay status not success, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", "订单状态码非SUCCESS"); | |||
| return XmlUtil.getRequestXml(resultMap); | |||
| } | |||
| String payBillNo = paramMap.get("out_trade_no"); | |||
| String timEndStr = paramMap.get("time_end"); | |||
| Long payBillId = Long.valueOf(payBillNo.substring(0,payBillNo.length()-12)); | |||
| WxPayBill payBill = wxPayBillMapper.selectById(payBillId); | |||
| if (payBill == null) { | |||
| logger.warn("notify bill, wxpay check pay bill not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", "订单不存在"); | |||
| return XmlUtil.getRequestXml(resultMap); | |||
| } | |||
| // 验证支付金额 | |||
| if (!paramMap.get("total_fee").equals(payBill.getPayAmount().toString())) { | |||
| logger.warn("notify bill, wxpay check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", "订单总金额不一致"); | |||
| return XmlUtil.getRequestXml(resultMap); | |||
| } | |||
| Date timeEnd = null; | |||
| try { | |||
| timeEnd = Utility.getDateFromString(timEndStr); | |||
| } catch (ParseException e) { | |||
| logger.error("解析timeEnd失败"); | |||
| timeEnd = new Date(); | |||
| } | |||
| payBill.setPayTimeEnd(timeEnd); | |||
| // 处理支付成功 | |||
| handleBillPaySuccess(payBill, paramMap.get("transaction_id")); | |||
| logger.info("notify bill, wxpay checksign success, paramMap:{}, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "SUCCESS"); | |||
| resultMap.put("return_msg", "OK"); | |||
| return XmlUtil.getRequestXml(resultMap); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| logger.warn("notify bill, checksign error, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR); | |||
| } | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", "FAILED"); | |||
| return XmlUtil.getRequestXml(resultMap); | |||
| } | |||
| @Override | |||
| public WxPayBill getById(Long id) { | |||
| return wxPayBillMapper.selectById(id); | |||
| } | |||
| @Override | |||
| public void updatePayBillStatus(WxPayBill payBill) { | |||
| try { | |||
| logger.info("更新支付订单"); | |||
| wxPayBillMapper.updateById(payBill); | |||
| }catch (MallinkException e){ | |||
| logger.info("更新支付订单失败"); | |||
| throw new MallinkException(ErrorCode.ORDER_UPDATE_ERR); | |||
| } | |||
| } | |||
| @Override | |||
| public WxPayBill queryPayBill(WxPayBill payBill) { | |||
| List<WxPayBill> select = wxPayBillMapper.selectList(new QueryWrapper(payBill)); | |||
| if(select.size()>0){ | |||
| return select.get(0); | |||
| } | |||
| return null; | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void handleBillPaySuccess(WxPayBill record, String transactionId) { | |||
| Date currentDate = new Date(); | |||
| EnumPayStatus payStatus = EnumPayStatus.getEnum(record.getPayBillStatus()); | |||
| // 判断支付状态 | |||
| if (payStatus == EnumPayStatus.PAY_STATUS_SUCCESS) { | |||
| // 已经是成功状态,只更新transactionId | |||
| try { | |||
| record.setUpdateTime(currentDate); | |||
| record.setTransactionId(transactionId); | |||
| wxPayBillMapper.updateById(record); | |||
| WxBillAll wxBillAll = new WxBillAll(); | |||
| wxBillAll.setId(record.getBillId()); | |||
| wxBillAll.setBillTypeValue(record.getBillTypeValue()); | |||
| WxMerchantBUser buser = wxMerchantBUserService.getById(record.getbUserId()); | |||
| WxMerchant wxMerchant = wxMerchantService.getById(buser.getMerchantId()); | |||
| List<Map<String, Object>> bills = wxBillAllService.listBill(wxBillAll, wxMerchant,false, false, false, false); | |||
| if (bills.size() > 0) { | |||
| Map<String, Object> bill = bills.get(0); | |||
| bill.put("userId", record.getbUserId()); | |||
| wxBillAllService.updateBill(bill); | |||
| } | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| return; | |||
| } | |||
| WxBillAll wxBillAll = new WxBillAll(); | |||
| wxBillAll.setId(record.getBillId()); | |||
| wxBillAll.updateTenantInfo(record); | |||
| List<Map<String, Object>> bills = wxBillAllService.listBill(wxBillAll, null,false, false, false, false); | |||
| if (bills.size()==0) { | |||
| logger.error("pay success handle, bill " + record.getBillId() + " not found , payBillNo : " + record.getPayBillNo()); | |||
| throw new MallinkException(ErrorCode.BILL_ROUTINE_IS_NOT_FOUND); | |||
| } | |||
| Map<String, Object> bill; | |||
| bill = bills.get(0); | |||
| bill.put("userId", record.getbUserId()); | |||
| // 修改支付订单状态 | |||
| WxPayBill updateBill = new WxPayBill(); | |||
| updateBill.setId(record.getId()); | |||
| updateBill.setBillId(record.getBillId()); | |||
| updateBill.setUpdateTime(currentDate); | |||
| updateBill.setPayBillStatus(EnumPayStatus.PAY_STATUS_SUCCESS.getCode()); | |||
| updateBill.setTransactionId(transactionId); | |||
| try { | |||
| wxPayBillMapper.updateById(updateBill); | |||
| } catch (Exception e) { | |||
| logger.error("支付订单数据库更新失败: " + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "支付订单数据库更新失败: " + e.getMessage()); | |||
| } | |||
| //修改账单状态 | |||
| try { | |||
| wxBillAllService.updateBill(bill); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| throw new MallinkException(ErrorCode.BILL_UPDATE_FAILED); | |||
| } | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void handlePayBillStatusUpdate(WxPayBill record) { | |||
| // 判断支付状态 | |||
| if (record.getPayBillStatus().equals(EnumPayStatus.PAY_STATUS_WAIT.getCode())) { | |||
| logger.error("pay handle, payBill " + record.getPayBillNo() + | |||
| "is complete, billId : " + record.getBillId()); | |||
| return; | |||
| } | |||
| WxBillAll wxBillAll = new WxBillAll(); | |||
| wxBillAll.setId(record.getBillId()); | |||
| wxBillAll.setBillTypeValue(record.getBillTypeValue()); | |||
| WxMerchantBUser buser = wxMerchantBUserService.getById(record.getbUserId()); | |||
| WxMerchant wxMerchant = wxMerchantService.getById(buser.getMerchantId()); | |||
| List<Map<String, Object>> bills = wxBillAllService.listBill(wxBillAll, wxMerchant,false, false, false, false); | |||
| if (bills.size()==0) { | |||
| logger.error("pay handle, bill " + record.getBillId() + " not found , payBillNo : " + record.getPayBillNo()); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND); | |||
| } | |||
| Map<String, Object> bill = bills.get(0); | |||
| if (record.getId() > 0) { | |||
| // 1. get appinfo | |||
| WxAppinfo appInfo = null; | |||
| WxAppinfo appinfoQ = new WxAppinfo(); | |||
| appinfoQ.setTenantId(bill.get("tenantId").toString()); | |||
| appinfoQ.setType(EnumAppType.B.getCode()); | |||
| List<WxAppinfo> appList = wxAppinfoService.getList(appinfoQ); | |||
| if (appList.size() > 0) { | |||
| appInfo = appList.get(0); | |||
| } | |||
| // 检查支付订单状态 | |||
| WxPayBill payBillQ = new WxPayBill(); | |||
| payBillQ.setId(record.getId()); | |||
| payBillQ.setTenantId(bill.get("tenantId").toString()); | |||
| payBillQ.setBillId(record.getBillId()); | |||
| WxPayBill updateBill; | |||
| try { | |||
| updateBill = wxPayBillMapper.selectOne(new QueryWrapper(payBillQ)); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_NOT_FOUND); | |||
| } | |||
| if (record.getPayBillStatus().equals(EnumPayStatus.PAY_STATUS_FAIL.getCode())) { | |||
| // 支付订单取消 | |||
| if (updateBill.getPayAmount() > 0) { | |||
| payBillQ = new WxPayBill(); | |||
| payBillQ.setTenantId(bill.get("tenantId").toString()); | |||
| payBillQ.setBillId(record.getBillId()); | |||
| List<WxPayBill> payBills = wxPayBillMapper.selectList(new QueryWrapper(payBillQ)); | |||
| for (WxPayBill payBill : payBills) { | |||
| if(payBill.getPayTimeEnd().before(new Date())){ | |||
| payBillClose(appInfo, payBill); | |||
| } | |||
| } | |||
| } | |||
| } else { | |||
| // 支付订单成功? | |||
| if (updateBill.getPayBillStatus().equals(EnumPayStatus.PAY_STATUS_SUCCESS.getCode())) { | |||
| if (!StringUtils.isBlank(updateBill.getTransactionId())) { | |||
| // 回调已设置成功 | |||
| wxBillAllService.updateBill(bill); | |||
| // 继续修改订单状态 | |||
| } else { | |||
| // 回调异常 | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "transactionId未获取"); | |||
| } | |||
| } else if (updateBill.getPayBillStatus().equals(EnumPayStatus.PAY_STATUS_WAIT.getCode())) { | |||
| // 回调未返回,主动检查支付订单状态 | |||
| try { | |||
| record.setPayBillNo(String.valueOf(record.getId())); | |||
| String response = wechatPayBillQuery(appInfo, record); | |||
| logger.info("pay bill 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)) { | |||
| // 查询支付订单 -- 已支付 | |||
| // 继续更改状态 | |||
| wxBillAllService.updateBill(bill); | |||
| } else { | |||
| // 支付订单未成功,返回异常 | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), "支付订单未成功"); | |||
| } | |||
| } | |||
| } catch (MallinkException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (RuntimeException e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| throw new MallinkException(ErrorCode.PAY_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -586,26 +586,6 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| } | |||
| } | |||
| /** | |||
| * 微信退款结果通知 | |||
| * | |||
| * @param paramMap 异步通知参数 | |||
| * @param payWay 支付方式 | |||
| * @return | |||
| */ | |||
| @Override | |||
| public String notify(Map<String, String> paramMap, EnumPayWay payWay,EnumPayVersion payVersion) { | |||
| RefundNotifyAdapterResult notifyResult = payServiceFactory.getRefundPayAdapterService(payWay.getCode(),payVersion.getCode()).notify(paramMap, payWay); | |||
| if (!notifyResult.isSuccess()) { | |||
| return notifyResult.getReturnJson(); | |||
| }else { | |||
| if (null != notifyResult.getRefundOrder()) { | |||
| handleRefundSuccess(notifyResult.getRefundOrder()); | |||
| } | |||
| return notifyResult.getReturnJson(); | |||
| } | |||
| } | |||
| @Override | |||
| public void callback(Map<String, String> paramMap, EnumPayWay payWay) { | |||
| @@ -1,35 +0,0 @@ | |||
| package com.iformall.service.msg.impl; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.iformall.domain.po.msg.BaseMsg; | |||
| import com.iformall.domain.po.msg.FmInsideNotifyRefundSuccessMsg; | |||
| import com.iformall.enums.EnumPayVersion; | |||
| import com.iformall.enums.EnumPayWay; | |||
| import com.iformall.service.WxRefundOrderService; | |||
| import com.iformall.service.msg.MsgSendService; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.Map; | |||
| /** | |||
| * | |||
| * @author Stormeye Wu | |||
| * @date 2019/5/10 | |||
| */ | |||
| @Service | |||
| public class FmInsideNotifyRefundSuccessMsgServiceImpl implements MsgSendService { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| private WxRefundOrderService wxRefundOrderService; | |||
| @Override | |||
| public void send(BaseMsg baseMsg) throws Exception { | |||
| FmInsideNotifyRefundSuccessMsg msg = (FmInsideNotifyRefundSuccessMsg)baseMsg; | |||
| Map<String, String> paramMap = JSON.parseObject(msg.getJsonMsg(), Map.class); | |||
| wxRefundOrderService.notify(paramMap, EnumPayWay.getEnum(msg.getPayWay()),EnumPayVersion.getEnum(msg.getPayVersion())); | |||
| } | |||
| } | |||
| @@ -49,7 +49,7 @@ public class AliPagePayAdapterService extends BaseAliPayAdapterService implement | |||
| AlipayClient alipayClient = maUtil.getAliPayClient(appInfo, payAccount); | |||
| AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); | |||
| //异步接收地址,仅支持http/https,公网可访问 | |||
| request.setNotifyUrl(payAccount.getNotifyUrl() + "/aliNotify"); | |||
| request.setNotifyUrl(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/aliNotify"); | |||
| //同步跳转地址,仅支持http/https | |||
| // request.setReturnUrl(""); | |||
| /******必传参数******/ | |||
| @@ -238,7 +238,7 @@ public class TtMiniAppPayAdapterService extends BaseTtPayAdapterService implemen | |||
| preOrder.setSubject(appInfo.getName()+"-"+orderPay.getOrderDetail()); | |||
| preOrder.setBody(appInfo.getName()+"-"+orderPay.getOrderDetail()); | |||
| preOrder.setValidTime(60*60); | |||
| // preOrder.setNotifyUrl();//回调地址 | |||
| preOrder.setNotifyUrl(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/ttNotify");//回调地址 | |||
| // preOrder.setThirdpartyId();//第三方服务商ID | |||
| // preOrder.setStoreId(record.getStoreId()); | |||
| CreatePreOrderResult preOrderResult = DouYinPayHelper.createPreOrder(preOrder); | |||
| @@ -77,7 +77,7 @@ public class WxMiniAppPaySFTAdapterService extends BaseWxPaySFTAdapterService i | |||
| } | |||
| req.setAttach(getAttach(record)); | |||
| req.setOut_trade_no(String.valueOf(composeOrder.getMainOrderId())); | |||
| req.setNotify_url(payAccount.getPayNotifyV3Url(appInfo.getProjectType())); | |||
| req.setNotify_url(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/pay/v3"); | |||
| SFTPaySettleReq settle = new SFTPaySettleReq(); | |||
| settle.setProfit_sharing(true); | |||
| @@ -117,7 +117,7 @@ public class WxMiniAppPaySFTAdapterService extends BaseWxPaySFTAdapterService i | |||
| payer.setOpenid(openId); | |||
| req.setCombine_payer_info(payer); | |||
| req.setNotify_url(payAccount.getPayNotifyV3Url(appInfo.getProjectType())); | |||
| req.setNotify_url(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/pay/v3"); | |||
| return req; | |||
| } | |||
| @@ -73,7 +73,7 @@ public class WxMiniAppPayAdapterService extends BaseWxPayV2AdapterService implem | |||
| wxPayOrderP.setTotal_fee(composeOrder.getPayment()); | |||
| wxPayOrderP.setSpbill_create_ip(record.getIp()); // 终端IP | |||
| wxPayOrderP.setGoods_tag(productName); | |||
| wxPayOrderP.setNotify_url(payAccount.getPayNotifyUrl()); | |||
| wxPayOrderP.setNotify_url(payAccount.getNotifyUrl() +"/pay"); | |||
| wxPayOrderP.setTrade_type(WxPay.TradeType.JSAPI.name()); // 终端类型 | |||
| wxPayOrderP.setProduct_id(String.valueOf(composeOrder.getMainOrderId())); // 订单ID | |||
| wxPayOrderP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| @@ -149,7 +149,7 @@ public class WxMiniAppPayAdapterService extends BaseWxPayV2AdapterService implem | |||
| wxPayOrderSP.setTotal_fee(composeOrder.getPayment().toString()); | |||
| wxPayOrderSP.setSpbill_create_ip(record.getIp()); // 终端IP | |||
| wxPayOrderSP.setGoods_tag(productName); // 券ID | |||
| wxPayOrderSP.setNotify_url(payAccount.getPayNotifyUrl()); | |||
| wxPayOrderSP.setNotify_url(payAccount.getNotifyUrl() +"/pay"); | |||
| wxPayOrderSP.setTrade_type(WxPay.TradeType.JSAPI.name()); // 终端类型 | |||
| wxPayOrderSP.setProduct_id(String.valueOf(composeOrder.getMainOrderId())); // | |||
| wxPayOrderSP.setTime_start(Utility.getDataFormatStringYYYYMMDDHHmmss(currentDate)); | |||
| @@ -79,7 +79,7 @@ public class WxMiniAppPayV3AdapterService extends BaseWxPayV3AdapterService impl | |||
| } | |||
| req.setOut_trade_no(record.getPayOrderNo()); | |||
| req.setAttach(getAttach(record)); | |||
| req.setNotify_url(payAccount.getPayNotifyV3Url(appInfo.getProjectType())); | |||
| req.setNotify_url(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/pay/v3"); | |||
| V3PaySettleReq settle = new V3PaySettleReq(); | |||
| settle.setProfit_sharing(true); | |||
| @@ -107,7 +107,7 @@ public class WxMiniAppPayV3AdapterService extends BaseWxPayV3AdapterService impl | |||
| req.setDescription("weixin miniApp product"); | |||
| } | |||
| req.setOut_trade_no(productOrderPay.getOrderNumber()); | |||
| req.setNotify_url(payAccount.getPayNotifyV3Url(appInfo.getProjectType())); | |||
| req.setNotify_url(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/refund/v3"); | |||
| V3PayAmountReq amout = new V3PayAmountReq(); | |||
| amout.setTotal(productOrderPay.getPayAmount()); | |||
| @@ -185,8 +185,7 @@ public class WxMiniAppPayV3AdapterService extends BaseWxPayV3AdapterService impl | |||
| V3CombinePayerReq payer = new V3CombinePayerReq(); | |||
| payer.setSub_openid(openId); | |||
| req.setCombine_payer_info(payer); | |||
| req.setNotify_url(payAccount.getPayNotifyV3Url(appInfo.getProjectType())); | |||
| req.setNotify_url(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/pay/v3"); | |||
| return req; | |||
| } | |||
| @@ -49,7 +49,7 @@ public class WxNativePayV3AdapterService extends BaseWxPayV3AdapterService imple | |||
| req.setDescription("weixin miniApp product"); | |||
| } | |||
| req.setOut_trade_no(productOrderPay.getOrderNumber()); | |||
| req.setNotify_url(payAccount.getPayNotifyV3Url(appInfo.getProjectType())); | |||
| req.setNotify_url(payAccount.getNotifyUrl() + "/" + appInfo.getProjectType() +"/pay/v3"); | |||
| V3PayAmountReq amout = new V3PayAmountReq(); | |||
| amout.setTotal(productOrderPay.getPayAmount()); | |||
| @@ -33,11 +33,5 @@ public interface RefundPayAdapterService { | |||
| public RefundAdapterResult queryRefund(WxAppinfo appInfo,WxPayAccount payAccount, WxRefundOrder record); | |||
| public RefundAdapterResult queryRefund(WxAppinfo appInfo,WxPayAccount payAccount, ProductOrderRefund orderRefund); | |||
| /** | |||
| * 平台方 异步通知 | |||
| * @param paramMap | |||
| * @param payWay | |||
| */ | |||
| public RefundNotifyAdapterResult notify(Map<String, String> paramMap, EnumPayWay payWay); | |||
| } | |||
| @@ -134,66 +134,4 @@ public class AliRefundAdapterService implements RefundPayAdapterService{ | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR); | |||
| } | |||
| @Autowired | |||
| WxAppinfoService wxAppinfoService; | |||
| @Autowired | |||
| WxPayAccountService wxPayAccountService; | |||
| @Autowired | |||
| WxPayOrderMapper wxPayOrderMapper; | |||
| @Autowired | |||
| WxRefundOrderMapper wxRefundOrderMapper; | |||
| private RefundNotifyAdapterResult notifyErrorResult(String msg) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", msg); | |||
| return new RefundNotifyAdapterResult(false,ErrorCode.REFUND_ORDER_ERROR.getCode(),msg,XmlUtil.getRequestXml(resultMap)); | |||
| } | |||
| private RefundNotifyAdapterResult notifySuccessResult(String transactionId,String refundId,WxRefundOrder refundOrder) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "SUCCESS"); | |||
| resultMap.put("return_msg", "OK"); | |||
| return new RefundNotifyAdapterResult(transactionId, refundId,refundOrder,XmlUtil.getRequestXml(resultMap)); | |||
| } | |||
| @Override | |||
| public RefundNotifyAdapterResult notify(Map<String, String> paramMap, EnumPayWay payWay) { | |||
| log.info("TtRefundAdapterService notify{}"+paramMap); | |||
| String tenantId = paramMap.get("tenantId"); | |||
| /** | |||
| * | |||
| */ | |||
| String refundno = paramMap.get("cp_refundno"); | |||
| Long refundOrderId = Long.valueOf(refundno); | |||
| WxRefundOrder refundOrder = wxRefundOrderMapper.selectById(refundOrderId); | |||
| String appId = paramMap.get("appid"); | |||
| WxAppinfo appinfo = wxAppinfoService.getByAppIdFromRedis(appId,tenantId); | |||
| if (appinfo == null) { | |||
| log.error("未找到appid信息:" + appId); | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appinfo.getPayId()); | |||
| if (payAccount == null) { | |||
| log.error("未找到mch_id信息:" + appId); | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_FOUND); | |||
| } | |||
| if (!"SUCCESS".equals(paramMap.get("status"))) { | |||
| log.error("notify refund, wxpay status not success, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单状态码非SUCCESS"); | |||
| } | |||
| // 验证退款金额 | |||
| if (!paramMap.get("refund_amount").equals(refundOrder.getRefundFee().toString())) { | |||
| log.error("notify refund, wxpay check refund_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款总金额不一致"); | |||
| } | |||
| log.info("notify refund, wxpay checksign success, paramMap:{}, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifySuccessResult(refundOrder.getPayOrderNo(), paramMap.get("cp_refundno"), refundOrder); | |||
| } | |||
| } | |||
| @@ -189,65 +189,5 @@ public class TtRefundAdapterService implements RefundPayAdapterService{ | |||
| } | |||
| } | |||
| @Autowired | |||
| WxAppinfoService wxAppinfoService; | |||
| @Autowired | |||
| WxPayAccountService wxPayAccountService; | |||
| @Autowired | |||
| WxPayOrderMapper wxPayOrderMapper; | |||
| @Autowired | |||
| WxRefundOrderMapper wxRefundOrderMapper; | |||
| private RefundNotifyAdapterResult notifyErrorResult(String msg) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", msg); | |||
| return new RefundNotifyAdapterResult(false,ErrorCode.REFUND_ORDER_ERROR.getCode(),msg,XmlUtil.getRequestXml(resultMap)); | |||
| } | |||
| private RefundNotifyAdapterResult notifySuccessResult(String transactionId,String refundId,WxRefundOrder refundOrder) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "SUCCESS"); | |||
| resultMap.put("return_msg", "OK"); | |||
| return new RefundNotifyAdapterResult(transactionId, refundId,refundOrder,XmlUtil.getRequestXml(resultMap)); | |||
| } | |||
| @Override | |||
| public RefundNotifyAdapterResult notify(Map<String, String> paramMap, EnumPayWay payWay) { | |||
| log.info("TtRefundAdapterService notify{}"+paramMap); | |||
| String tenantId = paramMap.get("tenantId"); | |||
| /** | |||
| * | |||
| */ | |||
| String refundno = paramMap.get("cp_refundno"); | |||
| Long refundOrderId = Long.valueOf(refundno); | |||
| WxRefundOrder refundOrder = wxRefundOrderMapper.selectById(refundOrderId); | |||
| String appId = paramMap.get("appid"); | |||
| WxAppinfo appinfo = wxAppinfoService.getByAppIdFromRedis(appId,tenantId); | |||
| if (appinfo == null) { | |||
| log.error("未找到appid信息:" + appId); | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountService.getByIdFromRedis(appinfo.getPayId()); | |||
| if (payAccount == null) { | |||
| log.error("未找到mch_id信息:" + appId); | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_FOUND); | |||
| } | |||
| if (!"SUCCESS".equals(paramMap.get("status"))) { | |||
| log.error("notify refund, wxpay status not success, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单状态码非SUCCESS"); | |||
| } | |||
| // 验证退款金额 | |||
| if (!paramMap.get("refund_amount").equals(refundOrder.getRefundFee().toString())) { | |||
| log.error("notify refund, wxpay check refund_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款总金额不一致"); | |||
| } | |||
| log.info("notify refund, wxpay checksign success, paramMap:{}, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifySuccessResult(refundOrder.getPayOrderNo(), paramMap.get("cp_refundno"), refundOrder); | |||
| } | |||
| } | |||
| @@ -224,7 +224,7 @@ public class WxRefundAdapterService implements RefundPayAdapterService{ | |||
| wxRefundOrderP.setRefund_desc("用户自己退款"); | |||
| } | |||
| } | |||
| wxRefundOrderP.setNotify_url(payAccount.getRefundNotifyUrl()); | |||
| wxRefundOrderP.setNotify_url(payAccount.getNotifyUrl() +"/refund"); | |||
| return wxRefundOrderP; | |||
| } | |||
| @@ -295,7 +295,7 @@ public class WxRefundAdapterService implements RefundPayAdapterService{ | |||
| wxRefundOrderSP.setRefund_desc("用户自己退款"); | |||
| } | |||
| } | |||
| wxRefundOrderSP.setNotify_url(payAccount.getRefundNotifyUrl()); | |||
| wxRefundOrderSP.setNotify_url(payAccount.getNotifyUrl() +"/refund"); | |||
| wxRefundOrderSP.setSign_type("HMAC-SHA256"); | |||
| return wxRefundOrderSP; | |||
| } | |||
| @@ -510,188 +510,4 @@ public class WxRefundAdapterService implements RefundPayAdapterService{ | |||
| return null; | |||
| } | |||
| @Autowired | |||
| WxAppinfoMapper wxAppinfoMapper; | |||
| @Autowired | |||
| WxPayAccountMapper wxPayAccountMapper; | |||
| @Autowired | |||
| WxPayOrderMapper wxPayOrderMapper; | |||
| @Autowired | |||
| WxRefundOrderMapper wxRefundOrderMapper; | |||
| private RefundNotifyAdapterResult notifyErrorResult(String msg) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "FAIL"); | |||
| resultMap.put("return_msg", msg); | |||
| return new RefundNotifyAdapterResult(false,ErrorCode.REFUND_ORDER_ERROR.getCode(),msg,XmlUtil.getRequestXml(resultMap)); | |||
| } | |||
| private RefundNotifyAdapterResult notifySuccessResult(String transactionId,String refundId,WxRefundOrder refundOrder) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("return_code", "SUCCESS"); | |||
| resultMap.put("return_msg", "OK"); | |||
| return new RefundNotifyAdapterResult(transactionId, refundId,refundOrder,XmlUtil.getRequestXml(resultMap)); | |||
| } | |||
| @Override | |||
| public RefundNotifyAdapterResult notify(Map<String, String> paramMap, EnumPayWay payWay) { | |||
| // how to get wechatAppId, wechatMchId, partnerKey | |||
| String appId = paramMap.get("appid"); | |||
| String subAppId = paramMap.get("sub_appid"); | |||
| String mchId = paramMap.get("mch_id"); | |||
| String subMchId = paramMap.get("sub_mch_id"); | |||
| if (StringUtils.isBlank(subAppId) && StringUtils.isBlank(subMchId)) { | |||
| // 普通商户号 | |||
| WxAppinfo appinfo = wxAppinfoMapper.findByAppId(appId); | |||
| if (appinfo == null) { | |||
| log.error("未找到appid信息:" + appId); | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectById(appinfo.getPayId()); | |||
| if (payAccount == null) { | |||
| log.error("未找到mch_id信息:" + mchId); | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_FOUND); | |||
| } | |||
| if (!mchId.equalsIgnoreCase(payAccount.getMchId())) { | |||
| log.error("mch_id不对应:" + mchId + ",account:" + payAccount.getMchId()); | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_EQUAL); | |||
| } | |||
| String partnerKey = payAccount.getApiKey(); | |||
| try { | |||
| if (payWay.getType() == EnumPayWay.EnumPayWayType.WX_MINIPAY) { | |||
| if (!"SUCCESS".equals(paramMap.get("return_code"))) { | |||
| log.warn("notify refund, wxpay status not success, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单状态码非SUCCESS"); | |||
| } | |||
| // 解密req_info | |||
| String req_info = CipherUtil.decryptReqInfo(paramMap.get("req_info"), partnerKey); | |||
| Map<String, String> reqMap = WxPayment.xmlToMap(req_info); | |||
| String refundIdStr = reqMap.get("out_refund_no"); | |||
| Long refundOrderId = Long.valueOf(refundIdStr); | |||
| WxRefundOrder refundOrder = wxRefundOrderMapper.selectById(refundOrderId); | |||
| if (refundOrder == null) { | |||
| log.warn("notify refund, wxpay check pay order not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款订单不存在"); | |||
| } | |||
| // 验证支付金额 | |||
| if (!reqMap.get("total_fee").equals(refundOrder.getTotalFee().toString())) { | |||
| log.warn("notify refund, wxpay check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单总金额不一致"); | |||
| } | |||
| // 验证退款金额 | |||
| if (!reqMap.get("refund_fee").equals(refundOrder.getRefundFee().toString())) { | |||
| log.warn("notify refund, wxpay check refund_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款总金额不一致"); | |||
| } | |||
| String payOrderNo = reqMap.get("out_trade_no"); | |||
| Long payOrderId = Long.valueOf(payOrderNo); | |||
| WxPayOrder payOrder = wxPayOrderMapper.selectById(payOrderId,refundOrder.getTenantId()); | |||
| if (payOrder == null) { | |||
| log.warn("notify refund, wxpay check pay order not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("支付订单不存在"); | |||
| } | |||
| // 验证支付金额 | |||
| if (!reqMap.get("total_fee").equals(payOrder.getPayAmount().toString())) { | |||
| log.warn("notify refund, wxpay check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("支付订单总金额不一致"); | |||
| } | |||
| // 处理支付成功 | |||
| //handleRefundSuccess(refundOrder, reqMap.get("transaction_id"), reqMap.get("refund_id")); | |||
| log.info("notify refund, wxpay checksign success, paramMap:{}, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifySuccessResult(reqMap.get("transaction_id"), reqMap.get("refund_id"), refundOrder); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| log.warn("notify refund paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| log.warn("notify refund, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } else { | |||
| // 服务号回调 | |||
| WxAppinfo appinfo = wxAppinfoMapper.findByAppId(subAppId); | |||
| if (appinfo == null) { | |||
| log.error("未找到appid信息:" + appId); | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectById(appinfo.getPayId()); | |||
| if (payAccount == null) { | |||
| log.error("未找到mch_id信息:" + mchId); | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_FOUND); | |||
| } | |||
| if (!mchId.equalsIgnoreCase(payAccount.getMchId())) { | |||
| log.error("mch_id不对应:" + mchId + ",account:" + payAccount.getMchId()); | |||
| throw new MallinkException(ErrorCode.MCH_INFO_NOT_EQUAL); | |||
| } | |||
| String partnerKey = payAccount.getApiKey(); | |||
| try { | |||
| if (payWay.getType() == EnumPayWay.EnumPayWayType.WX_MINIPAY) { | |||
| if (!"SUCCESS".equals(paramMap.get("return_code"))) { | |||
| log.warn("notify order, wxpay status not success, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单状态码非SUCCESS"); | |||
| } | |||
| // 解密req_info | |||
| String req_info = CipherUtil.decryptReqInfo(paramMap.get("req_info"), partnerKey); | |||
| log.info(req_info); | |||
| Map<String, String> reqMap = WxPayment.xmlToMap(req_info); | |||
| String refundIdStr = reqMap.get("out_refund_no"); | |||
| Long refundOrderId = Long.valueOf(refundIdStr); | |||
| WxRefundOrder refundOrder = wxRefundOrderMapper.selectById(refundOrderId); | |||
| if (refundOrder == null) { | |||
| log.warn("notify refund, wxpay check pay order not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款订单不存在"); | |||
| } | |||
| // 验证支付金额 | |||
| if (!reqMap.get("total_fee").equals(refundOrder.getTotalFee().toString())) { | |||
| log.warn("notify refund, wxpay check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单总金额不一致"); | |||
| } | |||
| // 验证退款金额 | |||
| if (!reqMap.get("refund_fee").equals(refundOrder.getRefundFee().toString())) { | |||
| log.warn("notify refund, wxpay check refund_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款总金额不一致"); | |||
| } | |||
| String payOrderNo = reqMap.get("out_trade_no"); | |||
| if (payOrderNo == null) { | |||
| return notifySuccessResult(reqMap.get("transaction_id"), reqMap.get("refund_id"), null); | |||
| } | |||
| Long payOrderId = Long.valueOf(payOrderNo); | |||
| WxPayOrder payOrder = wxPayOrderMapper.selectById(payOrderId,refundOrder.getTenantId()); | |||
| if (payOrder == null) { | |||
| log.warn("notify refund, wxpay check pay order not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("支付订单不存在"); | |||
| } | |||
| // 验证支付金额 | |||
| if (!reqMap.get("total_fee").equals(payOrder.getPayAmount().toString())) { | |||
| log.warn("notify refund, wxpay check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("支付订单总金额不一致"); | |||
| } | |||
| log.info("notify refund, wxpay checksign success, paramMap:{}, paramMap: " + reqMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifySuccessResult(reqMap.get("transaction_id"), reqMap.get("refund_id"), refundOrder); | |||
| } | |||
| } catch (RuntimeException e) { | |||
| log.warn("notify refund, wepay checksign error, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR); | |||
| } catch (Exception e) { | |||
| log.warn("notify refund, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| return notifyErrorResult("FAILED"); | |||
| } | |||
| } | |||
| @@ -89,7 +89,7 @@ public class WxRefundV3AdapterService implements RefundPayAdapterService{ | |||
| req.setReason("用户自己退款"); | |||
| } | |||
| } | |||
| req.setNotify_url(payAccount.getRefundNotifyV3Url()); | |||
| req.setNotify_url(payAccount.getNotifyUrl() +"/pay"); | |||
| V3PayRefundAmountReq amount = new V3PayRefundAmountReq(); | |||
| amount.setRefund(record.getRefundFee()); | |||
| amount.setTotal(record.getTotalFee()); | |||
| @@ -251,91 +251,4 @@ public class WxRefundV3AdapterService implements RefundPayAdapterService{ | |||
| } | |||
| } | |||
| @Autowired | |||
| WxAppinfoMapper wxAppinfoMapper; | |||
| @Autowired | |||
| WxPayAccountMapper wxPayAccountMapper; | |||
| @Autowired | |||
| WxPayOrderMapper wxPayOrderMapper; | |||
| @Autowired | |||
| WxRefundOrderMapper wxRefundOrderMapper; | |||
| private RefundNotifyAdapterResult notifyErrorResult(String msg) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("code", "FAIL"); | |||
| resultMap.put("message", msg); | |||
| return new RefundNotifyAdapterResult(false,ErrorCode.REFUND_ORDER_ERROR.getCode(),msg,JSON.toJSONString(resultMap)); | |||
| } | |||
| private RefundNotifyAdapterResult notifySuccessResult(String transactionId,String refundId,WxRefundOrder refundOrder) { | |||
| SortedMap resultMap = new TreeMap(); | |||
| resultMap.put("code", "SUCCESS"); | |||
| resultMap.put("message", "OK"); | |||
| return new RefundNotifyAdapterResult(transactionId, refundId,refundOrder,JSON.toJSONString(resultMap)); | |||
| } | |||
| @Override | |||
| public RefundNotifyAdapterResult notify(Map<String, String> paramMap, EnumPayWay payWay) { | |||
| String tenantId = paramMap.get("tenantId"); | |||
| String resource = paramMap.get("resource"); | |||
| JSONObject reObj = JSON.parseObject(resource); | |||
| WxPayService wxPayService = wxPayAccountService.getWxPayService(tenantId); | |||
| try { | |||
| String decryptContent = WxPayV3.decrypt(wxPayService, reObj.getString("ciphertext"), reObj.getString("nonce"), reObj.getString("associated_data")); | |||
| log.info(decryptContent); | |||
| JSONObject decryptObj = JSON.parseObject(decryptContent); | |||
| String spMchId = decryptObj.getString("sp_mchid"); | |||
| String subMchId = decryptObj.getString("sub_mchid"); | |||
| String transactionId = decryptObj.getString("transaction_id"); | |||
| String outTradeNo = decryptObj.getString("out_trade_no"); | |||
| String refundId = decryptObj.getString("refund_id"); | |||
| String outRefundNo = decryptObj.getString("out_refund_no"); | |||
| String refundStatus = decryptObj.getString("refund_status"); | |||
| String successTime = decryptObj.getString("success_time"); | |||
| String userReceivedAccount = decryptObj.getString("user_received_account"); | |||
| JSONObject rObj = decryptObj.getJSONObject("amount"); | |||
| int total = rObj.getIntValue("total"); | |||
| int refund = rObj.getIntValue("refund"); | |||
| int payerTotal = rObj.getIntValue("payer_total"); | |||
| int payerRefund = rObj.getIntValue("payer_refund"); | |||
| Long payOrderId = Long.valueOf(outTradeNo); | |||
| WxPayOrder payOrder = wxPayOrderMapper.selectById(payOrderId,tenantId); | |||
| if (payOrder == null) { | |||
| log.warn("notify refund, wxpay v3 check pay order not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("支付订单不存在"); | |||
| } | |||
| // 验证支付金额 | |||
| if (payerTotal != payOrder.getPayAmount().intValue()) { | |||
| log.warn("notify refund, wxpay v3 check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("支付订单总金额不一致"); | |||
| } | |||
| Long refundOrderId = Long.valueOf(outRefundNo); | |||
| WxRefundOrder refundOrder = wxRefundOrderMapper.selectById(refundOrderId); | |||
| if (refundOrder == null) { | |||
| log.warn("notify refund, wxpay v3 check pay order not exists, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款订单不存在"); | |||
| } | |||
| // 验证支付金额 | |||
| if (payerTotal != refundOrder.getTotalFee().intValue()) { | |||
| log.warn("notify refund, wxpay v3 check total_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("订单总金额不一致"); | |||
| } | |||
| // 验证退款金额 | |||
| if (payerRefund != refundOrder.getRefundFee().intValue()) { | |||
| log.warn("notify refund, wxpay v3 check refund_fee is invalid, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString()); | |||
| return notifyErrorResult("退款总金额不一致"); | |||
| } | |||
| return notifySuccessResult(transactionId, refundId, refundOrder); | |||
| } catch (RuntimeException e) { | |||
| log.warn("notify refund paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| log.warn("notify refund, paramMap: " + paramMap.toString() + ", payWay:" + payWay.toString() + ", e:" + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| @@ -336,49 +336,4 @@ public class MaUtil { | |||
| return alipayClient; | |||
| } | |||
| public static void main(String[] args) { | |||
| AlipayConfig alipayConfig = new AlipayConfig(); | |||
| alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do"); | |||
| alipayConfig.setAppId("2021004122675328"); | |||
| alipayConfig.setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjYOqonp3RZMFCdh+rEdVKzVzEjk1ACXguNX2p6V1wN39hrkU1zR57PKKjU227p5jRy3MzeRupvvvt7aOLZM/9nDWh0OvqU3KBIRfKY1F46vqHE+GM06FWvhCDCSnSz98cCZN5GAWGDuV7zKQC6LekahiQH2HOEK+wJEkU9a/Y1RlZX3kmuCADhkywpLk4iUvQDP9QCgei9Vu4wHibGJZhJtbOgvap3k6nWrogAHZeuJ0O9HU3aizpcJFPn+OffA7U6zaBkP268AzclI1EgkH2lnosRJieLdcRskRB5AI5h8HF+SEzDwtytDwh1y7KBziR1j7H+oScDLcfhnvQwnw9AgMBAAECggEAHmQl4o3273+dj55nj6v2"); | |||
| alipayConfig.setFormat("json"); | |||
| alipayConfig.setCharset("UTF-8"); | |||
| alipayConfig.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxdq6ugtmAv1jGX/bMbxPQRgc5Muz8uzfr5d77yV7Z760NZcETVFZXfX3FMN/DwF/9sL4UZEx5Bd8yg46R8kgVFPKmfJf6kK7I/Cw9DtniZ9X+KSJzZerzkvttFgrKOGb5AhHupswrWmYgSopuPeYixUetuutCT6hFOx9rv9U9xS+eRoSW1JIGxioTXJ+tVS6fXh5lJVrC+Lj+5TMVZIu/WXrY6Ph+IgHMMVtwj7laiuVuefjVl9TbdPH7PRkAmdFcWcNDqPyBRsy5eLhC1LRPkOm2W5ydqVQ9yTI4rMq8wuAueX3Rcpgh/b+xZgJYv+Zlpb9/37TQHVDi5YnOlLlNQIDAQAB"); | |||
| alipayConfig.setSignType("RSA2"); | |||
| try { | |||
| AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig); | |||
| AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); | |||
| //异步接收地址,仅支持http/https,公网可访问 | |||
| request.setNotifyUrl("https://mtest.metavatar.cc/C/payCallBack/aliNotify"); | |||
| //同步跳转地址,仅支持http/https | |||
| // request.setReturnUrl(""); | |||
| /******必传参数******/ | |||
| JSONObject bizContent = new JSONObject(); | |||
| //商户订单号,商家自定义,保持唯一性 | |||
| bizContent.put("out_trade_no", "test00000000000001"); | |||
| //支付金额,最小值0.01元 | |||
| bizContent.put("total_amount", "0.01"); | |||
| //订单标题,不可使用特殊符号 | |||
| bizContent.put("subject", "test"); | |||
| //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY | |||
| bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); | |||
| bizContent.put("qr_pay_mode",1); | |||
| String after15 = DateUtils.date2String(DateUtils.getSecondsTimeAfter(15 * 60, new Date())); | |||
| bizContent.put("time_expire", after15); | |||
| request.setBizContent(bizContent.toString()); | |||
| AlipayTradePagePayResponse response = alipayClient.pageExecute(request); | |||
| System.out.println(response.toString()); | |||
| } catch (AlipayApiException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| } | |||