@squadco/js
Version:
Simplify the integration process with Squad's comprehensive payment solutions using the Squad JavaScript SDK.
141 lines (137 loc) • 8.02 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const virtual_account_1 = __importDefault(require("./virtual-account"));
class SquadTransfer extends virtual_account_1.default {
constructor(publicKey, privateKey, environment) {
super(publicKey, privateKey, environment);
this.baseTransferUrl = "/payout";
}
/**
* @desc This method allows you lookup/confirm the account name of the
* recipient you intend to credit. This should be done before initiating the transfer.
* @arg {string} bankCode - The unique NIP code that identifies a bank
* @arg {string} accountNumber - The account number you want to transfer to
*/
accountLookup(bankCode, accountNumber) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (!bankCode ||
typeof bankCode !== "string" ||
!accountNumber ||
typeof accountNumber !== "string")
throw new Error("Validation Error! BankCode or AccountNumber must be a string");
const dataToSend = {
bank_code: bankCode,
account_number: accountNumber,
};
try {
const squadResponse = yield this.Axios.post(`${this.baseTransferUrl}/account/lookup`, dataToSend);
return squadResponse.data;
}
catch (error) {
console.warn((_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message);
return (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data;
}
});
}
/**
* @desc This method allows you to transfer funds from your Squad Wallet
* to the account you have looked up. Please be informed that we will not be
* held liable for mistake in transferring to a wrong account or an account
* that wasn't looked up.
Transaction Reference: Transaction Reference used to initiate a transfer
must be unique per transfer. Kindly ensure that you append your merchant ID
to the transaction Reference you are creating. This is compulsory as it will throw
an error if you don't append it.
For instance: If my Squad Merchant ID is SBABCKDY and i want to create a transaction ref for my transfer, then I will have something like:
"transactionReference":"SBABCKDY_12345".
@arg {string} transactionData.transactionReference - Unique Transaction Reference used to initiate a transfer.
Please ensure your merchantId to the transaction reference you're creating. This
is compulsory as it will throw an error if you don't append it.
@arg {string} transactionData.amount - Amount to be transferred. Amount is in Kobo
@arg {string} transactionData.transactionDataebankCode - Unique NIP code that identifies a Bank.
@arg {string} transactionData.accountNumber - 10-digit NUBAN account number to be transferred to. Must be an account that has been looked up and vetted
to be transferred to.
@arg {string} transactionData.accountName - The account name tied to the account number you are transferring to which you've looked up with the accountLookup method.
@arg {string} transactionData.currencyId - Takes only the value NGN
@arg {string} transactionData.remark - A unique remark that will be sent with the transfer
*/
transferFunds(transactionData) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (!transactionData || typeof transactionData !== "object")
throw new Error("Validation Error! Invalid transaction data");
const dataToSend = {
remark: transactionData.remark,
bank_code: transactionData.bankCode,
currency_id: transactionData.currencyId,
amount: transactionData.amount,
account_number: transactionData.accountNumber,
transaction_reference: transactionData.transactionReference,
account_name: transactionData.accountName,
};
try {
const squadResponse = yield this.Axios.post(`${this.baseTransferUrl}/transfer`, dataToSend);
return squadResponse.data;
}
catch (error) {
console.warn((_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message);
return (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data;
}
});
}
/**
* @desc This method allows you re-query the status of a transfer made to know if it was successful, failed, reversed or pending.
* @arg {string} transactionReference - Unique Transaction Reference used to initiate a transfer.
* Please ensure that you append your merchant ID to the transaction Reference you
* are creating. This is compulsory as it will throw an error if you don't append it
*/
reQueryFundsTransfer(transactionReference) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (!transactionReference || typeof transactionReference !== "string")
throw new Error("Validation Error ! Transaction Reference must be a string");
const dataToSend = {
transaction_reference: transactionReference,
};
try {
const squadResponse = yield this.Axios.post(`${this.baseTransferUrl}/requery`, dataToSend);
return squadResponse.data;
}
catch (error) {
console.warn((_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message);
return (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data;
}
});
}
/**
* @desc This method allows you retrieve the details of all transfers you have done from your Squad Wallet using this transfer solution.
* @arg None
*/
getAllTransfers() {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const squadResponse = yield this.Axios.get(`${this.baseTransferUrl}/list`);
return squadResponse.data;
}
catch (error) {
console.warn((_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message);
return (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data;
}
});
}
}
exports.default = SquadTransfer;