open-stripe-checkout.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * openStripeCheckout()
  3. *
  4. * Open the Stripe Checkout modal dialog and resolve when it is closed.
  5. *
  6. * -----------------------------------------------------------------
  7. * @param {String} stripePublishableKey
  8. * @param {String} billingEmailAddress
  9. * -----------------------------------------------------------------
  10. * @returns {Dictionary?} (or undefined if the form was cancelled)
  11. * e.g.
  12. * {
  13. * stripeToken: '…',
  14. * billingCardLast4: '…',
  15. * billingCardBrand: '…',
  16. * billingCardExpMonth: '…',
  17. * billingCardExpYear: '…'
  18. * }
  19. */
  20. parasails.registerUtility('openStripeCheckout', async function openStripeCheckout(stripePublishableKey, billingEmailAddress) {
  21. // Cache (& use cached) "checkout handler" globally on the page so that we
  22. // don't end up configuring it more than once (i.e. so Stripe.js doesn't
  23. // complain).
  24. var CACHE_KEY = '_cachedStripeCheckoutHandler';
  25. if (!window[CACHE_KEY]) {
  26. window[CACHE_KEY] = StripeCheckout.configure({
  27. key: stripePublishableKey,
  28. });
  29. }
  30. var checkoutHandler = window[CACHE_KEY];
  31. // Track whether the "token" callback was triggered.
  32. // (If it has NOT at the time the "closed" callback is triggered, then we
  33. // know the checkout form was cancelled.)
  34. var hasTriggeredTokenCallback;
  35. // Build a Promise & send it back as our "thenable" (AsyncFunction's return value).
  36. // (this is necessary b/c we're wrapping an api that isn't `await`-compatible)
  37. return new Promise((resolve, reject)=>{
  38. try {
  39. // Open Stripe checkout.
  40. // (https://stripe.com/docs/checkout#integration-custom)
  41. checkoutHandler.open({
  42. name: 'NEW_APP_NAME',
  43. description: 'Link your credit card.',
  44. panelLabel: 'Save card',
  45. email: billingEmailAddress,
  46. locale: 'auto',
  47. zipCode: false,
  48. allowRememberMe: false,
  49. closed: ()=>{
  50. // If the Checkout dialog was cancelled, resolve undefined.
  51. if (!hasTriggeredTokenCallback) {
  52. resolve();
  53. }
  54. },
  55. token: (stripeData)=>{
  56. // After payment info has been successfully added, and a token
  57. // was obtained...
  58. hasTriggeredTokenCallback = true;
  59. // Normalize token and billing card info from Stripe and resolve
  60. // with that.
  61. let stripeToken = stripeData.id;
  62. let billingCardLast4 = stripeData.card.last4;
  63. let billingCardBrand = stripeData.card.brand;
  64. let billingCardExpMonth = String(stripeData.card.exp_month);
  65. let billingCardExpYear = String(stripeData.card.exp_year);
  66. resolve({
  67. stripeToken,
  68. billingCardLast4,
  69. billingCardBrand,
  70. billingCardExpMonth,
  71. billingCardExpYear
  72. });
  73. }//Œ
  74. });//_∏_
  75. } catch (err) {
  76. reject(err);
  77. }
  78. });//_∏_
  79. });