Skip to content

Commit 500ebad

Browse files
committed
feat: Add CashAppPay walkthrough example
1 parent ea08e08 commit 500ebad

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

public/examples/cash-app-pay.html

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1" />
5+
<link href="/app.css" rel="stylesheet" />
6+
<script
7+
type="text/javascript"
8+
src="https://sandbox.web.squarecdn.com/v1/square.js"
9+
></script>
10+
<script>
11+
const appId = '{APPLICATION_ID}';
12+
const locationId = '{LOCATION_ID}';
13+
14+
function buildPaymentRequest(payments) {
15+
const paymentRequest = payments.paymentRequest({
16+
countryCode: 'US',
17+
currencyCode: 'USD',
18+
total: {
19+
amount: '1.00',
20+
label: 'Total',
21+
},
22+
});
23+
return paymentRequest;
24+
}
25+
26+
async function initializeCashApp(payments) {
27+
const paymentRequest = buildPaymentRequest(payments);
28+
const cashAppPay = await payments.cashAppPay(paymentRequest, {
29+
redirectURL: window.location.href,
30+
referenceId: 'my-website-00000001',
31+
});
32+
const buttonOptions = {
33+
shape: 'semiround',
34+
width: 'full',
35+
};
36+
await cashAppPay.attach('#cash-app-pay', buttonOptions);
37+
return cashAppPay;
38+
}
39+
40+
async function createPayment(token) {
41+
const body = JSON.stringify({
42+
locationId,
43+
sourceId: token,
44+
});
45+
46+
const paymentResponse = await fetch('/payment', {
47+
method: 'POST',
48+
headers: {
49+
'Content-Type': 'application/json',
50+
},
51+
body,
52+
});
53+
54+
if (paymentResponse.ok) {
55+
return paymentResponse.json();
56+
}
57+
58+
const errorBody = await paymentResponse.text();
59+
throw new Error(errorBody);
60+
}
61+
62+
// status is either SUCCESS or FAILURE;
63+
function displayPaymentResults(status) {
64+
const statusContainer = document.getElementById(
65+
'payment-status-container'
66+
);
67+
if (status === 'SUCCESS') {
68+
statusContainer.classList.remove('is-failure');
69+
statusContainer.classList.add('is-success');
70+
} else {
71+
statusContainer.classList.remove('is-success');
72+
statusContainer.classList.add('is-failure');
73+
}
74+
75+
statusContainer.style.visibility = 'visible';
76+
}
77+
78+
document.addEventListener('DOMContentLoaded', async function () {
79+
if (!window.Square) {
80+
throw new Error('Square.js failed to load properly');
81+
}
82+
83+
let payments;
84+
try {
85+
payments = window.Square.payments(appId, locationId);
86+
} catch {
87+
const statusContainer = document.getElementById(
88+
'payment-status-container'
89+
);
90+
statusContainer.className = 'missing-credentials';
91+
statusContainer.style.visibility = 'visible';
92+
return;
93+
}
94+
95+
let cashAppPay;
96+
try {
97+
cashAppPay = await initializeCashApp(payments);
98+
} catch (e) {
99+
console.error('Initializing Cash App Pay failed', e);
100+
}
101+
if (cashAppPay) {
102+
cashAppPay.addEventListener(
103+
'ontokenization',
104+
async function ({ detail }) {
105+
const tokenResult = detail.tokenResult;
106+
if (tokenResult.status === 'OK') {
107+
const paymentResults = await createPayment(tokenResult.token);
108+
109+
displayPaymentResults('SUCCESS');
110+
console.debug('Payment Success', paymentResults);
111+
} else {
112+
let errorMessage = `Tokenization failed with status: ${tokenResult.status}`;
113+
114+
if (tokenResult.errors) {
115+
errorMessage += ` and errors: ${JSON.stringify(
116+
tokenResult.errors
117+
)}`;
118+
}
119+
displayPaymentResults('FAILURE');
120+
throw new Error(errorMessage);
121+
}
122+
}
123+
);
124+
}
125+
});
126+
</script>
127+
</head>
128+
<body>
129+
<form id="payment-form">
130+
<h2>Pay $1.00</h2>
131+
<div id="cash-app-pay"></div>
132+
</form>
133+
<div id="payment-status-container"></div>
134+
</body>
135+
</html>

public/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ <h3>Examples</h3>
7777
<li>
7878
<a href="/examples/card-styling-simple.html">Card Styling</a>
7979
</li>
80+
<li>
81+
<a href="/examples/cash-app-pay.html">Cash App Pay</a>
82+
</li>
8083
</ul>
8184
</div>
8285
</div>

0 commit comments

Comments
 (0)