|
| 1 | +import {inject} from '@loopback/core'; |
| 2 | +import {repository, DataObject} from '@loopback/repository'; |
| 3 | +import { |
| 4 | + post, |
| 5 | + Request, |
| 6 | + requestBody, |
| 7 | + response, |
| 8 | + Response, |
| 9 | + RestBindings, |
| 10 | +} from '@loopback/rest'; |
| 11 | +import {v4 as uuidv4} from 'uuid'; |
| 12 | +import {Subscriptions} from '../models'; |
| 13 | +import {GatewayBindings, IGateway} from '../providers'; |
| 14 | +import {SubscriptionsRepository} from '../repositories'; |
| 15 | +import {authenticate, STRATEGY} from 'loopback4-authentication'; |
| 16 | +import {authorize} from 'loopback4-authorization'; |
| 17 | +import {PermissionKey} from '../enums/permission-key.enum'; |
| 18 | +import {STATUS_CODE} from '@sourceloop/core'; |
| 19 | +import {ResponseMessage} from '../enums'; |
| 20 | +const dotenvExt = require('dotenv-extended'); |
| 21 | +const path = require('path'); |
| 22 | +dotenvExt.load({ |
| 23 | + path: path.join(process.env.INIT_CWD, '.env'), |
| 24 | + defaults: path.join(process.env.INIT_CWD, '.env.defaults'), |
| 25 | + errorOnMissing: false, |
| 26 | + includeProcessEnv: true, |
| 27 | +}); |
| 28 | +const redirectStatusCode = 302; |
| 29 | +const permRedirectStatusCode = 302; |
| 30 | + |
| 31 | +export class SubscriptionTransactionsController { |
| 32 | + constructor( |
| 33 | + @inject(RestBindings.Http.RESPONSE) private readonly res: Response, |
| 34 | + @inject(RestBindings.Http.REQUEST) private readonly req: Request, |
| 35 | + @inject(GatewayBindings.GatewayHelper) |
| 36 | + private readonly gatewayHelper: IGateway, |
| 37 | + @repository(SubscriptionsRepository) |
| 38 | + private readonly subscriptionRepository: SubscriptionsRepository, |
| 39 | + ) {} |
| 40 | + |
| 41 | + @authenticate(STRATEGY.BEARER) |
| 42 | + @authorize({permissions: [PermissionKey.CreateSubscription]}) |
| 43 | + @post('/create-subscription-and-pay') |
| 44 | + @response(redirectStatusCode, { |
| 45 | + description: 'Subscription model instance', |
| 46 | + content: { |
| 47 | + 'text/html': {}, |
| 48 | + }, |
| 49 | + }) |
| 50 | + async subscriptionandtransactionscreate( |
| 51 | + @requestBody({ |
| 52 | + content: { |
| 53 | + 'application/json': { |
| 54 | + schema: { |
| 55 | + type: 'object', |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | + subscriptions: Subscriptions, |
| 61 | + ): Promise<void> { |
| 62 | + const subscriptionEntity = { |
| 63 | + id: uuidv4(), |
| 64 | + totalAmount: subscriptions?.totalAmount, |
| 65 | + status: subscriptions?.status, |
| 66 | + metaData: subscriptions?.metaData ?? {}, |
| 67 | + paymentGatewayId: subscriptions?.paymentGatewayId, |
| 68 | + paymentMethod: subscriptions?.paymentmethod, |
| 69 | + currency: subscriptions?.currency, |
| 70 | + startDate: subscriptions.startDate |
| 71 | + ? new Date(subscriptions.startDate) |
| 72 | + : new Date(), |
| 73 | + endDate: subscriptions.endDate |
| 74 | + ? new Date(subscriptions.endDate) |
| 75 | + : new Date(), |
| 76 | + planId: subscriptions?.planId, |
| 77 | + }; |
| 78 | + const newSubscription = await this.subscriptionRepository.create( |
| 79 | + subscriptionEntity, |
| 80 | + ); |
| 81 | + const hostUrl = this.req.get('host'); |
| 82 | + const hostProtocol = this.req.protocol; |
| 83 | + this.req.query.method = newSubscription.paymentMethod; |
| 84 | + return this.res.redirect( |
| 85 | + redirectStatusCode, |
| 86 | + `${hostProtocol}://${hostUrl}/transactions/subscription/${newSubscription.id}?method=${newSubscription.paymentMethod}`, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @post('/subscription/transaction/charge') |
| 91 | + @response(STATUS_CODE.OK, { |
| 92 | + description: 'Subscription model instance', |
| 93 | + content: { |
| 94 | + 'application/json': {}, |
| 95 | + }, |
| 96 | + }) |
| 97 | + async subscriptionTransactionscharge( |
| 98 | + @requestBody({ |
| 99 | + content: { |
| 100 | + 'application/x-www-form-urlencoded': { |
| 101 | + schema: { |
| 102 | + type: 'object', |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | + chargeResponse: DataObject<{}>, |
| 108 | + ): Promise<unknown> { |
| 109 | + chargeResponse = { |
| 110 | + ...chargeResponse, |
| 111 | + subscriptionId: this.req.query.subscriptionId, |
| 112 | + }; |
| 113 | + const chargeHelper = await this.gatewayHelper.subscriptionCharge( |
| 114 | + chargeResponse, |
| 115 | + ); |
| 116 | + const hostUrl = this.req.get('host'); |
| 117 | + const hostProtocol = this.req.protocol; |
| 118 | + const defaultUrl = `${hostProtocol}://${hostUrl}`; |
| 119 | + if (chargeHelper?.res === ResponseMessage.Sucess) { |
| 120 | + return this.res.redirect( |
| 121 | + permRedirectStatusCode, |
| 122 | + `${process.env.SUCCESS_CALLBACK_URL ?? defaultUrl}`, |
| 123 | + ); |
| 124 | + } else { |
| 125 | + return this.res.redirect( |
| 126 | + permRedirectStatusCode, |
| 127 | + `${process.env.FAILURE_CALLBACK_URL ?? defaultUrl}`, |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments