Created
March 5, 2021 13:12
-
-
Save deepu105/8133b0327ba4e0dc91251ec094553894 to your computer and use it in GitHub Desktop.
payment-api-payment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.