UNPKG

@yabetoo/mtn-momo-js

Version:

MTN Mobile Money API Client for NodeJS written in TypeScript

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