Source/Datacard.js

"use strict";
/**
 *  @module     Datacard
 *  @overview   Defines the `Datacard` 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 Operator_1 = require("./Operator");
const index_1 = require("../index");
const RechargeError = require("./RechargeError");
/**
 *  Encapsulates all the DTH rechage functionalities.
 */
class Datacard {
    constructor(operator, phoneNumber, amount) {
        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
     */
    async Recharge(creds, transactionID, live = false) {
        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 index_1.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
     */
    static async CheckStatus(creds, transactionID) {
        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
     */
    static async AllOperators(creds) {
        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();
        for (var operator of response) {
            operators.push(new Operator_1.Operator(operator));
        }
        return operators;
    }
}
exports.Datacard = Datacard;
//# sourceMappingURL=Datacard.js.map