Skip to content

Commit e49733c

Browse files
committed
Release Version - 0.52.0
1 parent 56345bf commit e49733c

19 files changed

+486
-3
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@boldcommerce/checkout-frontend-library",
3-
"version": "0.51.0",
3+
"version": "0.52.0",
44
"main": "lib/index.js",
55
"types": "lib/index.d.ts",
66
"license": "MIT",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
apiTypeKeys,
3+
checkApiResponse,
4+
fetchAPI,
5+
getApiOptions,
6+
getApiUrl,
7+
IApiReturnObject,
8+
ICreatePaymentGatewayOrderRequest,
9+
keysToTestFromResponse,
10+
} from 'src';
11+
12+
/** addPayment
13+
*
14+
* Adds a new payment to the order.
15+
*
16+
* @param {@link ICreatePaymentGatewayOrderRequest} requestBody - create payment gateway order for specific payment gateways.
17+
* @param {number} [numOfRetries=0] - Number of retries for some HTTP errors - Number from 0 to 5
18+
*
19+
* @returns {Promise<IApiReturnObject>} response
20+
*/
21+
export async function createPaymentGatewayOrder(requestBody: ICreatePaymentGatewayOrderRequest, numOfRetries= 0): Promise<IApiReturnObject> {
22+
const {createPaymentGatewayOrder} = apiTypeKeys;
23+
const options = getApiOptions(createPaymentGatewayOrder, requestBody);
24+
const url = getApiUrl(createPaymentGatewayOrder);
25+
const fetchRes = await fetchAPI(url, options, numOfRetries);
26+
return checkApiResponse(fetchRes, [keysToTestFromResponse.data, keysToTestFromResponse.applicationState]);
27+
}

src/payment/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from './addPayment';
22
export * from './deletePayment';
33
export * from './deleteGiftCardPayment';
44
export * from './getPaymentList';
5+
export * from './createPaymentGatewayOrder';
56
export * from './updatePayment';

