Created
September 27, 2021 10:33
-
-
Save threetwotwo/5b443c9b19b74bd1922d9ec6c9b28e2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 admin = require("firebase-admin"); | |
| admin.initializeApp(); | |
| const cors = require("cors")({ origin: true }); | |
| const axios = require('axios') | |
| exports.payWithStripeAPI = functions.https.onRequest((request, response) => { | |
| let secretKey; | |
| let stripe; | |
| cors(request, response, async () => { | |
| try { | |
| if(request.body.production) { | |
| functions.logger.log("Stripe Production ", request.body.production); | |
| secretKey = functions.config().stripe_production.key; | |
| stripe = require("stripe")(secretKey); | |
| } else { | |
| functions.logger.log("Stripe Test", request.body.production); | |
| secretKey = functions.config().stripe_test.key; | |
| stripe = require("stripe")(secretKey); | |
| } | |
| const stripeObject = { | |
| amount: (request.body.amount * 100), | |
| currency: 'eur', | |
| source: request.body.token, | |
| transfer_data: { | |
| amount: (((request.body.amount * 100)) * 0.9) - 25, | |
| destination: 'acct_1IvGExR7tkmLM4u5', | |
| }, | |
| } | |
| functions.logger.log("Stripe Object ", stripeObject, request.body.amount, {structuredData: true}); | |
| const received = { | |
| amount: request.body.amount, | |
| currency: 'eur', | |
| source: request.body.token, | |
| card: request.body.card, | |
| createdAt: request.body.createdAt, | |
| type: request.body.type, | |
| userId: request.body.userId, | |
| products: request.body.products, | |
| orderId: request.body.orderId, | |
| } | |
| functions.logger.log("Received Object ", received, {structuredData: true}); | |
| const newTransaction = await admin.firestore().collection('transactions').add(received); | |
| functions.logger.log("newTransaction Object ", newTransaction, {structuredData: true}); | |
| if (newTransaction) { | |
| functions.logger.log("newTransaction true ", {structuredData: true}); | |
| const getPaymentMethod = await stripe.paymentMethods.create({ | |
| type: 'card', | |
| card: { | |
| token:request.body.token | |
| }, | |
| }); | |
| functions.logger.log("getPaymentMethod Object ", getPaymentMethod, {structuredData: true}); | |
| const stripeOperation = await stripe.paymentIntents.create({ | |
| amount: (request.body.amount * 100), | |
| currency: 'eur', | |
| transfer_group: 'Testus', | |
| }); | |
| functions.logger.log("stripeOperation paymentIntents Create Object ", stripeOperation, {structuredData: true}); | |
| const confirmPayment = await stripe.paymentIntents.confirm( | |
| stripeOperation.id, | |
| {payment_method: getPaymentMethod.id} | |
| ); | |
| functions.logger.log("stripeOperation confirmPayment Response Status ", confirmPayment.status, {structuredData: true}); | |
| // Create a Transfer to the connected account (later): | |
| const transfer = await stripe.transfers.create({ | |
| amount: (((request.body.amount * 100) - 100) * 0.9) - 25, | |
| currency: 'eur', | |
| destination: 'acct_1IvGExR7tkmLM4u5', | |
| transfer_group: 'Testus', | |
| source_transaction: confirmPayment.charges.data[0].id, | |
| }); | |
| functions.logger.log("stripeOperation transfer Store Object ", transfer, {structuredData: true}); | |
| // Create a second Transfer to another connected account (later): | |
| const secondTransfer = await stripe.transfers.create({ | |
| amount: 100, | |
| currency: 'eur', | |
| destination: 'acct_1IvGMnQtMXW61fdo', | |
| transfer_group: 'Testus', | |
| source_transaction: confirmPayment.charges.data[0].id, | |
| }); | |
| functions.logger.log("stripeOperation transfer Store Object ", secondTransfer, {structuredData: true}); | |
| if (stripeOperation) { | |
| const delivery = { | |
| orderId: request.body.orderId, | |
| orderTime: admin.firestore.FieldValue.serverTimestamp(), | |
| userId: request.body.userId, | |
| products: request.body.products, | |
| address: request.body.address, | |
| status: 0, | |
| updatedAt: admin.firestore.FieldValue.serverTimestamp(), | |
| client_name: request.body.client_name | |
| } | |
| const newDelivery = await admin.firestore().collection('delivery').add(delivery); | |
| functions.logger.log("newDelivery Object ", newDelivery, {structuredData: true}); | |
| const updatedTransaction = { | |
| stripeIntentCreate: stripeOperation, | |
| stripeIntentConfirm: confirmPayment, | |
| status: true, | |
| deliveryId: newDelivery.id | |
| } | |
| const updateTransaction = await admin.firestore().doc('/transactions/' + newTransaction.id).set(updatedTransaction, { merge: true }); | |
| functions.logger.log('TRANSACTION Update true', updateTransaction, {structuredData: true}); | |
| const productIdArray = request.body.products.slice().map(x => { | |
| return { | |
| id: x.productId, | |
| quantity: request.body.products.length, | |
| } | |
| }) | |
| const checkoutArray = [{ | |
| storeId: request.body.products[0].store_id, | |
| products: productIdArray | |
| }] | |
| const sendToCloudFunc = { | |
| data: { | |
| stripe: { | |
| chargeId: confirmPayment.charges.data[0].id | |
| }, | |
| email: request.body.client_email, | |
| checkout: checkoutArray, | |
| } | |
| } | |
| functions.logger.log('TOSEND PRODUCT ID ARRAY', productIdArray, {structuredData: true}); | |
| functions.logger.log('TOSEND ', sendToCloudFunc, {structuredData: true}); | |
| const stripe_verifyPayment = await axios.post('https://us-central1-labels-webapp.cloudfunctions.net/stripe_verifyPayment', sendToCloudFunc) | |
| functions.logger.log('response', stripe_verifyPayment, {structuredData: true}); | |
| response.status(200).send({ updatedTransaction, message: "Stripe operation success", stripe: stripeOperation, transaction: updateTransaction }); | |
| return; | |
| } else { | |
| const updatedTransaction = { | |
| stripeResponse: stripeOperation, | |
| status: false | |
| } | |
| const updateTransaction = await admin.firestore().doc('/transactions/' + newTransaction.id).set(updatedTransaction, { merge: true }); | |
| functions.logger.log('TRANSACTION Update false', updateTransaction, {structuredData: true}); | |
| response.status(404).send({ updatedTransaction, message: "stripe", error: updateTransaction }); | |
| return; | |
| } | |
| } else { | |
| response.status(404).send({ status: false, message: "transaction", error: newTransaction }); | |
| } | |
| } catch (err) { | |
| functions.logger.log('catch', err, {structuredData: true}); | |
| response.status(404).send({ status: false, message: "CORS", error: err }); | |
| } | |
| }); | |
| }); | |
| exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => { | |
| const customer = await stripe.customers.create({ email: user.email }); | |
| const intent = await stripe.setupIntents.create({ | |
| customer: customer.id, | |
| }); | |
| await admin.firestore().collection('stripe_customers').doc(user.uid).set({ | |
| customer_id: customer.id, | |
| setup_secret: intent.client_secret, | |
| }); | |
| return; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment