@stripe/stripe-js
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
@@ -1,3 +0,2 @@ | ||
var V3_URL = 'https://js.stripe.com/v3'; | ||
var hasInjectedScript = false; // Execute our own script injection after a tick to give users time to | ||
var V3_URL = 'https://js.stripe.com/v3'; // Execute our own script injection after a tick to give users time to | ||
// do their own script injection. | ||
@@ -19,3 +18,2 @@ | ||
if (!script) { | ||
hasInjectedScript = true; | ||
script = document.createElement('script'); | ||
@@ -45,3 +43,2 @@ script.src = V3_URL; | ||
}); | ||
var hasCalledLoadStripe = false; | ||
var loadStripe = function loadStripe() { | ||
@@ -52,3 +49,2 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
hasCalledLoadStripe = true; | ||
return stripePromise.then(function (maybeStripe) { | ||
@@ -58,23 +54,3 @@ return maybeStripe ? maybeStripe.apply(void 0, args) : null; | ||
}; | ||
var STRIPE_NOT_LOADED_ERROR_TEXT = "Stripe.js has not yet loaded. Instead of calling `Stripe` directly, try using the `loadStripe` utility from this package.\n\nSee https://stripe.com/docs/js/including for more information.\n"; | ||
var STRIPE_UNAVAILABLE_ERROR_TEXT = "window.Stripe is not defined. Did you include Stripe.js on your page?\n\nFor compliance reasons, Stripe.js must be loaded directly from https://js.stripe.com, and cannot be included in a bundle or hosted yourself. This npm module exists as a convenience, but delegates to window.Stripe.\n\nYou can load Stripe.js by using the `loadStripe` utility from this package, or by including the following <script> tag on your page:\n\n<script src=\"".concat(V3_URL, "\"></script>\n\nSee https://stripe.com/docs/js/including for more information.\n"); | ||
var hasUserIncludedScript = function hasUserIncludedScript() { | ||
return document.querySelector("script[src=\"".concat(V3_URL, "\"]")) && !hasInjectedScript; | ||
}; | ||
var Stripe = function Stripe() { | ||
if (window && window.Stripe) { | ||
var _window; | ||
return (_window = window).Stripe.apply(_window, arguments); | ||
} | ||
if (hasCalledLoadStripe || hasUserIncludedScript()) { | ||
throw new Error(STRIPE_NOT_LOADED_ERROR_TEXT); | ||
} | ||
throw new Error(STRIPE_UNAVAILABLE_ERROR_TEXT); | ||
}; | ||
export { Stripe, loadStripe }; | ||
export { loadStripe }; |
@@ -5,4 +5,3 @@ 'use strict'; | ||
var V3_URL = 'https://js.stripe.com/v3'; | ||
var hasInjectedScript = false; // Execute our own script injection after a tick to give users time to | ||
var V3_URL = 'https://js.stripe.com/v3'; // Execute our own script injection after a tick to give users time to | ||
// do their own script injection. | ||
@@ -24,3 +23,2 @@ | ||
if (!script) { | ||
hasInjectedScript = true; | ||
script = document.createElement('script'); | ||
@@ -50,3 +48,2 @@ script.src = V3_URL; | ||
}); | ||
var hasCalledLoadStripe = false; | ||
var loadStripe = function loadStripe() { | ||
@@ -57,3 +54,2 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
hasCalledLoadStripe = true; | ||
return stripePromise.then(function (maybeStripe) { | ||
@@ -63,24 +59,3 @@ return maybeStripe ? maybeStripe.apply(void 0, args) : null; | ||
}; | ||
var STRIPE_NOT_LOADED_ERROR_TEXT = "Stripe.js has not yet loaded. Instead of calling `Stripe` directly, try using the `loadStripe` utility from this package.\n\nSee https://stripe.com/docs/js/including for more information.\n"; | ||
var STRIPE_UNAVAILABLE_ERROR_TEXT = "window.Stripe is not defined. Did you include Stripe.js on your page?\n\nFor compliance reasons, Stripe.js must be loaded directly from https://js.stripe.com, and cannot be included in a bundle or hosted yourself. This npm module exists as a convenience, but delegates to window.Stripe.\n\nYou can load Stripe.js by using the `loadStripe` utility from this package, or by including the following <script> tag on your page:\n\n<script src=\"".concat(V3_URL, "\"></script>\n\nSee https://stripe.com/docs/js/including for more information.\n"); | ||
var hasUserIncludedScript = function hasUserIncludedScript() { | ||
return document.querySelector("script[src=\"".concat(V3_URL, "\"]")) && !hasInjectedScript; | ||
}; | ||
var Stripe = function Stripe() { | ||
if (window && window.Stripe) { | ||
var _window; | ||
return (_window = window).Stripe.apply(_window, arguments); | ||
} | ||
if (hasCalledLoadStripe || hasUserIncludedScript()) { | ||
throw new Error(STRIPE_NOT_LOADED_ERROR_TEXT); | ||
} | ||
throw new Error(STRIPE_UNAVAILABLE_ERROR_TEXT); | ||
}; | ||
exports.Stripe = Stripe; | ||
exports.loadStripe = loadStripe; |
{ | ||
"name": "@stripe/stripe-js", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "Stripe.js loading utility", | ||
@@ -5,0 +5,0 @@ "main": "dist/stripe.js", |
@@ -5,36 +5,20 @@ # Stripe.js ES Module | ||
**Note**: For compliance reasons, Stripe.js must be loaded directly from | ||
`https://js.stripe.com`, and cannot be included in a bundle or hosted yourself. | ||
This package wraps the global `Stripe` function provided by the Stripe.js | ||
script as an ES module. | ||
**Note**: To be | ||
[PCI compliant](https://stripe.com/docs/security#validating-pci-compliance), you | ||
must load Stripe.js directly from `https://js.stripe.com`. You cannot include it | ||
in a bundle or host it yourself. This package wraps the global `Stripe` function | ||
provided by the Stripe.js script as an ES module. | ||
[![npm version](https://img.shields.io/npm/v/@stripe/stripe-js.svg?style=flat-square)](https://www.npmjs.com/package/@stripe/stripe-js) | ||
## Usage | ||
## Installation | ||
### `Stripe` | ||
Use `npm` to install the Stripe.js module: | ||
To use the exported `Stripe` function, first include the Stripe.js script on | ||
each page of your site. | ||
```html | ||
<script src="https://js.stripe.com/v3/"></script> | ||
```sh | ||
npm install @stripe/stripe-js | ||
``` | ||
Then import and use Stripe.js as you would any other module. | ||
## Usage | ||
```js | ||
import {Stripe} from '@stripe/stripe-js'; | ||
const stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx'); | ||
``` | ||
We’ve placed a random API key in this example. Replace it with your [actual | ||
publishable API keys](https://dashboard.stripe.com/account/apikeys) to test | ||
this code through your Stripe account. | ||
For more information on how to use Stripe.js, please refer to the [Stripe.js | ||
API reference](https://stripe.com/docs/js) or learn to [accept | ||
a payment](https://stripe.com/docs/payments/accept-a-payment) with Stripe. | ||
### `loadStripe` | ||
@@ -44,3 +28,4 @@ | ||
object once Stripe.js has loaded. If necessary, it will load Stripe.js for you | ||
by inserting the Stripe.js script tag. | ||
by inserting the Stripe.js script tag. If you call `loadStripe` in a server | ||
environment it will resolve to `null`. | ||
@@ -53,22 +38,24 @@ ```js | ||
We’ve placed a random API key in this example. Replace it with your [actual | ||
publishable API keys](https://dashboard.stripe.com/account/apikeys) to test | ||
this code through your Stripe account. | ||
We’ve placed a random API key in this example. Replace it with your | ||
[actual publishable API keys](https://dashboard.stripe.com/account/apikeys) to | ||
test this code through your Stripe account. | ||
For more information on how to use Stripe.js once it loads, please refer to the | ||
[Stripe.js API reference](https://stripe.com/docs/js) or learn to [accept | ||
a payment](https://stripe.com/docs/payments/accept-a-payment) with Stripe. | ||
[Stripe.js API reference](https://stripe.com/docs/js) or learn to | ||
[accept a payment](https://stripe.com/docs/payments/accept-a-payment) with | ||
Stripe. | ||
## Ensuring Stripe.js is available everywhere | ||
To best leverage Stripe’s advanced fraud functionality, ensure that Stripe.js | ||
is loaded on every page, not just your checkout page. This allows Stripe to | ||
detect anomalous behavior that may be indicative of fraud as customers browse | ||
your website. | ||
To best leverage Stripe’s advanced fraud functionality, ensure that Stripe.js is | ||
loaded on every page, not just your checkout page. This allows Stripe to detect | ||
anomalous behavior that may be indicative of fraud as customers browse your | ||
website. | ||
If you are adding the `<script>` tag manually, make sure you do so on every | ||
page. If you are relying on the script insertion that this module provides, and | ||
you utilize code splitting or only include your JavaScript app on your checkout | ||
page, you will need to take extra steps to ensure Stripe.js is available | ||
everywhere. | ||
By default, this module will insert a `<script>` tag that loads Stripe.js from | ||
`https://js.stripe.com`. This happens as a side effect immediately upon | ||
importing this module. If you utilize code splitting or only include your | ||
JavaScript app on your checkout page, the Stripe.js script will only be | ||
available in parts of your site. To ensure Stripe.js is available everywhere, | ||
you can perform either of the following steps: | ||
@@ -87,5 +74,5 @@ ### Import as a side effect | ||
Manually add the Stripe.js script tag to the `<head>` of each page on your | ||
site. If you use `loadStripe`, it will use this script tag rather than | ||
inserting a new one. | ||
Manually add the Stripe.js script tag to the `<head>` of each page on your site. | ||
If an existing script tag is already present, this module will not insert a new | ||
one. When you call `loadStripe`, it will use the existing script tag. | ||
@@ -105,3 +92,3 @@ ```html | ||
If you would like to contribute to React Stripe.js, please make sure to read | ||
our [contributor guidelines](CONTRIBUTING.md). | ||
If you would like to contribute to React Stripe.js, please make sure to read our | ||
[contributor guidelines](CONTRIBUTING.md). |
const V3_URL = 'https://js.stripe.com/v3'; | ||
let hasInjectedScript = false; | ||
// Execute our own script injection after a tick to give users time to | ||
@@ -21,3 +19,2 @@ // do their own script injection. | ||
if (!script) { | ||
hasInjectedScript = true; | ||
script = document.createElement('script'); | ||
@@ -52,39 +49,5 @@ script.src = V3_URL; | ||
let hasCalledLoadStripe = false; | ||
export const loadStripe = (...args) => { | ||
hasCalledLoadStripe = true; | ||
return stripePromise.then((maybeStripe) => | ||
export const loadStripe = (...args) => | ||
stripePromise.then((maybeStripe) => | ||
maybeStripe ? maybeStripe(...args) : null | ||
); | ||
}; | ||
const STRIPE_NOT_LOADED_ERROR_TEXT = `Stripe.js has not yet loaded. Instead of calling \`Stripe\` directly, try using the \`loadStripe\` utility from this package. | ||
See https://stripe.com/docs/js/including for more information. | ||
`; | ||
const STRIPE_UNAVAILABLE_ERROR_TEXT = `window.Stripe is not defined. Did you include Stripe.js on your page? | ||
For compliance reasons, Stripe.js must be loaded directly from https://js.stripe.com, and cannot be included in a bundle or hosted yourself. This npm module exists as a convenience, but delegates to window.Stripe. | ||
You can load Stripe.js by using the \`loadStripe\` utility from this package, or by including the following <script> tag on your page: | ||
<script src="${V3_URL}"></script> | ||
See https://stripe.com/docs/js/including for more information. | ||
`; | ||
const hasUserIncludedScript = () => | ||
document.querySelector(`script[src="${V3_URL}"]`) && !hasInjectedScript; | ||
export const Stripe = (...args) => { | ||
if (window && window.Stripe) { | ||
return window.Stripe(...args); | ||
} | ||
if (hasCalledLoadStripe || hasUserIncludedScript()) { | ||
throw new Error(STRIPE_NOT_LOADED_ERROR_TEXT); | ||
} | ||
throw new Error(STRIPE_UNAVAILABLE_ERROR_TEXT); | ||
}; |
@@ -99,44 +99,2 @@ /* eslint-disable global-require */ | ||
}); | ||
describe('Stripe proxy', () => { | ||
it('proxies to window.Stripe when present', () => { | ||
const {Stripe} = require('./index'); | ||
window.Stripe = jest.fn((key) => ({key})); | ||
expect(Stripe('pk_test_foo')).toEqual({key: 'pk_test_foo'}); | ||
}); | ||
it('throws when Stripe.js has not yet loaded from a user injected script', () => { | ||
const {Stripe} = require('./index'); | ||
const script = document.createElement('script'); | ||
script.src = 'https://js.stripe.com/v3'; | ||
document.body.appendChild(script); | ||
expect(() => Stripe('pk_test_foo')).toThrow( | ||
'Stripe.js has not yet loaded.' | ||
); | ||
}); | ||
it('throws when Stripe.js has not yet loaded after calling loadStripe', () => { | ||
const {loadStripe, Stripe} = require('./index'); | ||
loadStripe(); | ||
expect(() => Stripe('pk_test_foo')).toThrow( | ||
'Stripe.js has not yet loaded.' | ||
); | ||
}); | ||
it('throws when Stripe.js has not been included', () => { | ||
const {Stripe} = require('./index'); | ||
return Promise.resolve(() => { | ||
// Wait for next tick to validate this error is thrown | ||
// even after our own script has been added. | ||
expect(() => Stripe('pk_test_foo')).toThrow( | ||
'window.Stripe.js is not defined.' | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13003
210
90