Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Google Pay through Braintree #894

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/recurly/google-pay/google-pay.braintree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Promise from 'promise';
import { GooglePay } from './google-pay';
import Debug from 'debug';
import BraintreeLoader from '../../util/braintree-loader';
import errors from '../errors';
import { payWithGoogle } from './pay-with-google';

const debug = Debug('recurly:google-pay:braintree');

export class GooglePayBraintree extends GooglePay {
configure (options) {
debug('Initializing client');

const authorization = options.braintree.clientAuthorization;

BraintreeLoader.loadModules('googlePayment', 'dataCollector')
.then(() => window.braintree.client.create({ authorization }))
.then(client => Promise.all([
window.braintree.dataCollector.create({ client }),
window.braintree.googlePayment.create({
client,
googlePayVersion: 2,
googleMerchantId: options.googleMerchantId,
})
]))
.then(([dataCollector, googlePayment]) => { this.braintree = { dataCollector, googlePayment }; })
.catch(err => this.emit('error', errors('google-pay-init-error', { err })))
.then(() => super.configure(options));
}

createButton ({ paymentOptions, isReadyToPayRequest, paymentDataRequest: recurlyPaymentDataRequest, buttonOptions }) {
// allow Braintree to set the tokenizationSpecification for its gateway
recurlyPaymentDataRequest.allowedPaymentMethods.forEach(method => {
if (method.tokenizationSpecification) delete method.tokenizationSpecification;
});
const paymentDataRequest = this.braintree.googlePayment.createPaymentDataRequest(recurlyPaymentDataRequest);
debug('Creating button', recurlyPaymentDataRequest, paymentDataRequest);
return payWithGoogle({
paymentOptions,
isReadyToPayRequest,
paymentDataRequest,
buttonOptions,
});
}

token (paymentData) {
debug('Creating token', paymentData);

return this.braintree.googlePayment
.parseResponse(paymentData)
.then(token => {
token.deviceData = this.braintree.dataCollector.deviceData;
paymentData.paymentMethodData.gatewayToken = token;
return super.token(paymentData);
});
}

mapPaymentData (paymentData) {
return {
type: 'braintree',
...super.mapPaymentData(paymentData),
};
}
}
5 changes: 4 additions & 1 deletion lib/recurly/google-pay/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GooglePay } from './google-pay';
import { GooglePayBraintree } from './google-pay.braintree';

/**
* Returns a GooglePay instance.
Expand All @@ -7,7 +8,9 @@ import { GooglePay } from './google-pay';
* @return {GooglePay}
*/
export function factory (options) {
const factoryClass = GooglePay;
const factoryClass = options?.braintree?.clientAuthorization
? GooglePayBraintree
: GooglePay;

return new factoryClass(Object.assign({}, options, { recurly: this }));
}
192 changes: 137 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading