UNPKG

@magic.batua/recharge

Version:

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

259 lines (238 loc) 10.8 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Source/Mobile/MobilePrepaid.js - Postman Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/ionicons.min.css"> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Datacard.html">Datacard</a><ul class='methods'><li data-type='method'><a href="Datacard.html#.AllOperators">AllOperators</a></li><li data-type='method'><a href="Datacard.html#.CheckStatus">CheckStatus</a></li><li data-type='method'><a href="Datacard.html#Recharge">Recharge</a></li></ul></li><li><a href="DTH.html">DTH</a><ul class='methods'><li data-type='method'><a href="DTH.html#.AllOperators">AllOperators</a></li><li data-type='method'><a href="DTH.html#.CheckStatus">CheckStatus</a></li><li data-type='method'><a href="DTH.html#Recharge">Recharge</a></li></ul></li><li><a href="Fixedline.html">Fixedline</a><ul class='methods'><li data-type='method'><a href="Fixedline.html#.CheckStatus">CheckStatus</a></li><li data-type='method'><a href="Fixedline.html#.GetOperators">GetOperators</a></li><li data-type='method'><a href="Fixedline.html#Pay">Pay</a></li></ul></li><li><a href="MobilePlan.html">MobilePlan</a></li><li><a href="MobilePostpaid.html">MobilePostpaid</a><ul class='methods'><li data-type='method'><a href="MobilePostpaid.html#.CheckStatus">CheckStatus</a></li><li data-type='method'><a href="MobilePostpaid.html#.GetOperators">GetOperators</a></li><li data-type='method'><a href="MobilePostpaid.html#Pay">Pay</a></li></ul></li><li><a href="MobilePrepaid.html">MobilePrepaid</a><ul class='methods'><li data-type='method'><a href="MobilePrepaid.html#.AllOperators">AllOperators</a></li><li data-type='method'><a href="MobilePrepaid.html#.CheckStatus">CheckStatus</a></li><li data-type='method'><a href="MobilePrepaid.html#.FindOperator">FindOperator</a></li><li data-type='method'><a href="MobilePrepaid.html#.GetPlans">GetPlans</a></li><li data-type='method'><a href="MobilePrepaid.html#Recharge">Recharge</a></li></ul></li><li><a href="Operator.html">Operator</a><ul class='methods'><li data-type='method'><a href="Operator.html#.InitList">InitList</a></li><li data-type='method'><a href="Operator.html#.WithCode">WithCode</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-RechargeError.html">RechargeError</a><ul class='methods'><li data-type='method'><a href="module-RechargeError.html#~Check">Check</a></li></ul></li><li><a href="module-Telecom.html">Telecom</a><ul class='methods'><li data-type='method'><a href="module-Telecom.html#~AllCircles">AllCircles</a></li><li data-type='method'><a href="module-Telecom.html#~CircleWithCode">CircleWithCode</a></li></ul></li></ul> </nav> <div id="main"> <h1 class="page-title">Source/Mobile/MobilePrepaid.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>"use strict"; /** * @module MobilePrepaid * @overview Defines the `MobilePrepaid` class. * * @author Animesh Mishra &lt;hello@animesh.ltd> * @copyright © Animesh Ltd. All Rights Reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); const Request = require("request-promise-native"); const Telecom = require("./TelecomCircle"); const RechargeError = require("../RechargeError"); const Operator_1 = require("../Operator"); const MobilePlan_1 = require("./MobilePlan"); const index_1 = require("../../index"); const error_1 = require("@magic.batua/error"); /** * Encapsulates all the mobile rechage functionalities. */ class MobilePrepaid { constructor(json, plan) { this.operator = json.operator; this.circle = json.circle; this.plan = plan; this.phone = json.phone; } /** * Carries out the mobile 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/mobile`, headers: { Accept: "application/json" }, qs: { client_id: creds.merchantID, client_key: creds.merchantKey, msisdn: this.phone, operator_code: this.operator.code, amount: this.plan.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 mobile 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/mobile`, 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 mobileOperators = Array(); for (var operator of response) { mobileOperators.push(new Operator_1.Operator(operator)); } return mobileOperators; } /** * Fetches operator and telecom circle info for the given phone number. * * @param phone Phone number to be queried * @param creds Merchant credentials to access Rocket in Pocket API * * @returns Operator and circle codes for the given mobile number */ static async FindOperator(phone, creds) { // Get operator for the given phone number let options = { method: "GET", uri: `https://${creds.baseURL}/operators/mobile/+91${phone}`, 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 response[0]; } /** * Returns plans for the given phone number. To do this, it first looks up the * operator and circle information for the user account. Then it calls Rocket in * Pocket's fetch plan API. * * @param phone Phone number to be recharged * @param creds Merchant credentials for accessing Rocket in Pocket API */ static async GetPlans(phone, creds) { // Get all mobile recharge operators let allOperators = await MobilePrepaid.AllOperators(creds); // Get operator and circle info for user's phone number let accountInfo = await MobilePrepaid.FindOperator(phone, creds); // Get circle let circle = Telecom.CircleWithCode(accountInfo.operator_circle.circle_code); // Get operator let operator = Operator_1.Operator.WithCode(accountInfo.operator_code, allOperators); if (circle == null || operator == null) { throw new error_1.ExternalError("Couldn't fetch plan info automatically.", "Rocket in Pocket", "Ask the user for operator/circle info instead."); } // Get plans let options = { method: "GET", uri: `https://${creds.baseURL}/operators/${operator.code}/plans`, headers: { Accept: "application/json" }, qs: { client_id: creds.merchantID, client_key: creds.merchantKey, circle: circle.code } }; let response = await Request(options); response = JSON.parse(response); let error = RechargeError.Check(response); if (error) { throw error; } return { operator: operator, circle: circle, phone: phone, plans: MobilePlan_1.MobilePlan.InitFromList(response) }; } } exports.MobilePrepaid = MobilePrepaid; //# sourceMappingURL=MobilePrepaid.js.map</code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated at Tue Mar 20 2018 03:11:49 GMT+0530 (IST) </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>