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

Hello. It's very interesting, thank you.
I can't see for now how this can be integrated in a hubs shop but I need to learn a little more about it .
I'm really into building a full Hubs shop with "buy inside" experience.

@lastdof
Copy link

lastdof commented Sep 24, 2020

I have an answer from camelgold who did a fork and explained me how this works :)

@Utopiah
Copy link
Author

Utopiah commented Sep 24, 2020

Glad you found a solution via @camelgod's fork! Just to clarify this is to interface Stripe with Hubs for payment but it's done prior to an in-VR experience. To do purchases while being in-VR I suggest to use in-app currency or token linked to the user account and the stripe payment. I'm very skeptical about in VR payment requiring to input credit card information. It might be feasible but a pretty challenging UX.

@lastdof
Copy link

lastdof commented Sep 24, 2020

Thank you. Yes, i understand it's maybe not the best solution to try to do in-app full payment, due to security problems. We prefer to let stripe taking care of that.
I think we will focus on the shopping experience where you can browse, get information about the product, meet and chat with other visitors and of course, chat with the shop owner.
We even maybe don't need a token or in-app currency ? Just a cart with information and total.
I think we can imagine having some data in gtlf objects like price and options for the cart.
The payment can be made after, back to a standard ecommerce cart. Maybe just plug it to a shopify API.

So, even with external payment, there's already enough to work on :)

I don't know if such a project could be interesting for others to start on discord ?

@Utopiah
Copy link
Author

Utopiah commented Sep 24, 2020

Well the information per item and per cart means tracking prices and amounts so in the end it can be thought of as an in-game token or currency.

Yes Discord would be good since I know quite a few others are interested about monetization in VR.

@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