From a17c5cf6861ff43f8facb04715799b26a4531732 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Sat, 25 Mar 2023 14:31:27 -0600 Subject: [PATCH] Moved stripe listener --- .env.sample | 2 +- .vscode/settings.json | 2 + _src/assets/scripts/cart.js | 2 +- .../scripts/{checkout => order}/monero.js | 0 _src/shop/checkout/monero.njk | 121 ------------------ _src/shop/order/monero.njk | 121 ++++++++++++++++++ docker-compose.yml.sample | 11 +- {stripe-webhook => hooks}/email-customer.js | 0 {stripe-webhook => hooks}/email-sales.js | 0 {stripe-webhook => hooks}/ntfy-sales.js | 0 {stripe-webhook => hooks}/remove-sid.js | 0 listeners/monero.js | 51 ++++++++ .../index.js => listeners/stripe.js | 11 +- package.json | 3 +- 14 files changed, 189 insertions(+), 135 deletions(-) create mode 100644 .vscode/settings.json rename _src/assets/scripts/{checkout => order}/monero.js (100%) delete mode 100644 _src/shop/checkout/monero.njk rename {stripe-webhook => hooks}/email-customer.js (100%) rename {stripe-webhook => hooks}/email-sales.js (100%) rename {stripe-webhook => hooks}/ntfy-sales.js (100%) rename {stripe-webhook => hooks}/remove-sid.js (100%) create mode 100644 listeners/monero.js rename stripe-webhook/index.js => listeners/stripe.js (87%) diff --git a/.env.sample b/.env.sample index 48f48d5..5d2ac2c 100644 --- a/.env.sample +++ b/.env.sample @@ -1,7 +1,7 @@ # Ports SITE_PORT="8080" API_PORT="8081" -HOOK_PORT="8082" +STRIPE_LISTENER_PORT="8082" # Domains SITE_DOMAIN="https://www.example.com" diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/_src/assets/scripts/cart.js b/_src/assets/scripts/cart.js index 13d5b1a..2463207 100644 --- a/_src/assets/scripts/cart.js +++ b/_src/assets/scripts/cart.js @@ -276,7 +276,7 @@ class Cart { constructor() { if (self.payment_method()==='USD') window.location = `/shop/checkout/stripe/?order=${parsedRes.id}` else if (self.payment_method()==='XMR') - window.location = `/shop/checkout/monero/?order=${parsedRes.id}` + window.location = `/shop/order/monero/?order=${parsedRes.id}` else { alert(`Invalid payment method: ${self.payment_method()}`) self.isLoading(false) diff --git a/_src/assets/scripts/checkout/monero.js b/_src/assets/scripts/order/monero.js similarity index 100% rename from _src/assets/scripts/checkout/monero.js rename to _src/assets/scripts/order/monero.js diff --git a/_src/shop/checkout/monero.njk b/_src/shop/checkout/monero.njk deleted file mode 100644 index be27a86..0000000 --- a/_src/shop/checkout/monero.njk +++ /dev/null @@ -1,121 +0,0 @@ ---- -layout: layouts/base.njk -title: Checkout ---- - - - -

Monero Checkout

- - - - - - - - - - -
ItemPriceQtySub
-
-
-

- 📦 Shipping
- -
-
-
, - - -

-

- 📇 Contact
- -
-
-

-
- - - - - - - - - - - - - - - -
Subtotal:
Tax:
Shipping:
Total:
Monero: -
-
-

To change order details, cancel it and return to your cart.

-
-

Payment status

-

Your order is for . -
have been submitted to the blockchain. -
have been unlocked. -

-

Your order will ship when have been unlocked. (Transactions unlock after receiving 10 confirmations.)

-

Your payment has been confirmed. We will ship your order as soon as possible and send an email with the tracking info.

-

Transactions

- - - - - - - - - - - - - - - - - - - -
DateTimeAmtFeeConfsBlockStat
- ⏲️ - - -
-

-
-
-

Monero payment

-

Please send XMR to this address:

-
-

You can also click, tap, or scan this qr code:

-
-
- - - - - - diff --git a/_src/shop/order/monero.njk b/_src/shop/order/monero.njk index e69de29..f25af3c 100644 --- a/_src/shop/order/monero.njk +++ b/_src/shop/order/monero.njk @@ -0,0 +1,121 @@ +--- +layout: layouts/base.njk +title: Checkout +--- + + + +

Monero Checkout

+ + + + + + + + + + +
ItemPriceQtySub
+
+
+

+ 📦 Shipping
+ +
+
+
, + + +

+

+ 📇 Contact
+ +
+
+

+
+ + + + + + + + + + + + + + + +
Subtotal:
Tax:
Shipping:
Total:
Monero: +
+
+

To change order details, cancel it and return to your cart.

+
+

Payment status

+

Your order is for . +
have been submitted to the blockchain. +
have been unlocked. +

+

Your order will ship when have been unlocked. (Transactions unlock after receiving 10 confirmations.)

+

Your payment has been confirmed. We will ship your order as soon as possible and send an email with the tracking info.

+

Transactions

+ + + + + + + + + + + + + + + + + + + +
DateTimeAmtFeeConfsBlockStat
+ ⏲️ + + +
+

+
+
+

Monero payment

+

Please send XMR to this address:

+
+

You can also click, tap, or scan this qr code:

