|
| 1 | +window.paypal |
| 2 | + .Buttons({ |
| 3 | + // Sets up the transaction when a payment button is clicked |
| 4 | + createOrder: function () { |
| 5 | + return fetch("/api/orders", { |
| 6 | + method: "post", |
| 7 | + // use the "body" param to optionally pass additional order information |
| 8 | + // like product skus and quantities |
| 9 | + body: JSON.stringify({ |
| 10 | + cart: [ |
| 11 | + { |
| 12 | + sku: "<YOUR_PRODUCT_STOCK_KEEPING_UNIT>", |
| 13 | + quantity: "<YOUR_PRODUCT_QUANTITY>", |
| 14 | + }, |
| 15 | + ], |
| 16 | + }), |
| 17 | + }) |
| 18 | + .then((response) => response.json()) |
| 19 | + .then((order) => order.id); |
| 20 | + }, |
| 21 | + // Finalize the transaction after payer approval |
| 22 | + onApprove: function (data) { |
| 23 | + return fetch(`/api/orders/${data.orderID}/capture`, { |
| 24 | + method: "post", |
| 25 | + }) |
| 26 | + .then((response) => response.json()) |
| 27 | + .then((orderData) => { |
| 28 | + // Successful capture! For dev/demo purposes: |
| 29 | + console.log( |
| 30 | + "Capture result", |
| 31 | + orderData, |
| 32 | + JSON.stringify(orderData, null, 2), |
| 33 | + ); |
| 34 | + const transaction = orderData.purchase_units[0].payments.captures[0]; |
| 35 | + alert(`Transaction ${transaction.status}: ${transaction.id} |
| 36 | +
|
| 37 | + See console for all available details |
| 38 | + `); |
| 39 | + // When ready to go live, remove the alert and show a success message within this page. For example: |
| 40 | + // var element = document.getElementById('paypal-button-container'); |
| 41 | + // element.innerHTML = '<h3>Thank you for your payment!</h3>'; |
| 42 | + // Or go to another URL: actions.redirect('thank_you.html'); |
| 43 | + }); |
| 44 | + }, |
| 45 | + }) |
| 46 | + .render("#paypal-button-container"); |
| 47 | + |
| 48 | +// If this returns false or the card fields aren't visible, see Step #1. |
| 49 | +if (window.paypal.HostedFields.isEligible()) { |
| 50 | + let orderId; |
| 51 | + |
| 52 | + // Renders card fields |
| 53 | + window.paypal.HostedFields.render({ |
| 54 | + // Call your server to set up the transaction |
| 55 | + createOrder: () => { |
| 56 | + return fetch("/api/orders", { |
| 57 | + method: "post", |
| 58 | + // use the "body" param to optionally pass additional order information |
| 59 | + // like product skus and quantities |
| 60 | + body: JSON.stringify({ |
| 61 | + cart: [ |
| 62 | + { |
| 63 | + sku: "<YOUR_PRODUCT_STOCK_KEEPING_UNIT>", |
| 64 | + quantity: "<YOUR_PRODUCT_QUANTITY>", |
| 65 | + }, |
| 66 | + ], |
| 67 | + }), |
| 68 | + }) |
| 69 | + .then((res) => res.json()) |
| 70 | + .then((orderData) => { |
| 71 | + orderId = orderData.id; // needed later to complete capture |
| 72 | + return orderData.id; |
| 73 | + }); |
| 74 | + }, |
| 75 | + styles: { |
| 76 | + ".valid": { |
| 77 | + color: "green", |
| 78 | + }, |
| 79 | + ".invalid": { |
| 80 | + color: "red", |
| 81 | + }, |
| 82 | + }, |
| 83 | + fields: { |
| 84 | + number: { |
| 85 | + selector: "#card-number", |
| 86 | + placeholder: "4111 1111 1111 1111", |
| 87 | + }, |
| 88 | + cvv: { |
| 89 | + selector: "#cvv", |
| 90 | + placeholder: "123", |
| 91 | + }, |
| 92 | + expirationDate: { |
| 93 | + selector: "#expiration-date", |
| 94 | + placeholder: "MM/YY", |
| 95 | + }, |
| 96 | + }, |
| 97 | + }).then((cardFields) => { |
| 98 | + document.querySelector("#card-form").addEventListener("submit", (event) => { |
| 99 | + event.preventDefault(); |
| 100 | + cardFields |
| 101 | + .submit({ |
| 102 | + // Cardholder's first and last name |
| 103 | + cardholderName: document.getElementById("card-holder-name").value, |
| 104 | + // Billing Address |
| 105 | + billingAddress: { |
| 106 | + // Street address, line 1 |
| 107 | + streetAddress: document.getElementById( |
| 108 | + "card-billing-address-street", |
| 109 | + ).value, |
| 110 | + // Street address, line 2 (Ex: Unit, Apartment, etc.) |
| 111 | + extendedAddress: document.getElementById( |
| 112 | + "card-billing-address-unit", |
| 113 | + ).value, |
| 114 | + // State |
| 115 | + region: document.getElementById("card-billing-address-state").value, |
| 116 | + // City |
| 117 | + locality: document.getElementById("card-billing-address-city") |
| 118 | + .value, |
| 119 | + // Postal Code |
| 120 | + postalCode: document.getElementById("card-billing-address-zip") |
| 121 | + .value, |
| 122 | + // Country Code |
| 123 | + countryCodeAlpha2: document.getElementById( |
| 124 | + "card-billing-address-country", |
| 125 | + ).value, |
| 126 | + }, |
| 127 | + }) |
| 128 | + .then(() => { |
| 129 | + fetch(`/api/orders/${orderId}/capture`, { |
| 130 | + method: "post", |
| 131 | + }) |
| 132 | + .then((res) => res.json()) |
| 133 | + .then((orderData) => { |
| 134 | + // Two cases to handle: |
| 135 | + // (1) Other non-recoverable errors -> Show a failure message |
| 136 | + // (2) Successful transaction -> Show confirmation or thank you |
| 137 | + // This example reads a v2/checkout/orders capture response, propagated from the server |
| 138 | + // You could use a different API or structure for your 'orderData' |
| 139 | + const errorDetail = |
| 140 | + Array.isArray(orderData.details) && orderData.details[0]; |
| 141 | + if (errorDetail) { |
| 142 | + var msg = "Sorry, your transaction could not be processed."; |
| 143 | + if (errorDetail.description) |
| 144 | + msg += "\n\n" + errorDetail.description; |
| 145 | + if (orderData.debug_id) msg += " (" + orderData.debug_id + ")"; |
| 146 | + return alert(msg); // Show a failure message |
| 147 | + } |
| 148 | + // Show a success message or redirect |
| 149 | + alert("Transaction completed!"); |
| 150 | + }); |
| 151 | + }) |
| 152 | + .catch((err) => { |
| 153 | + alert("Payment could not be captured! " + JSON.stringify(err)); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | +} else { |
| 158 | + // Hides card fields if the merchant isn't eligible |
| 159 | + document.querySelector("#card-form").style = "display: none"; |
| 160 | +} |
0 commit comments