-
Notifications
You must be signed in to change notification settings - Fork 274
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
cc0640c
commit aba8948
Showing
3 changed files
with
54 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as stripeJs from '@stripe/stripe-js'; | ||
import {isStripe, isPromise} from '../utils/guards'; | ||
|
||
const INVALID_STRIPE_ERROR = | ||
'Invalid prop `stripe` supplied to `Elements`. We recommend using the `loadStripe` utility from `@stripe/stripe-js`. See https://stripe.com/docs/stripe-js/react#elements-props-stripe for details.'; | ||
|
||
// We are using types to enforce the `stripe` prop in this lib, but in a real | ||
// integration `stripe` could be anything, so we need to do some sanity | ||
// validation to prevent type errors. | ||
const validateStripe = (maybeStripe: unknown): null | stripeJs.Stripe => { | ||
if (maybeStripe === null || isStripe(maybeStripe)) { | ||
return maybeStripe; | ||
} | ||
|
||
throw new Error(INVALID_STRIPE_ERROR); | ||
}; | ||
|
||
type ParsedStripeProp = | ||
| {tag: 'empty'} | ||
| {tag: 'sync'; stripe: stripeJs.Stripe} | ||
| {tag: 'async'; stripePromise: Promise<stripeJs.Stripe | null>}; | ||
|
||
export const parseStripeProp = (raw: unknown): ParsedStripeProp => { | ||
if (isPromise(raw)) { | ||
return { | ||
tag: 'async', | ||
stripePromise: Promise.resolve(raw).then(validateStripe), | ||
}; | ||
} | ||
|
||
const stripe = validateStripe(raw); | ||
|
||
if (stripe === null) { | ||
return {tag: 'empty'}; | ||
} | ||
|
||
return {tag: 'sync', stripe}; | ||
}; |
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 @@ | ||
export const registerWithStripeJs = (stripe: any) => { | ||
if (!stripe || !stripe._registerWrapper || !stripe.registerAppInfo) { | ||
return; | ||
} | ||
|
||
stripe._registerWrapper({name: 'react-stripe-js', version: _VERSION}); | ||
|
||
stripe.registerAppInfo({ | ||
name: 'react-stripe-js', | ||
version: _VERSION, | ||
url: 'https://stripe.com/docs/stripe-js/react', | ||
}); | ||
}; |