@prakrit_m/tmn-voucher
Version:
ไลบรารี TypeScript สำหรับการแลกคูปองอั่งเปา TrueMoney Wallet
194 lines (187 loc) • 7.65 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => redeemvouchers
});
module.exports = __toCommonJS(index_exports);
// src/constants.ts
var BASE_URL = "https://gift.truemoney.com";
// src/checkMaintenance.ts
async function checkMaintenance() {
const response = await fetch(BASE_URL + "/campaign/vouchers/configuration");
const data = await response.json();
if (data?.status?.code === "SUCCESS") {
return {
success: true,
code: "SUCCESS"
};
}
console.log("!\u0E17\u0E23\u0E39\u0E21\u0E31\u0E19\u0E19\u0E35\u0E48\u0E1B\u0E34\u0E14\u0E1B\u0E23\u0E31\u0E1A\u0E1B\u0E23\u0E38\u0E07\u0E23\u0E30\u0E1A\u0E1A");
return {
success: false,
code: "MAINTEINANCE",
message: data.data.ma.title_th
};
}
// src/validateInput.ts
var import_zod = __toESM(require("zod"), 1);
var inputSchema = import_zod.default.object({
phoneNumber: import_zod.default.string().length(10, "\u0E2B\u0E21\u0E32\u0E22\u0E40\u0E25\u0E02\u0E42\u0E17\u0E23\u0E28\u0E31\u0E1E\u0E17\u0E4C\u0E15\u0E49\u0E2D\u0E07\u0E21\u0E35 10 \u0E2B\u0E25\u0E31\u0E01").refine((x) => /^\d+$/.test(x), "\u0E2B\u0E21\u0E32\u0E22\u0E40\u0E25\u0E02\u0E42\u0E17\u0E23\u0E28\u0E31\u0E1E\u0E17\u0E4C\u0E15\u0E49\u0E2D\u0E07\u0E40\u0E1B\u0E47\u0E19\u0E15\u0E31\u0E27\u0E40\u0E25\u0E02").refine((x) => x.startsWith("0"), "\u0E2B\u0E21\u0E32\u0E22\u0E40\u0E25\u0E02\u0E42\u0E17\u0E23\u0E28\u0E31\u0E1E\u0E17\u0E4C\u0E15\u0E49\u0E2D\u0E07\u0E02\u0E36\u0E49\u0E19\u0E15\u0E49\u0E19\u0E14\u0E49\u0E27\u0E22 0"),
voucherUrl: import_zod.default.string().refine(
(x) => x.startsWith(BASE_URL + "/campaign/?v=") || x.startsWith(BASE_URL + "/campaign?v=") || x.startsWith(BASE_URL + "/campaign/voucher_detail?v=") || x.startsWith(BASE_URL + "/campaign/voucher_detail/?v="),
"\u0E23\u0E39\u0E1B\u0E41\u0E1A\u0E1A URL Voucher \u0E44\u0E21\u0E48\u0E16\u0E39\u0E01\u0E15\u0E49\u0E2D\u0E07"
).refine(
(x) => /^https:\/\/gift\.truemoney\.com\/campaign(\/voucher_detail)?(\?v=|\/\?v=)[A-Za-z0-9]+$/.test(x),
"\u0E23\u0E39\u0E1B\u0E41\u0E1A\u0E1A URL Voucher \u0E44\u0E21\u0E48\u0E16\u0E39\u0E01\u0E15\u0E49\u0E2D\u0E07"
)
});
var optionsSchema = import_zod.default.object({
amount: import_zod.default.number().int("amount: \u0E15\u0E49\u0E2D\u0E07\u0E40\u0E1B\u0E47\u0E19\u0E08\u0E33\u0E19\u0E27\u0E19\u0E40\u0E15\u0E47\u0E21").positive().min(100, "amount: \u0E08\u0E33\u0E19\u0E27\u0E19\u0E40\u0E07\u0E34\u0E19\u0E02\u0E31\u0E49\u0E19\u0E15\u0E48\u0E33 100 \u0E1A\u0E32\u0E17").max(2e7, "amount: \u0E08\u0E33\u0E19\u0E27\u0E19\u0E40\u0E07\u0E34\u0E19\u0E15\u0E49\u0E2D\u0E07\u0E44\u0E21\u0E48\u0E40\u0E01\u0E34\u0E19 200,000 \u0E1A\u0E32\u0E17")
});
async function validateInput(phoneNumber, voucherUrl, options) {
const validationResult = inputSchema.safeParse({ phoneNumber, voucherUrl });
if (!validationResult.success) {
return {
success: false,
code: "INVALID_INPUT",
message: validationResult.error.errors[0].message
};
}
if (options) {
const optionsValidationResult = optionsSchema.safeParse(options);
if (!optionsValidationResult.success) {
optionsValidationResult.error.errors.map((error) => {
console.log(error.message);
});
return {
success: false,
code: "INVALID_INPUT",
message: optionsValidationResult.error.errors[0].message
};
}
}
return {
success: true,
code: "SUCCESS"
};
}
// src/utils.ts
function satangToBaht(satang) {
const baht = satang / 100;
return baht.toFixed(2);
}
function bahtToSatang(bahtStr) {
const bahtFloat = parseFloat(bahtStr);
const satang = bahtFloat * 100;
return Math.round(satang);
}
// src/validateVoucher.ts
function isValidVoucher(voucher, options) {
const { amount_baht, redeemed_amount_baht, available, type, member } = voucher;
const amountInSatang = bahtToSatang(amount_baht);
if (member === 1) {
return satangToBaht(options.amount) === amount_baht;
}
if (member > 1) {
if (type === "R") {
const balance = amountInSatang - bahtToSatang(redeemed_amount_baht);
return available === 1 && options.amount === balance;
}
if (type === "F") {
const balance = amountInSatang / member;
return options.amount === balance;
}
}
return false;
}
// src/index.ts
async function redeemvouchers(phoneNumber, voucherUrl, options) {
const urlParams = new URLSearchParams(new URL(voucherUrl).search);
const voucherCode = urlParams.get("v");
const inputValidation = await validateInput(phoneNumber, voucherUrl, options);
if (!inputValidation.success) {
return inputValidation;
}
const maintenance = await checkMaintenance();
if (!maintenance.success) {
return maintenance;
}
if (options) {
const verifyResponse = await fetch(
BASE_URL + "/campaign/vouchers/" + voucherCode + "/verify"
).then((response) => response.json());
if (!verifyResponse.data || verifyResponse.status.code !== "SUCCESS") {
return {
success: false,
code: verifyResponse.status.code,
message: verifyResponse.status.message
};
}
const { voucher } = verifyResponse.data;
if (!isValidVoucher(voucher, options)) {
return {
success: false,
code: "CONDITION_NOT_MET",
message: "\u0E44\u0E21\u0E48\u0E15\u0E23\u0E07\u0E40\u0E07\u0E37\u0E48\u0E2D\u0E19\u0E44\u0E02"
};
}
}
const redeemResponse = await fetch(
BASE_URL + "/campaign/vouchers/" + voucherCode + "/redeem",
{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
mobile: phoneNumber,
voucher_hash: voucherCode
})
}
).then((response) => response.json());
if (redeemResponse.status.code === "SUCCESS") {
const successResponse = redeemResponse;
return {
success: true,
code: "SUCCESS",
message: redeemResponse.status.message,
amount: bahtToSatang(successResponse.data.my_ticket.amount_baht),
data: successResponse.data
};
}
return {
success: false,
code: redeemResponse.status.code,
message: redeemResponse.status.message,
data: redeemResponse.data
};
}
//# sourceMappingURL=index.cjs.map