摘要:本篇教程讲解了基于Node.js实现微信支付退款功能的方法,希望阅读本篇文章以后大家有所收获,帮助大家对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 }) ); } }); }); });
成功退款微信返回的数据
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,也是能退款出去的,只是不要忘记了要人工将订单状态置为退款状态。
本文由职坐标整理发布,学习更多的Node.js相关知识,请关注职坐标WEB前端Node.js频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号