Skip to content

Commit

Permalink
feat!: remove logic to infer callbackIntents based on callback methods
Browse files Browse the repository at this point in the history
Need to discuss with the rest of the team about whether or not this logic should be introduced.

Issue #13 has been raised to track this.
  • Loading branch information
Soc Sieng committed Jun 16, 2020
1 parent 5982614 commit 49e5ad2
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 29 deletions.
9 changes: 6 additions & 3 deletions examples/angular/src/app/app.component.html
Expand Up @@ -90,7 +90,8 @@
totalPrice: amount,
currencyCode: 'USD',
countryCode: 'US'
}
},
callbackIntents: ['PAYMENT_AUTHORIZATION']
}"
(loadpaymentdata)="onLoadPaymentData($event)"
(error)="onError($event)"
Expand Down Expand Up @@ -131,7 +132,8 @@
totalPrice: amount,
currencyCode: 'USD',
countryCode: 'US'
}
},
callbackIntents: ['PAYMENT_AUTHORIZATION']
}"
(loadpaymentdata)="onLoadPaymentData($event)"
(error)="onError($event)"
Expand All @@ -158,7 +160,8 @@
currencyCode: 'USD',
countryCode: 'US'
},
shippingAddressRequired: true
shippingAddressRequired: true,
callbackIntents: ['PAYMENT_AUTHORIZATION']
}"
(loadpaymentdata)="onLoadPaymentData($event)"
(error)="onError($event)"
Expand Down
3 changes: 3 additions & 0 deletions examples/html/app.js
Expand Up @@ -132,6 +132,7 @@ const googlePayButtons = [
},
},
existingPaymentRequired: controls.existingPaymentRequired.value === 'true',
callbackIntents: ['PAYMENT_AUTHORIZATION'],
onLoadPaymentData,
onPaymentAuthorized,
};
Expand All @@ -151,6 +152,7 @@ const googlePayButtons = [
},
},
existingPaymentRequired: controls.existingPaymentRequired.value === 'true',
callbackIntents: ['PAYMENT_AUTHORIZATION'],
onLoadPaymentData,
onPaymentAuthorized,
};
Expand All @@ -171,6 +173,7 @@ const googlePayButtons = [
},
existingPaymentRequired: controls.existingPaymentRequired.value === 'true',
shippingAddressRequired: true,
callbackIntents: ['PAYMENT_AUTHORIZATION'],
onLoadPaymentData,
onPaymentAuthorized,
};
Expand Down
1 change: 1 addition & 0 deletions examples/react/src/examples/DisplayItemsExample.tsx
Expand Up @@ -70,6 +70,7 @@ export default (props: any) => {
},
],
},
callbackIntents: ['PAYMENT_AUTHORIZATION'],
}}
onLoadPaymentData={paymentRequest => {
console.log('Success', paymentRequest);
Expand Down
1 change: 1 addition & 0 deletions examples/react/src/examples/DynamicPriceUpdateExample.tsx
Expand Up @@ -90,6 +90,7 @@ const paymentRequest: google.payments.api.PaymentDataRequest = {
description: o.description,
})),
},
callbackIntents: ['SHIPPING_ADDRESS', 'SHIPPING_OPTION'],
};

