|
| 1 | +import { APPENV, phoneNumber } from '@flexpay/common'; |
| 2 | +import { HttpStatus, Injectable, Logger } from '@nestjs/common'; |
| 3 | +import { |
| 4 | + CheckoutResponse, |
| 5 | + MnoCheckout, |
| 6 | + ErrorResponse, |
| 7 | +} from 'azampay/lib/shared/interfaces/base.interface'; |
| 8 | +import crypto from 'crypto'; |
| 9 | + |
| 10 | +// Types |
| 11 | +type SelcomPayload = Record<string, string>; |
| 12 | +type Headers = Record<string, string>; |
| 13 | + |
| 14 | +@Injectable() |
| 15 | +export class SelcomService { |
| 16 | + /** |
| 17 | + * Compute HS256 Signature |
| 18 | + * @param params - Payload parameters |
| 19 | + * @param signedFields - Comma-separated keys to be signed |
| 20 | + * @param timestamp - ISO timestamp |
| 21 | + * @param apiSecret - Your API secret |
| 22 | + * @returns Base64-encoded signature string |
| 23 | + */ |
| 24 | + computeSignature = ( |
| 25 | + params: SelcomPayload, |
| 26 | + signedFields: string, |
| 27 | + timestamp: string, |
| 28 | + apiSecret: string, |
| 29 | + ): string => { |
| 30 | + const fields = signedFields.split(','); |
| 31 | + const signingString = [ |
| 32 | + `timestamp=${timestamp}`, |
| 33 | + ...fields.map((field) => `${field}=${params[field]}`), |
| 34 | + ].join('&'); |
| 35 | + |
| 36 | + return crypto |
| 37 | + .createHmac('sha256', apiSecret) |
| 38 | + .update(signingString) |
| 39 | + .digest('base64'); |
| 40 | + }; |
| 41 | + |
| 42 | + /** |
| 43 | + * Send POST request to SELCOM API |
| 44 | + * @param url - API endpoint |
| 45 | + * @param payload - Payload to send |
| 46 | + * @param headers - Authorization headers |
| 47 | + * @returns JSON response |
| 48 | + */ |
| 49 | + sendSelcomRequest = async ( |
| 50 | + url: string, |
| 51 | + payload: SelcomPayload, |
| 52 | + headers: Headers, |
| 53 | + ): Promise<any> => { |
| 54 | + const response = await fetch(url, { |
| 55 | + method: 'POST', |
| 56 | + headers, |
| 57 | + body: JSON.stringify(payload), |
| 58 | + }); |
| 59 | + |
| 60 | + if (!response.ok) { |
| 61 | + const text = await response.text(); |
| 62 | + Logger.debug(`SELCOM Error: ${text}`, 'SELCOM'); |
| 63 | + return { text }; |
| 64 | + } |
| 65 | + return response.json(); |
| 66 | + }; |
| 67 | + |
| 68 | + selcomPush = async ( |
| 69 | + request: MnoCheckout, |
| 70 | + ): Promise<CheckoutResponse | ErrorResponse> => { |
| 71 | + const { valid, value, withCode } = phoneNumber(request.accountNumber); |
| 72 | + if (!valid) { |
| 73 | + return { |
| 74 | + statusCode: HttpStatus.BAD_REQUEST, |
| 75 | + message: 'Invalid account number', |
| 76 | + success: false, |
| 77 | + code: 'FAIL', |
| 78 | + } as unknown as CheckoutResponse | ErrorResponse; |
| 79 | + } |
| 80 | + // ======= Config & Execution ======= // |
| 81 | + const apiKey = APPENV.SELCOM_APIKEY; |
| 82 | + const apiSecret = APPENV.SELCOM_APISECRET; |
| 83 | + |
| 84 | + const url = `${APPENV.SELCOM_APIURL}/wallet-payment`; |
| 85 | + |
| 86 | + const payload: SelcomPayload = { |
| 87 | + utilityref: value, |
| 88 | + transid: request.externalId, |
| 89 | + amount: request.amount, |
| 90 | + vendor: APPENV.SELCOM_VENDOR, |
| 91 | + msisdn: withCode, |
| 92 | + }; |
| 93 | + |
| 94 | + const authorization = Buffer.from(apiKey).toString('hex'); |
| 95 | + const timestamp = new Date().toISOString(); |
| 96 | + const signedFields = Object.keys(payload).join(','); |
| 97 | + const digest = this.computeSignature( |
| 98 | + payload, |
| 99 | + signedFields, |
| 100 | + timestamp, |
| 101 | + apiSecret, |
| 102 | + ); |
| 103 | + |
| 104 | + const headers: Headers = { |
| 105 | + 'Content-Type': 'application/json;charset=utf-8', |
| 106 | + Accept: 'application/json', |
| 107 | + 'Cache-Control': 'no-cache', |
| 108 | + 'Digest-Method': 'HS256', |
| 109 | + Authorization: `SELCOM ${authorization}`, |
| 110 | + Digest: digest, |
| 111 | + Timestamp: timestamp, |
| 112 | + 'Signed-Fields': signedFields, |
| 113 | + }; |
| 114 | + const result = await this.sendSelcomRequest(url, payload, headers); |
| 115 | + console.log(JSON.stringify(result, null, 2)); |
| 116 | + return result; |
| 117 | + }; |
| 118 | +} |
0 commit comments