条纹支付金额

Stripe Payment Amount

我已将条纹支付集成到我的应用程序中。它的配置方式使用户可以在 pin 模型上以小数形式输入他们想要出售的商品的价格。当价格在表单中输入时,它在视图中显示为美元金额。但是,当您 select 该商品的立即购买按钮和条带支付模式弹出时,它以美分显示价格(即我输入“10.0”并提交它,因此它显示 "Price $: 10.0 but when I select buy now Stripe interprets it as cents and displays "Pay $ .10" 在条纹支付模式中。)

我考虑过将用户的输入价格更改为美分,这样 Stripe 可以更好地解释它,但这会导致不好的结果 UI。

有什么简单的方法可以解决这个问题,使输入和输出显示的数量一致吗?

app/views/pins/

<%= form_tag charges_path, id: 'chargesForm' do %>
          <script src="https://checkout.stripe.com/checkout.js"></script>
          <%= hidden_field_tag 'stripeToken' %>
          <%= hidden_field_tag 'stripeEmail' %>  
          <button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span> Buy Now!</button>

          <script>
              var handler = StripeCheckout.configure({
                key: '<%= Rails.configuration.stripe[:publishable_key] %>',
                token: function(token, arg) {
                  document.getElementById("stripeToken").value = token.id;
                  document.getElementById("stripeEmail").value = token.email;
                  document.getElementById("chargesForm").submit();
                }
              });
               document.getElementById('btn-buy').addEventListener('click', function(e) {
                handler.open({
                  name: <%= @pin.manufacturer %>',
                  description: '<%= @pin.description %>',
                  amount: '<%= @pin.price %>'
              });
              e.preventDefault();
             })
          </script>
      <% end %>

app/views/pins/index.html.erb

<div id="pins" class="transitions-enabled">
 <% @pins.each do |pin| %>
  <div class="box panel panel-default">
    <div class="panel-body">
      <%= link_to (image_tag pin.image.url(:medium)), pin %>
      <p></p>
      <strong>Manufacturer:</strong>
      <%= pin.manufacturer %>
      <p></p>
      <strong>Price:</strong>
      <%= pin.price %>
      <p></p>
      <strong>Description:</strong>
      <%= pin.description %>
      <% if pin.is_multi? %>
        <strong>Quantity:</strong>
          <%= pin.quantity %>
          <% end %>
         <p></p>

app/db/migrate

class AddPriceToPins < ActiveRecord::Migration
  def change
    add_column :pins, :price, :decimal
  end
end

pin/controller

class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy, :bid]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

 def index
   @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page],     :per_page => 9)
  end

 def pin_params
     params.require(:pin).permit(:description, :price, :image, :image2, :image3, :image4, :image5, :manufacturer, :model)
    end
end

Stripe 的 API 始终希望您以最小货币单位传递金额。在美元中,这意味着将金额传递为美分。

您无需在此处更改您的 UI,您只需更改代码以在调用 handler.open 时将金额作为美分传递。由于您的金额已经在 @pin.price 中,您需要将其乘以 100 并四舍五入:

handler.open({
  name: '<%= @pin.manufacturer %>',
  description: '<%= @pin.description %>',
  amount: '<%= (@pin.price * 100).floor %>'
});