使用React JS进行条纹 [英] Stripe with React JS

查看:64
本文介绍了使用React JS进行条纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在React JS中使用Stripe.js创建令牌,但是找不到任何简单的方法。在node.js中,我将执行以下操作:

  stripeClient.tokens.create({
卡:{
数字: 4242424242424242,
exp_month:12,
exp_year:2050,
cvc: 123
}

但是Stripe npm模块在React JS中对我不起作用。我遇到错误:


无法解析模块'child_process'


因此,由于这显然是节点库,我会喜欢使用

 < script type = text / javascript src = https://js.stripe.com/ v2 />< / script> 

但是我不确定什么应该是最好的在React中实现外部库的实践

解决方案

您可以像添加其他客户端库一样继续进行添加



包括此脚本标签:

  < script type = text / javascript src = https://js.stripe.com/v2/>< / script> 

然后使用它在代码内公开的全局变量。

  import从'react'进行反应; 

//我们不必导入数据条,因为它在我们的全局命名空间中已经是
//。
Stripe.setPublishableKey(’pk_test_6pRNASCoBOKtIshFeQd4XMUh’);

虽然不如NPM要求的那样干净,但效果很好。 / p>

I need to create token with Stripe.js in React JS, but I can't find any easy way. In node.js I would do something like this:

   stripeClient.tokens.create({
      card: {
        number: '4242424242424242',
        exp_month: 12,
        exp_year: 2050,
        cvc: '123'
      }

But the Stripe npm module doesn't work for me in React JS. I'm getting error:

Cannot resolve module 'child_process'

So since this is node pibrary obviously, I would like to use

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

But I'm not sure what should be the best practice to implement external libraries in React

解决方案

You can just go ahead and add it like any other client side library, like you might have done in the past.

Include this script tag:

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

Then use the global that it exposes inside your code.

import React from 'react';

// we didn't have to import stripe, because it's already
// in our global namespace.
Stripe.setPublishableKey('pk_test_6pRNASCoBOKtIshFeQd4XMUh');

It's not as clean as being able to require it from NPM, but it will work just fine.

这篇关于使用React JS进行条纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