Hi, I'm trying to implement Apple Pay JS on web into a project, I've spent a lot of time reading through the documentation and trying to find code examples. But some things are still abstract to me. So this is the code I've come up with now for my react client-side, can someone tell me if I'm doing this right or if I'm missing something. At the moment I don't have access to an account that's enrolled in the apple developer program so I can't really test it at the moment.
const paymentRequest = {
"countryCode": "US",
"currencyCode": "USD",
"merchantCapabilities": [
"supports3DS"
],
"supportedNetworks": [
"visa",
"masterCard",
"amex",
"discover"
],
"total": {
"label": "Demo (Card is not charged)",
"type": "final",
"amount": "1.99"
}
}
const session = new ApplePaySession(6, paymentRequest);
session.onvalidatemerchant = (event) => {
// Backend server, makes request to https://<validation URL>/paymentSession
const serverURL = "https://";
const data = {
validationUrl: event.validationURL
};
fetch(serverURL, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(data)
})
.then(res => res.json())
.then(merchantSession => session.completeMerchantValidation(merchantSession));
};
session.onpaymentauthorized = (event) => {
const token = event.payment.token.paymentData;
// Token contains encrypted data of authorized payment
// Do something with the payment to capture funds
// Use payment details to populate pay sheet view
const authorizationResult = {
status: ApplePaySession.STATUS_SUCCESS,
errors: []
};
session.completePayment(authorizationResult);
};
session.begin();
};