Skip to content

Instantly share code, notes, and snippets.

@deepu105
Created March 5, 2021 13:12
Show Gist options
  • Save deepu105/8133b0327ba4e0dc91251ec094553894 to your computer and use it in GitHub Desktop.
Save deepu105/8133b0327ba4e0dc91251ec094553894 to your computer and use it in GitHub Desktop.
payment-api-payment.js
async function checkout() {
try {
const adyenPaymentMethods = await callServer("/api/getPaymentMethods");
// create a new payment request
const request = new PaymentRequest(buildSupportedPaymentMethodData(adyenPaymentMethods), buildShoppingCartDetails());
// show payment sheet
const payment = await request.show();
// Here we would process the payment.
const response = await callServer("/api/initiatePayment", {
// This works only for PCI compliant credit card payments.
// For non PCI compliant payments the data needs to be encrypted with something like https://github.com/Adyen/adyen-cse-web
// But encrypting data here is not secure as a malicious script may be able to access the data in memory here
paymentMethod: {
type: "scheme",
number: payment.details.cardNumber,
expiryMonth: payment.details.expiryMonth,
expiryYear: payment.details.expiryYear,
holderName: payment.details.cardholderName,
cvc: payment.details.cardSecurityCode,
},
});
// Handle the response code
switch (response.resultCode) {
case "Authorised":
await payment.complete("success");
window.location.href = "/result/success";
break;
case "Pending":
case "Received":
await payment.complete("unknown");
window.location.href = "/result/pending";
break;
case "Refused":
await payment.complete("fail");
window.location.href = "/result/failed";
break;
default:
await payment.complete("fail");
window.location.href = "/result/error";
break;
}
} catch (error) {
// ...
}
return false;
}
@IvanKolom
Copy link

It looks good! I also recommend to pay attention to SPD Technology https://spd.tech/payment-gateway-development/, because they specialize in creating customized payment system solutions and offer a professional approach to every project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment