|
| 1 | +async function setupFastlaneSdk() { |
| 2 | + fastlane.setLocale("en_us"); |
| 3 | + |
| 4 | + const fastlaneWatermark = await fastlane.FastlaneWatermarkComponent({ |
| 5 | + includeAdditionalInfo: true, |
| 6 | + }); |
| 7 | + fastlaneWatermark.render("#watermark-container"); |
| 8 | + |
| 9 | + const emailInput = document.getElementById("email-input"); |
| 10 | + const emailSubmitButton = document.getElementById("email-submit-button"); |
| 11 | + emailSubmitButton.addEventListener("click", async (e) => { |
| 12 | + e.preventDefault(); |
| 13 | + |
| 14 | + const { customerContextId } = await fastlane.identity.lookupCustomerByEmail( |
| 15 | + emailInput.value, |
| 16 | + ); |
| 17 | + |
| 18 | + let shouldRenderFastlaneMemberExperience = false; |
| 19 | + let profileData; |
| 20 | + if (customerContextId) { |
| 21 | + const response = |
| 22 | + await fastlane.identity.triggerAuthenticationFlow(customerContextId); |
| 23 | + |
| 24 | + if (response.authenticationState === "succeeded") { |
| 25 | + shouldRenderFastlaneMemberExperience = true; |
| 26 | + profileData = response.profileData; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + const emailForm = document.getElementById("email-form"); |
| 31 | + emailForm.setAttribute("hidden", true); |
| 32 | + |
| 33 | + const submitOrderButton = document.getElementById("submit-button"); |
| 34 | + submitOrderButton.removeAttribute("hidden"); |
| 35 | + |
| 36 | + if (shouldRenderFastlaneMemberExperience) { |
| 37 | + renderFastlaneMemberExperience(profileData); |
| 38 | + } else { |
| 39 | + renderFastlaneGuestExperience(); |
| 40 | + } |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function setShippingAddressDisplay(shippingAddress) { |
| 45 | + const { |
| 46 | + name: { fullName }, |
| 47 | + address: { addressLine1, adminArea2, adminArea1, postalCode }, |
| 48 | + } = shippingAddress; |
| 49 | + const shippingDisplayContainer = document.getElementById( |
| 50 | + "shipping-display-container", |
| 51 | + ); |
| 52 | + shippingDisplayContainer.removeAttribute("hidden"); |
| 53 | + shippingDisplayContainer.innerHTML = `<b>${fullName}</b><br><b>${adminArea2}</b><br><b>${adminArea1}</b><br><b>${postalCode}</b>`; |
| 54 | +} |
| 55 | + |
| 56 | +async function renderFastlaneMemberExperience(profileData) { |
| 57 | + if (profileData.shippingAddress) { |
| 58 | + setShippingAddressDisplay(profileData.shippingAddress); |
| 59 | + |
| 60 | + const changeAddressButton = document.getElementById( |
| 61 | + "change-shipping-button", |
| 62 | + ); |
| 63 | + |
| 64 | + changeAddressButton.removeAttribute("hidden"); |
| 65 | + changeAddressButton.addEventListener("click", async () => { |
| 66 | + const { selectedAddress, selectionChanged } = |
| 67 | + await fastlane.profile.showShippingAddressSelector(); |
| 68 | + |
| 69 | + if (selectionChanged) { |
| 70 | + profileData.shippingAddress = selectedAddress; |
| 71 | + setShippingAddressDisplay(profileData.shippingAddress); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + const fastlanePaymentComponent = await fastlane.FastlanePaymentComponent({ |
| 76 | + options: {}, |
| 77 | + shippingAddress: profileData.shippingAddress, |
| 78 | + }); |
| 79 | + |
| 80 | + fastlanePaymentComponent.render("#payment-container"); |
| 81 | + |
| 82 | + const submitButton = document.getElementById("submit-button"); |
| 83 | + submitButton.addEventListener("click", async () => { |
| 84 | + const { id } = await fastlanePaymentComponent.getPaymentToken(); |
| 85 | + |
| 86 | + const orderResponse = await createOrder(id); |
| 87 | + console.log("orderResponse: ", orderResponse); |
| 88 | + |
| 89 | + if (orderResponse.status === "COMPLETED") { |
| 90 | + alert("Order completed successfully! Check console for details."); |
| 91 | + } else { |
| 92 | + alert("There was an issue processing your order. Please try again."); |
| 93 | + } |
| 94 | + }); |
| 95 | + } else { |
| 96 | + // Render your shipping address form |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +async function renderFastlaneGuestExperience() { |
| 101 | + const cardTestingInfo = document.getElementById("card-testing-info"); |
| 102 | + cardTestingInfo.removeAttribute("hidden"); |
| 103 | + |
| 104 | + const FastlanePaymentComponent = await fastlane.FastlanePaymentComponent({}); |
| 105 | + await FastlanePaymentComponent.render("#card-container"); |
| 106 | + |
| 107 | + const submitButton = document.getElementById("submit-button"); |
| 108 | + submitButton.addEventListener("click", async () => { |
| 109 | + const { id } = await FastlanePaymentComponent.getPaymentToken(); |
| 110 | + |
| 111 | + const orderResponse = await createOrder(id); |
| 112 | + console.log("orderResponse: ", orderResponse); |
| 113 | + |
| 114 | + if (orderResponse.status === "COMPLETED") { |
| 115 | + alert("Order completed successfully! Check console for details."); |
| 116 | + } else { |
| 117 | + alert("There was an issue processing your order. Please try again."); |
| 118 | + } |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +async function createOrder(paymentToken) { |
| 123 | + const response = await fetch("/paypal-api/checkout/orders/create", { |
| 124 | + method: "POST", |
| 125 | + headers: { |
| 126 | + "Content-Type": "application/json", |
| 127 | + "PayPal-Request-Id": Date.now().toString(), |
| 128 | + }, |
| 129 | + body: JSON.stringify({ |
| 130 | + paymentSource: { |
| 131 | + card: { |
| 132 | + singleUseToken: paymentToken, |
| 133 | + }, |
| 134 | + }, |
| 135 | + purchaseUnits: [ |
| 136 | + { |
| 137 | + amount: { |
| 138 | + currencyCode: "USD", |
| 139 | + value: "10.00", |
| 140 | + breakdown: { |
| 141 | + itemTotal: { |
| 142 | + currencyCode: "USD", |
| 143 | + value: "10.00", |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + ], |
| 149 | + intent: "CAPTURE", |
| 150 | + }), |
| 151 | + }); |
| 152 | + const orderResponse = await response.json(); |
| 153 | + |
| 154 | + return orderResponse; |
| 155 | +} |
0 commit comments