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-4937 Update PaymentRequestSender with new request to proxy server #2754

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion packages/core/src/common/http-request/responses.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Response } from '@bigcommerce/request-sender';

import { ErrorResponseBody } from '@bigcommerce/checkout-sdk/payment-integration-api';

import { PaymentResponse } from '../../payment';
import { HeadlessPaymentMethodResponse, PaymentResponse } from '../../payment';
import HeadlessPaymentMethod from '../../payment/headless-payment-method';

export function getResponse<T>(
body: T,
Expand Down Expand Up @@ -38,6 +39,27 @@ export function getPaymentResponse<T>(
};
}

export function getHeadlessPaymentResponse<T>(
site: HeadlessPaymentMethod<T>,
headers = {},
status = 200,
statusText = 'OK',
): Response<HeadlessPaymentMethodResponse<T>> {
return {
body: {
data: {
site,
},
},
status,
statusText,
headers: {
'content-type': 'application/json',
...headers,
},
};
}

export function getErrorResponse(
body = getErrorResponseBody(),
headers = {},
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/payment/headless-payment-method-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { HeadlessPaymentMethodType } from './headless-payment-method-type';

const HeadlessPaymentMethodConfig: Record<string, HeadlessPaymentMethodType> = {
paypalcommerce: HeadlessPaymentMethodType.PAYPALCOMMERCE,
paypalcommercecredit: HeadlessPaymentMethodType.PAYPALCOMMERCECREDIT,
};

export default HeadlessPaymentMethodConfig;
7 changes: 7 additions & 0 deletions packages/core/src/payment/headless-payment-method-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import HeadlessPaymentMethod from './headless-payment-method';

export interface HeadlessPaymentMethodResponse<T = any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it work for you, if any be changed with unknown?

Suggested change
export interface HeadlessPaymentMethodResponse<T = any> {
export interface HeadlessPaymentMethodResponse<T = unknown> {

Copy link
Contributor

@bc-peng bc-peng Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we not able to define the actual type here?

Copy link
Contributor Author

@bc-nick bc-nick Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bc-peng removed T parameter as unnecessary in this case. we can define type as string

data: {
site: HeadlessPaymentMethod<T>;
};
}
4 changes: 4 additions & 0 deletions packages/core/src/payment/headless-payment-method-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum HeadlessPaymentMethodType {
PAYPALCOMMERCE = 'paypalcommerce.paypal',
PAYPALCOMMERCECREDIT = 'paypalcommerce.paypalcredit',
}
6 changes: 6 additions & 0 deletions packages/core/src/payment/headless-payment-method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default interface HeadlessPaymentMethod<T = any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any type be changed to unknown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to use the any type in this case for matching initializationData type with PaymentMethod interface which also uses any for initializationData.

paymentWalletWithInitializationData: {
clientToken?: string;
initializationData?: T;
};
}
19 changes: 19 additions & 0 deletions packages/core/src/payment/headless-payment-methods.mock.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move this to headless-payment?

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import HeadlessPaymentMethod from './headless-payment-method';

export const initializationData = {
merchantId: '100000',
paymentButtonStyles: {
checkoutTopButtonStyles: { color: 'blue', label: 'checkout', height: '36' },
},
};

export const encodedInitializationData = btoa(JSON.stringify(initializationData));

export function getHeadlessPaymentMethod(): HeadlessPaymentMethod {
return {
paymentWalletWithInitializationData: {
clientToken: 'clientToken',
initializationData: encodedInitializationData,
},
};
}
6 changes: 6 additions & 0 deletions packages/core/src/payment/headless-payment-request-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RequestOptions } from '../common/http-request';

export default interface HeadlessPaymentRequestOptions extends RequestOptions {
body?: { query: string };
headers: { Authorization: string; [key: string]: string };
}
5 changes: 5 additions & 0 deletions packages/core/src/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
export * from './payment-method-actions';
export * from './payment-method-types';
export * from './payment-status-types';
export * from './headless-payment-method-type';

export {
default as PaymentAdditionalAction,
Expand Down Expand Up @@ -37,12 +38,15 @@ export {
WithMollieIssuerInstrument,
} from './payment';
export { default as PaymentMethod } from './payment-method';
export { default as HeadlessPaymentMethod } from './headless-payment-method';
export { default as PaymentMethodMeta } from './payment-method-meta';
export { default as PaymentMethodConfig } from './payment-method-config';
export { default as HeadlessPaymentMethodConfig } from './headless-payment-method-config';
export { default as InitializationStrategy } from './payment-method-initialization-strategy';
export { default as PaymentMethodActionCreator } from './payment-method-action-creator';
export { default as paymentMethodReducer } from './payment-method-reducer';
export { default as PaymentMethodRequestSender } from './payment-method-request-sender';
export { default as HeadlessPaymentRequestOptions } from './headless-payment-request-options';
export {
default as PaymentMethodSelector,
PaymentMethodSelectorFactory,
Expand All @@ -55,6 +59,7 @@ export { default as PaymentRequestSender } from './payment-request-sender';
export { default as PaymentRequestTransformer } from './payment-request-transformer';
export { default as PaymentResponse } from './payment-response';
export { default as PaymentResponseBody } from './payment-response-body';
export { HeadlessPaymentMethodResponse } from './headless-payment-method-response';
export {
default as PaymentSelector,
PaymentSelectorFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
} from '@bigcommerce/request-sender';

import { ContentType, INTERNAL_USE_ONLY, SDK_VERSION_HEADERS } from '../common/http-request';
import { getResponse } from '../common/http-request/responses.mock';
import { getHeadlessPaymentResponse, getResponse } from '../common/http-request/responses.mock';

import { HeadlessPaymentMethodResponse } from './headless-payment-method-response';
import { getHeadlessPaymentMethod, initializationData } from './headless-payment-methods.mock';
import PaymentMethod from './payment-method';
import PaymentMethodRequestSender from './payment-method-request-sender';
import { getPaymentMethod, getPaymentMethods } from './payment-methods.mock';
Expand Down Expand Up @@ -136,4 +138,46 @@ describe('PaymentMethodRequestSender', () => {
});
});
});

