'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // class WxpayService extends CrudService { constructor(ctx) { super(ctx, 'wxpay'); this.appConfig = 'newCourtApp'; this.httpUtil = this.ctx.service.util.httpUtil; const { wechat } = this.app.config.httpPrefix; this.wxDomain = wechat; } /** * 查询订单 * @param {String} order_no 订单号 */ async search(order_no) { assert(order_no, '缺少订单号,无法查询订单信息'); const params = { config: this.appConfig, order_no }; const url = `${this.wxDomain}/pay/searchOrderByOrderNo`; const wxOrderReq = await this.httpUtil.cpost(url, params); return wxOrderReq; } /** * 创建订单,获取微信支付签名 * @param {Object} data 数据 */ async create(data) { const { money, openid, order_no, desc } = data; const wxOrderData = { config: this.appConfig, money, openid, order_no, desc }; const url = `${this.wxDomain}/pay/payOrder`; const res = await this.httpUtil.cpost(url, wxOrderData); if (res) return res; throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信下单失败!'); } async createNative(data) { const { money, order_no, desc, notice_url } = data; const wxOrderData = { config: this.appConfig, money, order_no, desc, notice_url }; const url = `${this.wxDomain}/pay/payNative`; const res = await this.httpUtil.cpost(url, wxOrderData); if (res) return res; throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信二维码支付下单失败!'); } /** * 关闭订单 * @param {String} order_no 订单号 */ async close(order_no) { assert(order_no, '缺少订单号,无法查询订单信息'); const params = { config: this.appConfig, order_no }; const url = `${this.wxDomain}/pay/closeOrder`; const res = await this.httpUtil.cpost(url, params); return res || 'ok'; } /** * 退款 * @param {String} order_no 订单号 * @param {String} reason 原因 */ async refund(order_no, reason) { assert(order_no, '缺少订单号,无法查询订单信息'); const url = `${this.wxDomain}/pay/refundOrder`; const params = { config: this.appConfig, order_no, reason }; const wxRefundReq = await this.httpUtil.cpost(url, params); return wxRefundReq || 'ok'; } } module.exports = WxpayService;