+
+
+ + + + + + diff --git a/docker-compose.yml.sample b/docker-compose.yml.sample index f35426a..4a01d82 100644 --- a/docker-compose.yml.sample +++ b/docker-compose.yml.sample @@ -64,14 +64,14 @@ services: - "./api:/app/api:ro" - "./lib:/app/lib:ro" - stripe-hook: + stripe-listener: restart: unless-stopped build: . - command: stripe-webhook - container_name: my_stripe-webhook + command: stripe-listener + container_name: my_stripe-listener environment: - STRIPE_SEC=${STRIPE_SEC} - - HOOK_PORT=${HOOK_PORT} + - STRIPE_LISTENER_PORT=${STRIPE_LISTENER_PORT} - SALES_EMAIL=${SALES_EMAIL} - MAIL_SERVER=${MAIL_SERVER} - MAIL_USER=${MAIL_USER} @@ -81,7 +81,8 @@ services: - "./_src:/app/_src" - "./orders:/app/orders" - "./sold:/app/sold" - - "./stripe-webhook:/app/stripe-webhook:ro" + - "./listeners:/app/listeners:ro" + - "./hooks:/app/hooks:ro" - "./lib:/app/lib:ro" moneropay: diff --git a/stripe-webhook/email-customer.js b/hooks/email-customer.js similarity index 100% rename from stripe-webhook/email-customer.js rename to hooks/email-customer.js diff --git a/stripe-webhook/email-sales.js b/hooks/email-sales.js similarity index 100% rename from stripe-webhook/email-sales.js rename to hooks/email-sales.js diff --git a/stripe-webhook/ntfy-sales.js b/hooks/ntfy-sales.js similarity index 100% rename from stripe-webhook/ntfy-sales.js rename to hooks/ntfy-sales.js diff --git a/stripe-webhook/remove-sid.js b/hooks/remove-sid.js similarity index 100% rename from stripe-webhook/remove-sid.js rename to hooks/remove-sid.js diff --git a/listeners/monero.js b/listeners/monero.js new file mode 100644 index 0000000..d722a2b --- /dev/null +++ b/listeners/monero.js @@ -0,0 +1,51 @@ +'use strict' +require('dotenv').config() +const fs = require('fs').promises +const express = require('express') +const app = express() +const ORDERS_DIR = `${__dirname}/../orders` +app.listen(process.env.MONERO_LISTENER_PORT) +.post('/', express.json(), async (req) => { + if (req.body.data.object.object==='charge') { + + // Check if paid + if (!req.body.data.object.paid) + return console.log(`[${req.body.id}] Charge unpaid!`) + + // Get order ID + const orderId = req.body.data.object.metadata.id + if (orderId == null) + return console.error(`[${req.body.id}] Charge has no metadata.id!`) + else + console.log(`[${req.body.id}] Charge paid for order ${orderId}`) + + // Get order file + const orderFile = `${ORDERS_DIR}/${orderId}.json` + let order; try { + order = await JSON.parse(await fs.readFile(orderFile)) + } catch (err) { + return console.error(`[${req.body.id}] Failed to retrieve order from ${orderFile}:\n${err}`) + } + + // Save paidDate to order + try { order.paidDate = new Date() + fs.writeFile(orderFile, JSON.stringify(order,null,2)) + } catch (err) { + console.error(`[${req.body.id}] Failed to write paidDate to ${orderFile}:\n${err}`) + } + + // Email customer + require('../hooks/email-customer')(order) + + // Ntfy sales team + require('../hooks/ntfy-sales')(order) + + // Email sales team + require('../hooks/email-sales')(order) + + // Remove single products from store + require('./remove-sid')(order) + + } + +}) diff --git a/stripe-webhook/index.js b/listeners/stripe.js similarity index 87% rename from stripe-webhook/index.js rename to listeners/stripe.js index 43beeb8..058728a 100644 --- a/stripe-webhook/index.js +++ b/listeners/stripe.js @@ -3,14 +3,13 @@ require('dotenv').config() const fs = require('fs').promises const express = require('express') const app = express() - const ORDERS_DIR = `${__dirname}/../orders` // Start webhook forwarder so we don't need a public endpoint require('../lib/run.js')(`stripe listen --api-key '${process.env.STRIPE_RES}' --forward-to 'http://localhost:${process.env.HOOK_PORT}/' --format 'JSON'`) // Receive that webhook -app.listen(process.env.HOOK_PORT) +app.listen(process.env.STRIPE_LISTENER_PORT) app.post('/', express.json(), async (req) => { if (req.body.data.object.object==='charge') { @@ -42,16 +41,16 @@ app.post('/', express.json(), async (req) => { } // Email customer - require('./email-customer')(order) + require('../hooks/email-customer')(order) // Ntfy sales team - require('./ntfy-sales')(order) + require('../hooks/ntfy-sales')(order) // Email sales team - require('./email-sales')(order) + require('../hooks/email-sales')(order) // Remove single products from store - require('./remove-sid')(order) + require('../hooks/remove-sid')(order) } diff --git a/package.json b/package.json index 048afa4..683945e 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "serve": "npx @11ty/eleventy --quiet --serve", "json": "npx @11ty/eleventy --to=json", "api": "node api/index.js", - "stripe-webhook": "node stripe-webhook/index.js" + "stripe-listener": "node listeners/stripe.js", + "monero-listener": "node listeners/monero.js" }, "author": { "name": "Keith Irwin",