-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use braintree-web from node_modules instead of cdn in edit payment …
…details - refactor and improve braintree.js wrapper
- Loading branch information
1 parent
77c2fce
commit cd50a02
Showing
12 changed files
with
315 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,47 @@ | ||
import { client } from 'braintree-web' | ||
|
||
import { createBraintreeClient } from 'PaymentGateways/braintree/braintree' | ||
import { BraintreeFormWrapper } from 'PaymentGateways/braintree/BraintreeForm' | ||
import { getClient } from 'PaymentGateways/braintree/braintree' | ||
import { safeFromJsonString } from 'utilities/json-utils' | ||
|
||
import type { Client } from 'braintree-web' | ||
import type { BraintreeError } from 'braintree-web' | ||
import type { BillingAddressData } from 'PaymentGateways/braintree/types' | ||
|
||
const CONTAINER_ID = 'braintree-form-wrapper' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
document.addEventListener('DOMContentLoaded', async () => { | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const container = document.getElementById(CONTAINER_ID) | ||
|
||
if (!container) { | ||
throw new Error('The target ID was not found: ' + CONTAINER_ID) | ||
} | ||
|
||
const { clientToken, threeDSecureEnabled, formActionPath, countriesList, selectedCountryCode } = container.dataset as Record<string, string> | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- FIXME: too much assumption here | ||
const billingAddress = safeFromJsonString<BillingAddressData>(container.dataset.billingAddress)! | ||
for (const key in billingAddress) { | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- FIXME | ||
if (billingAddress[key as keyof BillingAddressData] === null) { | ||
billingAddress[key as keyof BillingAddressData] = '' | ||
} | ||
const { clientToken, threeDSecureEnabled, formActionPath, countriesList, selectedCountryCode, billingAddress } = container.dataset as Record<string, string> | ||
|
||
const billingAddressData: BillingAddressData = { | ||
address: '', | ||
address1: '', | ||
address2: '', | ||
city: '', | ||
company: '', | ||
country: '', | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
phone_number: '', | ||
state: '', | ||
zip: '', | ||
...safeFromJsonString<Partial<BillingAddressData>>(billingAddress) | ||
} | ||
const braintreeClient = await createBraintreeClient(client, clientToken) as Client | ||
|
||
BraintreeFormWrapper({ | ||
braintreeClient, | ||
formActionPath: formActionPath, | ||
countriesList: countriesList, | ||
selectedCountryCode: selectedCountryCode, | ||
billingAddress, | ||
threeDSecureEnabled: threeDSecureEnabled === 'true' | ||
}, CONTAINER_ID) | ||
void getClient(clientToken) | ||
.then(braintreeClient => { | ||
BraintreeFormWrapper({ | ||
braintreeClient, | ||
formActionPath: formActionPath, | ||
countriesList: countriesList, | ||
selectedCountryCode: selectedCountryCode, | ||
billingAddress: billingAddressData, | ||
threeDSecureEnabled: safeFromJsonString<boolean>(threeDSecureEnabled) | ||
}, CONTAINER_ID) | ||
}) | ||
.catch((e: BraintreeError) => { | ||
console.error('Something went wrong with Braintree', e.message) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// TODO: we can probably re-use BraintreeForm here | ||
|
||
import { error } from 'utilities/flash' | ||
import { getClient, getHostedFields } from 'PaymentGateways/braintree/braintree' | ||
|
||
import type { BraintreeError } from 'braintree-web' | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
const submit = document.querySelector<HTMLButtonElement>('button[type="submit"]') | ||
const data = document.querySelector<HTMLSpanElement>('#braintree_data') | ||
const form = document.querySelector<HTMLFormElement>('form#new_customer') | ||
|
||
if (!(submit && data && form)) { | ||
throw new Error('Required Element not found') | ||
} | ||
|
||
const authorization = data.dataset.authorization ?? '' | ||
|
||
void getClient(authorization) | ||
.then(getHostedFields) | ||
.then(hostedFieldsInstance => { | ||
submit.removeAttribute('disabled') | ||
|
||
form.addEventListener('submit', (event: Event) => { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
hostedFieldsInstance.tokenize((tokenizeErr, payload) => { | ||
if (tokenizeErr) { | ||
error('Some of the credit card details are wrong. Please update them.') | ||
} else { | ||
document.querySelector('input#braintree_nonce')?.setAttribute('value', payload?.nonce ?? '') | ||
form.submit() | ||
} | ||
}) | ||
}) | ||
}) | ||
.catch((e: BraintreeError) => { | ||
console.error('Something is wrong with Braintree', e.message) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.