6 changed files with 196 additions and 0 deletions
Unified View
Diff Options
-
17dating-agency-mall-event/pom.xml
-
27dating-agency-mall-event/src/main/java/com/qniao/dam/domian/aggregate/paymentchannelorder/event/PaymentChannelOrderPaidEvent.java
-
5dating-agency-mall-server/pom.xml
-
34dating-agency-mall-server/src/main/java/com/qniao/dam/api/command/notify/user/NotifyUserCommandController.java
-
104dating-agency-mall-server/src/main/java/com/qniao/dam/application/service/notify/NotifyApplicationService.java
-
9dating-agency-mall-server/src/main/java/com/qniao/dam/domain/aggregate/paymentchannelorder/PaymentChannelOrderAggregate.java
@ -0,0 +1,27 @@ |
|||||
|
package com.qniao.dam.domian.aggregate.paymentchannelorder.event; |
||||
|
|
||||
|
import com.qniao.dam.domian.aggregate.paymentorder.constant.PaymentMethodEnum; |
||||
|
import com.qniao.domain.BaseDomainEvent; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
|
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class PaymentChannelOrderPaidEvent extends BaseDomainEvent { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
private Long id; |
||||
|
|
||||
|
private Long txnOrderId; |
||||
|
|
||||
|
private PaymentMethodEnum paymentMethod; |
||||
|
|
||||
|
private LocalDateTime paidTime; |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
package com.qniao.dam.api.command.notify.user; |
||||
|
|
||||
|
import cn.hutool.core.util.StrUtil; |
||||
|
import com.qniao.dam.application.service.notify.NotifyApplicationService; |
||||
|
import com.qniao.framework.annotation.IgnoreResponseAdvice; |
||||
|
import com.qniao.framework.exception.BizException; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import org.apache.commons.io.IOUtils; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
@Api(tags = "应用支付通知") |
||||
|
@RestController |
||||
|
@RequestMapping("user") |
||||
|
public class NotifyUserCommandController { |
||||
|
|
||||
|
@Resource |
||||
|
private NotifyApplicationService notifyApplicationService; |
||||
|
|
||||
|
@RequestMapping("/notify/wx") |
||||
|
@IgnoreResponseAdvice |
||||
|
public String wx(HttpServletRequest request) throws IOException { |
||||
|
String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding()); |
||||
|
if (StrUtil.isBlank(xmlResult)) { |
||||
|
throw new BizException("回调内容不能为空"); |
||||
|
} |
||||
|
return notifyApplicationService.wx(xmlResult); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,104 @@ |
|||||
|
package com.qniao.dam.application.service.notify; |
||||
|
|
||||
|
import cn.hutool.core.date.DateUtil; |
||||
|
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; |
||||
|
import com.github.binarywang.wxpay.config.WxPayConfig; |
||||
|
import com.github.binarywang.wxpay.exception.WxPayException; |
||||
|
import com.github.binarywang.wxpay.service.WxPayService; |
||||
|
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; |
||||
|
import com.qniao.dam.domain.aggregate.paymentchannelorder.PaymentChannelOrderAggregate; |
||||
|
import com.qniao.dam.domain.aggregate.paymentchannelorder.entity.PaymentChannelOrder; |
||||
|
import com.qniao.dam.domian.aggregate.paymentchannelorder.constant.PaymentChannelOrderStatusEnum; |
||||
|
import com.qniao.dam.infrastructure.persistent.dao.domain.PaymentChannelOrderDao; |
||||
|
import com.qniao.domain.BaseApplicationService; |
||||
|
import com.qniao.domain.BaseDomainEvent; |
||||
|
import com.qniao.framework.exception.BizException; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.util.Assert; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.math.RoundingMode; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class NotifyApplicationService extends BaseApplicationService { |
||||
|
|
||||
|
@Value("${weixin.mchid}") |
||||
|
private String mchId; |
||||
|
@Value("${weixin.mchkey}") |
||||
|
private String mchKey; |
||||
|
private static final String WX_SUCCESS_RTN = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"; |
||||
|
private static final String WX_FAIL_RTN = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>"; |
||||
|
public static final String RETURN_VALUE_SUCCESS = "SUCCESS"; |
||||
|
|
||||
|
@Resource |
||||
|
private PaymentChannelOrderDao paymentChannelOrderDao; |
||||
|
@Resource |
||||
|
private PaymentChannelOrderAggregate paymentChannelOrderAggregate; |
||||
|
|
||||
|
public String wx(String xmlResult) { |
||||
|
try { |
||||
|
WxPayOrderNotifyResult result = parseOrderNotifyResult(xmlResult); |
||||
|
// 查询订单 |
||||
|
PaymentChannelOrder paymentChannelOrder = paymentChannelOrderDao.selectById(result.getOutTradeNo()); |
||||
|
Assert.notNull(paymentChannelOrder, "支付订单不存在"); |
||||
|
// 验证业务数据是否正确 |
||||
|
checkWxPayParams(result, paymentChannelOrder); |
||||
|
// 更新订单信息 |
||||
|
paymentChannelOrder.setPaidTime(DateUtil.parseLocalDateTime(result.getTimeEnd(), "yyyyMMddHHmmss")); |
||||
|
paymentChannelOrder.setExtOrderNo(result.getTransactionId()); |
||||
|
BaseDomainEvent event = paymentChannelOrderAggregate.paid(paymentChannelOrder); |
||||
|
this.sendEvent(event); |
||||
|
return WX_SUCCESS_RTN; |
||||
|
} catch (WxPayException e) { |
||||
|
log.error("微信回调结果异常,异常原因", e); |
||||
|
} |
||||
|
return WX_FAIL_RTN; |
||||
|
} |
||||
|
|
||||
|
private WxPayOrderNotifyResult parseOrderNotifyResult(String xmlResult) throws WxPayException { |
||||
|
WxPayService wxPayService = new WxPayServiceImpl(); |
||||
|
WxPayOrderNotifyResult result = null; |
||||
|
WxPayConfig wxPayConfig = new WxPayConfig(); |
||||
|
wxPayConfig.setMchId(mchId); |
||||
|
wxPayConfig.setMchKey(mchKey); |
||||
|
wxPayService.setConfig(wxPayConfig); |
||||
|
// 解析并校验参数 |
||||
|
try { |
||||
|
result = wxPayService.parseOrderNotifyResult(xmlResult); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 验证微信参数 |
||||
|
*/ |
||||
|
private void checkWxPayParams(WxPayOrderNotifyResult params, PaymentChannelOrder paymentChannelOrder) { |
||||
|
//校验结果是否成功 |
||||
|
if (!RETURN_VALUE_SUCCESS.equalsIgnoreCase(params.getResultCode()) |
||||
|
&& RETURN_VALUE_SUCCESS.equalsIgnoreCase(params.getReturnCode())) { |
||||
|
String err = "returnCode=" + params.getReturnCode() + ",resultCode=" + params.getResultCode() + |
||||
|
",errCode={}" + params.getErrCode() + ",errCodeDes={}" + params.getErrCodeDes(); |
||||
|
log.error(err); |
||||
|
throw new BizException("支付结果为失败"); |
||||
|
} |
||||
|
checkCommonParams(new BigDecimal(params.getTotalFee()), paymentChannelOrder); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 验证订单数据 |
||||
|
*/ |
||||
|
private void checkCommonParams(BigDecimal amount, PaymentChannelOrder paymentChannelOrder) { |
||||
|
if (!PaymentChannelOrderStatusEnum.UNPAID.equals(paymentChannelOrder.getStatus())) { |
||||
|
throw new BizException("支付订单状态不正确"); |
||||
|
} |
||||
|
if (!(paymentChannelOrder.getAmount().multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP).compareTo(amount) == 0)) { |
||||
|
throw new BizException("支付金额错误"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save