Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Created August 3, 2020 16:48
Show Gist options
  • Save Utopiah/c551334af75ff83d1194829e0c5fb51e to your computer and use it in GitHub Desktop.
Save Utopiah/c551334af75ff83d1194829e0c5fb51e to your computer and use it in GitHub Desktop.
const express = require("express");
const app = express();
const fs = require('fs');
const fetch = require('node-fetch');
const tokenFile = './.ret.credentials'
const token = JSON.parse(fs.readFileSync(tokenFile, 'utf8')).token;
const accountURL = 'https://myhubsURL.com/api/v1/accounts'
app.use(express.json())
var customers = {}
// should discard anything not matching
// https://stripe.com/docs/webhooks/signatures https://stripe.com/docs/ips
app.post("/stripe-webhook", (req, res) => {
event = req.body
switch (event.type) {
case 'checkout.session.completed':
var email = customers[event.data.object.customer].email
var name = customers[event.data.object.customer].name
console.log('creating account for', email, name, 'via the account API')
registerHubsAccountFromEmail(email, name)
break;
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
if (typeof customers[paymentIntent.customer] === "undefined") customers[paymentIntent.customer] = {}
if (paymentIntent && paymentIntent.charges && paymentIntent.charges.data && paymentIntent.charges.data.length)
customers[paymentIntent.customer].receipt_url = paymentIntent.charges.data[0].receipt_url
console.log('payment succeeded!')
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent);
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
//console.log('paymentMethod', paymentMethod)
customers[paymentMethod.customer] = {}
customers[paymentMethod.customer].email = paymentMethod.billing_details.email
customers[paymentMethod.customer].name = paymentMethod.billing_details.name
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached(paymentMethod);
break;
// ... handle other event types
default:
// Unexpected event type
return res.status(400).end();
}
// Return a response to acknowledge receipt of the event
res.json({received: true});
});
app.listen(20002, () => {
console.log("Hubs Stripe microservice running on port 20002");
});
function registerHubsAccountFromEmail(email, name){
console.log('requesting account creation for', email)
fetch(accountURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'bearer ' + token
},
body: JSON.stringify( {"data": [{ "email": email, "name": name } ] })
}).then(reply => {
console.log('success');
console.log(reply);
}).catch(err => {
console.log('error');
console.log(err);
});
}
@lastdof
Copy link

lastdof commented Sep 24, 2020

Thank you :)
We WILL have our hubs shop experience :) It's the right time !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment