UNPKG

coinforbarter-react

Version:

CoinForBarter React Library - Integrate cryptocurrency payments for goods and services in your React App

245 lines 13 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoinForBarterCheckout = void 0; var types_1 = require("./types"); var config_1 = require("./config"); var iframeId = config_1.settings.iframeId; var Checkout = (function () { function Checkout(config) { this.config = config; this.iframeId = iframeId; this.initialize(); } Checkout.prototype.initialize = function () { return __awaiter(this, void 0, void 0, function () { var _a, publicKey, txRef, amount, currency, customer, customerPhoneNumber, customerFullName, customizations, body; return __generator(this, function (_b) { _a = this.config, publicKey = _a.publicKey, txRef = _a.txRef, amount = _a.amount, currency = _a.currency, customer = _a.customer, customerPhoneNumber = _a.customerPhoneNumber, customerFullName = _a.customerFullName, customizations = _a.customizations; body = { publicKey: publicKey, txRef: txRef, amount: amount, currency: currency, customer: customer, customerPhoneNumber: customerPhoneNumber, customerFullName: customerFullName, customizations: customizations, }; this.createIframe(body); return [2]; }); }); }; Checkout.prototype.createIframe = function (body) { try { var iframe = document.createElement("iframe"); var form = document.createElement("form"); form.setAttribute("action", config_1.settings.url); form.setAttribute("target", this.iframeId); form.setAttribute("method", "POST"); for (var _i = 0, _a = Object.entries(body); _i < _a.length; _i++) { var _b = _a[_i], name_1 = _b[0], value = _b[1]; if (name_1 === "currencies") { for (var index = 0; index < value.length; index++) { var currency = value[index]; var input = document.createElement("input"); input.setAttribute("name", name_1 + "[]"); input.setAttribute("value", currency); form.append(input); } } else if (name_1 === "customizations") { if (value) { for (var _c = 0, _d = Object.entries(value); _c < _d.length; _c++) { var _e = _d[_c], customizationName = _e[0], customizationValue = _e[1]; var input = document.createElement("input"); input.setAttribute("name", name_1 + "[" + customizationName + "]"); input.setAttribute("value", typeof customizationValue === "string" ? customizationValue : ""); form.append(input); } } } else { var input = document.createElement("input"); input.setAttribute("name", name_1); input.setAttribute("value", value); form.append(input); } } iframe.setAttribute("src", config_1.settings.url); iframe.setAttribute("name", this.iframeId); iframe.setAttribute("id", this.iframeId); iframe.setAttribute("style", "\n position: fixed;\n top: 0;\n left: 0;\n bottom: 0;\n right: 0;\n overflow: auto;\n display:block;\n width:100%;\n height:100vh;\n background:white;\n border:none;\n border-width:0;\n "); console.log(document); document.body.appendChild(form); document.body.appendChild(iframe); form.submit(); form.remove(); this.checkStatus(); } catch (error) { } }; Checkout.prototype.checkStatus = function () { var _this = this; window.addEventListener("message", function (_a) { var _b = _a.data, message = _b.message, value = _b.value; if (message === "CoinForBarter:inline:response") { _this.handleResponse(value.status, value.txRef, value.id); } }); }; Checkout.prototype.handleResponse = function (status, txRef, transactionId) { return __awaiter(this, void 0, void 0, function () { var params; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(status === "success")) return [3, 2]; params = { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ publicKey: this.config.publicKey }), }; return [4, fetch(config_1.settings.verifyUrl + "/" + transactionId + "/verify", params) .then(function (res) { return __awaiter(_this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!!res.ok) return [3, 2]; _a = types_1.CustomException.bind; _b = [{}]; return [4, res.json()]; case 1: throw new (_a.apply(types_1.CustomException, [void 0, __assign.apply(void 0, [__assign.apply(void 0, _b.concat([(_c.sent())])), { status: res.status }])]))(); case 2: return [2, res.json()]; } }); }); }) .then(function (_a) { var data = _a.data; _this.close(types_1.CloseType.Success, { data: data, status: status, transactionId: transactionId, txRef: txRef, }); }) .catch(function (err) { _this.close(types_1.CloseType.Error, { data: { err: err }, status: status, transactionId: transactionId, txRef: txRef, }); })]; case 1: _a.sent(); return [3, 3]; case 2: this.close(types_1.CloseType.Error, { status: status, transactionId: transactionId, txRef: txRef, }); _a.label = 3; case 3: if (this.config.redirectUrl) { return [2, window.open(this.config.redirectUrl)]; } return [2]; } }); }); }; Checkout.prototype.close = function (status, data) { var _a, _b, _c, _d, _e, _f, _g; var iframe = document.querySelector("#" + this.iframeId); iframe === null || iframe === void 0 ? void 0 : iframe.remove(); if (status === types_1.CloseType.Success) { if (this.config.onSuccess) { this.config.onSuccess({ status: data.status, transactionId: data.transactionId, txRef: data.txRef, amount: data.data.amount, amountReceived: (_a = data.data) === null || _a === void 0 ? void 0 : _a.amountReceived, currency: data.data.currency, customer: data.data.customerDetails, baseCurrency: data.data.baseCurrency, baseAmount: data.data.baseAmount, }); } } if (status === types_1.CloseType.Error) { if (this.config.onError) { this.config.onError({ status: data.status, transactionId: data.transactionId, txRef: data.txRef, amount: (_b = data.data) === null || _b === void 0 ? void 0 : _b.amount, amountReceived: (_c = data.data) === null || _c === void 0 ? void 0 : _c.amountReceived, currency: (_d = data.data) === null || _d === void 0 ? void 0 : _d.currency, customer: (_e = data.data) === null || _e === void 0 ? void 0 : _e.customerDetails, baseCurrency: (_f = data.data) === null || _f === void 0 ? void 0 : _f.baseCurrency, baseAmount: (_g = data.data) === null || _g === void 0 ? void 0 : _g.baseAmount, }); } } }; return Checkout; }()); var CoinForBarterCheckout = function (config) { return new Checkout(config); }; exports.CoinForBarterCheckout = CoinForBarterCheckout; //# sourceMappingURL=CoinForBarterCheckout.js.map