UNPKG

@magic.batua/recharge

Version:

The Recharge module manages all the mobile prepaid, datacard and DTH recharge functionalities of the Magic Batua platform.

130 lines (116 loc) 4.15 kB
/** * @module Datacard * @overview Defines the `Datacard` class. * * @author Animesh Mishra <hello@animesh.ltd> * @copyright © Animesh Ltd. All Rights Reserved. */ import * as Request from "request-promise-native" import { Operator } from "./Operator" import { RIPCreds } from "./RIPCreds" import { RechargeBill } from "../index" import * as RechargeError from "./RechargeError" import { StatusResponse } from "./StatusResponse" /** * Encapsulates all the DTH rechage functionalities. */ export class Datacard { public operator: Operator public phoneNumber: string public amount: number public constructor(operator: Operator, phoneNumber: string, amount: number) { this.operator = operator this.phoneNumber = phoneNumber this.amount = amount } /** * Carries out the DTH recharge operation as specified. * * @param creds Merchant credentials to access Rocket in Pocket API * @param transactionID Transaction ID set by the Magic Transaction module */ public async Recharge(creds: RIPCreds, transactionID: string, live: boolean = false): Promise<RechargeBill> { let options = { method: "GET", uri: `https://${creds.baseURL}/recharge/data`, headers: { Accept: "application/json" }, qs: { client_id: creds.merchantID, client_key: creds.merchantKey, msisdn: this.phoneNumber, operator_code: this.operator.code, amount: this.amount, live: live, client_order_id: transactionID } } let response = await Request(options) response = JSON.parse(response) let error = RechargeError.Check(response) if(error) { throw error } return new RechargeBill(this, response) } // // Static Methods // /** * Checks status of a previously submitted recharge request. We make use of Rocket in * Pocket callbacks for status updates. So this is largely implemented as a backup in * case RIP callback systems fail. * * @param transactionID Magic Batua transaction ID of the recharge request */ public static async CheckStatus(creds: RIPCreds, transactionID: string): Promise<StatusResponse> { let options = { method: "GET", uri: `https://${creds.baseURL}/recharge/order`, headers: { Accept: "application/json" }, qs: { client_order_id: transactionID } } let response = await Request(options) response = JSON.parse(response) let error = RechargeError.Check(response) if(error) { throw error } return { operatorReference: response.opr_transid, vendorReference: response.rocket_trans_id, status: response.status, date: response.datetime } } /** * Fetches a list of all DTH rechage operators supported by the Rocket in Pocket * API. The result is an array of `Operator` types. * * @param creds Merchant credentials to connect with Rocket in Pocket API * @returns An array of operators */ public static async AllOperators(creds: RIPCreds): Promise<Array<Operator>> { let options = { method: "GET", uri: `https://${creds.baseURL}/operators/data`, headers: { Accept: "application/json" }, qs: { client_id: creds.merchantID, client_key: creds.merchantKey } } let response = await Request(options) response = JSON.parse(response) let error = RechargeError.Check(response) if(error) { throw error } let operators = Array<Operator>() for(var operator of response) { operators.push(new Operator(operator)) } return operators } }