UNPKG

@htilssu/wowo

Version:

Module tương tác với ứng dụng e-wallet môn học CNPM-DAPM

129 lines 5.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WoWoWallet = void 0; const axios_1 = __importDefault(require("axios")); const utils_1 = require("./utils"); class WoWoWallet { req; apiKey; baseUrl = "https://api.wowo.htilssu.id.vn"; /** * Tạo mới một instance của WoWoWallet * @param apiKey API KEY được cung cấp bởi WowoWallet * @param baseUrl URL của WowoWallet, mặc định là {@link https://api.wowo.htilssu.id.vn} */ constructor(apiKey, baseUrl) { if (!apiKey) { throw new Error("API KEY không được để trống"); } this.apiKey = apiKey; if (baseUrl && !(0, utils_1.isValidUrl)(baseUrl)) { throw new Error("Invalid baseUrl"); } this.baseUrl = baseUrl ?? this.baseUrl; this.req = axios_1.default.create({ baseURL: this.baseUrl, headers: { "X-API-KEY": this.apiKey } }); } /** * Tạo một đơn hàng mới * @param props Thông tin đơn hàng */ async createOrder(props) { if (props.items && props.items.length !== 0) { const itemHasNegativePriceOrNegativeQuantity = props.items.find(value => value.unitPrice < 0 || value.amount <= 0); if (itemHasNegativePriceOrNegativeQuantity) { throw new Error("Giá của item phải lớn hơn hoặc bằng 0, Số lượng phải lớn hơn 0"); } } if (props.money < 0) { throw new Error('Số tiền phải lớn hơn hoặc bằng 0'); } if (!(0, utils_1.isValidUrl)(props?.callback?.returnUrl)) { throw new Error("Đường dẫn return không hợp lệ"); } if (!(0, utils_1.isValidUrl)(props?.callback?.returnUrl)) { throw new Error("Đường dẫn callback không hợp lệ"); } //check url callback if (props.callback && !(0, utils_1.isValidUrl)(props?.callback?.successUrl)) { throw new Error("Đường dẫn callback không hợp lệ"); } const url = `${this.baseUrl}/v1/orders/create`; const response = await this.req.post(url, { ...props, ...props.callback }); return response.data; } async createWallet() { const url = `${this.baseUrl}/v1/application/wallet`; return (await this.req.post(url)).data; } async deleteWallet(walletId) { const url = `${this.baseUrl}/v1/application/wallet/${walletId}`; return (await this.req.delete(url)).status === 200; } async getWallet(walletId) { const url = `${this.baseUrl}/v1/application/wallet/${walletId}`; return (await this.req.get(url)).data; } async transferMoney(walletId, amount) { const url = `${this.baseUrl}/v1/application/transfer`; return (await this.req.post(url, { walletId, amount })).status === 200; } async getMoneyFromWallet(walletId, amount) { const url = `${this.baseUrl}/v1/application/withdraw`; return (await this.req.post(url, { walletId, amount })).status === 200; } /** * Hủy đơn hàng của bạn * * Mã code trả về: * - `200` Hủy đơn hàng thành công * - `400` Đơn hàng không thể hủy do đơn hàng đã được thanh toán hoặc đã hủy hoặc không thể hủy do bussiness rule * - `404` Đơn hàng không tồn tại * @remarks Đơn hàng chỉ có thể hủy khi đơn hàng đang ở trạng thái `PENDING` * @param orderId Mã đơn hàng cần hủy */ async cancelOrder(orderId) { const url = `${this.baseUrl}/v1/orders/${orderId}/cancel`; return (await this.req.post(url)).data; } /** * Lấy thông tin đơn hàng * * Mã code trả về: * - `200` Lấy thông tin đơn hàng thành công * - `404` Đơn hàng không tồn tại * @param orderId Mã đơn hàng cần lấy thông tin */ async getOrder(orderId) { const url = `${this.baseUrl}/v1/orders/${orderId}`; return (await this.req.get(url)).data; } /** * Refund đơn hàng * Mã code trả về: * - `200` Refund đơn hàng thành công * - `400` Đơn hàng không thể refund do đơn hàng đã hủy hoặc không thể refund do bussiness rule * - `404` Đơn hàng không tồn tại * @remarks Đơn hàng chỉ có thể refund khi đơn hàng đang ở trạng thái `SUCCESS` * @param orderId Mã đơn hàng cần refund * @param amount */ async refundOrder(orderId, amount) { const url = `${this.baseUrl}/v1/orders/${orderId}/refund`; return (await this.req.post(url, { amount })).data; } } exports.WoWoWallet = WoWoWallet; //# sourceMappingURL=WowoWallet.js.map