【问题标题】:Using javascript for Stripe Elements将 javascript 用于条纹元素
【发布时间】:2019-03-29 15:40:41
【问题描述】:

如何将我的 Javascript 文件包含到视图中?

我有我的条纹元素,但字段没有显示。

它在 sn-p 中有效,但在我的应用程序中无效...

// Create a Stripe client.
var stripe = Stripe('324576895435678976FL1LHd');

// Create an instance of Elements.
// Create an instance of Elements
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
  base: {
    color: '#32325d',
    lineHeight: '24px',
    fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
    fontSmoothing: 'antialiased',
    fontSize: '16px',
    '::placeholder': {
      color: '#aab7c4'
    }
  },
  invalid: {
    color: '#fa755a',
    iconColor: '#fa755a'
  }
};

// Create an instance of the card Element
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createToken(card).then(function(result) {
    if (result.error) {
      // Inform the user if there was an error
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server
      stripeTokenHandler(result.token);
    }
  });
});
*, label {
  font-family: "Helvetica Neue", Helvetica, sans-serif;
  font-size: 16px;
  font-variant: normal;
  padding: 0;
  margin: 0;
  -webkit-font-smoothing: antialiased;
}

#button-element {
  border: none;
  border-radius: 4px;
  outline: none;
  text-decoration: none;
  color: #fff;
  background: #32325d;
  white-space: nowrap;
  display: inline-block;
  height: 40px;
  line-height: 40px;
  padding: 0 14px;
  box-shadow: 0 4px 6px rgba(50, 50, 93, .11), 0 1px 3px rgba(0, 0, 0, .08);
  border-radius: 4px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.025em;
  text-decoration: none;
  -webkit-transition: all 150ms ease;
  transition: all 150ms ease;
  float: left;
  margin-left: 12px;
  margin-top: 31px;
}

#button-element:hover {
  transform: translateY(-1px);
  box-shadow: 0 7px 14px rgba(50, 50, 93, .10), 0 3px 6px rgba(0, 0, 0, .08);
  background-color: #43458b;
}

#form-element {
  padding: 30px;
  height: 120px;
}

label {
  font-weight: 500;
  font-size: 14px;
  display: block;
  margin-bottom: 8px;
}

#card-errors {
  height: 20px;
  padding: 4px 0;
  color: #fa755a;
}

.form-row {
  width: 70%;
  float: left;
}

.token {
  color: #32325d;
  font-family: 'Source Code Pro', monospace;
  font-weight: 500;
}

.wrapper {
  width: 90%;
  margin: 0 auto;
  height: 100%;
}

#stripe-token-handler {
  position: absolute;
  top: 0;
  left: 25%;
  right: 25%;
  padding: 20px 30px;
  border-radius: 0 0 4px 4px;
  box-sizing: border-box;
  box-shadow: 0 50px 100px rgba(50, 50, 93, 0.1),
    0 15px 35px rgba(50, 50, 93, 0.15),
    0 5px 15px rgba(0, 0, 0, 0.1);
  -webkit-transition: all 500ms ease-in-out;
  transition: all 500ms ease-in-out;
  transform: translateY(0);
  opacity: 1;
  background-color: white;
}

#stripe-token-handler.is-hidden {
  opacity: 0;
  transform: translateY(-80px);
}

/**
 * The CSS shown here will not be introduced in the Quickstart guide, but shows
 * how you can use CSS to style your Element's container.
 */


.StripeElement {
  background-color: white;
  padding: 8px 12px;
  border-radius: 4px;
  border: 1px solid transparent;
  box-shadow: 0 1px 3px 0 #e6ebf1;
  -webkit-transition: box-shadow 150ms ease;
  transition: box-shadow 150ms ease;
}

.StripeElement--focus {
  box-shadow: 0 1px 3px 0 #cfd7df;
}

.StripeElement--invalid {
  border-color: #fa755a;
}

.StripeElement--webkit-autofill {
  background-color: #fefde5 !important;
}

