2015-09-08 51 views
1

我拥有以下用于书本和收费的控制器代码。收费嵌套书籍内,像这样Rails条纹集成在我的一端显示功能,但不显示在条纹仪表板上

Rails.application.routes.draw do 
    resources :hugs do 
    resources :charges 
    end 
end 

书籍控制器

class BooksController < ApplicationController 

    def index 
     @books = Book.all 
    end 

    def create 
    end 

    def show 
     @book = Book.find(params[:id]) 
    end 
end 

收费控制器

class ChargesController < ApplicationController 
    def create 
     @book = Book.find(params[:book_id]) 


     customer = Stripe::Customer.create(
      :email => '[email protected]', 
      :card => params[:stripeToken] 
     ) 

     charge = Stripe::Charge.create(
      :customer => customer.id, 
      :amount  => @book.price, 
      :description => 'Rails Stripe customer', 
      :currency => 'usd' 
     ) 

     rescue Stripe::CardError => e 
      flash[:error] = e.message 
      redirect_to hugs_path 
    end 
end 

我手动创建控制台内的所有我的书的对象,所以我没有书籍控制器的新方法。

现在在每个图书展示页面上,我都有一个用于付款的条纹按钮。付款似乎在我的最后,但是当我检查仪表板时,没有收到付款。

该指南从here

采取哪些可能我是错在这里干什么?

+0

您是否可以检查仪表板中的活动日志?是否有任何事件?你在测试模式中使用它吗? – ddgd

+0

是的,我的确在使用测试模式。在日志本身中,它确实表明API调用已经完成,所以我对这里发生的事情感到困惑。 – user3277633

+0

日志显示/ v1 /收费200? –

回答

0

,你应该在你看你发布的关键:

<head> 

    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
    <script type="text/javascript" src="https://js.stripe.com/v2/"></script> 
    <link href='https://fonts.googleapis.com/css?family=Pacifico|Princess+Sofia' rel='stylesheet' type='text/css'> 
    <link href='https://fonts.googleapis.com/css?family=Julius+Sans+One' rel='stylesheet' type='text/css'> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
    <%= csrf_meta_tags %> 

    <!-- The required Stripe lib --> 


    <!-- jQuery is used only for this example; it isn't required to use Stripe --> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

    <script type="text/javascript"> 
    // This identifies your website in the createToken call below 
    Stripe.setPublishableKey('<%='pk_test_KfCg1YmVXwBYyEdPEWnfibF8'%>'); 

    var stripeResponseHandler = function(status, response) { 
     var $form = $('#payment-form'); 

     if (response.error) { 
     // Show the errors on the form 
     $form.find('.payment-errors').text(response.error.message); 
     $form.find('button').prop('disabled', false); 
     } else { 
     // token 
     var token = response.id; 
     // Insert token into the form 
     $form.append($('<input type="hidden" name="stripeToken" />').val(token)); 
     // re-submit 
     $form.get(0).submit(); 
     } 
    }; 

    jQuery(function($) { 
     $('#payment-form').submit(function(e) { 
     var $form = $(this); 

     // Disable the submit button to prevent repeated clicks 
     $form.find('button').prop('disabled', true); 

     //create the token before it hits the controller 
     Stripe.card.createToken({ 
      number: $('.card-number').val(), 
      cvc: $('.card-cvc').val(), 
      exp_month: $('.card-expiry-month').val(), 
      exp_year: $('.card-expiry-year').val()}, stripeResponseHandler); 

     // Prevent the form from submitting with the default action 
     return false; 
     }); 
    }); 
    </script> 
</head> 
    //code for form goes down here // 

你还需要确保已经安装了宝石(我假设你做什么,但认为它值得一提)。

你也''可能'需要重新启动你的服务器,并在终端设置你的环境变量。您可以一步完成:MY_KEY=sk_jasahhfu084374ksk_or_whatever rails s

+0

嗨,不知道这是否重要,但我遵循指导,并在我的“config/initializer”中拥有以下'Stripe.api_key = Rails.configuration.stripe [:secret_key]',因此应该设置API密钥已经 – user3277633

+0

请阅读第1步和第2步中的自定义使用表单:https://stripe.com/docs/tutorials/forms,您将看到不仅需要表单头部的条带链接,还需要可发布的键,以及必要的jQuery来创建一个令牌并从窗体中获取信息。另外我假设你已经这样做了,但你需要安装条纹宝石。最后,你是正确的secret_key不需要在控制器,但你需要的其余部分 – HolyMoly

+0

我已经更新了我的答案,包括我的付款表格的整个头,并删除有关控制器的东西,因为它的确是,在控制器中不需要 – HolyMoly