123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /* eslint-disable camelcase */
- const { appid, mchid, serial_no, key, plat_serial, refund_url, notify_url } =
- require('../config').wechatPay;
- const { host } = require('../config');
- const WxPay = require('wechatpay-node-v3');
- const fs = require('fs');
- const pay = new WxPay({
- appid,
- mchid,
- serial_no,
- key,
- publicKey: fs.readFileSync(`${__dirname}/apiclient_cert.pem`), // 公钥
- privateKey: fs.readFileSync(`${__dirname}/apiclient_key.pem`), // 秘钥
- });
- const model = {
- // 下单
- async placeOrder(obj) {
- const { description, out_trade_no, openid, total, url = '/external/pay/notice' } = obj;
- try {
- const res = await pay.transactions_native({
- appid, // 应用id
- mchid, // 直连商户号
- description, // 商品描述
- out_trade_no, // 商户订单号
- notify_url: `${host}${url}`, // 通知回调地址
- amount: {
- // 订单金额详情
- total, // 总金额
- currency: 'CNY', // 货币类型
- },
- });
- console.log(res);
- return res;
- } catch (error) {
- console.log('下单错误', error);
- console.log(error.response.data);
- return error.response.data;
- }
- },
- // 退款
- async refund(obj) {
- const { out_trade_no, total, reason, refund, out_refund_no, url } = obj;
- try {
- const res = await pay.refunds({
- out_refund_no, // 商户退款号
- reason, // 描述
- out_trade_no, // 商户订单号
- notify_url: `${host}/external/refund/notice`, // 通知回调地址
- amount: {
- // 订单金额详情
- total, // 总金额
- refund, // 金额
- currency: 'CNY', // 退款币种,
- },
- });
- console.log(res);
- return res;
- } catch (error) {
- console.log('下单错误', error);
- console.log(error.response.data);
- return false;
- }
- },
- decodeNotify(obj) {
- const { original_type, algorithm, ciphertext, associated_data, nonce } = obj;
- const res = pay.decipher_gcm(ciphertext, associated_data, nonce);
- },
- };
- module.exports = pay;
|