Move secret key and other data into /_config/config.json
authorNick Downing <nick@ndcode.org>
Mon, 27 Dec 2021 06:25:16 +0000 (17:25 +1100)
committerNick Downing <nick@ndcode.org>
Mon, 27 Dec 2021 06:27:02 +0000 (17:27 +1100)
_config/config.json [new file with mode: 0644]
create-checkout-session.html.jst

diff --git a/_config/config.json b/_config/config.json
new file mode 100644 (file)
index 0000000..f79d206
--- /dev/null
@@ -0,0 +1,5 @@
+{
+  "secret_key": "sk_test_wsFx86XDJWwmE4dMskBgJYrt",
+  "your_domain": "http://localhost:8080",
+  "price_id": "{{PRICE_ID}}"
+}
index 73386ef..65a98e0 100644 (file)
@@ -1,23 +1,24 @@
 // This is a public sample test API key.
 // To avoid exposing it, don't submit any personally identifiable information through requests with this API key.
 // Sign in to see your own test API key embedded in code samples.
-const stripe = require('stripe')('sk_test_wsFx86XDJWwmE4dMskBgJYrt')
-
-const YOUR_DOMAIN = 'http://localhost:8080'
+let stripe = require('stripe')
 
 return async env => {
-  const session = await stripe.checkout.sessions.create(
+  let config = await env.site.get_json('/_config/config.json')
+
+  stripe_inst = stripe(config.secret_key)
+  let session = await stripe_inst.checkout.sessions.create(
     {
       line_items: [
         {
           // Provide the exact Price ID (for example, pr_1234) of the product you want to sell
-          price: '{{PRICE_ID}}',
+          price: config.price_id,
           quantity: 1,
         },
       ],
       mode: 'payment',
-      success_url: `${YOUR_DOMAIN}/success.html`,
-      cancel_url: `${YOUR_DOMAIN}/cancel.html`,
+      success_url: `${config.your_domain}/success.html`,
+      cancel_url: `${config.your_domain}/cancel.html`,
     }
   )