nakapay-react
Version:
React components for NakaPay Bitcoin Lightning payments
350 lines (349 loc) • 23 kB
JavaScript
;
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NakaPayModal = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var qrcode_react_1 = __importDefault(require("qrcode.react"));
var socket_io_client_1 = require("socket.io-client");
var ably_1 = __importDefault(require("ably"));
var NakaPayModal = function (_a) {
var payment = _a.payment, onClose = _a.onClose, onPaymentSuccess = _a.onPaymentSuccess, onPaymentError = _a.onPaymentError, webhookUrl = _a.webhookUrl, _b = _a.useWebhooks, useWebhooks = _b === void 0 ? false : _b, _c = _a.useSSE, useSSE = _c === void 0 ? false : _c, _d = _a.useAbly, useAbly = _d === void 0 ? false : _d, ablyApiKey = _a.ablyApiKey, ablyAuthUrl = _a.ablyAuthUrl, _e = _a.ablyAuthMethod, ablyAuthMethod = _e === void 0 ? 'POST' : _e, _f = _a.ablyAuthHeaders, ablyAuthHeaders = _f === void 0 ? { 'Content-Type': 'application/json' } : _f, ablyAuthParams = _a.ablyAuthParams, _g = _a.pollInterval, pollInterval = _g === void 0 ? 2000 : _g, _h = _a.statusEndpoint, statusEndpoint = _h === void 0 ? '/api/payment-status' : _h;
var _j = (0, react_1.useState)(payment.status), currentStatus = _j[0], setCurrentStatus = _j[1];
var _k = (0, react_1.useState)(3600), timeLeft = _k[0], setTimeLeft = _k[1];
var _l = (0, react_1.useState)(false), copySuccess = _l[0], setCopySuccess = _l[1];
var _m = (0, react_1.useState)(false), socketConnected = _m[0], setSocketConnected = _m[1];
var _o = (0, react_1.useState)(false), sseConnected = _o[0], setSSEConnected = _o[1];
var _p = (0, react_1.useState)(false), ablyConnected = _p[0], setAblyConnected = _p[1];
var socketRef = (0, react_1.useRef)(null);
var eventSourceRef = (0, react_1.useRef)(null);
var ablyRef = (0, react_1.useRef)(null);
// Setup Ably connection for real-time updates
(0, react_1.useEffect)(function () {
if (!useAbly || currentStatus !== 'pending')
return;
// Check if we have either API key or auth URL
if (!ablyApiKey && !ablyAuthUrl) {
console.warn('Ably enabled but no authentication method provided');
return;
}
var ably;
if (ablyAuthUrl) {
// Use secure token authentication (recommended)
ably = new ably_1.default.Realtime({
authUrl: ablyAuthUrl,
authMethod: ablyAuthMethod,
authHeaders: ablyAuthHeaders,
authParams: __assign({ paymentId: payment.id }, ablyAuthParams)
});
}
else {
// Fallback to API key (not recommended for client-side)
console.warn('Using Ably API key directly in client-side code is not recommended. Use ablyAuthUrl instead.');
ably = new ably_1.default.Realtime(ablyApiKey);
}
ablyRef.current = ably;
ably.connection.on('connected', function () {
setAblyConnected(true);
});
ably.connection.on('disconnected', function () {
setAblyConnected(false);
});
var channel = ably.channels.get("payment-".concat(payment.id));
// Subscribe to payment-success event (sent by business webhooks)
channel.subscribe('payment-success', function (message) {
var data = message.data;
if (data.paymentId === payment.id) {
if (data.event === 'payment.completed') {
setCurrentStatus('completed');
if (onPaymentSuccess) {
onPaymentSuccess(__assign(__assign({}, payment), { status: 'completed' }));
}
// Auto-close modal after 3 seconds
setTimeout(function () {
onClose();
}, 3000);
}
else if (data.event === 'payment.failed') {
setCurrentStatus('failed');
if (onPaymentError) {
onPaymentError(new Error(data.reason || 'Payment failed'));
}
}
else if (data.event === 'payment.expired') {
setCurrentStatus('expired');
if (onPaymentError) {
onPaymentError(new Error('Payment expired'));
}
}
}
});
return function () {
if (ablyRef.current) {
ablyRef.current.connection.close();
ablyRef.current = null;
}
};
}, [payment.id, useAbly, ablyApiKey, currentStatus, onPaymentSuccess, onPaymentError]);
// Fallback: WebSocket connection for non-Vercel environments
(0, react_1.useEffect)(function () {
if (!useWebhooks || useAbly || useSSE || !webhookUrl || currentStatus !== 'pending')
return;
var socket = (0, socket_io_client_1.io)(webhookUrl);
socketRef.current = socket;
socket.on('connect', function () {
setSocketConnected(true);
socket.emit('join-payment-room', payment.id);
});
socket.on('disconnect', function () { return setSocketConnected(false); });
socket.on('payment-completed', function (data) {
if (data.paymentId === payment.id) {
setCurrentStatus('completed');
if (onPaymentSuccess)
onPaymentSuccess(__assign(__assign({}, payment), { status: 'completed' }));
// Auto-close modal after 3 seconds
setTimeout(function () {
onClose();
}, 3000);
}
});
return function () {
if (socketRef.current) {
socketRef.current.disconnect();
socketRef.current = null;
}
};
}, [payment.id, useWebhooks, useAbly, useSSE, webhookUrl, currentStatus]);
// Fallback: Server-Sent Events
(0, react_1.useEffect)(function () {
if (!useSSE || useAbly || currentStatus !== 'pending')
return;
var eventSource = new EventSource("/api/payments/stream?paymentId=".concat(payment.id));
eventSourceRef.current = eventSource;
eventSource.onopen = function () { return setSSEConnected(true); };
eventSource.onerror = function () { return setSSEConnected(false); };
eventSource.onmessage = function (event) {
try {
var data = JSON.parse(event.data);
if (data.type === 'payment-update' && data.paymentId === payment.id) {
if (data.event === 'payment.completed') {
setCurrentStatus('completed');
if (onPaymentSuccess)
onPaymentSuccess(__assign(__assign({}, payment), { status: 'completed' }));
// Auto-close modal after 3 seconds
setTimeout(function () {
onClose();
}, 3000);
}
}
}
catch (error) {
console.error('SSE parsing error:', error);
}
};
return function () {
if (eventSourceRef.current) {
eventSourceRef.current.close();
eventSourceRef.current = null;
}
};
}, [payment.id, useSSE, useAbly, currentStatus]);
// Final fallback: Polling
(0, react_1.useEffect)(function () {
if (useWebhooks || useSSE || useAbly || currentStatus !== 'pending')
return;
var pollStatus = function () { return __awaiter(void 0, void 0, void 0, function () {
var response, statusData, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4, , 5]);
return [4 /*yield*/, fetch("".concat(statusEndpoint, "/").concat(payment.id))];
case 1:
response = _a.sent();
if (!response.ok) return [3 /*break*/, 3];
return [4 /*yield*/, response.json()];
case 2:
statusData = _a.sent();
setCurrentStatus(statusData.status);
if (statusData.status === 'completed' && onPaymentSuccess) {
onPaymentSuccess(statusData);
// Auto-close modal after 3 seconds
setTimeout(function () {
onClose();
}, 3000);
}
_a.label = 3;
case 3: return [3 /*break*/, 5];
case 4:
error_1 = _a.sent();
console.error('Polling error:', error_1);
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
}); };
var interval = setInterval(pollStatus, pollInterval);
return function () { return clearInterval(interval); };
}, [payment.id, currentStatus, useWebhooks, useSSE, useAbly]);
// Countdown timer
(0, react_1.useEffect)(function () {
if (currentStatus !== 'pending')
return;
var timer = setInterval(function () {
setTimeLeft(function (prev) {
if (prev <= 1) {
setCurrentStatus('expired');
if (onPaymentError)
onPaymentError(new Error('Payment expired'));
return 0;
}
return prev - 1;
});
}, 1000);
return function () { return clearInterval(timer); };
}, [currentStatus, onPaymentError]);
var handleCopyInvoice = function () { return __awaiter(void 0, void 0, void 0, function () {
var error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, navigator.clipboard.writeText(payment.invoice)];
case 1:
_a.sent();
setCopySuccess(true);
setTimeout(function () { return setCopySuccess(false); }, 2000);
return [3 /*break*/, 3];
case 2:
error_2 = _a.sent();
console.error('Copy failed:', error_2);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
var formatTime = function (seconds) {
var mins = Math.floor(seconds / 60);
var secs = seconds % 60;
return "".concat(mins.toString().padStart(2, '0'), ":").concat(secs.toString().padStart(2, '0'));
};
var overlayStyle = {
position: 'fixed', top: 0, left: 0, right: 0, bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.6)', display: 'flex', alignItems: 'center',
justifyContent: 'center', zIndex: 10000, backdropFilter: 'blur(4px)'
};
var modalStyle = {
backgroundColor: 'white', borderRadius: '12px', padding: '32px',
maxWidth: '400px', width: '90%', textAlign: 'center',
boxShadow: '0 20px 60px rgba(0, 0, 0, 0.3)', position: 'relative'
};
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("style", { children: "\n @keyframes fadeIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }\n @keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }\n " }), (0, jsx_runtime_1.jsx)("div", { className: "nakapay-modal-overlay", style: overlayStyle, onClick: onClose, children: (0, jsx_runtime_1.jsxs)("div", { className: "nakapay-modal", style: modalStyle, onClick: function (e) { return e.stopPropagation(); }, children: [(0, jsx_runtime_1.jsx)("button", { onClick: onClose, style: {
position: 'absolute', top: '16px', right: '16px', background: 'none',
border: 'none', fontSize: '24px', cursor: 'pointer', color: '#666'
}, children: "\u00D7" }), (0, jsx_runtime_1.jsxs)("div", { style: { marginBottom: '24px' }, children: [(0, jsx_runtime_1.jsx)("h3", { style: { margin: '0 0 16px 0', fontSize: '20px', fontWeight: '600', color: '#1a1a1a' }, children: "Lightning Payment" }), (0, jsx_runtime_1.jsxs)("p", { style: { margin: '0', color: '#666', fontSize: '16px' }, children: [payment.amount, " sats (~$", (payment.amount * 0.0005).toFixed(2), ")"] }), (0, jsx_runtime_1.jsx)("p", { style: { margin: '8px 0 0 0', color: '#888', fontSize: '14px' }, children: payment.description })] }), currentStatus === 'pending' && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { style: {
margin: '24px 0', padding: '16px', borderRadius: '8px',
background: 'linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%)',
border: '2px solid #e2e8f0',
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}, children: (function () {
try {
return (0, jsx_runtime_1.jsx)(qrcode_react_1.default, { value: payment.invoice, size: 200, style: { borderRadius: '8px' } });
}
catch (error) {
console.error('QR Code generation failed:', error);
// Fallback: show invoice text with instruction to copy
return ((0, jsx_runtime_1.jsxs)("div", { style: {
padding: '20px',
textAlign: 'center',
background: '#f0f0f0',
borderRadius: '8px',
width: '100%'
}, children: [(0, jsx_runtime_1.jsx)("div", { style: { marginBottom: '10px', fontSize: '14px', color: '#666' }, children: "QR code unavailable - copy invoice below" }), (0, jsx_runtime_1.jsx)("div", { style: {
fontSize: '12px',
fontFamily: 'monospace',
wordBreak: 'break-all',
background: 'white',
padding: '10px',
borderRadius: '4px',
border: '1px solid #ddd'
}, children: payment.invoice })] }));
}
})() }), (0, jsx_runtime_1.jsx)("div", { style: {
margin: '16px 0', padding: '12px', background: '#f5f5f5', borderRadius: '6px',
fontFamily: 'monospace', fontSize: '10px', wordBreak: 'break-all',
color: '#333', maxHeight: '60px', overflowY: 'auto'
}, children: payment.invoice }), (0, jsx_runtime_1.jsx)("button", { onClick: handleCopyInvoice, style: {
width: '100%', padding: '12px', margin: '16px 0',
background: copySuccess ? '#10B981' : '#F7931A', color: 'white',
border: 'none', borderRadius: '6px', fontWeight: '600',
cursor: 'pointer', transition: 'background 0.2s ease'
}, children: copySuccess ? 'Copied!' : 'Copy Invoice' }), (0, jsx_runtime_1.jsx)("div", { style: {
margin: '16px 0', padding: '12px', background: '#EBF8FF',
border: '1px solid #BEE3F8', borderRadius: '6px', color: '#2B6CB0', fontSize: '14px'
}, children: (0, jsx_runtime_1.jsxs)("div", { style: { display: 'flex', alignItems: 'center', justifyContent: 'center' }, children: [(0, jsx_runtime_1.jsx)("div", { style: {
width: '8px', height: '8px', borderRadius: '50%', marginRight: '8px',
background: ablyConnected ? '#10B981' : socketConnected ? '#10B981' : sseConnected ? '#10B981' : '#F59E0B',
animation: 'pulse 2s infinite'
} }), useAbly ? (ablyConnected ? "\u26A1 Real-time connected (".concat(formatTime(timeLeft), ")") : 'Connecting to Ably...') : useWebhooks ? (socketConnected ? "Waiting for payment... (".concat(formatTime(timeLeft), ")") : 'Connecting...') : useSSE ? (sseConnected ? "Waiting for payment... (".concat(formatTime(timeLeft), ")") : 'Connecting...') : "Checking payment... (".concat(formatTime(timeLeft), ")")] }) })] })), currentStatus === 'completed' && ((0, jsx_runtime_1.jsxs)("div", { style: {
background: 'linear-gradient(135deg, #D1FAE5 0%, #A7F3D0 100%)',
border: '2px solid #10B981', borderRadius: '12px', padding: '32px',
margin: '16px 0', animation: 'fadeIn 0.5s ease-out'
}, children: [(0, jsx_runtime_1.jsx)("div", { style: { fontSize: '48px', marginBottom: '16px' }, children: "\uD83C\uDF89" }), (0, jsx_runtime_1.jsx)("h3", { style: { color: '#065F46', fontSize: '24px', marginBottom: '12px' }, children: "Payment Successful!" }), (0, jsx_runtime_1.jsxs)("p", { style: { color: '#047857', fontSize: '16px', margin: '0' }, children: ["Your payment of ", payment.amount, " sats has been confirmed."] }), (0, jsx_runtime_1.jsx)("p", { style: { color: '#047857', fontSize: '14px', margin: '12px 0 0 0', opacity: 0.8 }, children: "This modal will close automatically in 3 seconds..." })] })), (currentStatus === 'failed' || currentStatus === 'expired') && ((0, jsx_runtime_1.jsxs)("div", { style: {
background: 'linear-gradient(135deg, #FEE2E2 0%, #FECACA 100%)',
border: '2px solid #EF4444', borderRadius: '12px', padding: '32px',
margin: '16px 0', animation: 'fadeIn 0.5s ease-out'
}, children: [(0, jsx_runtime_1.jsx)("div", { style: { fontSize: '48px', marginBottom: '16px' }, children: "\u274C" }), (0, jsx_runtime_1.jsxs)("h3", { style: { color: '#7F1D1D', fontSize: '24px', marginBottom: '12px' }, children: ["Payment ", currentStatus === 'failed' ? 'Failed' : 'Expired'] }), (0, jsx_runtime_1.jsx)("p", { style: { color: '#991B1B', fontSize: '16px', margin: '0' }, children: currentStatus === 'failed' ? 'The payment could not be completed.' : 'The payment request has expired.' })] }))] }) })] }));
};
exports.NakaPayModal = NakaPayModal;