src/state/setApplicationState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function setApplicationState({
2424
order_meta_data,
2525
shipping,
2626
order_total,
27+
order_balance,
2728
resumable_link,
2829
created_via,
2930
is_processed,
@@ -47,6 +48,7 @@ export function setApplicationState({
4748
applicationState.display_exchange_rate = display_exchange_rate;
4849
}
4950
applicationState.order_total = order_total;
51+
applicationState.order_balance = order_balance;
5052
applicationState.resumable_link = resumable_link;
5153
applicationState.created_via = created_via;
5254
applicationState.is_processed = is_processed;

src/types/apiInterfaces.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface IApiSuccessResponse {
1717
ICssStylingPaymentIframeResponse |
1818
ICheckInventoryResponse |
1919
IAddPaymentResponse |
20+
ICreatePaymentGatewayOrderResponse |
2021
IUpdateLineItemQuantityResponse |
2122
IProcessOrderResponse |
2223
IPatchOrderMetaDataResponse |
@@ -103,6 +104,7 @@ export interface IAlternatePaymentMethodType {
103104
BRAINTREE_GOOGLE: string;
104105
BRAINTREE_APPLE: string;
105106
PPCP_APPLE: string;
107+
PPCP: string;
106108
}
107109

108110
export type IInventoryStage = 'initial' | 'final';
@@ -212,6 +214,15 @@ export interface IAddPaymentResponse {
212214
application_state: IApplicationState | undefined;
213215
}
214216

217+
export interface ICreatePaymentGatewayOrderPaypalResponse {
218+
id: string
219+
}
220+
221+
export interface ICreatePaymentGatewayOrderResponse {
222+
data: ICreatePaymentGatewayOrderPaypalResponse | Record<string, unknown>;
223+
application_state: IApplicationState | undefined;
224+
}
225+
215226
export interface IGetPaymentIframeUrl {
216227
url: string | undefined;
217228
}
@@ -261,6 +272,7 @@ export interface IApiTypes {
261272
checkInventory: IApiTypesDetail;
262273
validateDiscount: IApiTypesDetail;
263274
getPaymentList: IApiTypesDetail;
275+
createPaymentGatewayOrder: IApiTypesDetail;
264276
addPayment: IApiTypesDetail;
265277
updatePayment: IApiTypesDetail;
266278
deletePayment: IApiTypesDetail;
@@ -298,6 +310,7 @@ export interface IApiTypeKeys {
298310
validateDiscount: keyof IApiTypes;
299311
getPaymentList: keyof IApiTypes;
300312
addPayment: keyof IApiTypes;
313+
createPaymentGatewayOrder: keyof IApiTypes;
301314
updatePayment: keyof IApiTypes;
302315
deletePayment: keyof IApiTypes;
303316
deleteGiftCardPayment: keyof IApiTypes;
@@ -347,7 +360,7 @@ export interface IApiTypesDetail {
347360
keysToTest?: Array<string>;
348361
}
349362

350-
export type IAlternativePaymentMethod = Array<IExpressPayStripe | IExpressPayPaypal | IExpressPayBraintreeGoogle | IExpressPayBraintreeApple | IExpressPayPaypalCommercePlatform> ;
363+
export type IAlternativePaymentMethod = Array<IExpressPayStripe | IExpressPayPaypal | IExpressPayBraintreeGoogle | IExpressPayBraintreeApple | IExpressPayPaypalCommercePlatform | IExpressPayPaypalCommercePlatformButton> ;
351364
export type IExternalPaymentGateways = Array<IExternalPaymentGateway>;
352365

353366
export interface IOrderInitialData {
@@ -434,6 +447,19 @@ export interface IExpressPayPaypalCommercePlatform {
434447
merchant_id: string;
435448
}
436449

450+
export interface IExpressPayPaypalCommercePlatformButton {
451+
'public_id': string,
452+
'merchant_id': string,
453+
'partner_id': string,
454+
'is_3ds_enabled': boolean,
455+
'style': Record<string, unknown>,
456+
'apple_pay_enabled': boolean,
457+
'type': string,
458+
'merchant_country': string,
459+
'payment_types': Record<string, unknown>,
460+
'is_dev': boolean
461+
}
462+
437463
export interface IExpressPayBraintree {
438464
type: string;
439465
public_id: string;
@@ -497,6 +523,7 @@ export interface IApplicationState {
497523
discounts: Array<IDiscount>;
498524
payments: Array<IPayment>;
499525
order_total: number;
526+
order_balance: number;
500527
order_meta_data: IOrderMetaData;
501528
currency: ICurrency;
502529
display_currency: ICurrency | null;
@@ -729,6 +756,16 @@ export interface IAddPaymentRequest {
729756
custom_attributes?: Record<string, string|number|boolean>;
730757
}
731758

759+
export interface ICreatePaymentGatewayOrderPaypalPayload {
760+
payment_type: string;
761+
locale: string;
762+
}
763+
764+
export interface ICreatePaymentGatewayOrderRequest{
765+
gateway_type: string;
766+
payment_data: ICreatePaymentGatewayOrderPaypalPayload | Record<string, unknown>
767+
}
768+
732769
export type IUpdatePaymentRequest = IAddPaymentRequest;
733770

734771
export type IDeletePaymentRequest = IAddPaymentRequest;
@@ -757,6 +794,7 @@ export type IGetApiOptionsBody =
757794
IUpdatePaymentRequest |
758795
IDeletePaymentRequest |
759796
IPatchOrderMetaDataRequest |
797+
ICreatePaymentGatewayOrderRequest |
760798
Record<string, unknown>;
761799

762800
export interface IShippingLine {

src/utils/buildAddressBatchRequest.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import {
3+
apiTypeKeys,
4+
getBillingAddress,
5+
getShippingAddress,
6+
IAddress,
7+
IBatchableRequest
8+
} from 'src';
9+
import {isObjectEmpty} from 'src/utils/isObjectEmpty';
10+
import {isObjectEquals} from 'src/utils/isObjectEquals';
11+
12+
13+
export const buildAddressBatchRequest = (
14+
address: IAddress,
15+
type: 'shipping'|'billing'
16+
): IBatchableRequest | null => {
17+
const {setBillingAddress, updateBillingAddress, setShippingAddress, updateShippingAddress} = apiTypeKeys;
18+
19+
const prevAddress = type === 'shipping' ? getShippingAddress() : getBillingAddress();
20+
const isPrevEmpty = isObjectEmpty(prevAddress);
21+
const isEqual = isObjectEquals(prevAddress, address);
22+
23+
if (isPrevEmpty && !isEqual) {
24+
return {
25+
apiType: type === 'shipping' ? setShippingAddress : setBillingAddress,
26+
payload: address
27+
};
28+
} else if (!isEqual) {
29+
return {
30+
apiType: type === 'shipping' ? updateShippingAddress : updateBillingAddress,
31+
payload: address
32+
};
33+
}
34+
return null;
35+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {apiTypeKeys, getApplicationState, IBatchableRequest} from 'src';
2+
import {isObjectEmpty, isObjectEquals} from 'src/utils';
3+
4+
5+
export const buildCustomerBatchRequest = (firstName: string, lastName: string, email: string): IBatchableRequest | null => {
6+
const {addGuestCustomer, updateCustomer} = apiTypeKeys;
7+
const {customer: prevCustomer} = getApplicationState();
8+
const previous = {
9+
email: prevCustomer.email_address,
10+
firstName: prevCustomer.first_name,
11+
lastName: prevCustomer.last_name
12+
};
13+
const userLogin = !!prevCustomer.platform_id && prevCustomer.platform_id !== '0';
14+
if (!userLogin) {
15+
const payload = {
16+
first_name: firstName,
17+
last_name: lastName,
18+
email_address: email,
19+
accepts_marketing: prevCustomer.accepts_marketing
20+
};
21+
if (isObjectEmpty(previous)) {
22+
return {apiType: addGuestCustomer, payload};
23+
} else if (!isObjectEquals({firstName, lastName, email}, previous)) {
24+
return {apiType: updateCustomer, payload};
25+
}
26+
}
27+
return null;
28+
};

src/utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ export * from './fetchAPI';
22
export * from './fetchError';
33
export * from './apiUrl';
44
export * from './getApiOptions';
5+
export * from './buildAddressBatchRequest';
6+
export * from './buildCustomerBatchRequest';
57
export * from './getSubrequestApiOptions';
68
export * from './apiResponse';
79
export * from './isObject';
810
export * from './findKeyInObject';
911
export * from './getErrorFromFieldName';
12+
export * from './isObjectEmpty';
13+
export * from './isObjectEquals';

src/utils/isObjectEmpty.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
2+
export function isObjectEmpty(object: any): boolean {
3+
return object ? Object.values(object).every(x => x === null || x === '' || x === undefined) : true;
4+
}

0 commit comments

Comments
 (0)