export default (props: any) => {
Expand Down
Expand Up @@ -53,6 +53,7 @@ export default (props: any) => {
countryCode: 'US',
},
shippingAddressRequired: true,
callbackIntents: ['SHIPPING_ADDRESS'],
}}
onLoadPaymentData={paymentRequest => {
console.log('Success', paymentRequest);
Expand Down
1 change: 1 addition & 0 deletions examples/react/src/examples/PaymentDataChangedExample.tsx
Expand Up @@ -53,6 +53,7 @@ export default (props: any) => {
countryCode: 'US',
},
shippingAddressRequired: true,
callbackIntents: ['SHIPPING_ADDRESS'],
}}
onLoadPaymentData={paymentRequest => {
console.log('Success', paymentRequest);
Expand Down
2 changes: 2 additions & 0 deletions examples/vue/src/components/Examples.vue
Expand Up @@ -131,6 +131,7 @@
currencyCode: 'USD',
countryCode: 'US',
},
callbackIntents: ['PAYMENT_AUTHORIZATION'],
}"
v-on:loadpaymentdata="onLoadPaymentData"
v-on:error="onError"
Expand Down Expand Up @@ -158,6 +159,7 @@
countryCode: 'US',
},
shippingAddressRequired: true,
callbackIntents: ['PAYMENT_AUTHORIZATION'],
}"
v-on:loadpaymentdata="onLoadPaymentData"
v-on:error="onError"
Expand Down
2 changes: 2 additions & 0 deletions src/button-element/README.md
Expand Up @@ -233,6 +233,7 @@ integrating Google Pay into your website.
<td>
<p>Invoked when a user chooses a payment method. This callback should be used to validate whether or not the payment method can be used to complete a payment.</p>
<p>This would be typically used to perform pre-authorization to ensure that the card is valid and has sufficient funds.</p>
<p>Note that in order to use this callback <code>paymentRequest.callbackIntents</code> must include <code>PAYMENT_AUTHORIZATION</code>.</p>
<p>See <a href="https://developers.google.com/pay/api/web/reference/client#onPaymentAuthorized">payment authorization reference</a> for more information.</p>
</td>
</tr>
Expand All @@ -242,6 +243,7 @@ integrating Google Pay into your website.
</td>
<td>
<p>Invoked when payment the user changes payment data options including payment method, shipping details, and contact details. This callback can be used to dynamically update <code>transactionInfo</code> when payment details, shipping address, or shipping options change.</p>
<p>Note that in order to use this callback <code>paymentRequest.callbackIntents</code> must include either <code>SHIPPING_ADDRESS</code> or <code>SHIPPING_OPTION</code>.</p>
<p>See <a href="https://developers.google.com/pay/api/web/reference/client#onPaymentDataChanged">payment data changed reference</a> for more information.</p>
</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions src/button-react/README.md
Expand Up @@ -152,13 +152,15 @@ integrating Google Pay into your website.
<td>
<p>Invoked when a user chooses a payment method. This callback should be used to validate whether or not the payment method can be used to complete a payment.</p>
<p>This would be typically used to perform pre-authorization to ensure that the card is valid and has sufficient funds.</p>
<p>Note that in order to use this callback <code>paymentRequest.callbackIntents</code> must include <code>PAYMENT_AUTHORIZATION</code>.</p>
<p>See <a href="https://developers.google.com/pay/api/web/reference/client#onPaymentAuthorized">payment authorization reference</a> for more information.</p>
</td>
</tr>
<tr>
<td><p>onPaymentDataChanged</p></td>
<td>
<p>Invoked when payment the user changes payment data options including payment method, shipping details, and contact details. This callback can be used to dynamically update <code>transactionInfo</code> when payment details, shipping address, or shipping options change.</p>
<p>Note that in order to use this callback <code>paymentRequest.callbackIntents</code> must include either <code>SHIPPING_ADDRESS</code> or <code>SHIPPING_OPTION</code>.</p>
<p>See <a href="https://developers.google.com/pay/api/web/reference/client#onPaymentDataChanged">payment data changed reference</a> for more information.</p>
</td>
</tr>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/button-manager.test.ts
Expand Up @@ -182,7 +182,8 @@ describe('PaymentsClient options', () => {
});
});

describe('Callbacks', () => {
// TODO: #13 re-enable inferrence if/when we agree as a team
describe.skip('Callbacks', () => {
it('maintains default callback values', () => {
const manager = new ButtonManager(managerOptions);
const config: Config = {
Expand Down
51 changes: 26 additions & 25 deletions src/lib/button-manager.ts
Expand Up @@ -182,31 +182,32 @@ export class ButtonManager {
}
}

// infer callback intents if not set
if (!request.callbackIntents && (config.onPaymentDataChanged || config.onPaymentAuthorized)) {
const intents: google.payments.api.CallbackIntent[] = [];
if (config.onPaymentDataChanged) {
if (!request.shippingAddressRequired && !request.shippingOptionRequired) {
throw new Error(
'onPaymentDataChanged must be used with either shippingAddressRequired or shippingOptionRequired',
);
}

if (request.shippingAddressRequired) {
intents.push('SHIPPING_ADDRESS');
}

if (request.shippingOptionRequired) {
intents.push('SHIPPING_OPTION');
}
}

if (config.onPaymentAuthorized) {
intents.push('PAYMENT_AUTHORIZATION');
}

request.callbackIntents = intents;
}
// TODO: #13 re-enable inferrence if/when we agree as a team
// // infer callback intents if not set
// if (!request.callbackIntents && (config.onPaymentDataChanged || config.onPaymentAuthorized)) {
// const intents: google.payments.api.CallbackIntent[] = [];
// if (config.onPaymentDataChanged) {
// if (!request.shippingAddressRequired && !request.shippingOptionRequired) {
// throw new Error(
// 'onPaymentDataChanged must be used with either shippingAddressRequired or shippingOptionRequired',
// );
// }

// if (request.shippingAddressRequired) {
// intents.push('SHIPPING_ADDRESS');
// }

// if (request.shippingOptionRequired) {
// intents.push('SHIPPING_OPTION');
// }
// }

// if (config.onPaymentAuthorized) {
// intents.push('PAYMENT_AUTHORIZATION');
// }

// request.callbackIntents = intents;
// }

// infer billingAddressRequired
request.allowedPaymentMethods = request.allowedPaymentMethods.map(pm => {
Expand Down

0 comments on commit 49e5ad2

Please sign in to comment.