Created
December 29, 2022 13:03
-
-
Save livefiredev/7d458fcacd0ace31101f32c5785f8820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stripe = require('stripe')('put-your-stripe-secret-key-here'); | |
const express = require('express'); | |
const cors = require('cors'); | |
const app = express(); | |
const fs = require('fs'); | |
app.use(cors({ | |
origin: '*' | |
})); | |
app.use( | |
"/webhook", | |
express.json({ | |
verify: (req, res, buf) => { | |
req.rawBody = buf.toString(); | |
}, | |
}) | |
); | |
app.use(express.json()); | |
app.post('/create_customer', async (request, response) => { | |
const customerEmailAddress = request.body.customerEmailId; | |
const customer = await stripe.customers.create({ | |
description: `${customerEmailAddress} via API`, | |
email: customerEmailAddress | |
}); | |
console.log(customer); | |
let theCreatedCustomerId = customer.id; | |
response.send({ | |
customerId: theCreatedCustomerId | |
}); | |
}); | |
app.post('/create_checkout_link', async (request, response) => { | |
const priceId = request.body.priceId; | |
const customerId = request.body.customerId; | |
const session = await stripe.checkout.sessions.create({ | |
billing_address_collection: 'auto', | |
line_items: [ | |
{ | |
price: priceId, | |
quantity: 1, | |
}, | |
], | |
mode: 'subscription', | |
success_url: `http://localhost:3000/?success=true`, | |
cancel_url: `http://localhost:3000?canceled=true`, | |
customer: customerId | |
}); | |
console.log(session); | |
response.send({ | |
url: session.url | |
}); | |
}); | |
app.post('/create_billing_portal_link', async (request, response) => { | |
const customerId = request.body.customerId; | |
const session = await stripe.billingPortal.sessions.create({ | |
customer: customerId, | |
configuration: 'bpc_1MIO9GSICZrnQHl2SAecWs3K', | |
return_url: 'http://localhost:3000/?back_from=billing', | |
}); | |
console.log(session); | |
response.send({ | |
url: session.url | |
}); | |
}); | |
app.post( | |
'/webhook', express.raw(), (request, response) => { | |
let event = request.body; | |
const endpointSecret = 'whsec_PUT_YOUR_END_POINT_SECRET_HERE'; | |
if (endpointSecret) { | |
const signature = request.headers['stripe-signature']; | |
try { | |
event = stripe.webhooks.constructEvent( | |
request.rawBody, | |
signature, | |
endpointSecret | |
); | |
} catch (err) { | |
console.log(`⚠️ Webhook signature verification failed.`, err.message); | |
return response.sendStatus(400); | |
} | |
} | |
const eventType = event.type; | |
const unixTimeStamp = Math.floor(Date.now() / 1000); | |
const completeEventBody = event; | |
let fileName = `${unixTimeStamp}-${eventType}`; | |
let webhookFileContent = JSON.stringify(completeEventBody, null, 4); | |
fs.appendFile(`./webhook_events/${fileName}.json`, webhookFileContent, function (err) { | |
if (err) throw err; | |
console.log(`Webhook event file saved with name: ${fileName}!`); | |
}); | |
response.send(); | |
} | |
); | |
app.listen(4242); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment