wxpay.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. async createNative(data) {
  39. const { money, order_no, desc, notice_url } = data;
  40. const wxOrderData = { config: this.appConfig, money, order_no, desc, notice_url };
  41. const url = `${this.wxDomain}/pay/payNative`;
  42. const res = await this.httpUtil.cpost(url, wxOrderData);
  43. if (res) return res;
  44. throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信二维码支付下单失败!');
  45. }
  46. /**
  47. * 关闭订单
  48. * @param {String} order_no 订单号
  49. */
  50. async close(order_no) {
  51. assert(order_no, '缺少订单号,无法查询订单信息');
  52. const params = { config: this.appConfig, order_no };
  53. const url = `${this.wxDomain}/pay/closeOrder`;
  54. const res = await this.httpUtil.cpost(url, params);
  55. return res || 'ok';
  56. }
  57. /**
  58. * 退款
  59. * @param {String} order_no 订单号
  60. * @param {String} reason 原因
  61. */
  62. async refund(order_no, reason) {
  63. assert(order_no, '缺少订单号,无法查询订单信息');
  64. const url = `${this.wxDomain}/pay/refundOrder`;
  65. const params = { config: this.appConfig, order_no, reason };
  66. const wxRefundReq = await this.httpUtil.cpost(url, params);
  67. return wxRefundReq || 'ok';
  68. }
  69. }
  70. module.exports = WxpayService;