Skip to content

Instantly share code, notes, and snippets.

@alekseyg
Created March 15, 2017 22:03
Show Gist options
  • Save alekseyg/fabbdc0f5d82140eedf829efdb443f4a to your computer and use it in GitHub Desktop.
Save alekseyg/fabbdc0f5d82140eedf829efdb443f4a to your computer and use it in GitHub Desktop.
What Stripe.applePay.checkAvailability actually does
Stripe.applePay.checkAvailability = function (callback) {
if (location.protocol === 'https:') {
var canMakePayments = window.ApplePaySession && ApplePaySession.canMakePayments()
if (/^pk_test_/.test(Stripe.key || Stripe.publishableKey)) {
callback(canMakePayments)
} else if (canMakePayments) {
var merchantId = "merchant." + window.location.hostname + ".stripe"
ApplePaySession.canMakePaymentsWithActiveCard(merchantId).then(callback)
} else {
callback(false)
}
} else {
console.warn("To use Apple Pay, you must serve your page over HTTPS.")
callback(false)
}
}
@squgeim
Copy link

squgeim commented Dec 15, 2017

Thanks for this.

They should change line 6 to be called asynchronously, maybe setTimeout(callback(canMakePayments), 0).

I was calling this function to check if apple pay is supported before starting the apple pay session. Since this function is synchronous in test mode, we saw the issue about having to start apple pay session directly from user action only after it was deployed to production.

It was a silly mistake but this function could have warned me earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment