UNPKG

@yabetoo/mtn-momo-js

Version:

MTN Mobile Money API Client for NodeJS written in TypeScript

2 lines (1 loc) 15 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("uuid"),require("assert"),require("axios")):"function"==typeof define&&define.amd?define(["exports","uuid","assert","axios"],r):r((e||self).mtnMomoJs={},e.uuid,e.assert,e.axios)}(this,function(e,r,t,n){function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u,o,a,c,s=/*#__PURE__*/i(n);function l(){return l=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},l.apply(this,arguments)}function E(e,r){e.prototype=Object.create(r.prototype),e.prototype.constructor=e,p(e,r)}function f(e){return f=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},f(e)}function p(e,r){return p=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},p(e,r)}function d(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(e){return!1}}function v(e,r,t){return v=d()?Reflect.construct.bind():function(e,r,t){var n=[null];n.push.apply(n,r);var i=new(Function.bind.apply(e,n));return t&&p(i,t.prototype),i},v.apply(null,arguments)}function y(e){var r="function"==typeof Map?new Map:void 0;return y=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,t)}function t(){return v(e,arguments,f(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),p(t,e)},y(e)}function h(e,r){if(null==e)return{};var t,n,i={},u=Object.keys(e);for(n=0;n<u.length;n++)r.indexOf(t=u[n])>=0||(i[t]=e[t]);return i}e.PayerType=void 0,(u=e.PayerType||(e.PayerType={})).MSISDN="MSISDN",u.EMAIL="EMAIL",u.PARTY_CODE="PARTY_CODE",e.Environment=void 0,(o=e.Environment||(e.Environment={})).SANDBOX="sandbox",o.PRODUCTION="production",e.Status=void 0,(a=e.Status||(e.Status={})).SUCCESSFUL="SUCCESSFUL",a.PENDING="PENDING",a.FAILED="FAILED",e.FailureReason=void 0,(c=e.FailureReason||(e.FailureReason={})).PAYEE_NOT_FOUND="PAYEE_NOT_FOUND",c.PAYER_NOT_FOUND="PAYER_NOT_FOUND",c.NOT_ALLOWED="NOT_ALLOWED",c.NOT_ALLOWED_TARGET_ENVIRONMENT="NOT_ALLOWED_TARGET_ENVIRONMENT",c.INVALID_CALLBACK_URL_HOST="INVALID_CALLBACK_URL_HOST",c.INVALID_CURRENCY="INVALID_CURRENCY",c.SERVICE_UNAVAILABLE="SERVICE_UNAVAILABLE",c.INTERNAL_PROCESSING_ERROR="INTERNAL_PROCESSING_ERROR",c.NOT_ENOUGH_FUNDS="NOT_ENOUGH_FUNDS",c.PAYER_LIMIT_REACHED="PAYER_LIMIT_REACHED",c.PAYEE_NOT_ALLOWED_TO_RECEIVE="PAYEE_NOT_ALLOWED_TO_RECEIVE",c.PAYMENT_NOT_APPROVED="PAYMENT_NOT_APPROVED",c.RESOURCE_NOT_FOUND="RESOURCE_NOT_FOUND",c.APPROVAL_REJECTED="APPROVAL_REJECTED",c.EXPIRED="EXPIRED",c.TRANSACTION_CANCELED="TRANSACTION_CANCELED",c.RESOURCE_ALREADY_EXIST="RESOURCE_ALREADY_EXIST";var R=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,t)||this,Object.setPrototypeOf(function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(n),(this instanceof r?this.constructor:void 0).prototype),n}return E(r,e),r}(/*#__PURE__*/y(Error)),_=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="ApprovalRejectedError",r}return E(r,e),r}(R),A=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="ExpiredError",r}return E(r,e),r}(R),N=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="InternalProcessingError",r}return E(r,e),r}(R),O=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="InvalidCallbackUrlHostError",r}return E(r,e),r}(R),m=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="InvalidCurrencyError",r}return E(r,e),r}(R),I=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="NotAllowedTargetEnvironmentError",r}return E(r,e),r}(R),T=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="NotAllowedError",r}return E(r,e),r}(R),b=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="NotEnoughFundsError",r}return E(r,e),r}(R),P=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="PayeeNotFoundError",r}return E(r,e),r}(R),L=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="PayeeNotAllowedToReceiveError",r}return E(r,e),r}(R),D=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="PayerLimitReachedError",r}return E(r,e),r}(R),q=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="PayerNotFoundError",r}return E(r,e),r}(R),S=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="PaymentNotApprovedError",r}return E(r,e),r}(R),C=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="ResourceAlreadyExistError",r}return E(r,e),r}(R),U=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="ResourceNotFoundError",r}return E(r,e),r}(R),F=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="ServiceUnavailableError",r}return E(r,e),r}(R),g=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="TransactionCancelledError",r}return E(r,e),r}(R),w=/*#__PURE__*/function(e){function r(){var r;return(r=e.apply(this,arguments)||this).name="UnspecifiedError",r}return E(r,e),r}(R);function V(e){if(!e.response)return e;var r=e.response.data||{};return k(r.code,r.message)}function k(r,t){return r===e.FailureReason.APPROVAL_REJECTED?new _(t):r===e.FailureReason.EXPIRED?new A(t):r===e.FailureReason.INTERNAL_PROCESSING_ERROR?new N(t):r===e.FailureReason.INVALID_CALLBACK_URL_HOST?new O(t):r===e.FailureReason.INVALID_CURRENCY?new m(t):r===e.FailureReason.NOT_ALLOWED?new T(t):r===e.FailureReason.NOT_ALLOWED_TARGET_ENVIRONMENT?new I(t):r===e.FailureReason.NOT_ENOUGH_FUNDS?new b(t):r===e.FailureReason.PAYEE_NOT_FOUND?new P(t):r===e.FailureReason.PAYEE_NOT_ALLOWED_TO_RECEIVE?new L(t):r===e.FailureReason.PAYER_LIMIT_REACHED?new D(t):r===e.FailureReason.PAYER_NOT_FOUND?new q(t):r===e.FailureReason.PAYMENT_NOT_APPROVED?new S(t):r===e.FailureReason.RESOURCE_ALREADY_EXIST?new C(t):r===e.FailureReason.RESOURCE_NOT_FOUND?new U(t):r===e.FailureReason.SERVICE_UNAVAILABLE?new F(t):r===e.FailureReason.TRANSACTION_CANCELED?new g(t):new w(t)}function Y(e){var r=k(e.reason);return r.transaction=e,r}function x(e){var r=e.primaryKey;t.strictEqual(j(r),!0,"primaryKey is required"),t.strictEqual(B(r),!0,"primaryKey must be a string")}function M(e){return!isNaN(parseInt(e,10))}function j(e){return!!e}function B(e){return"string"==typeof e}function X(e){return/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i.test(e)}var H=["callbackUrl"],G=/*#__PURE__*/function(){function n(e){this.client=e}var i=n.prototype;return i.requestToPay=function(e){var n=this,i=e.callbackUrl,u=h(e,H);return function(e){var r=e||{},n=r.amount,i=r.currency,u=r.payer;return Promise.resolve().then(function(){t.strictEqual(j(n),!0,"amount is required"),t.strictEqual(M(n),!0,"amount must be a number"),t.strictEqual(j(i),!0,"currency is required"),t.strictEqual(j(u),!0,"payer is required"),t.strictEqual(j(u.partyId),!0,"payer.partyId is required"),t.strictEqual(j(u.partyIdType),!0,"payer.partyIdType is required"),t.strictEqual(B(i),!0,"amount must be a string")})}(u).then(function(){var e=r.v4();return n.client.post("/collection/v1_0/requesttopay",u,{headers:l({"X-Reference-Id":e},i?{"X-Callback-Url":i}:{})}).then(function(){return e})})},i.getTransaction=function(r){return this.client.get("/collection/v1_0/requesttopay/"+r).then(function(e){return e.data}).then(function(r){return r.status===e.Status.FAILED?Promise.reject(Y(r)):Promise.resolve(r)})},i.getBalance=function(){return this.client.get("/collection/v1_0/account/balance").then(function(e){return e.data})},i.isPayerActive=function(r,t){return void 0===t&&(t=e.PayerType.MSISDN),this.client.get("/collection/v1_0/accountholder/"+t+"/"+r+"/active").then(function(e){return e.data})},n}(),W=["callbackUrl","referenceId"],K=/*#__PURE__*/function(){function n(e){this.client=e}var i=n.prototype;return i.transfer=function(e){var n=this,i=e.callbackUrl,u=e.referenceId,o=void 0===u?r.v4():u,a=h(e,W);return function(e){var r=e||{},n=r.amount,i=r.currency,u=r.payee,o=r.referenceId;return Promise.resolve().then(function(){t.strictEqual(j(o),!0,"referenceId is required"),t.strictEqual(X(o),!0,"referenceId must be a valid uuid v4"),t.strictEqual(j(n),!0,"amount is required"),t.strictEqual(M(n),!0,"amount must be a number"),t.strictEqual(j(i),!0,"currency is required"),t.strictEqual(j(u),!0,"payee is required"),t.strictEqual(j(u.partyId),!0,"payee.partyId is required"),t.strictEqual(j(u.partyIdType),!0,"payee.partyIdType is required"),t.strictEqual(B(i),!0,"amount must be a string")})}(l({referenceId:o},a)).then(function(){return n.client.post("/disbursement/v1_0/transfer",a,{headers:l({"X-Reference-Id":o},i?{"X-Callback-Url":i}:{})}).then(function(){return o})})},i.getTransaction=function(r){return this.client.get("/disbursement/v1_0/transfer/"+r).then(function(e){return e.data}).then(function(r){return r.status===e.Status.FAILED?Promise.reject(Y(r)):Promise.resolve(r)})},i.getBalance=function(){return this.client.get("/disbursement/v1_0/account/balance").then(function(e){return e.data})},i.isPayerActive=function(r,t){return void 0===t&&(t=e.PayerType.MSISDN),this.client.get("/disbursement/v1_0/accountholder/"+String(t).toLowerCase()+"/"+r+"/active").then(function(e){return e.data}).then(function(e){return!!e.result&&e.result})},n}(),z=["callbackUrl","referenceId"],J=/*#__PURE__*/function(){function n(e){this.client=e}var i=n.prototype;return i.remit=function(e){var n=this,i=e.callbackUrl,u=e.referenceId,o=void 0===u?r.v4():u,a=h(e,z);return function(e){var r=e||{},n=r.amount,i=r.currency,u=r.payee,o=r.referenceId;return Promise.resolve().then(function(){t.strictEqual(j(o),!0,"referenceId is required"),t.strictEqual(X(o),!0,"referenceId must be a valid uuid v4"),t.strictEqual(j(n),!0,"amount is required"),t.strictEqual(M(n),!0,"amount must be a number"),t.strictEqual(j(i),!0,"currency is required"),t.strictEqual(j(u),!0,"payee is required"),t.strictEqual(j(u.partyId),!0,"payee.partyId is required"),t.strictEqual(j(u.partyIdType),!0,"payee.partyIdType is required"),t.strictEqual(B(i),!0,"amount must be a string")})}(l({referenceId:o},a)).then(function(){return n.client.post("/remittance/v1_0/transfer",a,{headers:l({"X-Reference-Id":o},i?{"X-Callback-Url":i}:{})}).then(function(){return o})})},i.getTransaction=function(r){return this.client.get("/remittance/v1_0/transfer/"+r).then(function(e){return e.data}).then(function(r){return r.status===e.Status.FAILED?Promise.reject(Y(r)):Promise.resolve(r)})},i.getBalance=function(){return this.client.get("/remittance/v1_0/account/balance").then(function(e){return e.data})},i.isPayerActive=function(r,t){return void 0===t&&(t=e.PayerType.MSISDN),this.client.get("/remittance/v1_0/accountholder/"+String(t).toLowerCase()+"/"+r+"/active").then(function(e){return e.data}).then(function(e){return!!e.result&&e.result})},n}(),$=/*#__PURE__*/function(){function e(e){this.client=e}var t=e.prototype;return t.create=function(e){var t=r.v4();return this.client.post("/v1_0/apiuser",{providerCallbackHost:e},{headers:{"X-Reference-Id":t}}).then(function(){return t})},t.login=function(e){return this.client.post("/v1_0/apiuser/"+e+"/apikey").then(function(e){return e.data})},e}();function Q(e,r){return void 0===r&&(r=s.default.create()),r.defaults.baseURL=e.baseUrl,r.defaults.headers={"Ocp-Apim-Subscription-Key":e.primaryKey,"X-Target-Environment":e.environment||"sandbox"},function(e){return e.interceptors.response.use(function(e){return e},function(e){return Promise.reject(V(e))}),e}(r)}function Z(e,r){return r.interceptors.request.use(function(r){return e().then(function(e){return l({},r,{headers:l({},r.headers,{Authorization:"Bearer "+e})})})}),r}function ee(e,r){var t;return function(){return function(e){return!e||!e.expires||Date.now()>e.expires}(t)?e(r).then(function(e){var r=e.expires_in;return{accessToken:e.access_token,expires:Date.now()+1e3*r-6e4}}).then(function(e){return(t=e).accessToken}):Promise.resolve(t.accessToken)}}var re=function(e,r){void 0===r&&(r=Q(e));var t=ie(e);return r.post("/collection/token/",null,{headers:{Authorization:"Basic "+t}}).then(function(e){return e.data})},te=function(e,r){void 0===r&&(r=Q(e));var t=ie(e);return r.post("/disbursement/token/",null,{headers:{Authorization:"Basic "+t}}).then(function(e){return e.data})},ne=function(e,r){void 0===r&&(r=Q(e));var t=ie(e);return r.post("/remittance/token/",null,{headers:{Authorization:"Basic "+t}}).then(function(e){return e.data})};function ie(e){return Buffer.from(e.userId+":"+e.userSecret).toString("base64")}var ue={baseUrl:"https://sandbox.momodeveloper.mtn.com",environment:e.Environment.SANDBOX};e.ApprovalRejectedError=_,e.ExpiredError=A,e.InternalProcessingError=N,e.InvalidCallbackUrlHostError=O,e.InvalidCurrencyError=m,e.MtnMoMoError=R,e.NotAllowedError=T,e.NotAllowedTargetEnvironmentError=I,e.NotEnoughFundsError=b,e.PayeeNotAllowedToReceiveError=L,e.PayeeNotFoundError=P,e.PayerLimitReachedError=D,e.PayerNotFoundError=q,e.PaymentNotApprovedError=S,e.ResourceAlreadyExistError=C,e.ResourceNotFoundError=U,e.ServiceUnavailableError=F,e.TransactionCancelledError=g,e.UnspecifiedError=w,e.create=function(r){var n,i,u;return i=(n=r).baseUrl,u=n.environment,t.strictEqual(j(n.callbackHost),!0,"callbackHost is required"),u&&u!==e.Environment.SANDBOX&&(t.strictEqual(j(i),!0,"baseUrl is required if environment is not sandbox"),t.strictEqual(B(i),!0,"baseUrl must be a string")),{Collections:function(e){!function(e){var r,n,i;x(e),i=(r=e).userSecret,t.strictEqual(j(n=r.userId),!0,"userId is required"),t.strictEqual(B(n),!0,"userId must be a string"),t.strictEqual(j(i),!0,"userSecret is required"),t.strictEqual(B(i),!0,"userSecret must be a string"),t.strictEqual(X(n),!0,"userId must be a valid uuid v4")}(e);var n=l({},ue,r,e),i=Z(ee(re,n),Q(n));return new G(i)},Disbursements:function(e){var t=l({},ue,r,e),n=Z(ee(te,t),Q(t));return new K(n)},Remittances:function(e){var t=l({},ue,r,e),n=Z(ee(ne,t),Q(t));return new J(n)},Users:function(e){x(e);var t=Q(l({},ue,r,e));return new $(t)}}},e.getError=k,e.getTransactionError=Y,e.handleError=V});