@hachther/mesomb
Version:
JS client for browser to perform mobile payment operation with MeSomb
338 lines (337 loc) • 16.2 kB
JavaScript
"use strict";
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.WalletOperation = void 0;
var AOperation_1 = require("./AOperation");
var Wallet_1 = require("../models/Wallet");
var RandomGenerator_1 = require("../util/RandomGenerator");
var WalletTransaction_1 = require("../models/WalletTransaction");
var PaginatedWallets_1 = require("../models/PaginatedWallets");
var PaginatedWalletTransactions_1 = require("../models/PaginatedWalletTransactions");
var WalletOperation = /** @class */ (function (_super) {
__extends(WalletOperation, _super);
function WalletOperation(_a) {
var providerKey = _a.providerKey, accessKey = _a.accessKey, secretKey = _a.secretKey, _b = _a.language, language = _b === void 0 ? 'en' : _b;
var _this = _super.call(this, { target: providerKey, accessKey: accessKey, secretKey: secretKey, language: language }) || this;
_this.service = 'wallet';
return _this;
}
/**
* Create a wallet for a user
*
* @param {WalletCreationRequest} data to create a wallet
* @param {string} [data.firstName] of the wallet owner
* @param {string} data.lastName of the wallet owner
* @param {string} [data.email] of the wallet owner
* @param {string} data.phoneNumber of the wallet owner
* @param {string} [data.country=CM] of the wallet owner
* @param {string} data.gender of the wallet owner
* @param {string} [data.nonce] unique string on each request
* @param {string} [data.number] The unique numeric wallet identifier, if not set we will generate one for you
*
* @returns {Wallet} wallet
*/
WalletOperation.prototype.createWallet = function (_a) {
var firstName = _a.firstName, lastName = _a.lastName, email = _a.email, phoneNumber = _a.phoneNumber, _b = _a.country, country = _b === void 0 ? 'CM' : _b, gender = _a.gender, nonce = _a.nonce, accNumber = _a.number;
return __awaiter(this, void 0, void 0, function () {
var endpoint, body, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
endpoint = 'wallet/wallets/';
body = {
last_name: lastName,
country: country,
phone_number: phoneNumber,
gender: gender,
};
if (firstName) {
body.first_name = firstName;
}
if (email) {
body.email = email;
}
if (accNumber) {
body.number = accNumber;
}
_c = Wallet_1.default.bind;
return [4 /*yield*/, this.executeRequest('POST', endpoint, new Date(), nonce !== null && nonce !== void 0 ? nonce : RandomGenerator_1.default.nonce(), body)];
case 1: return [2 /*return*/, new (_c.apply(Wallet_1.default, [void 0, _d.sent()]))()];
}
});
});
};
/**
* Get a wallet by its id
*
* @param {number} id of the wallet to get
*
* @returns {Wallet} wallet
*/
WalletOperation.prototype.getWallet = function (id) {
return __awaiter(this, void 0, void 0, function () {
var endpoint, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/wallets/".concat(id, "/");
_a = Wallet_1.default.bind;
return [4 /*yield*/, this.executeRequest('GET', endpoint, new Date(), RandomGenerator_1.default.nonce())];
case 1: return [2 /*return*/, new (_a.apply(Wallet_1.default, [void 0, _b.sent()]))()];
}
});
});
};
/**
* Get a paginated list of wallets
*
* @param {number} page of the wallet to get
*
* @returns {PaginatedWallets} paginated wallets
*/
WalletOperation.prototype.getWallets = function (page) {
if (page === void 0) { page = 1; }
return __awaiter(this, void 0, void 0, function () {
var endpoint, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/wallets/?page=".concat(page);
_a = PaginatedWallets_1.default.bind;
return [4 /*yield*/, this.executeRequest('GET', endpoint, new Date(), RandomGenerator_1.default.nonce())];
case 1: return [2 /*return*/, new (_a.apply(PaginatedWallets_1.default, [void 0, _b.sent()]))()];
}
});
});
};
/**
* Create a wallet for a user
* @param id of the wallet to update
* @param {WalletCreationRequest} data to update
* @param {string} [data.firstName] of the wallet owner
* @param {string} data.lastName of the wallet owner
* @param {string} [data.email] of the wallet owner
* @param {string} data.phoneNumber of the wallet owner
* @param {string} [data.country=CM] of the wallet owner
* @param {string} data.gender of the wallet owner
* @param {string} [data.nonce] unique string on each request
*/
WalletOperation.prototype.updateWallet = function (id, _a) {
var firstName = _a.firstName, lastName = _a.lastName, email = _a.email, phoneNumber = _a.phoneNumber, _b = _a.country, country = _b === void 0 ? 'CM' : _b, gender = _a.gender, nonce = _a.nonce;
return __awaiter(this, void 0, void 0, function () {
var endpoint, body, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
endpoint = "wallet/wallets/".concat(id, "/");
body = {
last_name: lastName,
country: country,
phone_number: phoneNumber,
gender: gender,
};
if (firstName) {
body.first_name = firstName;
}
if (email) {
body.email = email;
}
_c = Wallet_1.default.bind;
return [4 /*yield*/, this.executeRequest('PUT', endpoint, new Date(), nonce !== null && nonce !== void 0 ? nonce : RandomGenerator_1.default.nonce(), body)];
case 1: return [2 /*return*/, new (_c.apply(Wallet_1.default, [void 0, _d.sent()]))()];
}
});
});
};
/**
* Get a wallet by its id
*
* @param {number} id of the wallet to get
*/
WalletOperation.prototype.deleteWallet = function (id) {
return __awaiter(this, void 0, void 0, function () {
var endpoint;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
endpoint = "wallet/wallets/".concat(id, "/");
return [4 /*yield*/, this.executeRequest('DELETE', endpoint, new Date(), RandomGenerator_1.default.nonce())];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Remove money from a wallet
* @param {number} wallet id
* @param {amount} amount to add
* @param {boolean} force to force the operation if balance is not enough
*
* @returns {WalletTransaction} transaction
*/
WalletOperation.prototype.removeMoney = function (wallet, amount, force) {
if (force === void 0) { force = false; }
return __awaiter(this, void 0, void 0, function () {
var endpoint, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/wallets/".concat(wallet, "/adjust/");
_a = WalletTransaction_1.default.bind;
return [4 /*yield*/, this.executeRequest('POST', endpoint, new Date(), RandomGenerator_1.default.nonce(), {
amount: amount,
force: force,
direction: -1,
})];
case 1: return [2 /*return*/, new (_a.apply(WalletTransaction_1.default, [void 0, _b.sent()]))()];
}
});
});
};
/**
* Add money to a wallet
*
* @param {number} wallet id
* @param {number} amount to add
*
* @returns {WalletTransaction} transaction
*/
WalletOperation.prototype.addMoney = function (wallet, amount) {
return __awaiter(this, void 0, void 0, function () {
var endpoint, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/wallets/".concat(wallet, "/adjust/");
_a = WalletTransaction_1.default.bind;
return [4 /*yield*/, this.executeRequest('POST', endpoint, new Date(), RandomGenerator_1.default.nonce(), { amount: amount, direction: 1 })];
case 1: return [2 /*return*/, new (_a.apply(WalletTransaction_1.default, [void 0, _b.sent()]))()];
}
});
});
};
/**
* Transfer money from a wallet to another
*
* @param {number} from wallet id
* @param {number} to wallet id
* @param {number} amount to transfer
* @param {boolean} force to force the operation if balance is not enough
*
* @returns {WalletTransaction} transaction
*/
WalletOperation.prototype.transferMoney = function (from, to, amount, force) {
if (force === void 0) { force = false; }
return __awaiter(this, void 0, void 0, function () {
var endpoint, body, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/wallets/".concat(from, "/transfer/");
body = {
amount: amount,
to: to,
force: force,
};
_a = WalletTransaction_1.default.bind;
return [4 /*yield*/, this.executeRequest('POST', endpoint, new Date(), RandomGenerator_1.default.nonce(), body)];
case 1: return [2 /*return*/, new (_a.apply(WalletTransaction_1.default, [void 0, _b.sent()]))()];
}
});
});
};
/**
* Get a transaction
*
* @param {number} id of the transaction
*
* @returns {WalletTransaction} transaction
*/
WalletOperation.prototype.getTransaction = function (id) {
return __awaiter(this, void 0, void 0, function () {
var endpoint, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/transactions/".concat(id, "/");
_a = WalletTransaction_1.default.bind;
return [4 /*yield*/, this.executeRequest('GET', endpoint, new Date(), RandomGenerator_1.default.nonce())];
case 1: return [2 /*return*/, new (_a.apply(WalletTransaction_1.default, [void 0, _b.sent()]))()];
}
});
});
};
/**
* Get a transaction
* @param page of the transaction
* @param wallet id of the wallet
*/
WalletOperation.prototype.getTransactions = function (page, wallet) {
return __awaiter(this, void 0, void 0, function () {
var endpoint, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = "wallet/transactions/?page=".concat(page).concat(wallet ? "&wallet=".concat(wallet) : '');
_a = PaginatedWalletTransactions_1.default.bind;
return [4 /*yield*/, this.executeRequest('GET', endpoint, new Date(), RandomGenerator_1.default.nonce())];
case 1: return [2 /*return*/, new (_a.apply(PaginatedWalletTransactions_1.default, [void 0, _b.sent()]))()];
}
});
});
};
return WalletOperation;
}(AOperation_1.AOperation));
exports.WalletOperation = WalletOperation;