async function onPayPalWebSdkLoaded() {
try {
// Get client token for authentication
const clientToken = await getBrowserSafeClientToken();
// Create PayPal SDK instance
const sdkInstance = await window.paypal.createInstance({
clientToken,
components: ["paypal-payments"],
pageType: "checkout",
});
// Check eligibility for all payment methods
const paymentMethods = await sdkInstance.findEligibleMethods({
currencyCode: "USD",
});
// Setup PayPal button if eligible
if (paymentMethods.isEligible("paypal")) {
setupPayPalButton(sdkInstance);
}
// Setup Pay Later button if eligible
if (paymentMethods.isEligible("paylater")) {
const paylaterPaymentMethodDetails = paymentMethods.getDetails("paylater");
setupPayLaterButton(sdkInstance, paylaterPaymentMethodDetails);
}
// Setup PayPal Credit button if eligible
if (paymentMethods.isEligible("credit")) {
const paypalCreditPaymentMethodDetails = paymentMethods.getDetails("credit");
setupPayPalCreditButton(sdkInstance, paypalCreditPaymentMethodDetails);
}
} catch (error) {
console.error("SDK initialization error:", error);
}
}
// Shared payment session options for all payment methods
const paymentSessionOptions = {
// Called when user approves a payment
async onApprove(data) {
console.log("Payment approved:", data);
try {
const orderData = await captureOrder({
orderId: data.orderId,
});
console.log("Payment captured successfully:", orderData);
handlePaymentSuccess(orderData);
} catch (error) {
console.error("Payment capture failed:", error);
handlePaymentError(error);
}
},
// Called when user cancels a payment
onCancel(data) {
console.log("Payment cancelled:", data);
handlePaymentCancellation();
},
// Called when an error occurs during payment
onError(error) {
console.error("Payment error:", error);
handlePaymentError(error);
},
};
// Setup standard PayPal button
async function setupPayPalButton(sdkInstance) {
const paypalPaymentSession = sdkInstance.createPayPalOneTimePaymentSession(
paymentSessionOptions,
);
const paypalButton = document.querySelector("#paypal-button");
paypalButton.removeAttribute("hidden");
paypalButton.addEventListener("click", async () => {
try {
await paypalPaymentSession.start(
{ presentationMode: "auto" }, // Auto-detects best presentation mode
createOrder(),
);
} catch (error) {
console.error("PayPal payment start error:", error);
handlePaymentError(error);
}
});
}
// Set up Pay Later button
async function setupPayLaterButton(sdkInstance, paylaterPaymentMethodDetails) {
const paylaterPaymentSession = sdkInstance.createPayLaterOneTimePaymentSession(
paymentSessionOptions
);
const { productCode, countryCode } = paylaterPaymentMethodDetails;
const paylaterButton = document.querySelector("#paylater-button");
// Configure button with Pay Later specific details
paylaterButton.productCode = productCode;
paylaterButton.countryCode = countryCode;
paylaterButton.removeAttribute("hidden");
paylaterButton.addEventListener("click", async () => {
try {
await paylaterPaymentSession.start(
{ presentationMode: "auto" },
createOrder(),
);
} catch (error) {
console.error("Pay Later payment start error:", error);
handlePaymentError(error);
}
});
}
// Setup PayPal Credit button
async function setupPayPalCreditButton(sdkInstance, paypalCreditPaymentMethodDetails) {
const paypalCreditPaymentSession = sdkInstance.createPayPalCreditOneTimePaymentSession(
paymentSessionOptions
);
const { countryCode } = paypalCreditPaymentMethodDetails;
const paypalCreditButton = document.querySelector("#paypal-credit-button");
// Configure button with PayPal Credit specific details
paypalCreditButton.countryCode = countryCode;
paypalCreditButton.removeAttribute("hidden");
paypalCreditButton.addEventListener("click", async () => {
try {
await paypalCreditPaymentSession.start(
{ presentationMode: "auto" },
createOrder(),
);
} catch (error) {
console.error("PayPal Credit payment start error:", error);
handlePaymentError(error);
}
});
}