简体   繁体   English

Stripe :: InvalidRequestError(此客户没有附加的付款来源)

[英]Stripe::InvalidRequestError (This customer has no attached payment source)

There seems to be a problem with sending customer data and token back to Stripe's server. 将客户数据和令牌发送回Stripe的服务器似乎存在问题。 I'm currently using the test api to make dummy transactions. 我目前正在使用测试API进行虚拟交易。 But, I cannot get past this point for no reason at all. 但是,我完全不能无缘无故地超越这一点。 I've tried everything to fix this error. 我已尽一切努力解决此错误。 I will post the code and error message below. 我将在下面发布代码和错误消息。

Subscriptions_Controller.rb Subscriptions_Controller.rb

class SubscriptionsController < ApplicationController
  before_filter :authenticate_user!, except: [:new]
  before_action :redirect_to_signup, only: [:new]

  def show

  end

  def new
    @subscription = current_user.subscription
    if @subscription.active
      @stripe_customer = Stripe::Customer.retrieve(@subscription.stripe_user_id)
      @stripe_subscription = @stripe_customer.subscription.first
    end
  end

  def create
    token = params[:stripeToken]
    customer = Stripe::Customer.create(
        :source => token,
        :plan => "gbsubscriptionlevel1",
        :email => current_user.email
    )

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

    current_user.subscription.stripe_user_id = customer.id
    current_user.subscription.active = true
    current_user.subscription.save

    redirect_to users_index_path
  end

  def cancel_subscription
    @stripe_customer = Stripe::Customer.retrieve(
        current_user.subscription.stripe_user_id
    )
    @stripe_subscription = @stripe_customer.subscription.first
  end

  private
  def redirect_to_signup
    if !user_signed_in?
      session["user_return_to"] = new_subscription_path
      redirect_to new_user_registration_path
    end
  end
end

_stripe_form.html.erb _stripe_form.html.erb

<div class="container">
  <%= form_tag subscription_path, id: 'payment-form' do %>

  <form action="/subscription" method="POST" id="payment-form">
    <span class="payment-errors"></span>

    <div class="row">
      <div class="col-xs-4">
        <label>
          <span>Card Number</span>
          <input value="4242 4242 4242 4242" class="form-control" type="text" size="20" data-stripe="number"/>
        </label>
      </div>
    </div>

    <div class="row">
      <div class="col-xs-1">
        <label>
          <span>CVC</span>
          <input value="123" class="form-control" type="text" size="4" data-stripe="cvc"/>
        </label>
      </div>
    </div>

    <div class="row">
      <div class="col-xs-1">
        <label>MM</label>
        <input value="12" class="form-control" type="text" size="2" data-stripe="exp-month" placeholder="01"/>
      </div>
      <div class="col-xs-1">
        <label>YYYY</label>
        <input value="2019" class="form-control" type="text" size="3" data-stripe="exp-year" placeholder="2020"/>
      </div>
    </div>

    <div class="row">
      <div class="col-xs-1">
        <br/>
        <button class="btn btn-primary-outline" type="submit">Create Subscription</button>
      </div>
    </div>
  </form>
  <% end %>
  <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
</div>

subscription.js subscription.js

Stripe.setPublishableKey('pk_test_tmBNNUvHTmtWXLhSL1q647iH');

function stripeResponseHandler(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 {
        // response contains id and card, which contains additional card details
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));
        // and submit
        $form.get(0).submit();
    }
}

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

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

        Stripe.card.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
    });
});

stripe.rb stripe.rb

Stripe.api_key = ENV["STRIPE_API_TEST_SECRET_KEY"]

That response typically arises after you try to attach a Subscription to someone with no card token attached. 通常,在您尝试将订阅附加到未附加卡令牌的某人后,通常会出现该响应。 From your code, I'd hazard a guess that you're running into a CardError and then despite that, trying to attach a Subscription anyway, which will not work. 从您的代码中,我很可能会猜测您正在遇到CardError,然后尽管如此,无论如何尝试附加Subscription,这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM