Skip to content

Commit 060d1b0

Browse files
committed
feat(payment): PAYPAL-4937 updates related to new requirements
1 parent df96d5d commit 060d1b0

18 files changed

+99
-258
lines changed

packages/core/src/cart/cart-action-creator.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { RequestOptions } from '@bigcommerce/checkout-sdk/payment-integration-ap
66
import { InternalCheckoutSelectors } from '../checkout';
77
import { cachableAction } from '../common/data-store';
88
import ActionOptions from '../common/data-store/action-options';
9-
import { MissingDataError, MissingDataErrorType } from '../common/error/errors';
109

1110
import { CartActionType, LoadCartAction } from './cart-actions';
1211
import CartRequestSender from './cart-request-sender';
@@ -22,22 +21,12 @@ export default class CartActionCreator {
2221
return (store) => {
2322
return Observable.create((observer: Observer<LoadCartAction>) => {
2423
const state = store.getState();
25-
const jwtToken = state.config.getStorefrontJwtToken();
26-
27-
if (!jwtToken) {
28-
throw new MissingDataError(MissingDataErrorType.MissingPaymentToken);
29-
}
24+
const host = state.config.getHost();
3025

3126
observer.next(createAction(CartActionType.LoadCartRequested, undefined));
3227

3328
this._cartRequestSender
34-
.loadCard(cartId, {
35-
headers: {
36-
Authorization: `Bearer ${jwtToken}`,
37-
'Content-Type': 'application/json',
38-
},
39-
...options,
40-
})
29+
.loadCard(cartId, host, options)
4130
.then((response) => {
4231
observer.next(
4332
createAction(CartActionType.LoadCartSucceeded, response.body),

packages/core/src/cart/cart-request-sender.ts

Lines changed: 19 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,18 @@ import { ContentType, RequestOptions, SDK_VERSION_HEADERS } from '../common/http
66

77
import { LineItemMap } from './index';
88

9-
interface LoadCartRequestOptions extends RequestOptions {
10-
body?: { query: string };
11-
headers: { Authorization: string; [key: string]: string };
12-
}
13-
149
interface LoadCartResponse {
15-
data: {
16-
site: {
17-
cart: {
18-
amount: {
19-
currencyCode: string;
20-
};
21-
entityId: string;
22-
lineItems: {
23-
physicalItems: Array<{
24-
name: string;
25-
entityId: string;
26-
quantity: string;
27-
productEntityId: string;
28-
}>;
29-
};
30-
};
31-
};
10+
amount: {
11+
currencyCode: string;
12+
};
13+
entityId: string;
14+
lineItems: {
15+
physicalItems: Array<{
16+
name: string;
17+
entityId: string;
18+
quantity: string;
19+
productEntityId: string;
20+
}>;
3221
};
3322
}
3423

@@ -48,113 +37,27 @@ export default class CartRequestSender {
4837
return this._requestSender.post(url, { body, headers, timeout });
4938
}
5039

51-
loadCard(cartId: string, options: LoadCartRequestOptions): Promise<Response<Cart>> {
52-
const url = `/graphql`;
53-
54-
const graphQLQuery = `
55-
query {
56-
site {
57-
cart(entityId: "${cartId}") {
58-
currencyCode
59-
entityId
60-
id
61-
isTaxIncluded
62-
discounts {
63-
entityId
64-
discountedAmount {
65-
currencyCode
66-
value
67-
}
68-
}
69-
discountedAmount {
70-
currencyCode
71-
value
72-
}
73-
baseAmount {
74-
currencyCode
75-
value
76-
}
77-
amount {
78-
currencyCode
79-
value
80-
}
81-
lineItems {
82-
physicalItems {
83-
brand
84-
couponAmount {
85-
value
86-
}
87-
discountedAmount {
88-
value
89-
}
90-
discounts {
91-
discountedAmount {
92-
value
93-
}
94-
entityId
95-
}
96-
extendedListPrice {
97-
value
98-
}
99-
extendedSalePrice {
100-
value
101-
}
102-
giftWrapping {
103-
amount {
104-
value
105-
}
106-
message
107-
name
108-
}
109-
isShippingRequired
110-
isTaxable
111-
listPrice {
112-
value
113-
}
114-
name
115-
originalPrice {
116-
value
117-
}
118-
entityId
119-
quantity
120-
salePrice {
121-
value
122-
}
123-
sku
124-
url
125-
}
126-
}
127-
}
128-
}
129-
}`;
40+
loadCard(cartId: string, host?: string, options?: RequestOptions): Promise<Response<Cart>> {
41+
const path = 'cart-information';
42+
const url = host ? `${host}/${path}` : `/${path}`;
13043

131-
const requestOptions: LoadCartRequestOptions = {
44+
const requestOptions: RequestOptions = {
13245
...options,
133-
headers: {
134-
...options.headers,
135-
'Content-Type': 'application/json',
136-
},
137-
body: {
138-
query: graphQLQuery,
46+
params: {
47+
cartId,
13948
},
14049
};
14150

14251
return this._requestSender
143-
.post<LoadCartResponse>(url, {
52+
.get<LoadCartResponse>(url, {
14453
...requestOptions,
14554
})
14655
.then(this.transformToCartResponse);
14756
}
14857

14958
private transformToCartResponse(response: Response<LoadCartResponse>): Response<Cart> {
15059
const {
151-
body: {
152-
data: {
153-
site: {
154-
cart: { amount, entityId, lineItems },
155-
},
156-
},
157-
},
60+
body: { amount, entityId, lineItems },
15861
} = response;
15962

16063
const mappedLineItems: LineItemMap = {

packages/core/src/checkout-buttons/create-headless-checkout-wallet-initializer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ import HeadlessCheckoutWalletStrategyActionCreator from './headless-checkout-wal
1414
export default function createHeadlessCheckoutWalletInitializer(
1515
options?: HeadlessCheckoutWalletInitializerOptions,
1616
): HeadlessCheckoutWalletInitializer {
17-
const { host, locale = 'en', storefrontJwtToken, siteLink } = options ?? {};
17+
const { host, locale = 'en' } = options ?? {};
1818

1919
const config: ConfigState = {
2020
meta: {
2121
host,
2222
locale,
23-
storefrontJwtToken,
24-
siteLink,
2523
},
2624
errors: {},
2725
statuses: {},
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export default interface HeadlessCheckoutWalletInitializerOptions {
22
host?: string;
33
locale?: string;
4-
storefrontJwtToken?: string;
5-
siteLink?: string;
64
}

packages/core/src/checkout-buttons/headless-checkout-wallet-initializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { isElementId, setUniqueElementId } from '../common/dom';
55

66
import { CheckoutButtonInitializeOptions, CheckoutButtonOptions } from './checkout-button-options';
77
import CheckoutButtonSelectors from './checkout-button-selectors';
8-
import HeadlessCheckoutWalletStrategyActionCreator from './headless-checkout-wallet-strategy-action-creator';
98
import createCheckoutButtonSelectors from './create-checkout-button-selectors';
9+
import HeadlessCheckoutWalletStrategyActionCreator from './headless-checkout-wallet-strategy-action-creator';
1010

1111
@bind
1212
export default class HeadlessCheckoutWalletInitializer {

packages/core/src/config/config-selector.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export default interface ConfigSelector {
1818
getHost(): string | undefined;
1919
getLocale(): string | undefined;
2020
getVariantIdentificationToken(): string | undefined;
21-
getStorefrontJwtToken(): string | undefined;
22-
getSiteLink(): string | undefined;
2321
getLoadError(): Error | undefined;
2422
isLoading(): boolean;
2523
}
@@ -102,16 +100,6 @@ export function createConfigSelectorFactory(): ConfigSelectorFactory {
102100
(data) => () => data,
103101
);
104102

105-
const getStorefrontJwtToken = createSelector(
106-
(state: ConfigState) => state.meta && state.meta.storefrontJwtToken,
107-
(data) => () => data,
108-
);
109-
110-
const getSiteLink = createSelector(
111-
(state: ConfigState) => state.meta && state.meta.siteLink,
112-
(data) => () => data,
113-
);
114-
115103
const getLoadError = createSelector(
116104
(state: ConfigState) => state.errors.loadError,
117105
(error) => () => error,
@@ -134,8 +122,6 @@ export function createConfigSelectorFactory(): ConfigSelectorFactory {
134122
getHost: getHost(state),
135123
getLocale: getLocale(state),
136124
getVariantIdentificationToken: getVariantIdentificationToken(state),
137-
getStorefrontJwtToken: getStorefrontJwtToken(state),
138-
getSiteLink: getSiteLink(state),
139125
getLoadError: getLoadError(state),
140126
isLoading: isLoading(state),
141127
};

packages/core/src/config/config-state.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export interface ConfigMetaState {
1212
variantIdentificationToken?: string;
1313
host?: string;
1414
locale?: string;
15-
storefrontJwtToken?: string;
16-
siteLink?: string;
1715
}
1816

1917
export interface ConfigErrorsState {

packages/core/src/payment-integration/create-payment-integration-selectors.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export default function createPaymentIntegrationSelectors({
1414
getStoreConfig,
1515
getStoreConfigOrThrow,
1616
getConfig,
17-
getStorefrontJwtToken,
18-
getSiteLink,
1917
},
2018
consignments: { getConsignments, getConsignmentsOrThrow },
2119
countries: { getCountries },
@@ -82,8 +80,6 @@ export default function createPaymentIntegrationSelectors({
8280
getPaymentStatusOrThrow,
8381
getPaymentRedirectUrl,
8482
getPaymentRedirectUrlOrThrow,
85-
getStorefrontJwtToken,
86-
getSiteLink,
8783
getPaymentMethod: clone(getPaymentMethod),
8884
getPaymentMethodOrThrow: clone(getPaymentMethodOrThrow),
8985
getPaymentMethodsMeta: clone(getPaymentMethodsMeta),

packages/core/src/payment/payment-method-action-creator.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Observable, Observer } from 'rxjs';
44

55
import { InternalCheckoutSelectors } from '../checkout';
66
import { ActionOptions, cachableAction } from '../common/data-store';
7-
import { MissingDataError, MissingDataErrorType } from '../common/error/errors';
87
import { RequestOptions } from '../common/http-request';
98

109
import {
@@ -169,11 +168,7 @@ export default class PaymentMethodActionCreator {
169168
return (store) =>
170169
Observable.create((observer: Observer<LoadPaymentMethodAction>) => {
171170
const state = store.getState();
172-
const jwtToken = state.config.getStorefrontJwtToken();
173-
174-
if (!jwtToken) {
175-
throw new MissingDataError(MissingDataErrorType.MissingPaymentToken);
176-
}
171+
const host = state.config.getHost();
177172

178173
observer.next(
179174
createAction(PaymentMethodActionType.LoadPaymentMethodRequested, undefined, {
@@ -182,13 +177,7 @@ export default class PaymentMethodActionCreator {
182177
);
183178

184179
this._requestSender
185-
.loadPaymentWalletWithInitializationData(methodId, {
186-
headers: {
187-
Authorization: `Bearer ${jwtToken}`,
188-
'Content-Type': 'application/json',
189-
},
190-
...options,
191-
})
180+
.loadPaymentWalletWithInitializationData(methodId, host, options)
192181
.then((response) => {
193182
observer.next(
194183
createAction(

packages/core/src/payment/payment-method-request-sender.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ const paymentMethodConfig: Record<string, HeadlessPaymentMethodType> = {
2020
paypalcommercecredit: HeadlessPaymentMethodType.PAYPALCOMMERCECREDIT,
2121
};
2222

23-
interface LoadPaymentWalletWithInitializationDataRequestOptions extends RequestOptions {
24-
body?: { query: string };
25-
headers: { Authorization: string; [key: string]: string };
26-
}
27-
2823
interface LoadPaymentMethodResponse<T = any> {
2924
data: {
3025
site: {
@@ -78,31 +73,17 @@ export default class PaymentMethodRequestSender {
7873

7974
loadPaymentWalletWithInitializationData(
8075
methodId: string,
81-
options: LoadPaymentWalletWithInitializationDataRequestOptions,
76+
host?: string,
77+
{ timeout }: RequestOptions = {},
8278
): Promise<Response<PaymentMethod>> {
83-
const url = `/graphql`;
84-
85-
const entityId = this.getPaymentEntityId(methodId);
86-
87-
const graphQLQuery = `
88-
query {
89-
site {
90-
paymentWalletWithInitializationData(filter: {paymentWalletEntityId: "${entityId}"}) {
91-
clientToken
92-
initializationData
93-
}
94-
}
95-
}
96-
`;
97-
98-
const requestOptions: LoadPaymentWalletWithInitializationDataRequestOptions = {
99-
headers: {
100-
...options.headers,
101-
'Content-Type': 'application/json',
102-
},
79+
const path = 'get-initialization-data';
80+
const url = host ? `${host}/${path}` : `/${path}`;
81+
82+
const requestOptions = {
10383
body: {
104-
query: graphQLQuery,
84+
entityId: this.getPaymentEntityId(methodId),
10585
},
86+
timeout,
10687
};
10788

10889
return this._requestSender

packages/payment-integration-api/src/payment-integration-selectors.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ export default interface PaymentIntegrationSelectors {
8383

8484
getConfig(): Config | undefined;
8585

86-
getStorefrontJwtToken(): string | undefined;
87-
88-
getSiteLink(): string | undefined;
89-
9086
getInstrumentsMeta(): InstrumentMeta | undefined;
9187

9288
getOrderMeta(): OrderMetaState | undefined;

packages/payment-integrations-test-utils/src/test-utils/payment-integration-service.mock.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ const state = {
5959
getPaymentRedirectUrl: jest.fn(),
6060
getPaymentRedirectUrlOrThrow: jest.fn(),
6161
isPaymentDataRequired: jest.fn(),
62-
getStorefrontJwtToken: jest.fn(),
63-
getSiteLink: jest.fn(),
6462
};
6563

6664
const createBuyNowCart = jest.fn(() => Promise.resolve(getCart()));

0 commit comments

Comments
 (0)