-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating ach example #218
Updating ach example #218
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,6 @@ | |
const appId = '{APPLICATION_ID}'; | ||
const locationId = '{LOCATION_ID}'; | ||
|
||
async function initializeCard(payments) { | ||
const card = await payments.card(); | ||
await card.attach('#card-container'); | ||
|
||
return card; | ||
} | ||
|
||
async function initializeACH(payments) { | ||
const ach = await payments.ach(); | ||
// Note: ACH does not have an .attach(...) method | ||
|
@@ -48,19 +41,31 @@ | |
} | ||
|
||
async function tokenize(paymentMethod, options = {}) { | ||
const tokenResult = await paymentMethod.tokenize(options); | ||
if (tokenResult.status === 'OK') { | ||
return tokenResult.token; | ||
} else { | ||
let errorMessage = `Tokenization failed with status: ${tokenResult.status}`; | ||
if (tokenResult.errors) { | ||
errorMessage += ` and errors: ${JSON.stringify( | ||
tokenResult.errors | ||
)}`; | ||
paymentMethod.addEventListener( | ||
'ontokenization', | ||
async function (event) { | ||
const { tokenResult, error } = event.detail; | ||
if (error !== undefined) { | ||
let errorMessage = `Tokenization failed with error: ${error}`; | ||
throw new Error(errorMessage); | ||
} | ||
if (tokenResult.status === 'OK') { | ||
const paymentResults = await createPayment(tokenResult.token); | ||
displayPaymentResults('SUCCESS'); | ||
|
||
console.debug('Payment Success', paymentResults); | ||
} else { | ||
let errorMessage = `Tokenization failed with status: ${tokenResult.status}`; | ||
if (tokenResult.errors) { | ||
errorMessage += ` and errors: ${JSON.stringify( | ||
tokenResult.errors | ||
)}`; | ||
} | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
|
||
throw new Error(errorMessage); | ||
} | ||
); | ||
paymentMethod.tokenize(options); | ||
} | ||
|
||
// status is either SUCCESS or FAILURE; | ||
|
@@ -93,7 +98,11 @@ | |
const billingContact = getBillingContact(form); | ||
const accountHolderName = `${billingContact.givenName} ${billingContact.familyName}`; | ||
|
||
return { accountHolderName }; | ||
return { | ||
accountHolderName, | ||
intent: 'CHARGE', | ||
total: { amount: 100, currencyCode: 'USD' }, | ||
}; | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', async function () { | ||
|
@@ -113,14 +122,6 @@ | |
return; | ||
} | ||
|
||
let card; | ||
try { | ||
card = await initializeCard(payments); | ||
} catch (e) { | ||
console.error('Initializing Card failed', e); | ||
return; | ||
} | ||
|
||
let ach; | ||
try { | ||
ach = await initializeACH(payments); | ||
|
@@ -137,33 +138,24 @@ | |
event.preventDefault(); | ||
|
||
try { | ||
// disable the submit button as we await tokenization and make a payment request. | ||
cardButton.disabled = true; | ||
achButton.disabled = true; | ||
const token = await tokenize(paymentMethod, options); | ||
const paymentResults = await createPayment(token); | ||
displayPaymentResults('SUCCESS'); | ||
|
||
console.debug('Payment Success', paymentResults); | ||
tokenize(paymentMethod, options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know ACH relies mostly on the event listener, but I feel like this should still be awaited..? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the errors will now be captured via the listener so we don't need to await this anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, the async keyword should be removed from the tokenize() method definition on line 50. |
||
} catch (e) { | ||
cardButton.disabled = false; | ||
achButton.disabled = false; | ||
displayPaymentResults('FAILURE'); | ||
console.error(e.message); | ||
} | ||
} | ||
|
||
const cardButton = document.getElementById('card-button'); | ||
cardButton.addEventListener('click', async function (event) { | ||
await handlePaymentMethodSubmission(event, card); | ||
}); | ||
|
||
const achButton = document.getElementById('ach-button'); | ||
achButton.addEventListener('click', async function (event) { | ||
const paymentForm = document.getElementById('payment-form'); | ||
const achOptions = getACHOptions(paymentForm); | ||
await handlePaymentMethodSubmission(event, ach, achOptions); | ||
}); | ||
// Checkpoint 2. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checkpoint is directly at odds with the idea of this example being completely focused on ACH and ACH alone, I think checkpoint 1 was the card example. We should pick a lane here - each payment method is independent, or the examples build on each other. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good callout |
||
if (ach) { | ||
const achButton = document.getElementById('ach-button'); | ||
achButton.addEventListener('click', async function (event) { | ||
const paymentForm = document.getElementById('payment-form'); | ||
const achOptions = getACHOptions(paymentForm); | ||
await handlePaymentMethodSubmission(event, ach, achOptions); | ||
}); | ||
} | ||
}); | ||
</script> | ||
</head> | ||
|
@@ -193,9 +185,6 @@ | |
/> | ||
</fieldset> | ||
<button id="ach-button" type="button">Pay with Bank Account</button> | ||
|
||
<div id="card-container"></div> | ||
<button id="card-button" type="button">Pay $1.00</button> | ||
</form> | ||
<div id="payment-status-container"></div> | ||
</body> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for removing the card flow from this example. It just muddles up the example as is.