1async function initializeCard(payments) { 2 const card = await payments.card(); 3 await card.attach('#card-container'); 4 5 return card; 6} 7 8function buildPaymentRequest(payments, amount) { 9 const req = payments.paymentRequest({ 10 countryCode: countryCode, 11 currencyCode: currencyCode, 12 total: { 13 amount: amount, 14 label: 'Total', 15 }, 16 requestShippingContact: true, 17 }); 18 19 let total = { 20 amount: amount, 21 label: 'total', 22 }; 23 24 req.addEventListener('afterpay_shippingaddresschanged', function () { 25 return { 26 shippingOptions: [ 27 { 28 amount: '0.00', 29 id: 'shipping-option-1', 30 label: 'Free', 31 taxLineItems: [ 32 { 33 amount: '0.00', 34 label: 'Tax', 35 }, 36 ], 37 total: total, 38 }, 39 ], 40 }; 41 }); 42 43 req.addEventListener( 44 'afterpay_shippingoptionchanged', 45 function (_option) { 46 // This event listener is for information purposes only. 47 // Changes here (or values returned) will not affect the Afterpay/Clearpay PaymentRequest. 48 } 49 ); 50 51 return req; 52} 53 54async function initializeAfterpay(payments, amount) { 55 try { 56 const paymentRequest = buildPaymentRequest(payments, amount); 57 const afterpay = await payments.afterpayClearpay(paymentRequest); 58 await afterpay.attach('#afterpay-button'); 59 60 return afterpay; 61 } catch (e) { 62 return null; 63 } 64} 65 66async function tokenize(paymentMethod) { 67 const tokenResult = await paymentMethod.tokenize(); 68 if (tokenResult.status === 'OK') { 69 return tokenResult.token; 70 } else { 71 let errorMessage = `Tokenization failed with status: ${tokenResult.status}`; 72 if (tokenResult.errors) { 73 errorMessage += ` and errors: ${JSON.stringify( 74 tokenResult.errors 75 )}`; 76 } 77 78 throw new Error(errorMessage); 79 } 80} 81 82async function verifyBuyer(payments, token, data) { 83 const verificationDetails = { 84 amount: data.amount, 85 /* collected from the buyer */ 86 billingContact: { 87 addressLines: data.address_lines, 88 familyName: data.first_name, 89 givenName: data.last_name, 90 email: data.email, 91 countryCode: data.country, 92 phone: data.phone, 93 state: data.state, 94 city: data.city, 95 postalCode: data.zip, 96 }, 97 currencyCode: data.currency, 98 intent: 'CHARGE', 99 }; 100 101 const verificationResults = await payments.verifyBuyer( 102 token, 103 verificationDetails 104 ); 105 return verificationResults.token; 106} 107 108 109// status is either SUCCESS or FAILURE; 110function displayPaymentResults(status) { 111 const statusContainer = document.getElementById('payment-status-container'); 112 if (status === 'SUCCESS') { 113 statusContainer.classList.remove('is-failure'); 114 statusContainer.classList.add('is-success'); 115 } else { 116 statusContainer.classList.remove('is-success'); 117 statusContainer.classList.add('is-failure'); 118 } 119 120 statusContainer.style.visibility = 'visible'; 121}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.