All files / platform/modules/payment/src/Controllers/Http SystemPaymentController.js

100% Statements 18/18
100% Branches 0/0
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140    1x 1x 1x       15x       15x 15x                                                                                     8x                 7x   7x                   7x 1x 1x                                                                                             7x 7x 7x 7x         3x       1x  
'use strict';
 
const Config = use('Config');
const configs = Config.get('modules.payment.general');
const namespace = configs.namespace;
 
class SystemPaymentController {
  static get inject() {
    return [`${namespace}/Services/PaymentService`, `${namespace}/Services/SystemService`];
  }
 
  constructor(paymentService, systemService) {
    this.paymentService = paymentService;
    this.systemService = systemService;
  }
 
  /**
   * @swagger
   *
   * /api/v1/apps/{appId}/payments:
   *   post:
   *     tags:
   *       - payments
   *     description: create payment from target system
   *     operationId: 'createPaymentBySystem'
   *     summary: 'createPaymentBySystem'
   *     parameters:
   *       - name: appId
   *         in: path
   *         required: true
   *         schema:
   *           type: string
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/SystemPayment'
   *     responses:
   *       200:
   *         description: |-
   *           payment code
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/CreatePaymentBySystemResponse'
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       422:
   *         $ref: '#/components/responses/BadData'
   *     deprecated: false
   *     security:
   *      - BasicAuth: []
   */
  async create({ request, params, response, transform }) {
    const system = await this.systemService.findOrFail(params.appId);
    const {
      // sourceId,
      bookingId,
      amount,
      paymentMethodCode,
      currencyCode,
      paymentStatusCode,
      originalAmount,
    } = request.all();
 
    const payload = {
      // sourceId,
      bookingId,
      amount,
      systemId: system.id,
      paymentMethodCode,
      currencyCode,
      paymentStatusCode,
      metadata: JSON.stringify({ originalAmount }),
    };
    const payment = await this.paymentService.createPaymentForSystem(payload);
    const data = await transform.item(payment, 'PaymentTransformer');
    return response.success(data);
  }
 
  /**
   * @swagger
   *
   * /api/v1/apps/{appId}/payments/{code}:
   *   put:
   *     tags:
   *       - payments
   *     description: update payment status
   *     parameters:
   *       - name: appId
   *         in: path
   *         required: true
   *         schema:
   *           type: string
   *       - name: code
   *         in: path
   *         required: true
   *         schema:
   *           type: string
   *     requestBody:
   *       content:
   *         application/json:
   *           schema:
   *             $ref: '#/components/schemas/UpdateFromSystem'
   *     responses:
   *       200:
   *         description: |-
   *           A data object contain payment info
   *         content:
   *           application/json; charset=utf-8:
   *             schema:
   *               $ref: '#/components/schemas/ApiResponse'
   *       401:
   *         $ref: '#/components/responses/Unauthorized'
   *       403:
   *         $ref: '#/components/responses/Forbidden'
   *       422:
   *         $ref: '#/components/responses/BadData'
   *     deprecated: false
   *     security:
   *      - BasicAuth: []
   */
 
  async update({ request, params, response, transform }) {
    const system = await this.systemService.findOrFail(params.appId);
    const paymentCode = params.code;
    const updatedData = request.only(['paymentStatusCode', 'paymentMethodCode']);
    await this.paymentService.updateFromSystem({
      updatedData,
      paymentCode,
      systemId: system.id,
    });
    return response.success({});
  }
}
 
module.exports = SystemPaymentController;