-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Google Pay through Braintree
To use Google Pay through Braintree, ensure that you have enabled Google Pay on a Braintree gateway in the Recurly Admin UI and on your Braintree account. Then, initialize `recurly.GooglePay(...)` with the `braintree.clientAuthorization` option: ```js const googlePay = recurly.GooglePay({ ...options, gatewayCode: '<Recurly Braintree gateway code>', braintree: { clientAuthorization: '<Braintree client authorization key>', }, }) ```
- Loading branch information
Showing
4 changed files
with
721 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.