【问题标题】:import stripe using node js + typescript使用节点 js + typescript 导入条带
【发布时间】:2017-06-19 08:00:06
【问题描述】:

我需要将条带导入我的应用程序

首先我安装了条带 npm 包

npm install stripe --save

Stripe 文档说在连接 api 之前应该设置密钥。

在Node中它喜欢这个

var stripe = require('stripe')(' your stripe API key ');

我需要把它转换成打字稿

我尝试了以下方式。但它对我不起作用

import * as stripe from 'stripe';
stripe('sk_test_...')

如果有人可以帮助我解决这个问题,那将极大地帮助我毫不拖延地继续我的项目。

谢谢

【问题讨论】:

标签: node.js typescript stripe-payments


【解决方案1】:

你可以参考 GitHub 上的 repo: https://github.com/stripe/stripe-node

import Stripe from 'stripe';

const stripe = new Stripe('sk_test_...', {
  apiVersion: '2020-08-27',
});

const createCustomer = async () => {
  const params: Stripe.CustomerCreateParams = {
    description: 'test customer',
  };

  const customer: Stripe.Customer = await stripe.customers.create(params);

  console.log(customer.id);
};
createCustomer();

【讨论】:

  • 这就像一个魅力!注意:API版本需要更新为“2020-08-27”。提交了修改。
【解决方案2】:

更新:以下解决方案已过时,对于使用stripe@8.0.1 或更高版本的用户,应使用David Dehghan 中的answer

正如britzkopf 所说,stripe 还没有提供自己的定义(可能永远不会),但您可以使用来自@types/stripe 的类型定义。

npm install stripe @types/stripe

然后如下导入构造Stripe类。

import * as Stripe from 'stripe';
const stripe = new Stripe('xxx_xxx_xxx');

如果您出于某种原因想要更细粒度的导入,您可以改用这种(有点老套)的方法。

import { resources } from 'stripe';
const stripeData = require('stripe')('xxx_xxx_xxx');
const customers = new resources.Customers(stripeData, null);

【讨论】:

    【解决方案3】:

    从 stripe 8.0.1 开始,包有自己的类型,因此不需要安装其他类型。像这样导入它:

    从“条纹”导入条纹;

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,提供的解决方案对我不起作用:

      import * as Stripe from 'stripe';
      const stripe = new Stripe('xxx_xxx_xxx');
      

      使用这种方法,我得到了这个错误

      [ts] 不能将“new”与类型缺少调用的表达式一起使用或 构造签名。 stripe.ts(1, 1):类型源自此导入。 命名空间风格的导入不能被调用或构造,并且会 在运行时导致失败。考虑使用默认导入或导入 需要在这里代替。 (别名) 类 Stripe (别名) 命名空间 Stripe 导入条纹

      我在tsconfig.json 中使用"allowSyntheticDefaultImports": true 让它工作了 使用此编译选项,以下内容在 TypeScript 中有效:

      import Stripe from "stripe";
      
      const secret = process.env.STRIPE_SECRET!;
      export const stripe = new Stripe(secret);
      

      【讨论】:

      • 这对我有用,谢谢! "allowSyntheticDefaultImports: true" 是我的默认设置,所以我不需要更改任何内容。
      【解决方案5】:

      这是feature request。去给它另一个竖起大拇指。

      【讨论】:

        猜你喜欢
        • 2013-08-25
        • 2016-06-17
        • 2020-10-01
        • 1970-01-01
        • 2012-11-03
        • 2017-04-03
        • 2019-01-26
        • 2016-12-11
        相关资源
        最近更新 更多