2016-06-21 123 views
0

我已将条形支付集成到我的应用程序中。它的配置方式使得用户可以在销钉型号上输入他们想要销售的商品的价格作为小数。当价格在表单中输入时,它在视图中显示为美元金额。但是,当您选择该项目的立即购买按钮并且条纹付款模式弹出时,它会以美分显示价格(即,我输入“10.0”并提交,因此它显示“价格$:10.0,但是当我选择立即购买Stripe将其解释为美分,并在条形支付模式中显示“Pay $ .10”。)条纹支付金额

我想过如何将用户的输入价格更改为美分,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

条纹的API总是期望你以最小的货币单位通过金额。以美元计算,这意味着以美分计算。

您不需要在这里更改您的用户界面,只需拨打handler.open即可更改您的代码,将该数量作为美分。由于您的金额已经在@pin.price之内,因此您需要将其乘以100并四舍五入:

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