-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucashpay-button.js
More file actions
189 lines (170 loc) · 6.5 KB
/
Copy pathucashpay-button.js
File metadata and controls
189 lines (170 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* ucashpay-button.js
*
* Drop-in snippet for Teachable's "Code Snippets" feature
* (Settings -> Code Snippets), or any page where you can inject HTML/JS.
*
* Renders a "Pay with U.CASH" button next to an external offer price and
* opens the U.CASH hosted checkout (embed.php) in a new tab on click.
*
* Non-custodial: the Store Cloud Token is publishable (safe to expose in the
* browser). U.CASH never holds funds; payments settle directly to the receive
* addresses you configure in your pay.u.cash store.
*
* ----------------------------------------------------------------------
* Contract used (client-side hosted pay link):
* GET https://pay.u.cash/embed.php?cloud=<CLOUD>&amount=<AMOUNT>¤cy=<CCY>
* &title=<TITLE>&external_reference=<REF>&redirect=<REDIRECT>
* ----------------------------------------------------------------------
*/
(function () {
'use strict';
// Optional global config: set window.UCASHPAY_CONFIG once before this script
// loads, or rely on data-* attributes on each button (recommended).
var GLOBAL_CONFIG = (typeof window !== 'undefined' && window.UCASHPAY_CONFIG) || {};
var PAY_BASE = 'https://pay.u.cash/embed.php';
function safe(value, fallback) {
var v = (value === null || value === undefined) ? '' : String(value).trim();
return v === '' ? (fallback || '') : v;
}
function buildPayUrl(opts) {
var params = {
cloud: safe(opts.cloud),
amount: safe(opts.amount),
currency: safe(opts.currency, 'USD'),
title: safe(opts.title, 'Pay with U.CASH'),
external_reference: safe(opts.external_reference, ''),
redirect: safe(opts.redirect, window.location ? window.location.href : '')
};
if (!params.cloud) {
throw new Error('[U.CASH] Missing required "cloud" (Store Cloud Token).');
}
if (!params.amount || isNaN(Number(params.amount)) || Number(params.amount) <= 0) {
throw new Error('[U.CASH] Missing or invalid "amount".');
}
var qs = [];
for (var k in params) {
if (params.hasOwnProperty(k) && params[k] !== '') {
qs.push(encodeURIComponent(k) + '=' + encodeURIComponent(params[k]));
}
}
return PAY_BASE + '?' + qs.join('&');
}
function resolveOptions(button) {
// data-* attributes win, then per-element config, then global config.
var ds = button.dataset || {};
return {
cloud: safe(ds.cloud, GLOBAL_CONFIG.cloud),
amount: safe(ds.amount, GLOBAL_CONFIG.amount),
currency: safe(ds.currency, GLOBAL_CONFIG.currency),
title: safe(ds.title, GLOBAL_CONFIG.title),
external_reference: safe(ds.externalReference || ds.externalreference, GLOBAL_CONFIG.externalReference),
redirect: safe(ds.redirect, GLOBAL_CONFIG.redirect)
};
}
function applyButtonStyle(button, label) {
button.type = 'button';
button.innerHTML = '';
var span = document.createElement('span');
span.textContent = label || 'Pay with U.CASH';
button.appendChild(span);
button.style.display = 'inline-flex';
button.style.alignItems = 'center';
button.style.justifyContent = 'center';
button.style.gap = '6px';
button.style.padding = '12px 22px';
button.style.fontSize = '15px';
button.style.fontWeight = '600';
button.style.fontFamily = 'system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
button.style.color = '#ffffff';
button.style.background = 'linear-gradient(135deg, #0a0a0a 0%, #2b2b2b 100%)';
button.style.border = '1px solid #1a1a1a';
button.style.borderRadius = '8px';
button.style.cursor = 'pointer';
button.style.textDecoration = 'none';
button.style.lineHeight = '1';
button.style.boxShadow = '0 1px 2px rgba(0,0,0,0.08)';
button.style.transition = 'transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease';
}
function attach(button) {
if (button.__ucashpayBound) return;
button.__ucashpayBound = true;
var opts = resolveOptions(button);
applyButtonStyle(button, opts.title);
button.addEventListener('mouseenter', function () {
button.style.transform = 'translateY(-1px)';
button.style.boxShadow = '0 4px 12px rgba(0,0,0,0.15)';
});
button.addEventListener('mouseleave', function () {
button.style.transform = '';
button.style.boxShadow = '0 1px 2px rgba(0,0,0,0.08)';
});
button.addEventListener('click', function (e) {
e.preventDefault();
try {
var url = buildPayUrl(opts);
window.open(url, '_blank', 'noopener,noreferrer');
} catch (err) {
// Surface configuration errors so merchants can see what is wrong.
console.error(err.message);
if (button.reportValidity) {
button.setCustomValidity && button.setCustomValidity(err.message);
}
}
});
}
function init(scope) {
var root = scope || document;
// Primary: declarative buttons.
var buttons = root.querySelectorAll('[data-ucashpay]');
buttons.forEach(function (b) { attach(b); });
// Convenience: render a button into a placeholder container.
var slots = root.querySelectorAll('[data-ucashpay-slot]');
slots.forEach(function (slot) {
if (slot.querySelector('[data-ucashpay]')) return;
var btn = document.createElement('button');
btn.setAttribute('data-ucashpay', '');
for (var i = 0; i < slot.attributes.length; i++) {
var attr = slot.attributes[i];
if (attr.name.indexOf('data-') === 0 && attr.name !== 'data-ucashpay-slot') {
btn.setAttribute(attr.name, attr.value);
}
}
attach(btn);
slot.appendChild(btn);
});
}
function onReady(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
// Auto-init on load.
onReady(function () { init(document); });
// Expose a tiny API for dynamic rendering (e.g. after a SPA route change).
window.UCASHPAY = {
init: init,
buildPayUrl: buildPayUrl,
render: function (target, opts) {
var btn = target instanceof HTMLElement
? target
: document.querySelector(target);
if (!btn) return;
if (opts) {
for (var k in opts) {
if (opts.hasOwnProperty(k)) {
var attr = ({
externalReference: 'data-external-reference'
})[k] || ('data-' + k.replace(/([A-Z])/g, '-$1').toLowerCase());
btn.setAttribute(attr, opts[k]);
}
}
}
btn.setAttribute('data-ucashpay', '');
attach(btn);
return btn;
}
};
})();