调用Stripe处理支付时遭遇TS2349错误,寻求解决方案
解决Stripe TS2349调用签名错误问题
问题根源
你在前端项目中错误安装了后端专用的stripe包,这个包是给Node.js服务端使用的,需要通过类实例化才能调用方法,而前端浏览器环境应该使用Stripe官方提供的前端SDK @stripe/stripe-js。
解决步骤
- 卸载后端的
stripe依赖
npm uninstall stripe
- 安装前端专用的Stripe SDK
npm install @stripe/stripe-js
- 正确的代码使用方式
import { loadStripe } from '@stripe/stripe-js'; // 替换为你的Stripe公钥 const stripePromise = loadStripe('pk_test_your_public_key'); // 示例支付触发逻辑 const handleInitiatePayment = async () => { const stripe = await stripePromise; if (!stripe) return; // 调用Stripe前端方法,比如跳转结账页 const { error } = await stripe.redirectToCheckout({ sessionId: 'your_checkout_session_id' }); if (error) { console.error('支付初始化失败:', error); } };
补充说明
后端的stripe包需要通过new Stripe(secretKey)实例化后才能调用API,而前端的@stripe/stripe-js采用异步加载的方式提供实例,类型定义完全适配前端TS环境,可直接解决你遇到的Type 'typeof import("stripe")' has no call signatures错误。
内容的提问来源于stack exchange,提问作者Mohd Ayan haque
相关产品推荐
相关产品推荐

