@magic.batua/recharge
Version:
The Recharge module manages all the mobile prepaid, datacard and DTH recharge functionalities of the Magic Batua platform.
155 lines (135 loc) • 4.71 kB
text/typescript
/**
* @module Fixedline
* @overview Defines the `Fixedline` class.
*
* @author Animesh Mishra <hello@animesh.ltd>
* @copyright © Animesh Ltd. All Rights Reserved.
*/
import * as Request from "request-promise-native"
import * as RechargeError from "./RechargeError"
import { Operator } from "./Operator"
import { RIPCreds } from "./RIPCreds"
import { RechargeBill } from "../index"
import { StatusResponse } from "./StatusResponse"
/** Describes the data required for initiating a Fixedline bill payment */
export interface InitFixedLine {
operator: Operator,
std: string,
phone: string,
accountNumber?: string,
type?: string,
amount: number
}
/**
* Manages all fixedline bill handling and payments.
*/
export class Fixedline {
/** Fixedline telecom operator */
public operator: Operator
/** STD code, without the 0 */
public std: string
/** Phone number without the STD code */
public phone: string
/** Amount in Rupees */
public amount: number
/** Customer account number. Only in case of BSNL/MTNL. */
public accountNumber?: string
/** Only for BSNL connections. LLI for individuals, LLC for businesses. */
public type?: string
public constructor(json: InitFixedLine) {
this.operator = json.operator
this.std = json.std
this.phone = json.phone
this.amount = json.amount
this.type = json.type
this.accountNumber = json.accountNumber
}
/**
* Pays a mobile postpaid bill.
*/
public async Pay(creds: RIPCreds, live: boolean = false): Promise<RechargeBill> {
let options = {
method: "GET",
uri: `https://${creds.baseURL}/recharge/bill`,
headers: {
Accept: "application/json"
},
qs: {
client_id: creds.merchantID,
client_key: creds.merchantKey,
std_code: this.std,
number: `${this.std}${this.phone}`,
cust_no: `${this.phone}`,
cust_acc: this.accountNumber,
bsnl_type: this.type,
operator_code: this.operator.code,
amount: this.amount,
live: live
}
}
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
//
/**
* Returns a list of all mobile postpaid operators supported by Rocket in Pocket
* API.
*/
public static async GetOperators(creds: RIPCreds): Promise<Array<Operator>> {
let options = {
method: "GET",
uri: `https://${creds.baseURL}/operators/bill`,
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
}
/**
* Returns the status of a postpaid bill payment done previously. We'll make use of Rocket in
* Pocket's callback URL mechanism instead, so this will largely acts as a backup.
*
* @param creds Merchant credentials to connect to the Rocket in Pocket API
* @param vendorID ID returned by Rocket in Pocket after successful recharge request
*/
public static async CheckStatus(creds: RIPCreds, vendorID: string): Promise<StatusResponse> {
let options = {
method: "GET",
uri: `https://${creds.baseURL}/recharge/${vendorID}`,
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 }
return {
vendorReference: response.rocket_trans_id,
operatorReference: response.opr_transid,
status: response.status,
date: new Date(response.datetime)
}
}
}