wxpay.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. //
  7. class WxpayService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'wxpay');
  10. this.appConfig = 'newCourtApp';
  11. this.httpUtil = this.ctx.service.util.httpUtil;
  12. const { wechat } = this.app.config.httpPrefix;
  13. this.wxDomain = wechat;
  14. }
  15. /**
  16. * 查询订单
  17. * @param {String} order_no 订单号
  18. */
  19. async search(order_no) {
  20. assert(order_no, '缺少订单号,无法查询订单信息');
  21. const params = { config: this.appConfig, order_no };
  22. const url = `${this.wxDomain}/pay/searchOrderByOrderNo`;
  23. const wxOrderReq = await this.httpUtil.cpost(url, params);
  24. return wxOrderReq;
  25. }
  26. /**
  27. * 创建订单,获取微信支付签名
  28. * @param {Object} data 数据
  29. */
  30. async create(data) {
  31. const { money, openid, order_no, desc } = data;
  32. const wxOrderData = { config: this.appConfig, money, openid, order_no, desc };
  33. const url = `${this.wxDomain}/pay/payOrder`;
  34. const res = await this.httpUtil.cpost(url, wxOrderData);
  35. if (res) return res;
  36. throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信下单失败!');
  37. }
  38. /**
  39. * 关闭订单
  40. * @param {String} order_no 订单号
  41. */
  42. async close(order_no) {
  43. assert(order_no, '缺少订单号,无法查询订单信息');
  44. const params = { config: this.appConfig, order_no };
  45. const url = `${this.wxDomain}/pay/closeOrder`;
  46. const res = await this.httpUtil.cpost(url, params);
  47. return res || 'ok';
  48. }
  49. /**
  50. * 退款
  51. * @param {String} order_no 订单号
  52. * @param {String} reason 原因
  53. */
  54. async refund(order_no, reason) {
  55. assert(order_no, '缺少订单号,无法查询订单信息');
  56. const url = `${this.wxDomain}/pay/refundOrder`;
  57. const params = { config: this.appConfig, order_no, reason };
  58. const wxRefundReq = await this.httpUtil.cpost(url, params);
  59. return wxRefundReq || 'ok';
  60. }
  61. }
  62. module.exports = WxpayService;