PayPal

// Close 3Ds Dialog function onClose() { const threedsElement = document.getElementById(โ€œthreedsโ€); threedsElement.innerHTML = โ€œโ€; } // Handle 3Ds Payload async function onHandle3Ds(payload, orderId) { const { liabilityShifted, liabilityShift } = payload; if (liabilityShift === โ€œPOSSIBLEโ€) { await onApproveCallback(orderId); } else if (liabilityShifted === false || liabilityShifted === undefined) { document.getElementById(โ€œthirdsโ€).innerHTML = `

You have the option to complete the payment at your own risk, meaning that the liability of any chargeback has not shifted from the merchant to the card issuer.

`; } } async function createOrderCallback() { resultMessage(โ€œโ€); try { const response = await fetch(โ€œ/api/ordersโ€, { method: โ€œPOSTโ€, headers: { โ€œContent-Typeโ€: โ€œapplication/jsonโ€, }, // use the โ€œbodyโ€ param to optionally pass additional order information // like product ids and quantities body: JSON.stringify({ cart: [ { id: โ€œYOUR_PRODUCT_IDโ€, quantity: โ€œYOUR_PRODUCT_QUANTITYโ€, }, ], }), }); const orderData = await response.json(); if (orderData.id) { return orderData.id; } else { const errorDetail = orderData?.details?.[0]; const errorMessage = errorDetail ? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})` : JSON.stringify(orderData); throw new Error(errorMessage); } } catch (error) { console.error(error); resultMessage(`Could not initiate PayPal Checkoutโ€ฆ

${error}`); } } async function onApproveCallback(orderId) { console.log(“orderId”, orderId); const threedsElement = document.getElementById(“threeds”); threedsElement.innerHTML = “”; try { const response = await fetch(`/api/orders/${orderId}/capture`, { method: “POST”, headers: { “Content-Type”: “application/json”, }, }); const orderData = await response.json(); // Three cases to handle: // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart() // (2) Other non-recoverable errors -> Show a failure message // (3) Successful transaction -> Show confirmation or thank you message const transaction = orderData?.purchase_units?.[0]?.payments?.captures?.[0] || orderData?.purchase_units?.[0]?.payments?.authorizations?.[0]; const errorDetail = orderData?.details?.[0]; // this actions.restart() behavior only applies to the Buttons component if (errorDetail?.issue === “INSTRUMENT_DECLINED” && !data.card && actions) { // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart() // recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/ return actions.restart(); } else if ( errorDetail || !transaction || transaction.status === “DECLINED” ) { // (2) Other non-recoverable errors -> Show a failure message let errorMessage; if (transaction) { errorMessage = `Transaction ${transaction.status}: ${transaction.id}`; } else if (errorDetail) { errorMessage = `${errorDetail.description} (${orderData.debug_id})`; } else { errorMessage = JSON.stringify(orderData); } throw new Error(errorMessage); } else { // (3) Successful transaction -> Show confirmation or thank you message // Or go to another URL: actions.redirect(‘thank_you.html’); resultMessage( `Transaction ${transaction.status}: ${transaction.id}

See console for all available details`, ); console.log( โ€œCapture resultโ€, orderData, JSON.stringify(orderData, null, 2), ); } } catch (error) { console.error(error); resultMessage( `Sorry, your transaction could not be processedโ€ฆ

${error}`, ); } } window.paypal .Buttons({ style: { shape: โ€œpillโ€, layout: โ€œverticalโ€, }, createOrder: createOrderCallback, onApprove: (data) => onApproveCallback(data.orderID), }) .render(โ€œ #paypal-button-containerโ€); // Example function to show a result to the user. Your site's UI library can be used instead. function resultMessage(message) { const container = document.querySelector(โ€œ#result-messageโ€); container.innerHTML = message; } // If this returns false or the card fields aren't visible, see Step #1. if (window.paypal.HostedFields.isEligible()) { let orderId; // Renders card fields window.paypal.HostedFields.render({ // Call your server to set up the transaction createOrder: async (data, actions) => { orderId = await createOrderCallback(data, actions); return orderId; }, styles: { โ€œ.validโ€: { color: โ€œgreenโ€, }, โ€œ.invalidโ€: { color: โ€œredโ€, }, }, fields: { number: { selector: โ€œ#card-numberโ€, placeholder: โ€œ 4111 1111 1111 1111โ€, }, cvv: { selector: โ€œ#cvvโ€, placeholder: โ€œ123โ€, }, expirationDate: { selector: โ€œ#expiration-dateโ€, placeholder: โ€œMM/YYโ€, }, }, }) .then((cardFields) => { document .querySelector(โ€œ#card-formโ€) .addEventListener(โ€œsubmitโ€, async (event) => { event.preventDefault(); try { const { value: cardHolderName } = document. getElementById(โ€œcard-holder-nameโ€); const { value: streetAddress } = document.getElementById( โ€œcard-billing-address-streetโ€, ); const { value: extendedAddress } = document.getElementById( โ€œcard-billing- address-unitโ€, ); const { value: region } = document.getElementById( โ€œcard-billing-address-stateโ€, ); const { value: locality } = document.getElementById( โ€œcard-billing-address-cityโ€, ); const { value: postalCode } = document.getElementById( โ€œcard-billing-address-zipโ€, ); const { value: countryCodeAlpha2 } = document.getElementById( โ€œcard-billing-address-countryโ€, ); const payload = await cardFields.submit({ cardHolderName, contingencies: [โ€œSCA_ALWAYSโ€], billingAddress: { streetAddress, extendedAddress, region, locality, postalCode, countryCodeAlpha2, }, }); await onHandle3Ds(payload, orderId); } catch (error) { alert(โ€œPayment could not be captured! โ€ + JSON.stringify(error)); } }); }); } else { // Hides card fields if the merchant isn't eligible document.querySelector(โ€œ#card-formโ€).style = โ€œdisplay: noneโ€; }

This site has been designed using images from Freepik.com