【问题标题】:Stripe function cannot be found找不到条纹功能
【发布时间】:2020-03-31 15:59:54
【问题描述】:

我在我的 angular7 应用程序中使用 Stripe 来通过 firebase 云功能处理付款。我目前正在尝试创建订阅付款。但是,当我尝试调用以下函数时(作为整个订阅设置的一种先决条件);

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

//Create payment method for customer
const {paymentMethod, createPaymentMethodError} = await stripe.createPaymentMethod({
    type: 'card',
    card: cardElementObj.card,
    billing_details: {
      name: userName,
      email: firebaseUserEmail
    },
  });

错误:

Property 'createPaymentMethod' does not exist on type 'Stripe'.

我的 node_modules(版本 7.14.0)中安装了条带包。

package.json 配置:

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tslint -p tslint.json && tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@types/stripe": "^7.13.11",
    "firebase": "^7.4.0",
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0",
    "moment": "^2.24.0",
    "stripe": "^7.14.0"
  },
  "devDependencies": {
    "@types/stripe-checkout": "^1.0.3",
    "@types/stripe-v3": "^3.1.9",
    "firebase-functions-test": "^0.1.6",
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },
  "private": true
}

感谢任何帮助!提前致谢。

【问题讨论】:

  • 如果您分享一些相关代码,我们可能会提供帮助。

标签: angular google-cloud-firestore google-cloud-functions stripe-payments


【解决方案1】:

Stripe 不提供 Typescript 包,因此您不需要 package.json 中的这些依赖项即可使其正常工作。而是:

首先,确保页面中包含脚本。请注意,根据their documentation,它必须始终从https://js.stripe.com/v3/ 加载。

然后,Typescript 代码的结构应该像这样来获取Stripe 的实例以供使用。

declare var Stripe : any;

@Component({
  selector    : 'payment-form',
  templateUrl : 'payment-form.component.html',
})
export class PaymentFormComponent implements OnInit {
  stripe: any;

  ngOnInit() {
    this.stripe = new Stripe(environment.stripeApiKey);
  }

  mySubmitFunction() {
    this.stripe.createPaymentMethod(...);
  }
}

在 Typescript 中使用 Javascript 库时,这种在组件定义之外声明变量的一般结构很常见。例如,在使用 Facebook 或 Google OAuth 库时,您必须遵循类似的步骤。

【讨论】:

  • 谢谢,我知道这一点,因为我正在我的应用程序中执行此操作,但是,我的问题是我正在使用云功能,并且没有 index.html 能够导入此文件在 Stripe 在他们的文档中指定的方法中,即使用脚本标签。如何导入?
  • 我不知道 Firebase Cloud 功能的限制,抱歉。我发现了 Google 发布的将 Stripe 与 Firebase 结合使用的 tutorialsample code,如果您还没有看到,您可能会发现它们很有用。另外,我看到您将此标记为已回答;真的吗?
  • 在某种程度上确实如此,因为我只是决定从前端调用这些函数。没有其他方法可以解决它。一个库用于客户端(即具有 createPaymentMethod 函数的库),直接调用 API 用于服务器端。所以解决方案很简单,从客户端执行这个功能。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2022-11-17
  • 2021-08-03
  • 2019-12-24
  • 2015-08-11
  • 1970-01-01
  • 2020-12-17
  • 2018-08-19
  • 2021-02-18
相关资源
最近更新 更多