条纹付款金额

时间:2016-06-21 08:26:16

标签: javascript ruby-on-rails ruby stripe-payments

我已将条带付款整合到我的应用程序中。它以某种方式配置,以便用户可以在销模型上输入他们想要作为小数销售的商品的价格。当价格以表格形式输入时,它在视图中显示为美元金额。但是,当您为该项目选择立即购买按钮并弹出条纹支付模式时,它会以美分显示价格(即我输入" 10.0"并提交它以便显示"价格$:10.0但是当我选择立即购买时,Stripe将其解释为美分,并在条形支付模式中显示"支付$ .10和#34;。

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

有没有简单的方法来解决这个问题,以便显示的数量对于输入和输出都是一致的?

应用程序/视图/针/

<%= 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 %>

应用程序/视图/针/ 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>

应用程序/分贝/迁移

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

销/控制器

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

1 个答案:

答案 0 :(得分:1)

Stripe的API总是希望您以最小的货币单位传递金额。以美元表示,这意味着将金额作为美分。

您无需在此更改自己的用户界面,只需更改代码即可在调用handler.open时将金额作为分数传递。由于您已经在@pin.price中已有金额,因此您需要将其乘以100并围绕该值:

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