UNPKG

shopier-api

Version:
194 lines (188 loc) 6.65 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { PlatformType: () => PlatformType, ProductType: () => ProductType, Shopier: () => Shopier }); module.exports = __toCommonJS(src_exports); var import_crypto = require("crypto"); // src/enums/platformTypes.enum.ts var PlatformType = /* @__PURE__ */ ((PlatformType2) => { PlatformType2[PlatformType2["IN_FRAME"] = 0] = "IN_FRAME"; PlatformType2[PlatformType2["NOT_IN_FRAME"] = 1] = "NOT_IN_FRAME"; return PlatformType2; })(PlatformType || {}); // src/enums/productTypes.enum.ts var ProductType = /* @__PURE__ */ ((ProductType2) => { ProductType2[ProductType2["REAL_OBJECT"] = 0] = "REAL_OBJECT"; ProductType2[ProductType2["DOWNLOADABLE_VIRTUAL"] = 1] = "DOWNLOADABLE_VIRTUAL"; ProductType2[ProductType2["DEFAULT"] = 2] = "DEFAULT"; return ProductType2; })(ProductType || {}); // src/index.ts var import_util = require("util"); // src/enums/currencyTypes.enum.ts var CurrencyType = /* @__PURE__ */ ((CurrencyType2) => { CurrencyType2[CurrencyType2["TL"] = 0] = "TL"; CurrencyType2[CurrencyType2["USD"] = 1] = "USD"; CurrencyType2[CurrencyType2["EUR"] = 2] = "EUR"; return CurrencyType2; })(CurrencyType || {}); // src/index.ts var Shopier = class { constructor(apiKey, apiSecret) { this.paymentUrl = "https://www.shopier.com/ShowProduct/api_pay4.php"; this.buyer = {}; this.orderBilling = {}; this.orderShipping = {}; this.currency = 0 /* TL */; this.moduleVersion = "1.0.4"; this.apiKey = apiKey; this.apiSecret = apiSecret; } setBuyer(fields) { this.buyer = fields; return this; } setOrderBilling(fields) { this.orderBilling = fields; return this; } setOrderShipping(fields) { this.orderShipping = fields; return this; } generateIForm(amount) { var _a, _b, _c; const args = { API_key: this.apiKey, website_index: 1, platform_order_id: (_a = this.buyer.platform_order_id) != null ? _a : this.buyer.buyer_id_nr, product_name: this.buyer.product_name, product_type: 0 /* REAL_OBJECT */ || this.buyer.product_type, buyer_name: this.buyer.buyer_name, buyer_surname: this.buyer.buyer_surname, buyer_email: this.buyer.buyer_email, buyer_account_age: (_b = this.buyer.buyer_account_age) != null ? _b : 0, buyer_id_nr: this.buyer.buyer_id_nr, buyer_phone: this.buyer.buyer_phone, billing_address: this.orderBilling.billing_address, billing_city: this.orderBilling.billing_city, billing_country: this.orderBilling.billing_country, billing_postcode: this.orderBilling.billing_postcode, shipping_address: this.orderShipping.shipping_address, shipping_city: this.orderShipping.shipping_city, shipping_country: this.orderShipping.shipping_country, shipping_postcode: this.orderShipping.shipping_postcode, total_order_value: amount, currency: this.currency, platform: 0 /* IN_FRAME */, is_in_frame: 0 /* IN_FRAME */, current_language: this.lang(), modul_version: this.moduleVersion, random_nr: Math.floor(Math.random() * (999999 - 1e5 + 1)) + 1e5, signature: "" }; const platformOrderId = (_c = this.buyer.platform_order_id) != null ? _c : this.buyer.buyer_id_nr; const data = args.random_nr + platformOrderId + args.total_order_value + args.currency; const hmac = (0, import_crypto.createHmac)("sha256", this.apiSecret); hmac.update(data); const signatureBase64 = hmac.digest("base64"); args.signature = signatureBase64; return args; } recursiveHtmlStringGenerator(args) { return Object.entries(args).map( ([key, value]) => `<input type="hidden" name="${key}" value="${value}">` ).join(""); } /** * @deprecated Use `generatePaymentHTML(amount: number)` instead */ payment(amount) { (0, import_util.deprecate)( this.payment, "payment(amount: number) is deprecated. Use generatePaymentHTML(amount: number) instead." ); return this.generatePaymentHTML(amount); } generatePaymentHTML(amount) { const obj = this.generateIForm(amount); return `<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title></title> </head> <form id="shopier_payment_form" method="post" action="${this.paymentUrl}"> ${this.recursiveHtmlStringGenerator(obj)} </form> <body> <script type="text/javascript"> document.getElementById("shopier_payment_form").submit(); </script> </body> </html>`; } setCurrency(currency) { this.currency = typeof currency === "number" ? currency : CurrencyType[currency]; return this; } lang() { const current_language = "tr-TR"; let current_lan = 1; if (current_language == "tr-TR") { current_lan = 0; } return current_lan; } // eslint-disable-next-line @typescript-eslint/no-explicit-any callback(body) { const data = `${body.random_nr}${body.platform_order_id}`; const hmac = (0, import_crypto.createHmac)("sha256", this.apiSecret); hmac.update(data); const expected = hmac.digest("base64"); if (body.signature === expected) { if (body.status === "success") { return { order_id: body.platform_order_id, payment_id: body.payment_id, installment: body.installment }; } else { return false; } } else { throw new Error("Signature is not valid."); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PlatformType, ProductType, Shopier }); //# sourceMappingURL=index.js.map