I’ve been able to make the button show on my website but it only creates payment token and shows it to the customer… Is there a way to initiate this payment without using node js? All the tutorials i see is using npm and react but i’m working on static HTML single page, please help me fix this code.
I should be sending the token to my backend but i don’t have a backend. The web page is a donation page, just a single static html where donator will input amount and click donate.
I Believe i am supposed to use my access token to initiate payment somewhere but i don’t know how to handle the payment without a backend.
<script src="https://web.squarecdn.com/v1/square.js"></script>
<form id="payment-form">
<input type="number" name="amount" placeHolder="amount" id="charge-amount" value>
<div id="cash-app-pay"></div>
</form>
<button id="paynow">Donate</button>
<script>
document.getElementById('paynow').addEventListener('click', async function () {
const payment = window.Square.payments(
"APP_ID",
"LOCATION_ID"
);
var chargeAmount = document.getElementById("charge-amount").value;
const cashReq = payment.paymentRequest({
countryCode: 'US',
currencyCode: 'USD',
total: { amount: chargeAmount,
label: 'Total'
},
});
const cashAppPay = await payment.cashAppPay(cashReq, {
redirectURL: window.location.href,
referenceId: `optional-reference-id`,
});
cashAppPay.addEventListener('ontokenization', ({ detail }) => {
const { tokenResult } = detail;
if (tokenResult.status === 'OK') {
alert(tokenResult.token);
}
});
await cashAppPay.attach('#cash-app-pay', {
shape: 'semiround',
theme: 'dark',
size: 'small',
});
});
</script>
Just help me add whatever is missing and let me know what next i should do.