Rails Stripe集成在我的端部出现功能,但不在Stripe仪表板上

时间:2015-09-08 18:40:06

标签: ruby-on-rails stripe-payments

我有以下用于书籍和收费的控制器代码。费用嵌套在书籍中,如此

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 => 'example@stripe.com',
            :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

我手动在控制台中创建了所有Book对象,因此我没有Book Controller的新方法。

现在,在每个图书展示页面,我都有一个条纹按钮进行付款。付款似乎正在我的工作,但当我检查仪表板时,没有收到任何付款。

该指南取自here

我可能在这里做错了什么?

1 个答案:

答案 0 :(得分: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