About node.js implementing the function of WeChat payment refund

不言
Release: 2018-06-27 10:11:45
Original
2883 people have browsed it

This article mainly introduces node.js to implement the refund function of WeChat payment. In WeChat development, there will be a refund if there is a payment. Such a function is very common. Friends who need it can refer to it

Origin

There will be a refund if payment is made

Note that the refund supports partial refund

The money in the left pocket Return to the right pocket Luo

The refund request of 0.01 yuan initiated this time is received in real time. Therefore, the refund initiated by the user on the mini program is only a request. In the background, the refund operation will be initiated on WeChat only after the background reviewer has reviewed everything correctly.

Introduce third-party module

Add "weixin-pay": "^1.1.7" to package.json

Code directory structure

Input parameters

{ 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帮助生成 }
Copy after login

Then wxpay will generate the remaining fields for us, such as nonce_str, sign, and of course the p12 certificate,

This early selection has been configured in the wxpay initial code, pfx: fs.readFileSync(__dirname '/../../../cert/apiclient_cert.p12'), //WeChat merchant platform certificate

lib/wechat/utils/wxpay.js source code

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;
Copy after login

There is also a util.js tool class

for verification and error callback

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, };
Copy after login

Initiated Refund request

The refund logic is as follows. First, find transaction_id/out_trade_no/total_fee from your own Order data table, and then add the out_refund_no refund form you generated. No., the partial amount of this refund is refund_fee, and finally it can be adjusted by wxpay.refund under the weixin-pay module. If successful, the order status will be changed to "Refund Successful"

// 退款 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 }) ); } }); }); });
Copy after login

The pitfalls of entry

1. The pitfall encountered this time is that refund_fee forgets to pass the value, which means that WeChat refund The payment supports partial refund. If it is a full refund, then assign it the same value as total_fee

2. The op_user_id mentioned on the Internet: weapp.MCHID is a non-required parameter

3. Just choose one of transaction_id and out_trade_no, so that a refund can be initiated even if the transaction_id is not recorded (for example, a callback for a successful payment is not written); the priority of the former is greater than that of the latter, so I have to distinguish between the two. The process of intentionally giving errors was verified.

4. An error was reported that the appid does not match the merchant number, return_code: 'FAIL', return_msg: 'The merchant number mch_id does not match the appid' It turns out that the mini program has not been bound to the official account WeChat payment. This is really a mistake.

Data returned by WeChat for successful refund

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”
Copy after login

Repeat refunds will be returned as follows

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”
Copy after login

last

What to do if you don’t have a refund function or don’t want to write a refund function? In fact, you can refund money from the backend of WeChat Pay at pay.weixin.qq.com, but you just don’t want to forget it. You need to manually set the order status to refund status.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use JS to implement the WeChat payment pop-up function

How to implement it in the WeChat applet Custom toast

The above is the detailed content of About node.js implementing the function of WeChat payment refund. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!