Skip to content

Commit 824514d

Browse files
committed
feature(payment): GooglePay-3307 Add requireShippingAddress prop to initialize call
1 parent 919bbf8 commit 824514d

13 files changed

+71
-9
lines changed

docs/interfaces/GooglePayCustomerInitializeOptions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [buttonColor](GooglePayCustomerInitializeOptions.md#buttoncolor)
1010
- [buttonType](GooglePayCustomerInitializeOptions.md#buttontype)
1111
- [container](GooglePayCustomerInitializeOptions.md#container)
12+
- [requireShippingAddress](GooglePayCustomerInitializeOptions.md#requireshippingaddress)
1213

1314
### Methods
1415

packages/google-pay-integration/src/gateways/google-pay-adyenv2-gateway.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ export default class GooglePayAdyenV2 extends GooglePayGateway {
3737

3838
async initialize(
3939
getPaymentMethod: () => PaymentMethod<GooglePayInitializationData>,
40+
requireShippingAddress?: boolean,
4041
isBuyNowFlow?: boolean,
4142
currencyCode?: string,
4243
): Promise<void> {
43-
await super.initialize(getPaymentMethod, isBuyNowFlow, currencyCode);
44+
await super.initialize(
45+
getPaymentMethod,
46+
requireShippingAddress,
47+
isBuyNowFlow,
48+
currencyCode,
49+
);
4450

4551
const paymentMethod = super.getPaymentMethod();
4652
const state = this._service.getState();

packages/google-pay-integration/src/gateways/google-pay-adyenv3-gateway.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ export default class GooglePayAdyenV3 extends GooglePayGateway {
3737

3838
async initialize(
3939
getPaymentMethod: () => PaymentMethod<GooglePayInitializationData>,
40+
requireShippingAddress?: boolean,
4041
isBuyNowFlow?: boolean,
4142
currencyCode?: string,
4243
): Promise<void> {
43-
await super.initialize(getPaymentMethod, isBuyNowFlow, currencyCode);
44+
await super.initialize(
45+
getPaymentMethod,
46+
requireShippingAddress,
47+
isBuyNowFlow,
48+
currencyCode,
49+
);
4450

4551
const paymentMethod = super.getPaymentMethod();
4652
const state = this._service.getState();

packages/google-pay-integration/src/gateways/google-pay-braintree-gateway.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ export default class GooglePayBraintreeGateway extends GooglePayGateway {
3636

3737
async initialize(
3838
getPaymentMethod: () => PaymentMethod<GooglePayInitializationData>,
39+
requireShippingAddress?: boolean,
3940
isBuyNowFlow?: boolean,
4041
currencyCode?: string,
4142
): Promise<void> {
42-
await super.initialize(getPaymentMethod, isBuyNowFlow, currencyCode);
43+
await super.initialize(
44+
getPaymentMethod,
45+
requireShippingAddress,
46+
isBuyNowFlow,
47+
currencyCode,
48+
);
4349

4450
let paymentMethod = super.getPaymentMethod();
4551

packages/google-pay-integration/src/gateways/google-pay-gateway.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('GooglePayGateway', () => {
149149
totalPrice: '0',
150150
};
151151

152-
await gateway.initialize(getGeneric, true, 'USD');
152+
await gateway.initialize(getGeneric, false, true, 'USD');
153153

154154
expect(gateway.getTransactionInfo()).toStrictEqual(expectedInfo);
155155
expect(paymentIntegrationService.getState().getCartOrThrow).not.toHaveBeenCalled();
@@ -158,7 +158,7 @@ describe('GooglePayGateway', () => {
158158
describe('should fail if:', () => {
159159
it('currencyCode is not passed (Buy Now flow)', async () => {
160160
try {
161-
await gateway.initialize(getGeneric, true);
161+
await gateway.initialize(getGeneric, false, true);
162162
} catch (error) {
163163
expect(error).toBeInstanceOf(InvalidArgumentError);
164164
}
@@ -232,6 +232,22 @@ describe('GooglePayGateway', () => {
232232

233233
await expect(gateway.getRequiredData()).resolves.toStrictEqual(expectedRequiredData);
234234
});
235+
236+
it('should require shipping address and options', async () => {
237+
const expectedRequiredData = {
238+
emailRequired: true,
239+
shippingAddressRequired: true,
240+
shippingOptionRequired: true,
241+
shippingAddressParameters: {
242+
phoneNumberRequired: true,
243+
allowedCountryCodes: ['AU', 'US', 'JP'],
244+
},
245+
};
246+
247+
await gateway.initialize(getGeneric, true);
248+
249+
await expect(gateway.getRequiredData()).resolves.toStrictEqual(expectedRequiredData);
250+
});
235251
});
236252

237253
describe('#getCallbackIntents', () => {

packages/google-pay-integration/src/gateways/google-pay-gateway.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343

4444
export default class GooglePayGateway {
4545
private _getPaymentMethodFn?: () => PaymentMethod<GooglePayInitializationData>;
46+
private _requireShippingAddress?: boolean;
4647
private _isBuyNowFlow = false;
4748
private _currencyCode?: string;
4849
private _currencyService?: CurrencyService;
@@ -258,10 +259,12 @@ export default class GooglePayGateway {
258259

259260
initialize(
260261
getPaymentMethod: () => PaymentMethod<GooglePayInitializationData>,
262+
requireShippingAddress?: boolean,
261263
isBuyNowFlow?: boolean,
262264
currencyCode?: string,
263265
): Promise<void> {
264266
this._getPaymentMethodFn = getPaymentMethod;
267+
this._requireShippingAddress = requireShippingAddress;
265268
this._isBuyNowFlow = Boolean(isBuyNowFlow);
266269
this._currencyCode = currencyCode;
267270

@@ -446,6 +449,10 @@ export default class GooglePayGateway {
446449
}
447450

448451
private _isShippingAddressRequired(): boolean {
452+
if (this._requireShippingAddress) {
453+
return true;
454+
}
455+
449456
const { getCartOrThrow, getStoreConfig, getShippingAddress } =
450457
this._paymentIntegrationService.getState();
451458

packages/google-pay-integration/src/google-pay-bigcommerce-payments/google-pay-bigcommerce-payments-gateway.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default class GooglePayBigCommercePaymentsGateway extends GooglePayGatewa
3131

3232
async initialize(
3333
getPaymentMethod: () => PaymentMethod<GooglePayPayPalCommerceInitializationData>,
34+
requireShippingAddress?: boolean,
3435
isBuyNowFlow?: boolean,
3536
currencyCode?: string,
3637
): Promise<void> {
@@ -40,7 +41,7 @@ export default class GooglePayBigCommercePaymentsGateway extends GooglePayGatewa
4041
throw new MissingDataError(MissingDataErrorType.MissingPaymentMethod);
4142
}
4243

43-
await super.initialize(getPaymentMethod, isBuyNowFlow, currency);
44+
await super.initialize(getPaymentMethod, requireShippingAddress, isBuyNowFlow, currency);
4445

4546
const paymentMethod = super.getPaymentMethod();
4647

packages/google-pay-integration/src/google-pay-button-strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default class GooglePayButtonStrategy implements CheckoutButtonStrategy {
9595
await this._googlePayPaymentProcessor.initialize(
9696
() => paymentMethod,
9797
this._getGooglePayClientOptions(),
98+
false,
9899
!!buyNowInitializeOptions,
99100
currencyCode,
100101
);

packages/google-pay-integration/src/google-pay-customer-initialize-options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export default interface GooglePayCustomerInitializeOptions {
3030
*/
3131
buttonType?: GooglePayButtonType;
3232

33+
/**
34+
* Require GooglePay to provide shipping address and methods.
35+
*/
36+
requireShippingAddress?: boolean;
37+
3338
/**
3439
* A callback that gets called when GooglePay fails to initialize or
3540
* selects a payment option.

packages/google-pay-integration/src/google-pay-customer-strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default class GooglePayCustomerStrategy implements CustomerStrategy {
6666
await this._googlePayPaymentProcessor.initialize(
6767
() => paymentMethod,
6868
this._getGooglePayClientOptions(paymentMethod.initializationData?.storeCountry),
69+
googlePayOptions.requireShippingAddress,
6970
);
7071
} catch {
7172
return;

0 commit comments

Comments
 (0)