Check if Stripe customer has PNID and if PNID exists

This commit is contained in:
Jonathan Barrow 2022-06-24 15:25:41 -04:00
parent 0241fb1f10
commit 027321911b
5 changed files with 106 additions and 1 deletions

View File

@ -31,5 +31,9 @@
}
}
},
"gmail": {
"user": "email@gmail.com",
"pass": "app-password"
},
"aes_key": "hex key here"
}

42
package-lock.json generated
View File

@ -18,6 +18,7 @@
"express-handlebars": "^5.3.1",
"express-locale": "^2.0.0",
"fs-extra": "^9.1.0",
"gmail-send": "^1.8.14",
"got": "^11.8.2",
"gray-matter": "^4.0.3",
"kaitai-struct": "^0.9.0",
@ -1953,6 +1954,15 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/gmail-send": {
"version": "1.8.14",
"resolved": "https://registry.npmjs.org/gmail-send/-/gmail-send-1.8.14.tgz",
"integrity": "sha512-hc+4Ej7ZJtw0G5sync10pmWkpPXIabkQ+p/a92lPPTXni3ChEU9sR2wxOvK6Hx+5Ou+2m9h1cVffWEgtR6Gzkw==",
"dependencies": {
"lodash": "^4.17.21",
"nodemailer": "^6.6.5"
}
},
"node_modules/got": {
"version": "11.8.2",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.2.tgz",
@ -2690,6 +2700,11 @@
"node": ">= 0.8.0"
}
},
"node_modules/lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"node_modules/lodash.clonedeep": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
@ -3034,6 +3049,14 @@
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.1.tgz",
"integrity": "sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw=="
},
"node_modules/nodemailer": {
"version": "6.7.5",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.7.5.tgz",
"integrity": "sha512-6VtMpwhsrixq1HDYSBBHvW0GwiWawE75dS3oal48VqRhUvKJNnKnJo2RI/bCVQubj1vgrgscMNW4DHaD6xtMCg==",
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/normalize-url": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz",
@ -5983,6 +6006,15 @@
"type-fest": "^0.20.2"
}
},
"gmail-send": {
"version": "1.8.14",
"resolved": "https://registry.npmjs.org/gmail-send/-/gmail-send-1.8.14.tgz",
"integrity": "sha512-hc+4Ej7ZJtw0G5sync10pmWkpPXIabkQ+p/a92lPPTXni3ChEU9sR2wxOvK6Hx+5Ou+2m9h1cVffWEgtR6Gzkw==",
"requires": {
"lodash": "^4.17.21",
"nodemailer": "^6.6.5"
}
},
"got": {
"version": "11.8.2",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.2.tgz",
@ -6506,6 +6538,11 @@
"type-check": "~0.4.0"
}
},
"lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"lodash.clonedeep": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
@ -6778,6 +6815,11 @@
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.1.tgz",
"integrity": "sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw=="
},
"nodemailer": {
"version": "6.7.5",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.7.5.tgz",
"integrity": "sha512-6VtMpwhsrixq1HDYSBBHvW0GwiWawE75dS3oal48VqRhUvKJNnKnJo2RI/bCVQubj1vgrgscMNW4DHaD6xtMCg=="
},
"normalize-url": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz",

View File

@ -26,6 +26,7 @@
"express-handlebars": "^5.3.1",
"express-locale": "^2.0.0",
"fs-extra": "^9.1.0",
"gmail-send": "^1.8.14",
"got": "^11.8.2",
"gray-matter": "^4.0.3",
"kaitai-struct": "^0.9.0",

View File

@ -4,13 +4,16 @@ const DiscordOauth2 = require('discord-oauth2');
const { v4: uuidv4 } = require('uuid');
const AdmZip = require('adm-zip');
const Stripe = require('stripe');
const gmail = require('gmail-send');
const database = require('../database');
const util = require('../util');
const config = require('../../config.json');
const { Router } = express;
const stripe = new Stripe(config.stripe.secret_key);
const router = new Router();
const sendGmail = gmail(config.gmail);
const { Router } = express;
const aesKey = Buffer.from(config.aes_key, 'hex');
// Create OAuth client
@ -591,10 +594,19 @@ router.post('/checkout/:priceId', async (request, response) => {
customer = searchResults[0];
} else {
customer = await stripe.customers.create({
email: account.email.address,
metadata: {
pnid_pid: pid
}
});
await database.PNID.updateOne({ pid }, { $set: {
connections: {
stripe: {
customer_id: customer.id
}
}
} }, { upsert: true }).exec();
}
const priceId = request.params.priceId;
@ -629,7 +641,45 @@ router.post('/stripe-wh', express.raw({ type: 'application/json' }), async (requ
const subscription = event.data.object;
const product = await stripe.products.retrieve(subscription.plan.product);
const customer = await stripe.customers.retrieve(subscription.customer);
if (!customer.metadata.pnid_pid && subscription.status !== 'canceled' && subscription.status !== 'unpaid') {
// No PNID PID linked to customer. Abort and refund!
await stripe.subscriptions.del(subscription.id);
const invoice = await stripe.invoices.retrieve(subscription.latest_invoice);
await stripe.refunds.create({
payment_intent: invoice.payment_intent
});
await sendGmail({
to: customer.email,
subject: 'Pretendo Subscription Failed - No Linked PNID',
text: `Your recent subscription to Pretendo Network has failed.\nThis is due to no PNID PID being linked to the Stripe customer account used. The subscription has been canceled and refunded. Please contact Jon immediately.\nStripe Customer ID: ${customer.id}`
});
return response.json({ received: true });
}
const pid = Number(customer.metadata.pnid_pid);
const pnid = await database.PNID.findOne({ pid });
if (!pnid && subscription.status !== 'canceled' && subscription.status !== 'unpaid') {
// PNID does not exist. Abort and refund!
await stripe.subscriptions.del(subscription.id);
const invoice = await stripe.invoices.retrieve(subscription.latest_invoice);
await stripe.refunds.create({
payment_intent: invoice.payment_intent
});
await sendGmail({
to: customer.email,
subject: 'Pretendo Subscription Failed - PNID Not Found',
text: `Your recent subscription to Pretendo Network has failed.\nThis is due to the provided PNID not being found. The subscription has been canceled and refunded. Please contact Jon immediately.\nStripe Customer ID: ${customer.id}\nPNID PID: ${pid}`
});
return response.json({ received: true });
}
const updateData = {};

View File

@ -8,6 +8,14 @@ const PNIDSchema = new Schema({
},
server_access_level: String,
access_level: Number,
connections: {
stripe: {
customer_id: String,
price_id: String,
tier_level: Number,
latest_webhook_timestamp: Number
}
}
});
module.exports = PNIDSchema;