Skip to content

Commit

Permalink
feat(payment): PAYPAL-4937 updates after review
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-nick committed Dec 24, 2024
1 parent 7afaa0f commit 01ba966
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
39 changes: 5 additions & 34 deletions packages/core/src/payment/payment-method-request-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
HeadlessPaymentRequestOptions,
} from './headless-payment';
import PaymentMethod from './payment-method';
import paymentMethodTransformer from './payment-method-transformer';

export default class PaymentMethodRequestSender {
constructor(private _requestSender: RequestSender) {}
Expand Down Expand Up @@ -58,12 +59,12 @@ export default class PaymentMethodRequestSender {
methodId: string,
options: HeadlessPaymentRequestOptions,
): Promise<Response<PaymentMethod>> {
const entityId = this.getPaymentEntityId(methodId);
const entityId = this._getPaymentEntityId(methodId);

const graphQLQuery = `
query {
site {
paymentWalletWithInitializationData(filter: {paymentWalletEntityId: "${entityId}"}) {
paymentWalletWithInitializationData(filter: { paymentWalletEntityId: "${entityId}" }) {
clientToken
initializationData
}
Expand All @@ -83,40 +84,10 @@ export default class PaymentMethodRequestSender {

return this._requestSender
.post<HeadlessPaymentMethodResponse>('/graphql', requestOptions)
.then((response) => this.transformToPaymentMethodResponse(response, methodId));
.then((response) => paymentMethodTransformer(response, methodId));
}

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

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

private getPaymentEntityId(methodId: string): HeadlessPaymentMethodType {
private _getPaymentEntityId(methodId: string): HeadlessPaymentMethodType {
const entityId = HeadlessPaymentMethodConfig[methodId];

if (!entityId) {
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/payment/payment-method-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Response } from '@bigcommerce/request-sender';

import { HeadlessPaymentMethodResponse } from './headless-payment';
import PaymentMethod from './payment-method';

export default function paymentMethodTransformer(
response: Response<HeadlessPaymentMethodResponse>,
methodId: string,
): Response<PaymentMethod> {
const {
body: {
data: {
site: {
paymentWalletWithInitializationData: { clientToken, initializationData },
},
},
},
} = response;

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

0 comments on commit 01ba966

Please sign in to comment.