.ElementsApp, .ElementsApp .InputElement {
  color: #32325d;line-height: 24px;font-family: "Helvetica Neue", Helvetica, sans-serif;font-size: 16px;height: 24px;-webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.ElementsApp:not(.is-autofilled) .InputElement:-webkit-autofill {
  color: #32325d;
  -webkit-text-fill-color: #32325d;
}
.ElementsApp .InputElement + .Input-placeholder--ie {
  opacity: 1;color: #aab7c4;
}
.ElementsApp .InputElement::-webkit-input-placeholder {
  opacity: 1;color: #aab7c4;
}
.ElementsApp .InputElement::-moz-placeholder {
  opacity: 1;color: #aab7c4;
}
.ElementsApp .InputElement:-ms-input-placeholder {
  opacity: 1;color: #aab7c4;
}
.ElementsApp .InputElement::placeholder {
  opacity: 1;color: #aab7c4;
}
.ElementsApp .InputElement.is-invalid {
  color: #fa755a;
}
.ElementsApp:not(.is-autofilled) .InputElement.is-invalid:-webkit-autofill {
  color: #fa755a;
  -webkit-text-fill-color: #fa755a;
}
.ElementsApp.is-invalid .Icon-fill--invalid {
  fill: #fa755a;
}
    <script src="https://js.stripe.com/v3/"></script>
    <script src="stripejs.js"></script>

    <form id="form-element" action="/charge" method="post" id="payment-form">
      <div class="form-row">
        <label for="card-element">
          Credit or debit card
        </label>
        <div id="card-element">
          <!-- a Stripe Element will be inserted here. -->
        </div>

        <!-- Used to display form errors -->
        <div id="card-errors" role="alert"></div>
      </div>

      <button id="button-element">Submit Payment</button>
    </form>
如果您在没有 JS 的情况下运行代码,这就是它在我的应用程序中的样子。所以我没有正确连接JS

关于我如何以正确的方式做到这一点的任何建议?

我试过了:

添加带有和不带有“.js”的“//= require stripejs”

将整个 JS 脚本添加到 application.js 文件中

将脚本直接添加到视图中 - 出现了一个字段,但可以正常工作。我不明白为什么它在 sn-p、jsfiddle 等中有效,但在我的应用程序中无效

在 CMD 中我收到此错误:

ActiveRecord::RecordNotFound (Couldn't find Listing with 'id'=stripejs):

app/controllers/listings_controller.rb:131:in `set_listing'

【问题讨论】:

    标签: javascript ruby-on-rails


    【解决方案1】:

    Rails 正在使用 Assets Pipeline 来交付资产。 您可以在官方指南here 中了解它。

    有几种方法可以在页面中导入 JS,但是最好的方法是使用 gem 版本的 stripe.js。 您可以将自定义 JS 代码放入您的 /app/assets/javascripts/application.*

    另一种方法是绕过 Assets Pipeline 并将您的资产放到 /public 中,这相当于您服务器的根目录。

    【讨论】:

    • 我的 applications.js 文件中有“//= require stripejs.js”。仍然无法正常工作——我也试过没有“.js”
    • 尝试将整个 JS 脚本添加到应用程序文件中,但没有成功。无法弄清楚为什么它在 sn-p 中有效,但在应用程序中无效
    • stripe-rails gem 的需求有多大...不应该只用 stripe gem 就可以满足我已有的工作吗?你怎么看?
    • 安装了 stripe-rails gem... 似乎不起作用。我不断收到一条错误消息,指出“未定义的方法 `secret_key'”......并且只有在我删除 stripe.rb 文件时才会起作用......我也在我的 .env 文件中的 stripe_secret_key 下添加了我的密钥。不确定我是否做错了什么。这应该有效。可能发生了一些非常小的事情
    • 您的问题的根源似乎不是缺少 JS 文件,您可以通过在浏览器中加载您的应用程序,打开开发者控制台并进入“Sources”来检查它是否实际交付" 选项卡,它应该包含所有正在提供给客户的资产。在开发中存储您的密钥的最佳方式是使用 secrets.yml ,它将包含您所有的敏感数据stackoverflow.com/questions/26498357/…
    猜你喜欢
    • 2021-09-26
    • 2021-05-16
    • 2023-03-25
    • 2021-03-17
    • 2017-10-13
    • 2020-03-26
    • 2021-10-20
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多