Skip to content
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

feat(payment): PAYPAL-4510 POC #2622

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BraintreeIntegrationService,
LocalPaymentInstance,
LocalPaymentsPayload,
NonInstantLocalPaymentMethods,
onPaymentStartData,
StartPaymentError,
} from '@bigcommerce/checkout-sdk/braintree-utils';
Expand All @@ -18,6 +19,7 @@ import {
PaymentMethodInvalidError,
PaymentRequestOptions,
PaymentStrategy,
isRequestError,
} from '@bigcommerce/checkout-sdk/payment-integration-api';
import { LoadingIndicator } from '@bigcommerce/checkout-sdk/ui';

Expand Down Expand Up @@ -68,9 +70,14 @@ export default class BraintreeLocalMethodsPaymentStrategy implements PaymentStra
await this.paymentIntegrationService.loadPaymentMethod(gatewayId);

const state = this.paymentIntegrationService.getState();
const storeConfig = state.getStoreConfigOrThrow();
const paymentMethod = state.getPaymentMethodOrThrow<BraintreeInitializationData>(gatewayId);

if (this.isNonInstantPaymentMethod(paymentMethod.method)) {
return;
}

const { clientToken, config, initializationData } = paymentMethod;
const storeConfig = state.getStoreConfigOrThrow();

if (!clientToken || !initializationData) {
throw new MissingDataError(MissingDataErrorType.MissingPaymentMethod);
Expand Down Expand Up @@ -104,7 +111,7 @@ export default class BraintreeLocalMethodsPaymentStrategy implements PaymentStra
const cart = state.getCartOrThrow();
const sessionId = await this.braintreeIntegrationService.getSessionId();
const billing = state.getBillingAddressOrThrow();
const { firstName, lastName, countryCode } = billing;
const { firstName, lastName, countryCode, phone } = billing;
const { currency, email, lineItems } = cart;
const isShippingRequired = lineItems.physicalItems.length > 0;
const grandTotal = state.getCheckoutOrThrow().outstandingBalance;
Expand All @@ -115,6 +122,44 @@ export default class BraintreeLocalMethodsPaymentStrategy implements PaymentStra

this.toggleLoadingIndicator(true);

if (this.isNonInstantPaymentMethod(payment.methodId)) {
const { methodId } = payment;
const paymentData = {
formattedPayload: {
vault_payment_instrument: null,
set_as_default_stored_instrument: null,
device_info: sessionId || null,
method_id: methodId,
[`${payment.methodId}_account`]: {
// take phone number from paymentData
phone
},
},
};

try {
await this.paymentIntegrationService.submitOrder();
await this.paymentIntegrationService.submitPayment({
methodId,
paymentData,
});
} catch (error) {
if (isRequestError(error)) {
let redirectUrl = error.body.additional_action_required.data.redirect_url;

if (redirectUrl) {
return new Promise(() => window.location.replace(redirectUrl));
}
}

this.toggleLoadingIndicator(false);

return Promise.reject(error);
}

return;
}

if (!this.localPaymentInstance) {
throw new PaymentMethodInvalidError();
}
Expand Down Expand Up @@ -216,4 +261,13 @@ export default class BraintreeLocalMethodsPaymentStrategy implements PaymentStra
onError(error);
}
}

/**
*
* Utils
*
* */
private isNonInstantPaymentMethod(methodId: string): boolean {
return methodId.toUpperCase() in NonInstantLocalPaymentMethods;
}
}
10 changes: 10 additions & 0 deletions packages/braintree-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,13 @@ export interface BraintreeError extends Error {
code: string | BraintreeErrorCode.KountNotEnabled;
details?: unknown;
}

/**
*
* Braintree non-instant payment methods
*
*/

export enum NonInstantLocalPaymentMethods {
TRUSTLY = 'trustly'
}