fleet/website/assets/js/utilities/open-stripe-checkout.js
Mike McNeil 27eae209fd
Move fleetdm.com into main Fleet repo (#83)
* rename dir

* no need to install website or docs from npm

At some point, would also be nice to be able to exclude assets/ as well, and to only install a pre-built version of Fleet's frontend code

* Bring in fleetdm.com website

From https://github.com/fleetdm/fleetdm.com as of https://github.com/fleetdm/fleetdm.com/releases/tag/v0.0.21

* add procfile for heroku

Using https://github.com/timanovsky/subdir-heroku-buildpack

* avoid getting anybody's hopes up

* Create deploy-fleet-website.yml (#82)

* Create deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* update pjs with SPDX-like license expressions.  also fix repo URL

and remove package lock

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* remove dummy uri

* Dissect deploy script

* Update deploy-fleet-website.yml

* workaround for eslintrc nesting issue

* lint fixes

* forgot the .js

* add per-commit git config

* Update deploy-fleet-website.yml

* might as well remove that

* cleanup

* connect w/ heroku app and have it actually push

* fix bug I introduced in 578a1a01ff

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* the beauty, the glory, of javascript

* GH actions don't like "\n"

* Update deploy-fleet-website.yml

* restore \n chars from 0d45e568f6

hoping I was wrong in 0d45e568f6 but see also https://github.community/t/what-is-the-correct-character-escaping-for-workflow-command-values-e-g-echo-xxxx/118465/5

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* Update deploy-fleet-website.yml

* rename script to prevent duplicate building

* Configure the real website

* clean up

* a test of the deploy workflow

* add handbook to npmignore

* I guess you could call this fixing a typo

* point workflow at master branch

* now clearly bogus: this completely unused version string
2020-12-02 14:48:03 -06:00

98 lines
3.4 KiB
JavaScript
Vendored
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* openStripeCheckout()
*
* Open the Stripe Checkout modal dialog and resolve when it is closed.
*
* -----------------------------------------------------------------
* @param {String} stripePublishableKey
* @param {String} billingEmailAddress
* @param {String} headingText (optional)
* @param {String} descriptionText (optional)
* @param {String} buttonText (optional)
* -----------------------------------------------------------------
* @returns {Dictionary?} (or undefined if the form was cancelled)
* e.g.
* {
* stripeToken: '…',
* billingCardLast4: '…',
* billingCardBrand: '…',
* billingCardExpMonth: '…',
* billingCardExpYear: '…'
* }
* -----------------------------------------------------------------
* Example usage:
* ```
* var billingInfo = await openStripeCheckout(
* 'pk_test_Qz5RfDmVV5IunTFAHtDqDWn4',
* '[email protected]'
* );
* ```
*/
parasails.registerUtility('openStripeCheckout', async function openStripeCheckout(stripePublishableKey, billingEmailAddress, headingText, descriptionText, buttonText) {
// Cache (& use cached) "checkout handler" globally on the page so that we
// don't end up configuring it more than once (i.e. so Stripe.js doesn't
// complain).
var CACHE_KEY = '_cachedStripeCheckoutHandler';
if (!window[CACHE_KEY]) {
window[CACHE_KEY] = StripeCheckout.configure({
key: stripePublishableKey,
});
}
var checkoutHandler = window[CACHE_KEY];
// Track whether the "token" callback was triggered.
// (If it has NOT at the time the "closed" callback is triggered, then we
// know the checkout form was cancelled.)
var hasTriggeredTokenCallback;
// Build a Promise & send it back as our "thenable" (AsyncFunction's return value).
// (this is necessary b/c we're wrapping an api that isn't `await`-compatible)
return new Promise((resolve, reject)=>{
try {
// Open Stripe checkout.
// (https://stripe.com/docs/checkout#integration-custom)
checkoutHandler.open({
name: headingText || 'NEW_APP_NAME',
description: descriptionText || 'Link your credit card.',
panelLabel: buttonText || 'Save card',
email: billingEmailAddress,//« So that Stripe doesn't prompt for an email address
locale: 'auto',
zipCode: false,
allowRememberMe: false,
closed: ()=>{
// If the Checkout dialog was cancelled, resolve undefined.
if (!hasTriggeredTokenCallback) {
resolve();
}
},
token: (stripeData)=>{
// After payment info has been successfully added, and a token
// was obtained...
hasTriggeredTokenCallback = true;
// Normalize token and billing card info from Stripe and resolve
// with that.
let stripeToken = stripeData.id;
let billingCardLast4 = stripeData.card.last4;
let billingCardBrand = stripeData.card.brand;
let billingCardExpMonth = String(stripeData.card.exp_month);
let billingCardExpYear = String(stripeData.card.exp_year);
resolve({
stripeToken,
billingCardLast4,
billingCardBrand,
billingCardExpMonth,
billingCardExpYear
});
}//Œ
});//_∏_
} catch (err) {
reject(err);
}
});//_∏_
});