Skip to content

Instantly share code, notes, and snippets.

@camelgod
Forked from Utopiah/hubs_stripe_backend.js
Created August 3, 2020 20:00
Show Gist options
  • Save camelgod/552ecacc578430d59906325dc04959b7 to your computer and use it in GitHub Desktop.
Save camelgod/552ecacc578430d59906325dc04959b7 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 for this. I don"t know how to integrate that in hubs, but I really work on building our VR shop :)

@camelgod
Copy link
Author

@SergeVI All thanks to Utopiah (this is just a fork in case it got removed). This runs outside of Hubs and it demonstrates how you can create hubs accounts from "anywhere".

The "Fetch" method in registerHubsAccountFromEmail function can be implemented in any website to create a hubs account. In this case, it is used together with a Node Express server (the "app" in the code) that is hosting a simple checkout in Stripe (payment platform).

@lastdof
Copy link

lastdof commented Sep 24, 2020

Ho yes, I see now, sorry. I've put a comment on his code too :) I forgot i went here.
Thank you for your information. I understand better now.
I'm planning a dev project to bring stripe in hubs, so we could really have an interactive xr shop.
Also some new cart and product component to integrate, in hubs and spoke.
Thank you.

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