您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

实例详解node.js实现微信支付退款功能

2024/5/14 15:13:58发布26次查看
微信开发越来越流行,我们知道在微信中有付款也有退款功能,这样的功能非常常见,这篇文章主要介绍了node.js实现微信支付退款功能,需要的朋友可以参考下,希望能帮助到大家。
缘起
有付款就会有退款
注意,退款支持部分退款
左口袋的钱退到右口袋罗
这次发起的退款请求0.01元是实时到账的,因此,用户在小程序端发起的退款只是一个请求到后台,后台审核人员审核无误后才后微信发起退款操作。
引入第三方module
在package.json 中加入weixin-pay: ^1.1.7这一条
代码目录结构
入参
{ transaction_id: '4200000005201712165508745023', // 交易  out_trade_no: '5b97cba0ae164bd58dfe9e77891d3aaf', // 自己这头的交易号  out_refund_no: '6f3240c353934105be34eb9f2d364cec', // 退款订单,自己生成  total_fee: 1, // 退款总额  nonce_str: '1xszw0op0kcdkomyxnyxheuf1faqefhu', // 随机串  appid: 'wxff154ce14ad59a55', // 小程序 appid  mch_id: '1447716902', // 微信支付商户id  sign: '416fcb62f9b8f03c82e83052cc77524b' // 签名,weixin-pay这个module帮助生成 }
然后由wxpay为我们生成其余字段,比如nonce_str,sign,当然还少不了p12证书,
这个早选在wxpay初始代码里已经配置了,pfx: fs.readfilesync(__dirname + '/../../../cert/apiclient_cert.p12'), //微信商户平台证书
lib/wechat/utils/wxpay.js的源码
const wxpay = require('weixin-pay'); // 引入weixin-pay这个第三方模块 const {weapp} = require('../../../utils/config'); // 我自己的全局配置文件,包括了appid key 等 const fs = require('fs'); const wxpay = wxpay({  appid: weapp.appid,  mch_id: weapp.mchid,  partner_key: weapp.key, //微信商户平台 api secret,非小程序 secret  pfx: fs.readfilesync(__dirname + '/../../../cert/apiclient_cert.p12'),  }); module.exports = wxpay;
另外还有一个util.js工具类
用于验证与错误回调
const wxpay = require('./wxpay'); const validatesign = results => {  const sign = wxpay.sign(results);  if (sign !== results.sign) {  const error = new error('微信返回参数签名结果不正确');  error.code = 'invalid_result_sign';  throw error;  };  return results; }; const handleerror = results => {  if (results.return_code === 'fail') {  throw new error(results.return_msg);  }  if (results.result_code !== 'success') {  const error = new error(results.err_code_des);  error.code = results.err_code;  throw error;  }  return results; }; module.exports = {  validatesign,  handleerror, };
发起退款请求
退款逻辑是这样的,先从自己这边的order数据表中查出transaction_id/out_trade_no/total_fee,再拼上自己生成的out_refund_no退款单号,本次退款部分金额refund_fee,最后由weixin-pay这个模块下的wxpay.refund调起就可以了,成功就把订单状态改成退款成功
// 退款 router.post('/refund', function(req, res) {  order.findbyid(req.body._id, (err, order) => {   if (err) {    console.log(err);   }   console.log(order);   // 生成微信设定的订单格式   var data = {    transaction_id: order.transactionid,    out_trade_no: order.tradeid,    out_refund_no: uuid().replace(/-/g, ''),    total_fee: order.amount,    refund_fee: order.amount   };   console.log(data);   // 先查询订单,再退订单   wxpay.refund(data, (err, result) => {    if (err) {     console.log(err);     res.send(      utils.json({       code: 500,       msg: '退款失败'      })     );    }    // 返回退款请求成功后,要将订单状态改成refunded    if (result.result_code === 'success') {     console.log(result);     order.status = 'refunded';     order.save((err, response) => {      res.send(       utils.json({        msg: '退款成功'       })      );     });    } else {     res.send(      utils.json({       code: 500,       msg: result.err_code_des      })     );    }   });  }); });
入参的坑
1.这次遇到的坑是refund_fee忘记传值,也就是说微信退款是支持部分退款的,如果是全额退款,那么将它赋值为total_fee相同
2.网上说的op_user_id: weapp.mchid这个参数是非必选的
3.transaction_id 与 out_trade_no 二选一即可,这样在没有记录transaction_id的情况(比如没有写支付成功的callback)下,也能发起退款;其中优先级前者大于后者,在我在分别前其一故意给错的过程中得到了验证。
4.报了一个appid与商户号不匹配的报错,return_code: 'fail', return_msg: '商户号mch_id与appid不匹配'原来是小程序还没绑定公众号微信支付,这真是一个乌龙。
成功退款微信返回的数据
 appid:wxff154ce14ad59a55  cash_fee:1  cash_refund_fee:1  coupon_refund_count:0  coupon_refund_fee:0  mch_id:1447716902  nonce_str:c44wovb6a4bqjfrk  out_refund_no:9ace1466432a4d548065dc8df95d904a  out_trade_no:5b97cba0ae164bd58dfe9e77891d3aaf  refund_channel:  refund_fee:1  refund_id:50000705182017121702756172970  result_code:success  return_code:success  return_msg:ok  sign:5c2e67b3250054e8a665bf1ae2e9bda3  total_fee:1  transaction_id:”4200000005201712165508745023”
重复退款将返回如下
 appid:wxff154ce14ad59a55  err_code:error  err_code_des:订单已全额退款  mch_id:1447716902  nonce_str:kp1ywlu7a5vizegk  result_code:fail  return_code:success  return_msg:ok  sign:”c2a7ded787bea644c325e37d96e9f41c”
最后
如果没有写退款功能或者不想写退款功能怎么办,其实可以从微信支付的后台pay.weixin.qq.com,也是能退款出去的,只是不想忘记了要人工将订单状态置为退款状态。
相关推荐:
php如何实现微信小程序支付及退款
php后端银联支付及退款实例
关于退款的10篇文章推荐
以上就是实例详解node.js实现微信支付退款功能的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product