无法从下一个 js 应用程序以条带形式发送税额

can not send tax amount in stripe from next js app

在我的项目中,我使用 WordPress woo-commerce 作为后端,使用 next js 作为前端。我正在尝试实施条纹支付。我可以在 stripe sessionData 中发送订单项,它们在 stripe 页面上完美显示,但是当我尝试发送带有订单项的税额时,我收到了错误。

import { createCheckoutSession } from "next-stripe/client"; // @see https://github.com/ynnoj/next-stripe
import { loadStripe } from "@stripe/stripe-js";

.....
.....

const createCheckoutSessionAndRedirect = async (orderData) => {
  const sessionData = {
    success_url:
      window.location.origin +
      `/thank-you?session_id={CHECKOUT_SESSION_ID}&order_id=${orderData.orderId}`,
    cancel_url: window.location.href,
    customer_email: orderData.customer_email,
    line_items: getStripeLineItems(orderData.products),
    metadata: getMetaData(
      orderData.billing,
      orderData.shipping,
      orderData.orderId
    ),
    payment_method_types: ["card"],
    mode: "payment",
    total_details:{
      amount_discount: 0,
      amount_shipping: Math.round(10 * 100),
      amount_tax: Math.round(10 * 100),
    },
  };
  console.log("Session from another1:", sessionData);

  const session = await createCheckoutSession(sessionData);

  console.log("Session from another2:", sessionData);
  console.log("from another2:", orderData);

  try {
    console.log("session data", session);
    const stripe = await loadStripe(
      process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
    );
    if (stripe) {
      stripe.redirectToCheckout({ sessionId: session.id });
    }
  } catch (error) {
    console.log(error);
  }
};

经过多次尝试,终于解决了问题

这是控制器。

const stripe = require("stripe")(process.env.STRIPE_PRIVATE_KEY);
const stripePaymentService = require('../service/stripePaymentService');


module.exports.checkoutSessionCreate = async (req, res) => {

    const responseFromService = await stripePaymentService.checkoutSessionCreate(req.body);

    console.log("this is session data", responseFromService);

    try{
        const session = await stripe.checkout.sessions.create(responseFromService);
        res.json({ url: session.url });
        console.log("success session:", session.url);
    }
    catch(e){
        res.status(500).json({ error: e.message })
        console.log("success session:", e.message);
    }
    
}

这是服务...

const stripe = require("stripe")(process.env.STRIPE_PRIVATE_KEY);
const lodash = require('lodash');

module.exports.checkoutSessionCreate = async (serviceData) => {
    // console.log(serviceData.products);

    const sessionData = {
        success_url:`${process.env.CLIENT_URL}/thank-you?session_id={CHECKOUT_SESSION_ID}&order_id=${serviceData.orderId}`,
        cancel_url: `${process.env.CLIENT_URL}`,
        customer_email: serviceData.customer_email,
        line_items: await getStripeLineItems(serviceData.products),
        metadata: getMetaData(
          serviceData.billing,
          serviceData.shipping,
          serviceData.orderId
        ),
        payment_method_types: ["card"],
        mode: "payment",
    };

    return sessionData;
}


const getMetaData = (billing, shipping, orderId) => {
    return {
      billing: JSON.stringify(billing),
      shipping: JSON.stringify(shipping),
      orderId,
    };
};

let getStripeLineItems =async (products) => {
    if (lodash.isEmpty(products) && !isArray(products)) {
        return [];
    }

    const productData = await Promise.all(
        products.map(async (product) => {
            const taxArr = await getTaxID(product.tax_data);
            return {
            quantity: product?.quantity ?? 0,
            name: product?.name ?? "",
            images: [product?.images ?? ""],
            amount: Math.round(product?.amount * 100),
            currency: product?.currency ?? "",
            tax_rates: taxArr,
            };
        })
    );
    return productData;
};

let getTaxID = async (taxData) => {
    let idArr = await Promise.all(
        taxData.map(async (item)=>{
            const taxRate = await stripe.taxRates.create({
                display_name: item.display_name,
                inclusive: item.inclusive,
                percentage: item.percentage,
            });
        
            return taxRate?.id;
        })
    );
    return idArr;
}

我希望这会对某人有所帮助....

您不能像您尝试的那样直接设置 total_details。那是一个计算出来的属性对象(ref), not part of the create endpoint (ref).

要向结帐会话添加税费,您应该使用 automatic taxes with Stripe Tax, or provide explicit tax rates