| 2 |
- |
1 |
<html>
|
|
|
2 |
<head>
|
|
|
3 |
<link href="/style.css" rel="stylesheet" />
|
|
|
4 |
<script
|
|
|
5 |
type="text/javascript"
|
|
|
6 |
src="https://sandbox.web.squarecdn.com/v1/square.js"
|
|
|
7 |
></script>
|
|
|
8 |
<script>
|
|
|
9 |
const appId = 'sandbox-sq0idb-r8fSW7557g5H5dQjlr1MVQ';
|
|
|
10 |
const locationId = 'L43GY2FSPNWDF';
|
|
|
11 |
|
|
|
12 |
async function initializeCard(payments) {
|
|
|
13 |
const card = await payments.card();
|
|
|
14 |
await card.attach('#card-container');
|
|
|
15 |
|
|
|
16 |
return card;
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
async function createPayment(token) {
|
|
|
20 |
const body = JSON.stringify({
|
|
|
21 |
locationId,
|
|
|
22 |
sourceId: token,
|
|
|
23 |
idempotencyKey: window.crypto.randomUUID(),
|
|
|
24 |
});
|
|
|
25 |
|
|
|
26 |
const paymentResponse = await fetch('/payment', {
|
|
|
27 |
method: 'POST',
|
|
|
28 |
headers: {
|
|
|
29 |
'Content-Type': 'application/json',
|
|
|
30 |
},
|
|
|
31 |
body,
|
|
|
32 |
});
|
|
|
33 |
|
|
|
34 |
if (paymentResponse.ok) {
|
|
|
35 |
return paymentResponse.json();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
const errorBody = await paymentResponse.text();
|
|
|
39 |
throw new Error(errorBody);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// New payment flow
|
|
|
43 |
async function tokenize(paymentMethod) {
|
|
|
44 |
const verificationDetails = {
|
|
|
45 |
amount: '1.00',
|
|
|
46 |
billingContact: {
|
|
|
47 |
givenName: 'John',
|
|
|
48 |
familyName: 'Doe',
|
|
|
49 |
email: 'john.doe@square.example',
|
|
|
50 |
phone: '3214563987',
|
|
|
51 |
addressLines: ['123 Main Street', 'Apartment 1'],
|
|
|
52 |
city: 'Tacoma',
|
|
|
53 |
state: 'WA',
|
|
|
54 |
countryCode: 'US',
|
|
|
55 |
},
|
|
|
56 |
currencyCode: 'USD',
|
|
|
57 |
intent: 'CHARGE',
|
|
|
58 |
customerInitiated: true,
|
|
|
59 |
sellerKeyedIn: false,
|
|
|
60 |
};
|
|
|
61 |
const tokenResult = await paymentMethod.tokenize(verificationDetails);
|
|
|
62 |
if (tokenResult.status === 'OK') {
|
|
|
63 |
return tokenResult.token;
|
|
|
64 |
} else {
|
|
|
65 |
let errorMessage = `Tokenization failed with status: ${tokenResult.status}`;
|
|
|
66 |
if (tokenResult.errors) {
|
|
|
67 |
errorMessage += ` and errors: ${JSON.stringify(
|
|
|
68 |
tokenResult.errors,
|
|
|
69 |
)}`;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
throw new Error(errorMessage);
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// status is either SUCCESS or FAILURE;
|
|
|
77 |
function displayPaymentResults(status) {
|
|
|
78 |
const statusContainer = document.getElementById(
|
|
|
79 |
'payment-status-container',
|
|
|
80 |
);
|
|
|
81 |
if (status === 'SUCCESS') {
|
|
|
82 |
statusContainer.classList.remove('is-failure');
|
|
|
83 |
statusContainer.classList.add('is-success');
|
|
|
84 |
} else {
|
|
|
85 |
statusContainer.classList.remove('is-success');
|
|
|
86 |
statusContainer.classList.add('is-failure');
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
statusContainer.style.visibility = 'visible';
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
document.addEventListener('DOMContentLoaded', async function () {
|
|
|
93 |
if (!window.Square) {
|
|
|
94 |
throw new Error('Square.js failed to load properly');
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
let payments;
|
|
|
98 |
try {
|
|
|
99 |
payments = window.Square.payments(appId, locationId);
|
|
|
100 |
} catch {
|
|
|
101 |
const statusContainer = document.getElementById(
|
|
|
102 |
'payment-status-container',
|
|
|
103 |
);
|
|
|
104 |
statusContainer.className = 'missing-credentials';
|
|
|
105 |
statusContainer.style.visibility = 'visible';
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
let card;
|
|
|
110 |
try {
|
|
|
111 |
card = await initializeCard(payments);
|
|
|
112 |
} catch (e) {
|
|
|
113 |
console.error('Initializing Card failed', e);
|
|
|
114 |
return;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
async function handlePaymentMethodSubmission(event, card) {
|
|
|
118 |
event.preventDefault();
|
|
|
119 |
|
|
|
120 |
try {
|
|
|
121 |
// disable the submit button as we await tokenization and make a payment request.
|
|
|
122 |
cardButton.disabled = true;
|
|
|
123 |
const token = await tokenize(card);
|
|
|
124 |
const paymentResults = await createPayment(token);
|
|
|
125 |
displayPaymentResults('SUCCESS');
|
|
|
126 |
|
|
|
127 |
console.debug('Payment Success', paymentResults);
|
|
|
128 |
} catch (e) {
|
|
|
129 |
cardButton.disabled = false;
|
|
|
130 |
displayPaymentResults('FAILURE');
|
|
|
131 |
console.error(e.message);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
const cardButton = document.getElementById('card-button');
|
|
|
136 |
cardButton.addEventListener('click', async function (event) {
|
|
|
137 |
await handlePaymentMethodSubmission(event, card);
|
|
|
138 |
});
|
|
|
139 |
});
|
|
|
140 |
</script>
|
|
|
141 |
</head>
|
|
|
142 |
<body>
|
|
|
143 |
<form id="payment-form">
|
|
|
144 |
<div id="card-container"></div>
|
|
|
145 |
<button id="card-button" type="button">Pay $1.00</button>
|
|
|
146 |
</form>
|
|
|
147 |
<div id="payment-status-container"></div>
|
|
|
148 |
</body>
|
|
|
149 |
</html>
|