8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

后端集成 Stripe 支付的 Node.JS 文件中出现错误

Nat 1月前

16 0

我在我的 server.js Node 文件中的这一行出现错误 - \'const determination = await stripe.tax.calculations.create({\', 错误是 \'await is only valid in async function\'.

我在 server.js Node 文件中的这一行出现错误 - \'const determination = await stripe.tax.calculations.create({\', 错误是 \'await is only valid in async function\'. 下面是我的 server.js 文件的代码

const stripe = require('stripe')('sk_test_');
const express = require('express');
const app = express();

app.use(express.json());

const calculation = await stripe.tax.calculations.create({
  currency: 'usd',
  line_items: [
    {
      amount: 1000,
      reference: 'L1',
      tax_code: 'txcd_99999999',
    },
  ],
  customer_details: {
    address: {
      line1: '920 5th Ave',
      city: 'Seattle',
      state: 'WA',
      postal_code: '98104',
      country: 'US',
    },
    address_source: 'shipping',
  },
});

app.post('/prepare-payment-sheet', async (req, res) => {
try {
    const customer = await stripe.customers.create();
    const ephemeralKey = await stripe.ephemeralKeys.create({customer: customer.id},
                                                           {apiVersion: '2024-06-20; payment_intent_with_tax_api_beta=v1;'});
    const paymentIntent = await stripe.paymentIntents.create({
        amount: 1099,
        currency: 'usd',
        customer: customer.id,
        automatic_payment_methods: {
            enabled: true,
        },
        async_workflows: {
            inputs: {
                tax: {
                calculation: '{{CALCULATION_ID}}',
                },
            },
        },
    });
    
    console.log(req.body)
    console.log(res.body)
    
    res.json({
        paymentIntentID: paymentIntent.id,
        clientSecret: paymentIntent.client_secret,
        ephemeralKey: ephemeralKey.secret,
        customer: customer.id,
        publishableKey: 'pk_test_'
    });
} catch(e) {
    res.status(400).json({ error: { message: e.message }});
}
});

app.post('/update-payment-sheet', async (req, res) => {

    const paymentIntent = await stripe.paymentIntents.update(
        req.body.paymentIntentID,
        {
            amount: req.body.amount,
        }
    );
    console.log(req.body)
    console.log(res.body)
    
    res.json({});
});

app.listen(3000, () => console.log('Running now on port 3000'));
app.get('/', (req,res) => res.json('My API is running'))

以下是 Stripe 网站上无效的代码副本

// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(
  'sk_test_',
  {apiVersion: '2024-06-20; payment_intent_with_tax_api_beta=v1;'}
);

const paymentIntent = await stripe.paymentIntents.create({
  amount: 1000,
  currency: 'usd',
  automatic_payment_methods: {
    enabled: true,
  },
  async_workflows: {
    inputs: {
      tax: {
        calculation: '{{CALCULATION_ID}}',
      },
    },
  },
});
帖子版权声明 1、本帖标题:后端集成 Stripe 支付的 Node.JS 文件中出现错误
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Nat在本站《node.js》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: