使用React JS Frontend完成条纹结帐会话后,如何将数据推向Firestore

发布于 2025-02-11 12:12:51 字数 4254 浏览 1 评论 0原文

使用React JS节点JS完成条纹结帐会话后,我正在尝试将结帐篮数据推到Firebase Firestore。结帐会话成功完成没有错误。但是,没有用户订单数据可以从下面的代码中将firebase firestore推向firebase firestore:

const { error } = await stripe.redirectToCheckout({
        sessionId
      }).then(()=>{
        db
        .collection('users')
        .doc(user?.uid)
        .collection('orders')
        .doc()
        .set({
         basket: basket,
        //  amount: paymentIntent.amount,
       })
     });

下面是后端和前端函数的整个代码

/index.js

const functions = require("firebase-functions");
const express=require("express");
const cors=require("cors");
require('dotenv').config({ path: './.env' });
//API
const stripe=require("stripe")('sk_test_51KM...zIP');
//App config
const app=express();
var admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
//middlewares
app.use(cors({origin: true}));
app.use(express.json());
async function createCheckoutSession(req, res) {
  const domainUrl = process.env.WEB_APP_URL;
  const { line_items, customer_email } = req.body;
  // check req body has line items and email
  if (!line_items || !customer_email) {
    return res.status(400).json({ error: 'missing required session parameters' });
  }
  let session; 
  try {
    session = await stripe.checkout.sessions.create({
      payment_method_types: ['card'],
      mode: 'payment',
      line_items,
      customer_email,
      success_url: `${domainUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
      // success_url: '/success?session_id={CHECKOUT_SESSION_ID}',
      cancel_url: `${domainUrl}/canceled`,
      shipping_address_collection: { allowed_countries: ['GB', 'US'] }
    }); 
    res.status(200).json({ sessionId: session.id, });
  } catch (error) {
    console.log(error);
    res.status(400).json({ error: 'an error occured, unable to create session'});
  }}
app.get('/',(req, res)=>res.send('Hello World!'));
app.post('/create-checkout-session', createCheckoutSession);
exports.api=functions.https.onRequest(app);

src/strac/strip-checkout.js,

import React, { useContext, useState,useEffect } from 'react';
import { useStripe } from '@stripe/react-stripe-js';
import CheckoutProduct from '../CheckoutProduct';
import { useStateValue } from '../StateProvider';
import { db } from '../firebase';
import { fetchFromAPI } from '../helpers';
import "./stripe-checkout.css";
import { useHistory } from 'react-router-dom/cjs/react-router-dom.min';

const StripeCheckout = () => {
  const history=useHistory();
  const [{basket, user},dispatch]=useStateValue();
  const [email, setEmail] = useState('');
  const [processing,setProcessing]=useState(""); 
  const stripe = useStripe();
  const handleGuestCheckout = async (e) => {
        e.preventDefault();
        setProcessing(true);
        const line_items = basket?.map(item => {
          return {
            quantity: 1,
            price_data: {
              currency: 'usd',
              unit_amount: item.price*100, // amount is in cents
              product_data: {
                name: item.title,
                description: item.material
                images: [item.image[0]],
              }}}})
      const response = await fetchFromAPI('create-checkout-session', {
        body: { line_items, customer_email: user.email },
      })
      const { sessionId } = response;
      const { error } = await stripe.redirectToCheckout({
        sessionId
      }).then(()=>{
        db
        .collection('users')
        .doc(user?.uid)
        .collection('orders')
        .doc()
        .set({
         basket: basket,
       })
     });
      console.log(sessionId);
      if (error) {
        console.log(error);
      }}
  return (
    <form onSubmit={handleGuestCheckout}>
      <div className='submit-btn'>
        <button type='submit' >
        <span>{processing ?<p>Processing</p>:
          "Proceed to Checkout"}</span>
        </button>
      </div>
    </form>
  );
}

export default StripeCheckout;

因为没有显示任何错误或警告,在这种情况下,如何将数据推到结帐课程后如何将数据推向Firestore?

I am trying to push the checkout basket data to firebase firestore after a stripe checkout session complete using react js node js. the checkout session successfully completed without error. However, there are NO user orders data being push to firebase firestore from the piece of code below:

const { error } = await stripe.redirectToCheckout({
        sessionId
      }).then(()=>{
        db
        .collection('users')
        .doc(user?.uid)
        .collection('orders')
        .doc()
        .set({
         basket: basket,
        //  amount: paymentIntent.amount,
       })
     });

Below is the whole pieces of codes of backend and frontend

Functions/index.js

const functions = require("firebase-functions");
const express=require("express");
const cors=require("cors");
require('dotenv').config({ path: './.env' });
//API
const stripe=require("stripe")('sk_test_51KM...zIP');
//App config
const app=express();
var admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
//middlewares
app.use(cors({origin: true}));
app.use(express.json());
async function createCheckoutSession(req, res) {
  const domainUrl = process.env.WEB_APP_URL;
  const { line_items, customer_email } = req.body;
  // check req body has line items and email
  if (!line_items || !customer_email) {
    return res.status(400).json({ error: 'missing required session parameters' });
  }
  let session; 
  try {
    session = await stripe.checkout.sessions.create({
      payment_method_types: ['card'],
      mode: 'payment',
      line_items,
      customer_email,
      success_url: `${domainUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
      // success_url: '/success?session_id={CHECKOUT_SESSION_ID}',
      cancel_url: `${domainUrl}/canceled`,
      shipping_address_collection: { allowed_countries: ['GB', 'US'] }
    }); 
    res.status(200).json({ sessionId: session.id, });
  } catch (error) {
    console.log(error);
    res.status(400).json({ error: 'an error occured, unable to create session'});
  }}
app.get('/',(req, res)=>res.send('Hello World!'));
app.post('/create-checkout-session', createCheckoutSession);
exports.api=functions.https.onRequest(app);

src/strip-checkout.js

import React, { useContext, useState,useEffect } from 'react';
import { useStripe } from '@stripe/react-stripe-js';
import CheckoutProduct from '../CheckoutProduct';
import { useStateValue } from '../StateProvider';
import { db } from '../firebase';
import { fetchFromAPI } from '../helpers';
import "./stripe-checkout.css";
import { useHistory } from 'react-router-dom/cjs/react-router-dom.min';

const StripeCheckout = () => {
  const history=useHistory();
  const [{basket, user},dispatch]=useStateValue();
  const [email, setEmail] = useState('');
  const [processing,setProcessing]=useState(""); 
  const stripe = useStripe();
  const handleGuestCheckout = async (e) => {
        e.preventDefault();
        setProcessing(true);
        const line_items = basket?.map(item => {
          return {
            quantity: 1,
            price_data: {
              currency: 'usd',
              unit_amount: item.price*100, // amount is in cents
              product_data: {
                name: item.title,
                description: item.material
                images: [item.image[0]],
              }}}})
      const response = await fetchFromAPI('create-checkout-session', {
        body: { line_items, customer_email: user.email },
      })
      const { sessionId } = response;
      const { error } = await stripe.redirectToCheckout({
        sessionId
      }).then(()=>{
        db
        .collection('users')
        .doc(user?.uid)
        .collection('orders')
        .doc()
        .set({
         basket: basket,
       })
     });
      console.log(sessionId);
      if (error) {
        console.log(error);
      }}
  return (
    <form onSubmit={handleGuestCheckout}>
      <div className='submit-btn'>
        <button type='submit' >
        <span>{processing ?<p>Processing</p>:
          "Proceed to Checkout"}</span>
        </button>
      </div>
    </form>
  );
}

export default StripeCheckout;

Since there is no shown any error or warning, how to push the data to firestore after stripe checkout session complete in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2025-02-18 12:12:51

Stripe Checkout返回到成功或取消净值。

除了COBECT_SESSION_ID,您可以在定义这些URL时添加到它。

从Stripe Checkout获取数据的通常方法是使用Firebase函数webhook。此webhook如果完成交易,或者失败时。此Webhook将数据存储在Firebase中。我有同样的问题,除了使用webhook外,没有其他适当的解决方案。

https://stripe.com/docs/webhooks

也有一个firebase Extension提供对条纹的支持,包括Webhook。基本上。它将为Firestore添加一些收藏,然后您可以查询。

https://firebase.google.com/productss/products/products/stripe-stripe-stripe-stripe--stripe--stripe--c,,,,,,,,,,, firestore-stripe付款

Stripe Checkout returns to either a success- or a cancel-URL.

It does not send data to both URLs, except the CHECKOUT_SESSION_ID you may add to it when defining these URLs.

The usual way to get data from Stripe Checkout is to use a Firebase Function Webhook. This Webhook is called by Stripe if a transactions is done, or upon failure etc. This Webhook stores the data in Firebase. I had the same problem and found no other, proper solution than using a Webhook.

https://stripe.com/docs/webhooks

There is also a Firebase Extension providing support for Stripe, including the Webhook. Basically. it will add some collections to Firestore, which you then can query.

https://firebase.google.com/products/extensions/stripe-firestore-stripe-payments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文