我在我的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)中安装了stripe软件包。
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
}
感谢您的帮助!预先感谢。
答案 0 :(得分: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库时,您必须遵循类似的步骤。