glide-nodejs-sdk
Version:
Glide NodeJS SDK
244 lines (204 loc) • 6.13 kB
JavaScript
const { validateSchema } = require("../utils");
const handleApiErrors = require("../errors/handleApiErrors");
const {
walletPinSchema,
createWalletSchema,
walletTransactionSchema,
batchCreditCustomerSchema,
walletToWalletTransferSchema,
customerBatchCreditCustomersSchema,
} = require("../validators/wallet.schema");
class Wallet {
constructor(request) {
this.request = request;
}
allCustomerWallets = async () => {
try {
const { data } = await this.request.get("/wallet");
if (data && data.status) {
return data.wallets;
} else {
throw new Error("Unable to get customer wallets");
}
} catch (error) {
handleApiErrors(error);
}
};
createCustomerWallet = async (payload) => {
try {
const customerDetails = await validateSchema({ schema: createWalletSchema, payload });
const { data } = await this.request.post("/wallet", customerDetails);
if (data && data.status) {
return data;
} else {
throw new Error("There is problem creating customer wallet");
}
} catch (error) {
handleApiErrors(error);
}
};
creditCustomerWallet = async (payload) => {
try {
const creditPayload = await validateSchema({ schema: walletTransactionSchema, payload });
const { data } = await this.request.post("/wallet/credit", creditPayload);
if (data && data.status) {
return data;
} else {
throw new Error("There is problem crediting customer wallet");
}
} catch (error) {
handleApiErrors(error);
}
};
debitCustomerWallet = async (payload) => {
try {
const debitPayload = await validateSchema({ schema: walletTransactionSchema, payload });
const { data } = await this.request.post("/wallet/debit", debitPayload);
if (data && data.status) {
return data;
} else {
throw new Error("There is problem debiting customer wallet");
}
} catch (error) {
handleApiErrors(error);
}
};
getCustomerWallet = async (customerId) => {
try {
const { data } = await this.request.get("/wallet/customer", { params: { customerId } });
if (data && data.status) {
return data.wallet;
} else {
throw new Error("We are unable to find the specified customer wallet.");
}
} catch (error) {
handleApiErrors(error);
}
};
setWalletTransactionPin = async (payload) => {
try {
const pinPayload = await validateSchema({ schema: walletPinSchema, payload });
const { data } = await this.request.put("/wallet/transaction-pin", pinPayload);
if (data && data.status) {
return data;
} else {
throw new Error("We are unable to set transaction pin.");
}
} catch (error) {
handleApiErrors(error);
}
};
getSettlementBalance = async () => {
try {
const { data } = await this.request.get("/wallet/settlement-balance");
if (data && data.status) {
return data.balance;
} else {
throw new Error("We are unable retreive settlement balance");
}
} catch (error) {
handleApiErrors(error);
}
};
settleCustomerBalances = async () => {
try {
const { data } = await this.request.post("/wallet/settle-customer-balance");
if (data && data.status) {
return data.status;
} else {
throw new Error("We are unable to settle customer balances.");
}
} catch (error) {
handleApiErrors(error);
}
};
closeCustomerWallet = async (customerId) => {
try {
const { data } = await this.request.post("/wallet/close", { customerId });
if (data && data.status) {
return data.status;
} else {
throw new Error("We are unable to frozen customer wallet.");
}
} catch (error) {
handleApiErrors(error);
}
};
enableCustomerWallet = async (customerId) => {
try {
const { data } = await this.request.post("/wallet/enable", { customerId });
if (data && data.status) {
return data.status;
} else {
throw new Error("We are unable to frozen customer wallet.");
}
} catch (error) {
handleApiErrors(error);
}
};
walletToWalletTransfer = async (payload) => {
try {
const transferPayload = await validateSchema({ schema: walletToWalletTransferSchema, payload });
const { data } = await this.request.post("/transfer/wallet", transferPayload);
if (data && data.status) {
return data.status;
} else {
throw new Error("We are unable to complete this transaction.");
}
} catch (error) {
handleApiErrors(error);
}
};
batchDebitCustomerWallets = async (payload) => {
try {
const transferPayload = await validateSchema({ schema: batchCreditCustomerSchema, payload });
const { data } = await this.request.post("/wallet/batch-debit-customer-wallet", transferPayload);
if (data && data.status) {
return data;
} else {
throw new Error("We are unable to complete this transaction.");
}
} catch (error) {
handleApiErrors(error);
}
};
batchCreditCustomerWallets = async (payload) => {
try {
const transferPayload = await validateSchema({ schema: batchCreditCustomerSchema, payload });
const { data } = await this.request.post("/wallet/batch-credit-customer-wallet", transferPayload);
if (data && data.status) {
return data;
} else {
throw new Error("We are unable to complete this transaction.");
}
} catch (error) {
handleApiErrors(error);
}
};
customerBatchCreditCustomerWallets = async (payload) => {
try {
const transferPayload = await validateSchema({ schema: customerBatchCreditCustomersSchema, payload });
const { data } = await this.request.post("/wallet/customer-batch-credit-customer-wallet", transferPayload);
if (data && data.status) {
return data;
} else {
throw new Error("We are unable to complete this transaction.");
}
} catch (error) {
handleApiErrors(error);
}
};
reverseBatchTransaction = async (batchReference) => {
try {
const { data } = await this.request.post("/wallet/reverse-batch-transaction", { batchReference });
if (data && data.status) {
return data;
} else {
throw new Error("We are unable to complete this transaction.");
}
} catch (error) {
handleApiErrors(error);
}
};
}
module.exports = Wallet;