Source/Mobile/MobilePostpaid.js

"use strict";
/**
 *  @module     MobilePostpaid
 *  @overview   Defines the `MobilePostpaid` class.
 *
 *  @author     Animesh Mishra <hello@animesh.ltd>
 *  @copyright  © Animesh Ltd. All Rights Reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const Request = require("request-promise-native");
const RechargeError = require("../RechargeError");
const Operator_1 = require("../Operator");
const index_1 = require("../../index");
/**
 *  Manages all mobile postpaid bill handling and payments.
 */
class MobilePostpaid {
    constructor(operator, phone, amount) {
        this.operator = operator;
        this.phone = phone;
        this.amount = amount;
    }
    /**
     *  Pays a mobile postpaid bill.
     */
    async Pay(creds, live = false) {
        let options = {
            method: "GET",
            uri: `https://${creds.baseURL}/recharge/mobile/bill`,
            headers: {
                Accept: "application/json"
            },
            qs: {
                client_id: creds.merchantID,
                client_key: creds.merchantKey,
                msisdn: this.phone,
                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 index_1.RechargeBill(this, response);
    }
    //
    //  Static Methods
    //
    /**
     *  Returns a list of all mobile postpaid operators supported by Rocket in Pocket
     *  API.
     */
    static async GetOperators(creds) {
        let options = {
            method: "GET",
            uri: `https://${creds.baseURL}/operators/mobile/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();
        for (var operator of response) {
            operators.push(new Operator_1.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
     */
    static async CheckStatus(creds, vendorID) {
        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)
        };
    }
}
exports.MobilePostpaid = MobilePostpaid;
//# sourceMappingURL=MobilePostpaid.js.map