【愚公系列】2022年10月 微信小程序-电商项目-微信支付小程序确认支付结果功能实现(node)


前言

微信小程序在支付成功后会给注册的接口发消息来通知订单支付成功的状态,下面是微信和接口通信的数据格式:
在这里插入图片描述

微信通知频率为:
15s/15s/30s/3m/10m/20m/30m/30m/30m/60m/3h/3h/3h/6h/6h-总计24h4m。

一、微信支付小程序确认支付结果功能实现(node)

const getRawBody = require( 'raw-body')
// all /apis/pay_notify
// 本地测试:http://localhost:3000/apis/pay_notify?test=true
// 微信支付成功通知接口
defaultRouter.all('/apis/pay_notify', async ctx=>{
    
    
  const testInLocal = !!ctx.request.query.test
  // console.log('testInLocal',testInLocal);
  var rawText = await getRawBody(ctx.req, {
    
    
      encoding: 'utf-8'
  });
  if (testInLocal){
    
    
    rawText = `<xml><appid><![CDATA[小程序appid]]></appid>
    <attach><![CDATA[支付测试]]></attach>
    <bank_type><![CDATA[OTHERS]]></bank_type>
    <cash_fee><![CDATA[1]]></cash_fee>
    <fee_type><![CDATA[CNY]]></fee_type>
    <is_subscribe><![CDATA[Y]]></is_subscribe>
    <mch_id><![CDATA[1410138302]]></mch_id>
    <nonce_str><![CDATA[1eTp670VVN04aRlpGBpHH0fKbEUgqMwK]]></nonce_str>
    <openid><![CDATA[o-hrq0EVYOTJHX9MWqk-LF-_KL0o]]></openid>
    <out_trade_no><![CDATA[2020vEPk8sib229F1rDkRgGhPh]]></out_trade_no>
    <result_code><![CDATA[SUCCESS]]></result_code>
    <return_code><![CDATA[SUCCESS]]></return_code>
    <sign><![CDATA[92AB862CF14B22193DDE9D86DC2D3701]]></sign>
    <time_end><![CDATA[20201109140319]]></time_end>
    <total_fee>1</total_fee>
    <trade_type><![CDATA[JSAPI]]></trade_type>
    <transaction_id><![CDATA[4200000728202011097892062758]]></transaction_id>
    </xml>`
  }
  
  try {
    
    
    var retobj = await wepay.notifyParse(rawText);
    console.log ("payNotify parsed:", retobj);
    /* retobj示例
    {
      appid: 'wxc3db312ddf9bcb01',
      attach: '附加信息',
      bank_type: 'OTHERS',
      cash_fee: '1',
      fee_type: 'CNY',
      is_subscribe: 'Y',
      mch_id: '1410138302',
      nonce_str: '6ma2Wk08YBGkvAaFAtSYP4el6wDBB4hd',
      openid: 'o-hrq0EVYOTJHX9MWqk-LF-_KL0o',
      out_trade_no: '20201aB6PprMLnwu7ev6aBgSZzw',
      result_code: 'SUCCESS',
      return_code: 'SUCCESS',
      sign: 'BDCFDAD06CCF5254C88F29D69B871FAE',
      time_end: '20201031173616',
      total_fee: '1',
      trade_type: 'JSAPI',
      transaction_id: '4200000727202010317871404188'
    }
    // return_code SUCCESS/FAIL此字段是通信标识,非交易标识
    // 业务结果	result_code SUCCESS/FAIL
    */
    // emitter.wechatSendOut({cmd:'payNotify', payload: retobj});
    if (retobj){
    
    
      // 商户单号
      let outTradeNo = retobj.out_trade_no
      let resultCode = retobj.result_code
      let payState = 0
      if (resultCode === 'SUCCESS'){
    
    
        // 支付成功,设置订单状态
        console.log("SUCCESS",resultCode, outTradeNo);
        payState = 1
      }else{
    
    
        payState = 2
      }
      // 存储交易单号备用
      let transactionId = retobj.transaction_id
      //  成功与失败都要同步订单状态
      let res = await Order.update({
        payState,
        transactionId
      },{
        where:{
          outTradeNo
        }
      })
      console.log(`支付状态更新${
    
    res[0] > 0?'成功':'失败'}`)
    }
    var xml = wepay.notifyResult({
    
    return_code: 'SUCCESS', return_msg: 'OK'});
    console.log("payNotify process ok: ", xml);
    ctx.body = xml;
  } catch (e) {
    
    
    console.log ("payNotify error: ", e);
    var xml = wepay.notifyResult({
    
    return_code: 'FAILURE', return_msg: 'FAIL'});
    ctx.body = xml;
  }
})

猜你喜欢

转载自blog.csdn.net/aa2528877987/article/details/127293051
今日推荐