describe('#loadPaymentWalletWithInitializationData()', () => {
let response: Response<HeadlessPaymentMethodResponse>;
const authorization = 'authorization-1234';

beforeEach(() => {
response = getHeadlessPaymentResponse(getHeadlessPaymentMethod());
jest.spyOn(requestSender, 'post').mockReturnValue(Promise.resolve(response));
});

it('loads headless payment method', async () => {
const walletInitData =
await paymentMethodRequestSender.loadPaymentWalletWithInitializationData(
'paypalcommerce',
{ headers: { Authorization: authorization } },
);

expect(requestSender.post).toHaveBeenCalledWith(
'/graphql',
expect.objectContaining({
headers: {
Authorization: authorization,
'Content-Type': 'application/json',
},
}),
);

expect(walletInitData).toEqual(
expect.objectContaining({
body: {
initializationData,
clientToken: 'clientToken',
id: 'paypalcommerce',
config: {},
method: '',
supportedCards: [],
type: 'PAYMENT_TYPE_API',
},
}),
);
});
});
});
79 changes: 79 additions & 0 deletions packages/core/src/payment/payment-method-request-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
SDK_VERSION_HEADERS,
} from '../common/http-request';

import HeadlessPaymentMethodConfig from './headless-payment-method-config';
import { HeadlessPaymentMethodResponse } from './headless-payment-method-response';
import { HeadlessPaymentMethodType } from './headless-payment-method-type';
import HeadlessPaymentRequestOptions from './headless-payment-request-options';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we group them into a headless-payment folder for better orgnization as I can see things are quite different here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the separate folder

import PaymentMethod from './payment-method';

export default class PaymentMethodRequestSender {
Expand Down Expand Up @@ -44,4 +48,79 @@ export default class PaymentMethodRequestSender {
params,
});
}

/**
* GraphQL payment requests
*/
loadPaymentWalletWithInitializationData(
methodId: string,
options: HeadlessPaymentRequestOptions,
): Promise<Response<PaymentMethod>> {
const url = `/graphql`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be reused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


const entityId = this.getPaymentEntityId(methodId);

const graphQLQuery = `
query {
site {
paymentWalletWithInitializationData(filter: {paymentWalletEntityId: "${entityId}"}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
paymentWalletWithInitializationData(filter: {paymentWalletEntityId: "${entityId}"}) {
paymentWalletWithInitializationData(filter: { paymentWalletEntityId: "${entityId}"}) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

clientToken
initializationData
}
}
}
`;

const requestOptions: HeadlessPaymentRequestOptions = {
headers: {
...options.headers,
'Content-Type': 'application/json',
},
body: {
query: graphQLQuery,
},
};

return this._requestSender
.post<HeadlessPaymentMethodResponse<string>>(url, requestOptions)
.then((response) => this.transformToPaymentMethodResponse(response, methodId));
}

private transformToPaymentMethodResponse(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private transformToPaymentMethodResponse(
private _transformToPaymentMethodResponse(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also maybe refactor this to a separate transformer file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the separate file

response: Response<HeadlessPaymentMethodResponse>,
methodId: string,
): Response<PaymentMethod> {
const {
body: {
data: {
site: { paymentWalletWithInitializationData },
},
},
} = response;

return {
...response,
body: {
initializationData: JSON.parse(
atob(paymentWalletWithInitializationData.initializationData),
),
clientToken: paymentWalletWithInitializationData.clientToken,
id: methodId,
config: {},
method: '',
supportedCards: [],
type: 'PAYMENT_TYPE_API',
},
};
}

private getPaymentEntityId(methodId: string): HeadlessPaymentMethodType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private getPaymentEntityId(methodId: string): HeadlessPaymentMethodType {
private _getPaymentEntityId(methodId: string): HeadlessPaymentMethodType {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const entityId = HeadlessPaymentMethodConfig[methodId];

if (!entityId) {
throw new Error('Unable to get payment entity id.');
}
Comment on lines +81 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen after throwing this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not be able to make a request in this case. It will be catch with PaymentMethodActionCreator when i add it by the next PR


return entityId;
}
}