Rails条纹连接令牌问题

时间:2015-04-15 16:48:11

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

似乎没有正确创建令牌或者它永远不会传递给条带而我不知道为什么。我已经删除了turbolinks bc我在SO上看到了问题。我遵循了每一个指南。在没有问题之前,我已经连接了条纹付款,经过几天的努力,我无法解决这个问题。请帮忙!

这是我的客户的简单付款方式,因此只需一次性收费。到目前为止还没有建立客户。

我收到的错误:您必须提供来源或客户ID

条纹错误响应:

error:
    type: "invalid_request_error"
    message: "You must supply either a source or a customer id"

new.html.erb

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

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


<%= javascript_include_tag 'payment' %>

<%= form_for @payment, html: {id: "payment-form"} do |f| %>
<div class="input-group">
  <span class="input-group-addon"><i class="fa fa-user"></i></span>
  <%= f.text_field :amount, autofocus: true, placeholder: "Amount", class: "form-control" %>
</div>
<div class="input-group">
  <span class="input-group-addon"><i class="fa fa-user"></i></span>
  <input class="form-control" data-stripe="number" maxlength="20" placeholder="Credit Card Number" type="text"></input>
</div>
<div class="input-group">
  <span class="input-group-addon"><i class="fa fa-user"></i></span>
  <input class="form-control" data-stripe="cvc" size="10" placeholder="Security Number" type="text"></input>
</div>
<div class="input-group">
    <center><p>Expiration Date:</p></center>
    <ul class="dates">
        <li><%= select_month(Date.today, {add_month_numbers: true},  class: 'form-control', data: {stripe: "exp-month"}, style: "height: 46px;") %></li>
        <li><%= select_year(Date.today.year, {start_year: Date.today.year, end_year: Date.today.year + 4}, class: 'form-control', data: {stripe: "exp-year"}, style: "height: 46px;") %></li>
    </ul>
</div>
<div class="row">
<div class="col-xs-12">
  <%= f.submit "Submit Payment", class: "btn btn-success col-xs-12" %>
</div>
</div>
<% end %>

付款控制人:

def new
    @company = Company.find(params[:id])
    @user = @company.users.first
    @payment = Payment.new
end

def create
    @result = StripeWrapper::Charge.create(source: params[:stripeToken])
    if @result.present?
      @payment = Payment.create(payment_params)
      flash[:success] = "Thank you for your payment"
      redirect_to thank_you_path
    else
      flash[:danger] = @result.error_message
      render :new
    end
end

private

def payment_params
  params.require(:payment).permit(:user_id, :company_id, :reference_id)
end

Stripe_wrapper.rb

module StripeWrapper
class Charge
    attr_reader :error_message, :response

    def initialize(options={})
        @response = options[:response]
        @error_message = options[:error_message]
    end

    def self.create(options={})
        begin
            response = Stripe::Charge.create({
                amount: 1000,
                currency: 'usd',
                source:  options[:source],
                description: "test charge",
                application_fee: 123
            }, {stripe_account: 'client_account_id'}
            )
            new(response: response)
        rescue Stripe::CardError => e
            new(error_message: e.message)
        end
    end

    def successful?
        response.present?
    end
end

class Customer
    attr_reader :response, :error_message

    def initialize(options={})
        @response = options[:response]
        @error_message = options[:error_message]
    end

    def self.create(options={})
        begin
        response = Stripe::Customer.create({
            card: options[:card],
            email: options[:user_email],
            description: "Service Pay"
            },
            {stripe_account: 'connect_client_id'}
        )
        new(response: response)
        rescue Stripe::CardError => e
          new(error_message: e.message)
        end
    end

    def successful?
        response.present?
    end

    def customer_token
        response.id
    end
end
end

payment.js

jQuery(function($) {
  $('#payment-form').submit(function(event) {
    var $form = $(this);
    $form.find('button').prop('disabled', true);
    Stripe.card.createToken($form, stripeResponseHandler);
    return false;
  });
});

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

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

2 个答案:

答案 0 :(得分:0)

您是否包含Stripe.js?将其添加到new.html.erb

的顶部
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script type="text/javascript">
  Stripe.setPublishableKey("#{ENV['STRIPE_PUBLISHABLE_KEY']}");
</script>
<%= javascript_include_tag 'payment' %>

ENV ['STRIPE_PUBLISHABLE_KEY']应在application.yml或类似位置设置。在调用StripeWrapper

之前,还需要将它放在create controller中
Stripe.api_key = ENV['STRIPE_SECRET_KEY']

答案 1 :(得分:0)

跳过条纹freenode频道并获得一些帮助后,我们发现了问题,我想把结果留给其他有这个问题的人。

事实证明,来自morris.js图表​​库的javascript错误导致javscript的其余部分(即payment.js文件)未被执行,因此未创建令牌。

导致morris.js错误是因为调用了jquery来显示图表(来自资产管道),但图表中没有div容器。我通过简单地设置一个具有正确id的div来解决这个问题,但没有数据元素,因此没有数据显示,并且样式显示:none,因此没有向用户显示任何内容。