Skip to content

Commit 0219559

Browse files
author
Soc Sieng
committed
fix!: return paymentMethodPresent when ready to pay changes
1 parent f6085ee commit 0219559

9 files changed

Lines changed: 51 additions & 15 deletions

File tree

examples/angular/src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
}"
164164
(loadpaymentdata)="onLoadPaymentData($event)"
165165
(error)="onError($event)"
166+
(readytopaychange)="onReadyToPayChange($event)"
166167
[paymentAuthorizedCallback]="onPaymentDataAuthorized"
167168
></google-pay-button>
168169
</div>

examples/angular/src/app/app.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import '@google-pay/button-element';
18+
19+
import { ReadyToPayChangeResponse } from '@google-pay/button-element';
1820
import { Component } from '@angular/core';
1921

2022
@Component({
@@ -68,4 +70,8 @@ export class AppComponent {
6870
transactionState: 'SUCCESS',
6971
};
7072
};
73+
74+
onReadyToPayChange = (event: CustomEvent<ReadyToPayChangeResponse>): void => {
75+
console.log('ready to pay change', event.detail);
76+
};
7177
}

examples/react/src/examples/CryptogramExample.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ export default (props: any) => {
5858
onLoadPaymentData={paymentRequest => {
5959
console.log('Success', paymentRequest);
6060
}}
61-
onReadyToPayChange={isReady => {
62-
setIsReadyToPay(isReady);
61+
onReadyToPayChange={result => {
62+
console.log('ready to pay change', result);
63+
setIsReadyToPay(result.isReadyToPay);
6364
}}
6465
existingPaymentMethodRequired={props.existingPaymentMethodRequired}
6566
style={{

examples/react/src/examples/ShippingOptionsExample.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export default (props: any) => {
8080
onLoadPaymentData={paymentRequest => {
8181
console.log('Success', paymentRequest);
8282
}}
83+
onReadyToPayChange={result => {
84+
console.log('ready to pay change', result);
85+
}}
8386
existingPaymentMethodRequired={props.existingPaymentMethodRequired}
8487
buttonColor={props.buttonColor}
8588
buttonType={props.buttonType}

examples/vue/src/components/Examples.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
}"
162162
v-on:loadpaymentdata="onLoadPaymentData"
163163
v-on:error="onError"
164+
v-on:readytopaychange="onReadyToPayChange"
164165
v-bind:onPaymentAuthorized.prop="onPaymentDataAuthorized"
165166
></google-pay-button>
166167
</div>
@@ -219,6 +220,9 @@ export default {
219220
return {
220221
transactionState: 'SUCCESS',
221222
};
223+
},
224+
onReadyToPayChange: (event) => {
225+
console.log('ready to pay change', event.detail);
222226
}
223227
}
224228
}

src/button-element/GooglePayButton.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Alias, Notify, NotifyAttribute, NotifyBooleanAttribute } from '../lib/property-decorators';
18-
import { ButtonManager, Config } from '../lib/button-manager';
18+
import { ButtonManager, Config, ReadyToPayChangeResponse } from '../lib/button-manager';
1919
import { name as softwareId, version as softwareVersion } from './package.json';
2020
import { debounce } from '../lib/debounce';
2121

@@ -55,7 +55,7 @@ class GooglePayButton extends HTMLElement {
5555
onPaymentAuthorized?: google.payments.api.PaymentAuthorizedHandler;
5656

5757
@Alias('readyToPayChangeCallback')
58-
onReadyToPayChange?: (isReadyToPay: boolean) => void;
58+
onReadyToPayChange?: (result: ReadyToPayChangeResponse) => void;
5959

6060
@Alias('loadPaymentDataCallback')
6161
onLoadPaymentData?: (paymentData: google.payments.api.PaymentData) => void;
@@ -126,11 +126,11 @@ class GooglePayButton extends HTMLElement {
126126
onPaymentAuthorized: this.onPaymentAuthorized,
127127
buttonColor: this.buttonColor,
128128
buttonType: this.buttonType,
129-
onReadyToPayChange: isReadyToPay => {
129+
onReadyToPayChange: result => {
130130
if (this.onReadyToPayChange) {
131-
this.onReadyToPayChange(isReadyToPay);
131+
this.onReadyToPayChange(result);
132132
}
133-
this.dispatch('readytopaychange', isReadyToPay);
133+
this.dispatch('readytopaychange', result);
134134
},
135135
onCancel: reason => {
136136
if (this.onCancel) {

src/button-element/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
*/
1616

1717
import GooglePayButton from './GooglePayButton';
18+
export { ReadyToPayChangeResponse } from '../lib/button-manager';
1819

1920
export default GooglePayButton;

src/button-react/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
*/
1616

1717
import GooglePayButton from './GooglePayButton';
18+
export { ReadyToPayChangeResponse } from '../lib/button-manager';
1819

1920
export default GooglePayButton;

src/lib/button-manager.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
import { loadScript } from '../lib/load-script';
2020

21+
export interface ReadyToPayChangeResponse {
22+
isButtonVisible: boolean;
23+
isReadyToPay: boolean;
24+
paymentMethodPresent?: boolean;
25+
}
26+
2127
export interface Config {
2228
environment?: google.payments.api.Environment;
2329
existingPaymentMethodRequired?: boolean;
@@ -27,7 +33,7 @@ export interface Config {
2733
onLoadPaymentData?: (paymentData: google.payments.api.PaymentData) => void;
2834
onCancel?: (reason: google.payments.api.PaymentsError) => void;
2935
onError?: (error: Error) => void;
30-
onReadyToPayChange?: (isReadyToPay: boolean) => void;
36+
onReadyToPayChange?: (result: ReadyToPayChangeResponse) => void;
3137
buttonColor?: google.payments.api.ButtonColor;
3238
buttonType?: google.payments.api.ButtonType;
3339
}
@@ -52,6 +58,7 @@ export class ButtonManager {
5258
private oldInvalidationValues?: any[];
5359

5460
isReadyToPay?: boolean;
61+
paymentMethodPresent?: boolean;
5562

5663
constructor(options: ButtonManagerOptions) {
5764
this.options = options;
@@ -278,11 +285,12 @@ export class ButtonManager {
278285
this.setClassName(element, [element.className, 'not-ready']);
279286
element.appendChild(button);
280287

281-
let isReadyToPay = false;
288+
let showButton = false;
289+
let readyToPay: google.payments.api.IsReadyToPayResponse | undefined;
282290

283291
try {
284-
const readyToPay = await this.client.isReadyToPay(this.createIsReadyToPayRequest(this.config));
285-
isReadyToPay =
292+
readyToPay = await this.client.isReadyToPay(this.createIsReadyToPayRequest(this.config));
293+
showButton =
286294
(readyToPay.result && !this.config.existingPaymentMethodRequired) ||
287295
(readyToPay.result && readyToPay.paymentMethodPresent && this.config.existingPaymentMethodRequired) ||
288296
false;
@@ -292,18 +300,29 @@ export class ButtonManager {
292300

293301
if (!this.isMounted()) return;
294302

295-
if (isReadyToPay) {
303+
if (showButton) {
296304
// remove hidden className
297305
this.setClassName(
298306
element,
299307
(element.className || '').split(' ').filter(className => className && className !== 'not-ready'),
300308
);
301309
}
302310

303-
if (this.isReadyToPay !== isReadyToPay) {
304-
this.isReadyToPay = isReadyToPay;
311+
if (this.isReadyToPay !== readyToPay?.result || this.paymentMethodPresent !== readyToPay?.paymentMethodPresent) {
312+
this.isReadyToPay = !!readyToPay?.result;
313+
this.paymentMethodPresent = readyToPay?.paymentMethodPresent;
314+
305315
if (this.config.onReadyToPayChange) {
306-
this.config.onReadyToPayChange(isReadyToPay);
316+
const readyToPayResponse: ReadyToPayChangeResponse = {
317+
isButtonVisible: showButton,
318+
isReadyToPay: this.isReadyToPay,
319+
};
320+
321+
if (this.paymentMethodPresent) {
322+
readyToPayResponse.paymentMethodPresent = this.paymentMethodPresent;
323+
}
324+
325+
this.config.onReadyToPayChange(readyToPayResponse);
307326
}
308327
}
309328
}

0 commit comments

Comments
 (0)