微信公众号支付--3--接收微信支付异步通知

在JSAPI支付官方文档https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6里面,微信团队明确指出,在微信浏览器里面打开H5网页中执行JS调起支付时,代码里面res.err_msg这个值将在用户支付成功后返回ok,但并不保证它绝对可靠,因此,用户支付是否成功不能在这里进行判断,而应该通过接收微信的异步通知来实现。

https://blog.youkuaiyun.com/hjfcgt123/article/details/104244974这篇微博中,我们配置文件中有一个属性名为:wechat.notifyUrl。这个参数的作用是什么呢?在生成预支付订单时,请求参数中有一个属性值叫做notify_url,此属性值必传,微信官方指出该属性为:异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。

当支付成功之后,微信会向该url发起异步通知,返回订单的支付状态。我们需要在这里判断订单是否支付成功,继而做其他业务逻辑操作。

一、官方文档

官方文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8

二、代码实现

1、controller

@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;


    @PostMapping("notify")
    public ModelAndView notify(@RequestBody String notifyDate) {
        //接收微信支付异步通知
        payService.notify(notifyDate);
        //返回给微信处理结果
        return new ModelAndView("pay/success");
    }
   
}

2、service实现类

@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String orderName = "微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderService orderService;


    @Override
    public PayResponse notify(String notifyDate) {
        /**
         * 接收微信支付异步通知
         * 1、验证签名(sdk已经内部实现)
         * 2、订单的支付状态(sdk已经内部实现)
         */
        PayResponse payResponse = bestPayService.asyncNotify(notifyDate);
        log.info("【微信支付】异步通知,payResponse={}", JsonUtils.toJson(payResponse));
        /**
         * 3、校验订单是否存在
         */
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());
        if (ObjectUtils.isEmpty(orderDTO)) {
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        /**
         * 4、校验订单是否已经被支付
         */
        if (orderDTO.getPayStatus().equals(PayStatusEnum.SUCCESS.getCode())) {
            return payResponse;
        }
        /**
         * 5、判断金额是否一致
         */
        if (!MathUtils.equals(payResponse.getOrderAmount(), orderDTO.getOrderAmount().doubleValue())) {
            log.error("【微信支付】异步通知,订单金额不一致,orderId={},微信通知金额={},系统金额={}",
                    payResponse.getOrderId(),
                    payResponse.getOrderAmount(),
                    orderDTO.getOrderAmount());
            throw new SellException(ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }
        /**
         * 5、修改订单的支付状态
         */
        orderService.paid(orderDTO);
        return payResponse;
    }

}

同样的通知微信支付后台可能会多次发送给商户系统。所以我们在接收了微信的异步支付通知,处理自身的业务逻辑之后,需要给微信支付后台返回确认消息,这样微信就不会再重复发送同一个订单的支付结果。

因此在controller中,我们返回了一个ModelAndView,对应的视图为src\main\resources\templates\pay\success.ftl,文件内容如下

<xml>
    <return_code><![CDATA[SUCCESS]]></return_code>
    <return_msg><![CDATA[OK]]></return_msg>
</xml>

 

### 实现Java对接微信公众号JSAPI支付(V2版本) #### Maven依赖 为了实现与微信公众号的JSAPI支付接口对接,首先需要引入必要的Maven依赖: ```xml <dependency> <groupId>com.github.wechatpay-apiv3</groupId> <artifactId>wechatpay-java</artifactId> <version>0.2.7</version> </dependency> ``` 此依赖提供了处理微信支付所需的工具和支持[^2]。 #### 构造统一下单请求参数 在进行支付前,服务器需向微信发起统一订单创建请求。以下是构建该请求所需的主要字段及其说明: | 参数名 | 类型 | 描述 | | --- | --- | --- | | appid | String | 应用ID | | mch_id | String | 商户号 | | nonce_str | String | 随机字符串 | | sign_type | String | 签名类型,默认为MD5 | | body | String | 商品描述 | | out_trade_no | String | 商家订单号 | | total_fee | Integer | 订单总金额(分) | | spbill_create_ip | String | 客户端IP地址 | | notify_url | String | 接收异步通知URL | 这些信息会被封装成`UnifiedOrderRequest`对象并提交给微信支付平台以获取预支付交易会话标识(prepay_id)[^1]。 #### 获取prepay_id后的前端调起支付配置 当成功获得`prepay_id`之后,服务端应返回如下JSON结构供前端页面使用JavaScript API完成实际付款流程: ```json { "appId": "", "timeStamp": "", "nonceStr": "", "package": "prepay_id=", "signType":"", "paySign":"" } ``` 其中各个属性的具体含义已在上述表格中有所提及;而`paySign`则是基于以上五个键值对按照特定规则计算得出的签名结果。 #### Java代码示例 下面给出一段简单的Spring Boot风格的服务方法用于处理整个过程中的核心逻辑部分——即生成下单请求以及解析响应数据提取出`prepay_id`: ```java import com.github.wxpay.sdk.WXPayUtil; // ... other imports ... @Service public class WeChatPaymentService { private static final Logger logger = LoggerFactory.getLogger(WeChatPaymentService.class); @Value("${wechat.app.id}") private String appId; // Other properties like merchant id, key... public Map<String, Object> createPrepay(String orderId, int amountInFen) throws Exception { SortedMap<String, String> params = new TreeMap<>(); // Fill required parameters... params.put("appid", this.appId); params.put("mch_id", this.merchantId); params.put("body", "Test Product"); params.put("out_trade_no", orderId); params.put("total_fee", String.valueOf(amountInFen)); params.put("spbill_create_ip", getLocalIp()); params.put("notify_url", notificationUrl); params.put("trade_type", "JSAPI"); // Add more fields as needed according to your business requirements String xmlString = WXPayUtil.generateSignedXml(params, this.key); HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder"); post.setEntity(new StringEntity(xmlString)); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(responseBody)); org.w3c.dom.Document document = builder.parse(is); NodeList prepayIdNodes = document.getElementsByTagName("prepay_id"); if(prepayIdNodes.getLength()>0){ Element element=(Element)prepayIdNodes.item(0); return buildFrontEndConfig(element.getTextContent()); }else{ throw new RuntimeException("Failed to obtain prepay_id from wechat server."); } } catch (Exception e) { logger.error(e.getMessage(),e); throw e; } } else { throw new IOException("Unexpected status code:" + response.getStatusLine().getStatusCode()); } } private Map<String,Object> buildFrontEndConfig(String prepayId)throws Exception{ long timestamp=System.currentTimeMillis()/1000L;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值