简体   繁体   English

使用Angular2和stripe.js导入条带

[英]Import Stripe using Angular2 and stripe.js

I am using Angular2, Ionic2 and Stripe.js for payment processing. 我使用Angular2,Ionic2和Stripe.js进行付款处理。 This thread here ionic2 with stripe payment gateway is using the plugin https://github.com/Telerik-Verified-Plugins/Stripe/blob/master/doc/index.md but it's not safe because you have to embed the Secret key inside the app. 这个离线带有条带支付网关的线程使用插件https://github.com/Telerik-Verified-Plugins/Stripe/blob/master/doc/index.md,但它不安全,因为你必须在内部嵌入密钥该应用程序。 Even the page is telling people not to use this plugin. 甚至页面也告诉人们不要使用这个插件。

I tried to use the node.js version here: 我试着在这里使用node.js版本:

https://www.npmjs.com/package/stripe https://www.npmjs.com/package/stripe

However, I cannot figure out how to do the var stripe = require('stripe')(' your stripe API key '); 但是,我无法弄清楚如何做var stripe = require('stripe')(' your stripe API key '); when in TypeScript, you have to use import . 在TypeScript中,您必须使用import

Finally, I did put <script type="text/javascript" src="https://js.stripe.com/v2/"></script> in index.html and the stripe variable shows globally inside each Component. 最后,我确实在index.html中放置了<script type="text/javascript" src="https://js.stripe.com/v2/"></script>stripe变量在每个Component中全局显示。 However, I don't feel like this is the proper way to do it as the stripe object may not be ready by the time I use it inside each Component or Page. 但是,我觉得这不是正确的方法,因为stripe对象在我在每个Component或Page中使用时可能还没有准备好。

What's the proper way to use Angular2 and Stripe.js? 使用Angular2和Stripe.js的正确方法是什么? Ionic2 specifically would be nice but optional. Ionic2特别好看但是可选的。

Thanks 谢谢

UPDATE 1 更新1

I tried npm install stripe and then used import '../../node_modules/stripe/lib/stripe.js'; 我尝试了npm install stripe然后使用import '../../node_modules/stripe/lib/stripe.js'; but still got error: 但仍然有错误:

TypeScript error: /Users/username/Documents/StripePayment/app/pages/home/home.ts(16,23): Error TS2304: Cannot find name 'Stripe'.
Error: Cannot find module '../../node_modules/stripe/lib/stripe.js' from '/Users/username/Documents/StripePayment/app/pages/home'

Here is my VS Code screenshot with directory structure: 这是我的VS Code截图,其中包含目录结构:

在此输入图像描述

Add the scripttag in the index.html and then put a declaration after the imports in home.ts 在index.html中添加脚本标签,然后在home.ts中导入后添加声明

declare var Stripe: any; I believe this is the correct way to import external libs in ng2 我相信这是在ng2中导入外部库的正确方法

Src: Nic Raboy There are a some more info there; Src: Nic Raboy那里有更多信息; the better way to install an external lib is to download the typedefs from DefinitelyTyped and install with $ typings install Then you should be able to import as usual 安装外部库的更好方法是从DefinitelyTyped下载typedef并使用$ typings install然后你应该可以像往常一样导入

This is, of course, if there are typedefs in the DefinitelyTyped repo. 当然,这是在DefinitelyTyped repo中有typedef。 There does not seem to exist typedefs for the Stripe library, though. 但是,Stripe库似乎没有typedef。

Stripe seems to have type definitions now so alongside 条纹似乎现在有类型定义

npm install --save stripe

you can also run the following to get TypeScript definitions: 您还可以运行以下命令来获取TypeScript定义:

npm install --save @types/stripe

you should then be able to so something like: 你应该能够像这样:

import { Stripe } from 'stripe'

The above is psudo code as Ive not tested it but will will be something similar. 以上是psudo代码,因为我没有测试它,但将会是类似的东西。

More info here: https://www.npmjs.com/package/@types/stripe 更多信息: https//www.npmjs.com/package/@types/stripe

The stripe.js library is intended for the server, requires the child_process module, and creates a server of its own. stripe.js库适用于服务器,需要child_process模块​​,并创建自己的服务器。 There is not a good way of importing this library directly to a browser environment. 没有一种将此库直接导入浏览器环境的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM