RefundOrder.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const API = require('../../lib/API')
  2. const db = require('../../plugin/DataBase/db')
  3. const AccessControl = require('../../lib/AccessControl')
  4. const { BaseStdResponse } = require('../../BaseStdResponse')
  5. const { executeOrderRefund } = require('../../lib/OrderRefundService')
  6. class RefundOrder extends API {
  7. constructor() {
  8. super()
  9. this.setPath('/Order/RefundOrder')
  10. this.setMethod('POST')
  11. }
  12. async onRequest(req, res) {
  13. const { uuid, session, orderId } = req.body
  14. if ([uuid, session, orderId].some(v => v === '' || v === null || v === undefined)) {
  15. return res.json({
  16. ...BaseStdResponse.MISSING_PARAMETER
  17. })
  18. }
  19. if (!await AccessControl.checkSession(uuid, session)) {
  20. return res.status(401).json({
  21. ...BaseStdResponse.ACCESS_DENIED
  22. })
  23. }
  24. try {
  25. const rows = await db.query(
  26. 'SELECT orderId FROM orders WHERE orderId = ? AND create_user = ? LIMIT 1',
  27. [orderId, uuid]
  28. )
  29. if (!rows || rows.length !== 1) {
  30. return res.json({
  31. ...BaseStdResponse.ERR,
  32. msg: '订单不存在'
  33. })
  34. }
  35. const result = await executeOrderRefund({
  36. orderId,
  37. operatorUuid: uuid,
  38. skipTimeLimit: false,
  39. logger: this.logger
  40. })
  41. if (!result.ok) {
  42. return res.json({
  43. ...BaseStdResponse.ERR,
  44. msg: result.msg
  45. })
  46. }
  47. this.logger.info(`用户申请退款成功,订单号:${orderId}`)
  48. return res.json({
  49. ...BaseStdResponse.OK,
  50. msg: result.msg
  51. })
  52. } catch (error) {
  53. this.logger.error(`用户退款接口异常 ${orderId}: ${error.stack || error}`)
  54. return res.json({
  55. ...BaseStdResponse.ERR,
  56. msg: '退款失败,请稍后再试'
  57. })
  58. }
  59. }
  60. }
  61. module.exports.RefundOrder = RefundOrder