Created
September 29, 2021 11:48
-
-
Save threetwotwo/2459ef03521fadb377ae2eef0da5736e 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 functions = require("firebase-functions"); | |
const stripe = require('stripe')(functions.config().stripe.key); | |
const admin = require("firebase-admin"); | |
const axios = require('axios'); | |
admin.initializeApp(); | |
// // Create and Deploy Your First Cloud Functions | |
// // https://firebase.google.com/docs/functions/write-firebase-functions | |
// | |
// exports.helloWorld = functions.https.onRequest((request, response) => { | |
// functions.logger.info("Hello logs!", {structuredData: true}); | |
// response.send("Hello from Firebase!"); | |
// }); | |
exports.stripeCreatePaymentIntent = functions.https.onRequest(async (req, res) => { | |
functions.logger.log("Stripe request payment intent", req.body, { structured_data: true }); | |
const amount = req.body.amount; | |
try { | |
// Create a PaymentIntent: | |
const paymentIntent = await stripe.paymentIntents.create({ | |
amount: amount, | |
currency: 'eur', | |
}); | |
return res.json({ paymentIntent: paymentIntent }); | |
} catch (e) { | |
console.log(e); | |
} | |
}); | |
exports.onOrderPaymentSuccess = functions.firestore | |
.document('/orders/{orderId}') | |
.onWrite(async (change, context) => { | |
const paymentIntentId = change.after.get('payment_intent'); | |
const paymentStatus = change.after.get('payment_status'); | |
const data = change.after.data(); | |
const transfers = data.transfers; | |
//Retrieve charge id | |
if (paymentIntentId && paymentStatus === 'success') { | |
const paymentIntent = await stripe.paymentIntents.retrieve( | |
paymentIntentId | |
); | |
functions.logger.log('paymentIntentId', paymentIntentId, { structuredData: true }); | |
const chargeId = paymentIntent.charges.data[0].id; | |
const storeId = data.items[0].product.store_id; | |
const productArray = data.items.slice().map(x => { | |
return { | |
'id': x.product.id, | |
'quantity': x.quantity, | |
'shopify': { 'variantId': x.variantId }, | |
} | |
}); | |
//Create transfers | |
await transfers.reduce(async (promise, transfer) => { | |
await promise; | |
transfer['source_transaction'] = chargeId; | |
const result = await stripe.transfers.create(transfer); | |
functions.logger.log("Stripe transfer", result, { structured_data: true }); | |
}, Promise.resolve()); | |
//Update inventory | |
const sendToCloudFunc = { | |
'data': { | |
'stripe': { | |
'chargeId': chargeId | |
}, | |
'email': data.client_email, | |
'checkout': [ | |
{ | |
'storeId': storeId, | |
'products': productArray | |
} | |
], | |
} | |
}; | |
const stripe_verifyPayment = await axios.post('https://us-central1-labels-webapp.cloudfunctions.net/stripe_verifyPayment', sendToCloudFunc); | |
functions.logger.log('response', stripe_verifyPayment, { structuredData: true }); | |
return; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment