-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
303 lines (267 loc) · 12.4 KB
/
Copy pathindex.html
File metadata and controls
303 lines (267 loc) · 12.4 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Global Payments - Developer Example</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://globalpayments-samples.github.io/css/styles.css">
</head>
<body>
<!-- Header -->
<header class="gp-header">
<div class="gp-container">
<div class="gp-header-content">
<div class="gp-logo">
<picture>
<source media="(min-width: 768px)" srcset="https://globalpayments-samples.github.io/assets/img/GP_logo.svg">
<img src="https://globalpayments-samples.github.io/assets/img/GP_favicon.svg" alt="Global Payments" style="height: 32px; width: auto;">
</picture>
</div>
<nav class="gp-header-nav">
<a href="https://developer.globalpay.com/">Documentation</a>
<a href="https://developer.globalpay.com/api/references-overview">API Reference</a>
<a href="https://developer.globalpay.com/docs/integration-options/sdk/overview">SDKs</a>
</nav>
</div>
</div>
</header>
<div class="gp-container">
<!-- Page Title -->
<h1 class="gp-page-title">SDK Integration Examples</h1>
<p class="gp-page-subtitle">Explore comprehensive examples of Global Payments SDK implementations across different payment scenarios.</p>
<!-- Tabs -->
<div class="gp-tabs">
<div class="gp-tab-list">
<button class="gp-tab-button active" data-tab="payment-form-wrapper">Payment Form</button>
</div>
<!-- Payment Form Tab -->
<div class="gp-tab-content active" id="payment-form-wrapper">
<div class="gp-card">
<div class="gp-card-header">
<h2 class="gp-card-title">Credit Card Payment Form</h2>
<p class="gp-card-description">
Secure payment processing using Global Payments tokenization with client-side form validation.
</p>
</div>
<form id="payment-form" class="gp-form" action="/process-payment.php" method="POST">
<div class="gp-form-group">
<label for="amount" class="gp-label">Amount ($):</label>
<input type="number" id="amount" name="amount" class="gp-input" min="0.01" step="0.01" value="10.00" required>
</div>
<div class="gp-form-group">
<label for="billing_zip" class="gp-label">Billing Zip Code:</label>
<input type="text" id="billing_zip" name="billing_zip" class="gp-input" value="12345" placeholder="12345" required>
</div>
<div id="credit-card-form"></div>
</form>
</div>
</div>
</div>
</div>
<!-- Footer -->
<!--
DEVELOPER NOTE: Update the following footer information to match your business:
- Company name and copyright year
- Update all links to point to your actual Terms of Service, Privacy Policy, and Refund Policy pages
- Refund Policy link is typically required by payment processor underwriting in addition to Terms and Privacy
- Ensure all linked policies are accessible and up-to-date before going live
-->
<footer class="gp-footer">
<div class="gp-container">
<div class="gp-footer-content">
<div class="gp-footer-section">
<p class="gp-footer-copyright">
© 2024 Your Company Name. All rights reserved.
</p>
</div>
<div class="gp-footer-section">
<nav class="gp-footer-nav">
<a href="/terms-of-service" class="gp-footer-link">Terms of Service</a>
<a href="/privacy-policy" class="gp-footer-link">Privacy Policy</a>
<a href="/refund-policy" class="gp-footer-link">Refund Policy</a>
</nav>
</div>
</div>
</div>
</footer>
<script src="https://js.paygateway.com/secure_payment/v1/globalpayments.js"></script>
<script>
// Global Payments Template JavaScript
class GPTemplate {
constructor() {
this.config = null;
this.cardForms = {};
this.init();
}
async init() {
await this.loadConfig();
await this.initPaymentForms();
}
async loadConfig() {
try {
// In a real implementation, this would fetch from your backend
// For demo purposes, we'll use a placeholder
await ensureGlobalPaymentsReady();
const config = await fetchWithRetry('/config.php');
if (typeof GlobalPayments !== 'undefined') {
GlobalPayments.configure({
'X-GP-Api-Key': config.data.apiKey,
'X-GP-Environment': 'test',
'enableAutoComplete': true
});
}
} catch (error) {
console.error('Failed to load configuration:', error);
this.showAlert('error', 'Failed to initialize payment system. Please refresh the page.');
}
}
async initPaymentForms() {
// Initialize the default active tab
await this.initCardFormForTab('payment-form');
}
async initCardFormForTab(tabId) {
if (this.cardForms[tabId] || typeof GlobalPayments === 'undefined') {
return;
}
let containerId;
switch (tabId) {
case 'payment-form':
containerId = '#credit-card-form';
break;
default:
return;
}
try {
const cardForm = GlobalPayments.creditCard.form(containerId, { style: 'gp-default' });
cardForm.on('token-success', (resp) => {
this.handleTokenSuccess(resp, tabId);
});
cardForm.on('token-error', (error) => {
this.handleTokenError(error, tabId);
});
this.cardForms[tabId] = cardForm;
} catch (error) {
console.error(`Failed to initialize card form for ${tabId}:`, error);
}
}
handleTokenSuccess(resp, tabId) {
const form = document.getElementById(this.getFormIdForTab(tabId));
this.setLoading(form, false);
if (tabId === 'tokenization') {
// Show token result
this.displayToken(resp.temporary_token);
this.showAlert('success', 'Card tokenized successfully!');
} else {
// Add token to form and submit
this.addTokenToForm(form, resp.temporary_token);
// In a real implementation, you would submit to your backend
form.submit();
}
}
handleTokenError(error, tabId) {
const form = document.getElementById(this.getFormIdForTab(tabId));
this.setLoading(form, false);
this.showAlert('error', `Error: ${error.message}`);
}
getFormIdForTab(tabId) {
const formMap = {
'payment-form': 'payment-form',
};
return formMap[tabId];
}
addTokenToForm(form, token) {
// Remove existing token input
const existingToken = form.querySelector('input[name="payment_token"]');
if (existingToken) {
existingToken.remove();
}
// Add new token input
const tokenInput = document.createElement('input');
tokenInput.type = 'hidden';
tokenInput.name = 'payment_token';
tokenInput.value = token;
form.appendChild(tokenInput);
}
displayToken(token) {
const tokenResult = document.getElementById('token-result');
const tokenDisplay = document.getElementById('token-display');
tokenDisplay.textContent = token;
tokenResult.classList.remove('gp-hidden');
}
setLoading(element, isLoading) {
if (isLoading) {
element.classList.add('gp-loading');
} else {
element.classList.remove('gp-loading');
}
}
showAlert(type, message) {
// Remove existing alerts
const existingAlerts = document.querySelectorAll('.gp-alert');
existingAlerts.forEach(alert => {
if (!alert.classList.contains('gp-alert-info') || alert.textContent.includes('Info:')) {
return; // Keep static info alerts
}
alert.remove();
});
// Create new alert
const alert = document.createElement('div');
alert.className = `gp-alert gp-alert-${type}`;
alert.textContent = message;
// Insert after the first card header
const firstCard = document.querySelector('.gp-tab-content.active .gp-card');
if (firstCard) {
const cardHeader = firstCard.querySelector('.gp-card-header');
if (cardHeader) {
cardHeader.after(alert);
}
}
// Auto-remove after 5 seconds
setTimeout(() => {
alert.remove();
}, 5000);
}
}
// Initialize template when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
new GPTemplate();
});
// Helper function to ensure GlobalPayments is loaded
function ensureGlobalPaymentsReady() {
return new Promise((resolve, reject) => {
if (typeof GlobalPayments !== 'undefined') {
resolve();
} else {
let attempts = 0;
const checkInterval = setInterval(() => {
attempts++;
if (typeof GlobalPayments !== 'undefined') {
clearInterval(checkInterval);
resolve();
} else if (attempts >= 10) {
clearInterval(checkInterval);
reject(new Error('GlobalPayments failed to initialize'));
}
}, 500);
}
});
}
// Fetch with retry logic
async function fetchWithRetry(url, maxAttempts = 3) {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return await response.json();
} catch (error) {
if (attempt === maxAttempts) throw error;
await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 100));
}
}
}
</script>
</body>
</html>