Skip to content

Commit 2820c9d

Browse files
refactor(payment): Moved BT Credit card payment strategy
1 parent 48e8552 commit 2820c9d

File tree

11 files changed

+419
-548
lines changed

11 files changed

+419
-548
lines changed

packages/braintree-integration/src/braintree-credit-card/braintree-credit-card-payment-strategy-initialize-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
BraintreeError,
33
BraintreeFormOptions,
4-
BraintreeThreeDSecureOptions,
54
} from '@bigcommerce/checkout-sdk/braintree-utils';
65
import { StandardError } from '@bigcommerce/checkout-sdk/payment-integration-api';
76
import { RequestOptions } from '@bigcommerce/request-sender';
7+
import { BraintreeThreeDSecureOptions } from '@bigcommerce/checkout-sdk/braintree-integration';
88

99
export interface PaymentRequestOptions extends RequestOptions {
1010
/**

packages/braintree-integration/src/braintree-credit-card/braintree-credit-card-payment-strategy.spec.ts

Lines changed: 299 additions & 524 deletions
Large diffs are not rendered by default.

packages/braintree-integration/src/braintree-credit-card/braintree-credit-card-payment-strategy.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ export default class BraintreeCreditCardPaymentStrategy implements PaymentStrate
5656
}
5757

5858
try {
59-
// this.braintreePaymentProcessor.initialize(clientToken, braintree);
60-
this.braintreeIntegrationService.initialize(clientToken);
59+
this.braintreePaymentProcessor.initialize(clientToken, braintree);
6160

6261
if (this.isHostedPaymentFormEnabled(methodId, gatewayId) && braintree?.form) {
6362
await this.braintreePaymentProcessor.initializeHostedForm(
@@ -69,7 +68,6 @@ export default class BraintreeCreditCardPaymentStrategy implements PaymentStrate
6968
}
7069

7170
this.is3dsEnabled = this.paymentMethod.config.is3dsEnabled;
72-
// this.deviceSessionId = await this.braintreePaymentProcessor.getSessionId();
7371
this.deviceSessionId = await this.braintreeIntegrationService.getSessionId();
7472

7573
// TODO: remove this part when BT AXO A/B testing will be finished
@@ -289,7 +287,6 @@ export default class BraintreeCreditCardPaymentStrategy implements PaymentStrate
289287
if (!clientToken) {
290288
throw new MissingDataError(MissingDataErrorType.MissingPaymentMethod);
291289
}
292-
293290
this.braintreeIntegrationService.initialize(clientToken);
294291

295292
await this.braintreeIntegrationService.getBraintreeFastlane(cart.id, config.testMode);

packages/braintree-integration/src/braintree-hosted-form/braintree-hosted-form.spec.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { EventEmitter } from 'events';
2-
3-
import { getBillingAddress } from '../../../billing/billing-addresses.mock';
4-
import { NotInitializedError } from '../../../common/error/errors';
5-
import { PaymentInvalidFormError } from '../../errors';
6-
7-
import { BraintreeHostedFields } from './braintree';
8-
import BraintreeHostedForm from './braintree-hosted-form';
9-
import { BraintreeFormOptions } from './braintree-payment-options';
10-
import BraintreeSDKCreator from './braintree-sdk-creator';
2+
import { BraintreeHostedFields } from '@bigcommerce/checkout-sdk/braintree-utils';
3+
import { BraintreeFormOptions } from '../braintree-payment-options';
4+
import { BraintreeHostedForm } from '@bigcommerce/checkout-sdk/braintree-integration';
5+
import { NotInitializedError, PaymentInvalidFormError } from '@bigcommerce/checkout-sdk/payment-integration-api';
6+
import { getBillingAddress } from '@bigcommerce/checkout-sdk/braintree-utils';
117

128
describe('BraintreeHostedForm', () => {
139
let braintreeSdkCreator: Pick<BraintreeSDKCreator, 'createHostedFields'>;

packages/braintree-integration/src/braintree-hosted-form/braintree-hosted-form.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
BraintreeHostedFields,
88
BraintreeHostedFieldsCreatorConfig,
99
BraintreeHostedFieldsState,
10-
BraintreeHostedFormError, BraintreeIntegrationService, BraintreeScriptLoader,
10+
BraintreeHostedFormError,
11+
BraintreeIntegrationService,
12+
BraintreeScriptLoader,
1113
TokenizationPayload,
1214
} from '@bigcommerce/checkout-sdk/braintree-utils';
1315
import {
@@ -96,6 +98,7 @@ export default class BraintreeHostedForm {
9698
}
9799

98100
const braintreeHostedFormState = this.cardFields.getState();
101+
console.log('CARD FIELDS', this.cardFields.getState());
99102

100103
if (!this.isValidForm(braintreeHostedFormState)) {
101104
this.handleValidityChange(braintreeHostedFormState);
@@ -151,10 +154,8 @@ export default class BraintreeHostedForm {
151154
async createHostedFields(
152155
options: Pick<BraintreeHostedFieldsCreatorConfig, 'fields' | 'styles'>,
153156
): Promise<BraintreeHostedFields> {
154-
const [client, hostedFields] = await Promise.all([
155-
this.braintreeIntegrationService.getClient(),
156-
this.braintreeScriptLoader.loadHostedFields(),
157-
]);
157+
const client = await this.braintreeIntegrationService.getClient();
158+
const hostedFields = await this.braintreeScriptLoader.loadHostedFields();
158159

159160
return hostedFields.create({ ...options, client });
160161
}
@@ -524,6 +525,7 @@ export default class BraintreeHostedForm {
524525
};
525526

526527
private isValidForm(event: BraintreeHostedFieldsState): boolean {
528+
console.log('isValidForm', event.fields);
527529
return (
528530
Object.keys(event.fields) as Array<keyof BraintreeHostedFieldsState['fields']>
529531
).every((key) => event.fields[key]?.isValid);

packages/braintree-integration/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ export { default as createBraintreeVisaCheckoutCustomerStrategy } from './braint
4747
export { default as createBraintreeVenmoButtonStrategy } from './braintree-venmo/create-braintree-venmo-button-strategy';
4848

4949
export { default as BraintreeHostedForm } from './braintree-hosted-form/braintree-hosted-form';
50+
export * from './braintree-payment-options';

packages/braintree-integration/src/mocks/braintree.mock.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
Braintree3DSVerifyCardCallback,
2+
Braintree3DSVerifyCardCallback, BraintreeClient, BraintreeModule, BraintreeModuleCreator,
33
BraintreeThreeDSecure,
44
PaypalButtonStyleColorOption,
55
} from '@bigcommerce/checkout-sdk/braintree-utils';
@@ -8,6 +8,7 @@ import {
88
DefaultCheckoutButtonHeight,
99
PaymentMethod,
1010
} from '@bigcommerce/checkout-sdk/payment-integration-api';
11+
import { BraintreeThreeDSecureOptions } from '@bigcommerce/checkout-sdk/braintree-integration';
1112

1213
export function getBraintreeAcceleratedCheckoutPaymentMethod(): PaymentMethod {
1314
return {
@@ -27,6 +28,79 @@ export function getBraintreeAcceleratedCheckoutPaymentMethod(): PaymentMethod {
2728
};
2829
}
2930

31+
export function getTokenizeResponseBody(): BraintreeTokenizeResponse {
32+
return {
33+
creditCards: [
34+
{
35+
nonce: 'demo_nonce',
36+
details: {
37+
bin: 'demo_bin',
38+
cardType: 'Visa',
39+
expirationMonth: '01',
40+
expirationYear: '2025',
41+
lastFour: '0001',
42+
lastTwo: '01',
43+
},
44+
description: 'ending in 01',
45+
type: 'CreditCard',
46+
binData: {
47+
commercial: 'bin_data_commercial',
48+
countryOfIssuance: 'bin_data_country_of_issuance',
49+
debit: 'bin_data_debit',
50+
durbinRegulated: 'bin_data_durbin_regulated',
51+
healthcare: 'bin_data_healthcare',
52+
issuingBank: 'bin_data_issuing_bank',
53+
payroll: 'bin_data_payroll',
54+
prepaid: 'bin_data_prepaid',
55+
productId: 'bin_data_product_id',
56+
},
57+
},
58+
],
59+
};
60+
}
61+
62+
export function getThreeDSecureOptionsMock(): BraintreeThreeDSecureOptions {
63+
return {
64+
addFrame: jest.fn(),
65+
removeFrame: jest.fn(),
66+
additionalInformation: {
67+
acsWindowSize: '01',
68+
},
69+
};
70+
}
71+
72+
export function getModuleCreatorMock<T>(
73+
module: BraintreeModule | BraintreeClient,
74+
): BraintreeModuleCreator<T> {
75+
return {
76+
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
77+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
78+
// @ts-ignore
79+
create: jest.fn(() => Promise.resolve(module)),
80+
};
81+
}
82+
83+
export function getBillingAddress() {
84+
return {
85+
id: '55c96cda6f04c',
86+
firstName: 'Test',
87+
lastName: 'Tester',
88+
89+
company: 'Bigcommerce',
90+
address1: '12345 Testing Way',
91+
address2: '',
92+
city: 'Some City',
93+
stateOrProvince: 'California',
94+
stateOrProvinceCode: 'CA',
95+
country: 'United States',
96+
countryCode: 'US',
97+
postalCode: '95555',
98+
shouldSaveAddress: true,
99+
phone: '555-555-5555',
100+
customFields: [],
101+
};
102+
}
103+
30104
export function getThreeDSecureMock(): BraintreeThreeDSecure {
31105
return {
32106
// eslint-disable-next-line @typescript-eslint/no-unused-expressions

packages/braintree-utils/src/braintree-integration-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export default class BraintreeIntegrationService {
100100
if (!this.client) {
101101
const clientToken = this.getClientTokenOrThrow();
102102
const clientCreator = await this.braintreeScriptLoader.loadClient();
103-
104103
this.client = clientCreator.create({ authorization: clientToken });
105104
}
106105

packages/braintree-utils/src/braintree-payment-processor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ import {
1818
BraintreeFormOptions,
1919
BraintreeIntegrationService,
2020
BraintreePaypalCheckout,
21-
BraintreePaypalSdkCreatorConfig, BraintreeRequestData,
21+
BraintreePaypalSdkCreatorConfig,
22+
BraintreeRequestData,
2223
BraintreeThreeDSecure,
23-
BraintreeThreeDSecureOptions,
2424
BraintreeVenmoCheckout,
2525
BraintreeVenmoCreatorConfig,
2626
BraintreeVerifyPayload,
2727
TokenizationPayload,
2828
} from './index';
29-
import { BraintreePaymentInitializeOptions } from '../../braintree-integration/src/braintree-payment-options';
29+
import {
30+
BraintreePaymentInitializeOptions,
31+
BraintreeThreeDSecureOptions,
32+
} from '@bigcommerce/checkout-sdk/braintree-integration';
3033
import isCreditCardInstrumentLike from './is-credit-card-instrument-like';
3134
import { BraintreeHostedForm } from '@bigcommerce/checkout-sdk/braintree-integration';
3235

packages/braintree-utils/src/mocks/braintree.mock.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '../types';
2323

2424
import { getVisaCheckoutTokenizedPayload } from './visacheckout.mock';
25+
import BillingAddress from '../../../core/src/billing/billing-address';
2526

2627
export function getBraintree(): PaymentMethod {
2728
return {
@@ -409,3 +410,24 @@ export function getBraintreeAddress(): BraintreeShippingAddressOverride {
409410
recipientName: 'Test Tester',
410411
};
411412
}
413+
414+
export function getBillingAddress(): BillingAddress {
415+
return {
416+
id: '55c96cda6f04c',
417+
firstName: 'Test',
418+
lastName: 'Tester',
419+
420+
company: 'Bigcommerce',
421+
address1: '12345 Testing Way',
422+
address2: '',
423+
city: 'Some City',
424+
stateOrProvince: 'California',
425+
stateOrProvinceCode: 'CA',
426+
country: 'United States',
427+
countryCode: 'US',
428+
postalCode: '95555',
429+
shouldSaveAddress: true,
430+
phone: '555-555-5555',
431+
customFields: [],
432+
};
433+
}

0 commit comments

Comments
 (0)