-
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.
- Loading branch information
1 parent
f6725ca
commit bd36def
Showing
10 changed files
with
1,051 additions
and
2 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
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
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
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,156 @@ | ||
import Emitter from 'component-emitter'; | ||
import { normalize } from '../../util/normalize'; | ||
import { FIELDS as TOKEN_FIELDS } from '../token'; | ||
import recurlyError from '../errors'; | ||
import { payWithGoogle } from './pay-with-google'; | ||
|
||
const getRecurlyInputsFromHtmlForm = ({ $form, inputNames }) => $form ? normalize($form, inputNames).values : {}; | ||
|
||
const getBillingAddressFromGoogle = ({ paymentData }) => { | ||
const googleBillingAddress = paymentData?.paymentMethodData?.info?.billingAddress || {}; | ||
const { | ||
name, | ||
address1, | ||
address2, | ||
countryCode, | ||
postalCode, | ||
locality, | ||
administrativeArea, | ||
} = googleBillingAddress; | ||
|
||
const fullNameSplitted = (name || '').trim().split(' '); | ||
const firstName = fullNameSplitted[0]; | ||
const lastName = fullNameSplitted.slice(1).join(' '); | ||
|
||
return { | ||
first_name: firstName, | ||
last_name: lastName, | ||
address1, | ||
address2, | ||
city: locality, | ||
state: administrativeArea, | ||
postal_code: postalCode, | ||
country: countryCode, | ||
}; | ||
}; | ||
|
||
const createRecurlyToken = ({ recurly, paymentData }) => { | ||
const userInputs = getRecurlyInputsFromHtmlForm({ $form: recurly.config.form, inputNames: TOKEN_FIELDS }); | ||
const userBillingAddress = getBillingAddressFromGoogle({ paymentData }); | ||
const userInputsOverrideBillingAddress = Object.keys(userInputs).some(k => k in userBillingAddress); | ||
|
||
const data = { | ||
...userInputs, | ||
...(!userInputsOverrideBillingAddress && userBillingAddress), | ||
google_pay_token: paymentData?.paymentMethodData?.tokenizationData?.token, | ||
}; | ||
|
||
return recurly.request.post({ route: '/google_pay/token', data }); | ||
}; | ||
|
||
const validateGooglePayOptions = options => { | ||
const requiredKeys = ['googleMerchantId', 'total', 'country', 'currency']; | ||
requiredKeys.forEach(key => { | ||
if (options[key] === undefined) { | ||
throw recurlyError('google-pay-config-missing', { opt: key }); | ||
} | ||
}); | ||
|
||
return options; | ||
} | ||
|
||
const validateRecurlyMerchantInfo = ({ recurlyMerchantInfo }) => { | ||
if (recurlyMerchantInfo.paymentMethods.length === 0) { | ||
throw recurlyError('google-pay-not-configured'); | ||
} | ||
|
||
return recurlyMerchantInfo; | ||
}; | ||
|
||
const getGoogleInfoFromMerchantInfo = ({ recurlyMerchantInfo, options }) => { | ||
const { siteMode, paymentMethods } = recurlyMerchantInfo; | ||
const { | ||
googleMerchantId, | ||
googleBusinessName, | ||
total, | ||
country, | ||
currency, | ||
requireBillingAddress, | ||
} = options; | ||
|
||
const environment = siteMode === 'production' ? 'PRODUCTION' : 'TEST'; | ||
const googlePayConfig = { | ||
apiVersion: 2, | ||
apiVersionMinor: 0, | ||
allowedPaymentMethods: paymentMethods.map(({ cardNetworks, authMethods, paymentGateway, direct }) => ({ | ||
type: 'CARD', | ||
parameters: { | ||
allowedCardNetworks: cardNetworks, | ||
allowedAuthMethods: authMethods, | ||
...(requireBillingAddress && { | ||
billingAddressRequired: true, | ||
billingAddressParameters: { | ||
format: 'FULL', | ||
}, | ||
}), | ||
}, | ||
tokenizationSpecification: { | ||
...(paymentGateway && { | ||
type: 'PAYMENT_GATEWAY', | ||
parameters: paymentGateway, | ||
}), | ||
...(direct && { | ||
type: 'DIRECT', | ||
parameters: direct, | ||
}), | ||
}, | ||
})), | ||
}; | ||
const paymentDataRequest = { | ||
...googlePayConfig, | ||
merchantInfo: { | ||
merchantId: googleMerchantId, | ||
merchantName: googleBusinessName, | ||
}, | ||
transactionInfo: { | ||
totalPriceStatus: 'FINAL', // only when the price will nto change | ||
totalPrice: total, | ||
currencyCode: currency, | ||
countryCode: country | ||
}, | ||
}; | ||
|
||
return { environment, googlePayConfig, paymentDataRequest }; | ||
}; | ||
|
||
const getGooglePayInfo = ({ recurly, options }) => { | ||
const { country, currency, gateway } = options; | ||
const data = { country, currency, gateway }; | ||
|
||
return new Promise((resolve, reject) => { | ||
try { | ||
validateGooglePayOptions(options); | ||
resolve(); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}) | ||
.then(() => recurly.request.get({ route: '/google_pay/info', data })) | ||
.then(recurlyMerchantInfo => validateRecurlyMerchantInfo({ recurlyMerchantInfo, options })) | ||
.then(recurlyMerchantInfo => getGoogleInfoFromMerchantInfo({ recurlyMerchantInfo, options })); | ||
}; | ||
|
||
const googlePay = (recurly, options) => { | ||
const emitter = new Emitter(); | ||
|
||
getGooglePayInfo({ recurly, options }) | ||
.then(googlePayInfo => payWithGoogle({ googlePayInfo, options })) | ||
.then(({ $button, getPaymentData }) => emitter.emit('ready', $button) && getPaymentData()) | ||
.then(paymentData => createRecurlyToken({ recurly, paymentData })) | ||
.then(token => emitter.emit('token', token)) | ||
.catch(err => emitter.emit('error', err)); | ||
|
||
return emitter; | ||
}; | ||
|
||
export { googlePay }; |
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,13 @@ | ||
import { googlePay } from './google-pay'; | ||
|
||
/** | ||
* Returns a GooglePay instance. | ||
* | ||
* @param {Object} options | ||
* @return {GooglePay} | ||
*/ | ||
export function factory (options) { | ||
const recurly = this; | ||
|
||
return googlePay(recurly, options); | ||
} |
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,50 @@ | ||
import { loadLibs } from '../../util/dom'; | ||
import recurlyError from '../errors'; | ||
|
||
const GOOGLE_PAY_LIB_URL = 'https://pay.google.com/gp/p/js/pay.js'; | ||
|
||
const payWithGoogle = ({ | ||
googlePayInfo: { | ||
environment, | ||
googlePayConfig, | ||
paymentDataRequest, | ||
}, | ||
options: { | ||
buttonOptions, | ||
}, | ||
}) => { | ||
let googlePayClient; | ||
let deferredPaymentData; | ||
|
||
const paymentData = new Promise((resolve, reject) => { | ||
deferredPaymentData = { resolve, reject }; | ||
}); | ||
|
||
const onGooglePayButtonClicked = () => googlePayClient.loadPaymentData(paymentDataRequest) | ||
.then(deferredPaymentData.resolve) | ||
.catch(err => deferredPaymentData.reject(recurlyError('google-pay-payment-failure', { err }))); | ||
|
||
return loadLibs(GOOGLE_PAY_LIB_URL) | ||
.then(() => { | ||
googlePayClient = new window.google.payments.api.PaymentsClient({ environment }); | ||
return googlePayClient.isReadyToPay(googlePayConfig); | ||
}) | ||
.catch(err => { | ||
throw recurlyError('google-pay-init-error', { err }); | ||
}) | ||
.then(({ result: isReadyToPay }) => { | ||
if (!isReadyToPay) { | ||
throw recurlyError('google-pay-not-available'); | ||
} | ||
}) | ||
.then(() => googlePayClient.createButton({ | ||
...buttonOptions, | ||
onClick: onGooglePayButtonClicked | ||
})) | ||
.then($button => ({ | ||
$button, | ||
getPaymentData: () => paymentData, | ||
})); | ||
}; | ||
|
||
export { payWithGoogle }; |
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.