Skip to content

Commit 76ebcc6

Browse files
Implementing the wallet payment endpoints (#15)
* Implementing the wallet payment endpoints --------- Co-authored-by: shagufa-ali <80478231+shagufa-ali@users.noreply.github.com>
1 parent ebf1a7c commit 76ebcc6

12 files changed

+339
-59
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export * from './orderMetaData';
3434
export * from './paymentIframe';
3535
export * from './order';
3636
export * from './externalPaymentGateway';
37+
export * from './walletPay';
3738
export {
3839
getApplicationState,
3940
getAddresses,

src/payment/createPaymentGatewayOrder.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/payment/index.ts

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

src/types/apiInterfaces.ts

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export interface IApiSuccessResponse {
1717
ICssStylingPaymentIframeResponse |
1818
ICheckInventoryResponse |
1919
IAddPaymentResponse |
20-
ICreatePaymentGatewayOrderResponse |
20+
IWalletPayCreateOrderResponse |
21+
IWalletPayOnShippingResponse |
22+
IWalletPayOnApproveResponse |
2123
IUpdateLineItemQuantityResponse |
2224
IProcessOrderResponse |
2325
IPatchOrderMetaDataResponse |
@@ -214,12 +216,22 @@ export interface IAddPaymentResponse {
214216
application_state: IApplicationState | undefined;
215217
}
216218

217-
export interface ICreatePaymentGatewayOrderPaypalResponse {
219+
export interface IWalletPayCreateOrderPaypalResponse {
218220
id: string
219221
}
220222

221-
export interface ICreatePaymentGatewayOrderResponse {
222-
payment_data: ICreatePaymentGatewayOrderPaypalResponse | Record<string, unknown>;
223+
export interface IWalletPayCreateOrderResponse {
224+
payment_data: IWalletPayCreateOrderPaypalResponse | Record<string, unknown>;
225+
application_state: IApplicationState | undefined;
226+
}
227+
228+
export interface IWalletPayOnShippingResponse {
229+
payment_data: Record<string, unknown>;
230+
application_state: IApplicationState | undefined;
231+
}
232+
233+
export interface IWalletPayOnApproveResponse {
234+
payment_data: Record<string, unknown>;
223235
application_state: IApplicationState | undefined;
224236
}
225237

@@ -273,14 +285,16 @@ export interface IApiTypes {
273285
checkInventory: IApiTypesDetail;
274286
validateDiscount: IApiTypesDetail;
275287
getPaymentList: IApiTypesDetail;
276-
createPaymentGatewayOrder: IApiTypesDetail;
277288
addPayment: IApiTypesDetail;
278289
updatePayment: IApiTypesDetail;
279290
deletePayment: IApiTypesDetail;
280291
deleteGiftCardPayment: IApiTypesDetail;
281292
patchOrderMetaData: IApiTypesDetail;
282293
batchRequest: IApiTypesDetail;
283294
addLog: IApiTypesDetail;
295+
walletPayCreateOrder: IApiTypesDetail,
296+
walletPayOnShipping: IApiTypesDetail,
297+
walletPayOnApprove: IApiTypesDetail,
284298
}
285299

286300
export interface IApiTypeKeys {
@@ -311,13 +325,15 @@ export interface IApiTypeKeys {
311325
validateDiscount: keyof IApiTypes;
312326
getPaymentList: keyof IApiTypes;
313327
addPayment: keyof IApiTypes;
314-
createPaymentGatewayOrder: keyof IApiTypes;
315328
updatePayment: keyof IApiTypes;
316329
deletePayment: keyof IApiTypes;
317330
deleteGiftCardPayment: keyof IApiTypes;
318331
patchOrderMetaData: keyof IApiTypes;
319332
batchRequest: keyof IApiTypes;
320333
addLog: keyof IApiTypes;
334+
walletPayCreateOrder: keyof IApiTypes;
335+
walletPayOnShipping: keyof IApiTypes;
336+
walletPayOnApprove: keyof IApiTypes;
321337
}
322338

323339
export interface IValidateAddress {
@@ -758,14 +774,38 @@ export interface IAddPaymentRequest {
758774
wallet_pay_type?: 'applepay'|'paywithgoogle'|'paypal',
759775
}
760776

761-
export interface ICreatePaymentGatewayOrderPaypalPayload {
777+
export interface IWalletPayCreateOrderPaypalPayload {
762778
payment_type: string;
763779
locale: string;
764780
}
765781

766-
export interface ICreatePaymentGatewayOrderRequest{
782+
export interface IWalletPayOnShippingPaypalPayload {
783+
payment_action_type: string;
784+
locale: string;
785+
paypal_order_id: string;
786+
shipping_address?: Record<string, unknown>;
787+
shipping_options?: Record<string, unknown>;
788+
}
789+
790+
export interface IWalletPayOnApprovePaypalPayload {
791+
payment_action_type: string;
792+
locale: string;
793+
paypal_order_id: string;
794+
}
795+
796+
export interface IWalletPayCreateOrderRequest{
797+
gateway_type: string;
798+
payment_data: IWalletPayCreateOrderPaypalPayload | Record<string, unknown>
799+
}
800+
801+
export interface IWalletPayOnShippingRequest{
802+
gateway_type: string;
803+
payment_data: IWalletPayOnShippingPaypalPayload | Record<string, unknown>
804+
}
805+
806+
export interface IWalletPayOnApproveRequest{
767807
gateway_type: string;
768-
payment_data: ICreatePaymentGatewayOrderPaypalPayload | Record<string, unknown>
808+
payment_data: IWalletPayOnApprovePaypalPayload | Record<string, unknown>
769809
}
770810

771811
export type IUpdatePaymentRequest = IAddPaymentRequest;
@@ -796,7 +836,9 @@ export type IGetApiOptionsBody =
796836
IUpdatePaymentRequest |
797837
IDeletePaymentRequest |
798838
IPatchOrderMetaDataRequest |
799-
ICreatePaymentGatewayOrderRequest |
839+
IWalletPayOnApproveRequest |
840+
IWalletPayCreateOrderRequest |
841+
IWalletPayOnShippingRequest |
800842
Record<string, unknown>;
801843

802844
export interface IShippingLine {

src/variables/constants.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,6 @@ export const apiTypes: IApiTypes = {
256256
useJwt: true,
257257
keysToTest: [...appStateKeysToTest]
258258
},
259-
createPaymentGatewayOrder: {
260-
path: '/payment_gateway_order',
261-
method: methods.POST,
262-
useJwt: true,
263-
keysToTest: [...appStateKeysToTest]
264-
},
265259
addPayment: {
266260
path: '/payments',
267261
method: methods.POST,
@@ -303,7 +297,25 @@ export const apiTypes: IApiTypes = {
303297
method: methods.POST,
304298
useJwt: true,
305299
keysToTest: [keysToTestFromResponse.data]
306-
}
300+
},
301+
walletPayCreateOrder: {
302+
path: '/wallet_pay/create_order',
303+
method: methods.POST,
304+
useJwt: true,
305+
keysToTest: [...appStateKeysToTest]
306+
},
307+
walletPayOnShipping: {
308+
path: '/wallet_pay/on_shipping',
309+
method: methods.POST,
310+
useJwt: true,
311+
keysToTest: [...appStateKeysToTest]
312+
},
313+
walletPayOnApprove: {
314+
path: '/wallet_pay/on_approve',
315+
method: methods.POST,
316+
useJwt: true,
317+
keysToTest: [...appStateKeysToTest]
318+
},
307319
};
308320

309321
export const apiTypeKeys: IApiTypeKeys = {
@@ -333,14 +345,16 @@ export const apiTypeKeys: IApiTypeKeys = {
333345
updateItem: 'updateItem',
334346
checkInventory: 'checkInventory',
335347
getPaymentList: 'getPaymentList',
336-
createPaymentGatewayOrder: 'createPaymentGatewayOrder',
337348
addPayment: 'addPayment',
338349
updatePayment: 'updatePayment',
339350
deletePayment: 'deletePayment',
340351
deleteGiftCardPayment: 'deleteGiftCardPayment',
341352
patchOrderMetaData: 'patchOrderMetaData',
342353
batchRequest: 'batchRequest',
343-
addLog: 'addLog'
354+
addLog: 'addLog',
355+
walletPayCreateOrder: 'walletPayCreateOrder',
356+
walletPayOnShipping: 'walletPayOnShipping',
357+
walletPayOnApprove: 'walletPayOnApprove',
344358
};
345359

346360
export const baseReturnObject: IApiReturnObject = {

src/walletPay/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './walletPayOnShipping';
2+
export * from './walletPayOnApprove';
3+
export * from './walletPayCreateOrder';

src/walletPay/walletPayCreateOrder.ts

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+
IWalletPayCreateOrderRequest,
9+
keysToTestFromResponse,
10+
} from 'src';
11+
12+
/** walletPayCreateOrder
13+
*
14+
* create new order on payment gateway
15+
*
16+
* @param {@link IWalletPayCreateOrderRequest} 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 walletPayCreateOrder(requestBody: IWalletPayCreateOrderRequest, numOfRetries= 0): Promise<IApiReturnObject> {
22+
const {walletPayCreateOrder} = apiTypeKeys;
23+
const options = getApiOptions(walletPayCreateOrder, requestBody);
24+
const url = getApiUrl(walletPayCreateOrder);
25+
const fetchRes = await fetchAPI(url, options, numOfRetries);
26+
return checkApiResponse(fetchRes, [keysToTestFromResponse.data, keysToTestFromResponse.applicationState]);
27+
}

src/walletPay/walletPayOnApprove.ts

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+
IWalletPayOnApproveRequest,
9+
keysToTestFromResponse,
10+
} from 'src';
11+
12+
/** walletPayOnApprove
13+
*
14+
* handles the on approve change event for wallet pay
15+
*
16+
* @param {@link IWalletPayOnApproveRequest} requestBody - on approve data from the wallet payment
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 walletPayOnApprove(requestBody: IWalletPayOnApproveRequest, numOfRetries= 0): Promise<IApiReturnObject> {
22+
const {walletPayOnApprove} = apiTypeKeys;
23+
const options = getApiOptions(walletPayOnApprove, requestBody);
24+
const url = getApiUrl(walletPayOnApprove);
25+
const fetchRes = await fetchAPI(url, options, numOfRetries);
26+
return checkApiResponse(fetchRes, [keysToTestFromResponse.data, keysToTestFromResponse.applicationState]);
27+
}

src/walletPay/walletPayOnShipping.ts

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+
IWalletPayOnShippingRequest,
9+
keysToTestFromResponse,
10+
} from 'src';
11+
12+
/** walletPayOnShipping
13+
*
14+
* handles the on shipping change event for wallet pay
15+
*
16+
* @param {@link IWalletPayOnShippingRequest} requestBody - on shipping data from the wallet payment
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 walletPayOnShipping(requestBody: IWalletPayOnShippingRequest, numOfRetries= 0): Promise<IApiReturnObject> {
22+
const {walletPayOnShipping} = apiTypeKeys;
23+
const options = getApiOptions(walletPayOnShipping, requestBody);
24+
const url = getApiUrl(walletPayOnShipping);
25+
const fetchRes = await fetchAPI(url, options, numOfRetries);
26+
return checkApiResponse(fetchRes, [keysToTestFromResponse.data, keysToTestFromResponse.applicationState]);
27+
}

tests/payment/createPaymentGatewayOrder.test.ts renamed to tests/walletPay/walletPayCreateOrder.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
apiTypeKeys,
33
baseReturnObject,
4-
createPaymentGatewayOrder,
54
methods,
6-
ICreatePaymentGatewayOrderRequest,
7-
ICreatePaymentGatewayOrderResponse
5+
IWalletPayCreateOrderRequest,
6+
IWalletPayCreateOrderResponse,
7+
walletPayCreateOrder
88
} from 'src';
99
import * as fetchAPI from 'src/utils/fetchAPI';
1010
import * as getApiOptions from 'src/utils/getApiOptions';
@@ -14,22 +14,22 @@ import * as apiResponse from 'src/utils/apiResponse';
1414

1515
describe('testing create payment gateway order ', () => {
1616
const returnObject = {...baseReturnObject};
17-
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/payment_gateway_order';
17+
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/wallet-pay/create-order';
1818
let optionsMock: RequestInit;
1919
let getApiOptionsSpy: jest.SpyInstance;
2020
let getApiUrlSpy: jest.SpyInstance;
2121
let fetchApiSpy: jest.SpyInstance;
2222
let checkApiResponseSpy: jest.SpyInstance;
2323

24-
const initPaymentPayload: ICreatePaymentGatewayOrderRequest = {
24+
const initPaymentPayload: IWalletPayCreateOrderRequest = {
2525
payment_data: {
2626
payment_type: 'paypal',
2727
locale: 'en-US',
2828
},
2929
gateway_type: 'paypal'
3030
};
3131

32-
const response: ICreatePaymentGatewayOrderResponse = {
32+
const response: IWalletPayCreateOrderResponse = {
3333
payment_data: {id: '1234'},
3434
application_state: applicationStateMock
3535
};
@@ -50,13 +50,13 @@ describe('testing create payment gateway order ', () => {
5050
});
5151

5252
test('calling init Payment', async () => {
53-
const res = await createPaymentGatewayOrder(initPaymentPayload);
53+
const res = await walletPayCreateOrder(initPaymentPayload);
5454

5555
expect(getApiOptionsSpy).toHaveBeenCalledTimes(1);
5656
expect(getApiUrlSpy).toHaveBeenCalledTimes(1);
5757
expect(fetchApiSpy).toHaveBeenCalledTimes(1);
58-
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.createPaymentGatewayOrder, initPaymentPayload);
59-
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.createPaymentGatewayOrder);
58+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.walletPayCreateOrder, initPaymentPayload);
59+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.walletPayCreateOrder);
6060
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
6161
expect(res).toStrictEqual(returnObject);
6262
});
@@ -66,13 +66,13 @@ describe('testing create payment gateway order ', () => {
6666
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);
6767
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
6868

69-
const res = await createPaymentGatewayOrder(initPaymentPayload, 1);
69+
const res = await walletPayCreateOrder(initPaymentPayload, 1);
7070

7171
expect(getApiOptionsSpy).toHaveBeenCalledTimes(1);
7272
expect(getApiUrlSpy).toHaveBeenCalledTimes(1);
7373
expect(fetchApiSpy).toHaveBeenCalledTimes(1);
74-
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.createPaymentGatewayOrder, initPaymentPayload);
75-
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.createPaymentGatewayOrder);
74+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.walletPayCreateOrder, initPaymentPayload);
75+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.walletPayCreateOrder);
7676
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
7777
expect(res).toStrictEqual(tempReturnObject);
7878
});

0 commit comments

Comments
 (0)