@hachther/mesomb
Version:
JS client for browser to perform mobile payment operation with MeSomb
182 lines (181 loc) • 10.8 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FundraisingOperation = void 0;
var AOperation_1 = require("./AOperation");
var ContributionResponse_1 = require("../models/ContributionResponse");
var Contribution_1 = require("../models/Contribution");
var RandomGenerator_1 = require("../util/RandomGenerator");
var FundraisingOperation = /** @class */ (function (_super) {
__extends(FundraisingOperation, _super);
function FundraisingOperation(_a) {
var fundKey = _a.fundKey, accessKey = _a.accessKey, secretKey = _a.secretKey, _b = _a.language, language = _b === void 0 ? 'en' : _b;
var _this = _super.call(this, { target: fundKey, accessKey: accessKey, secretKey: secretKey, language: language }) || this;
_this.service = 'fundraising';
return _this;
}
/**
* Collect money a user account
* [Check the documentation here](https://mesomb.hachther.com/en/api/schema/)
*
* @param {MoneyContributeRequest} params - amount to collect
* @param {'MTN' | 'ORANGE' | 'AIRTEL'} params.service - payment service with the possible values MTN, ORANGE, AIRTEL
* @param {string} params.payer - account number to collect from
* @param {string} [params.nonce] - unique string on each request
* @param {string} [params.country='CM'] - 2 letters country code of the service (configured during your service registration in MeSomb)
* @param {string} [params.currency='XAF'] - currency of your service depending on your country
* @param {string} [params.mode] - asynchronous or synchronous
* @param {boolean} [params.conversion=false] - true in case of foreign currently defined if you want to rely on MeSomb to convert the amount in the local currency
* @param {LocationRequest} [params.location] - Map containing the location of the customer with the following attributes: town, region and location all string.
* @param {boolean} [params.anonymous=false] - true if the contribution is anonymous
* @param {boolean} [params.acceptTerms=true] - true if the contributor accept the terms
* @param {Contact} [params.contact] - Contact of the contributor with the following attributes: phoneNumber string, email string. If contribution is anonymous, this field is not required
* @param {FullName} [params.fullName] - Full name of the contributor with the following attributes: firstName string, lastName string. If contribution is anonymous, this field is not required
* @param {string} [params.trxID] - if you want to include your contribution ID in the request
*
* @return TransactionResponse
*/
FundraisingOperation.prototype.makeContribution = function (_a) {
var contact = _a.contact, service = _a.service, payer = _a.payer, fullName = _a.fullName, amount = _a.amount, _b = _a.anonymous, anonymous = _b === void 0 ? false : _b, location = _a.location, _c = _a.acceptTerms, acceptTerms = _c === void 0 ? true : _c, nonce = _a.nonce, trxID = _a.trxID, _d = _a.mode, mode = _d === void 0 ? 'synchronous' : _d, _e = _a.country, country = _e === void 0 ? 'CM' : _e, _f = _a.currency, currency = _f === void 0 ? 'XAF' : _f, _g = _a.conversion, conversion = _g === void 0 ? false : _g;
return __awaiter(this, void 0, void 0, function () {
var endpoint, body, _h;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
endpoint = 'fundraising/contribute/';
body = {
service: service,
payer: payer,
amount: amount,
anonymous: anonymous,
accept_terms: acceptTerms,
country: country,
amount_currency: currency,
conversion: conversion,
};
if (trxID) {
body.trxID = trxID;
}
if (location) {
body.location = location;
}
if (contact) {
body.contact = {
phone_number: contact.phoneNumber,
};
if (contact.email) {
body.contact.email = contact.email;
}
}
if (fullName) {
body.full_name = {
last_name: fullName.lastName,
};
if (fullName.firstName) {
body.full_name.first_name = fullName.firstName;
}
}
_h = ContributionResponse_1.default.bind;
return [4 /*yield*/, this.executeRequest('POST', endpoint, new Date(), nonce !== null && nonce !== void 0 ? nonce : RandomGenerator_1.default.nonce(), body, mode)];
case 1: return [2 /*return*/, new (_h.apply(ContributionResponse_1.default, [void 0, _j.sent()]))()];
}
});
});
};
/**
* Fetch contributions base on MeSomb IDs or external IDs
*
* @param {string[]} ids - ids of the contributions
* @param {'MESOMB' | 'EXTERNAL'} [source='MESOMB'] - origin of the ID of the contributions (MESOMB for MeSomb and EXTERNAL for your system)
*
* @return Contribution[]
*/
FundraisingOperation.prototype.getContributions = function (ids, source) {
if (source === void 0) { source = 'MESOMB'; }
return __awaiter(this, void 0, void 0, function () {
var endpoint;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
endpoint = "fundraising/contributions/?ids=".concat(ids.join(','), "&source=").concat(source);
return [4 /*yield*/, this.executeRequest('GET', endpoint, new Date(), '')];
case 1: return [2 /*return*/, (_a.sent()).map(function (d) { return new Contribution_1.default(d); })];
}
});
});
};
/**
* Check contributions base on MeSomb IDs or external IDs
*
* @param {string[]} ids - ids of the contributions
* @param {'MESOMB' | 'EXTERNAL'} [source='MESOMB'] - origin of the ID of the contributions (MESOMB for MeSomb and EXTERNAL for your system)
*
* @return Contribution[]
*/
FundraisingOperation.prototype.checkContributions = function (ids, source) {
if (source === void 0) { source = 'MESOMB'; }
return __awaiter(this, void 0, void 0, function () {
var endpoint;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
endpoint = "fundraising/contributions/check/?ids=".concat(ids.join(','), "&source=").concat(source);
return [4 /*yield*/, this.executeRequest('GET', endpoint, new Date(), '')];
case 1: return [2 /*return*/, (_a.sent()).map(function (d) { return new Contribution_1.default(d); })];
}
});
});
};
return FundraisingOperation;
}(AOperation_1.AOperation));
exports.FundraisingOperation = FundraisingOperation;
;