J. M. Habibi
J. M. Habibi

Reputation: 131

stripe is not defined; please fix or add /*global Stripe*/

I searched around a bit and couldn't find my answer.

I'm running into the following errors when running my app from CoderManual.com:

My .js file says "stripe is not defined; please fix or add /*global Stripe*/"

And when I run the app through C9 I get

"Stripe::InvalidRequestError in Users::RegistrationsController#create"

Don't know what to do. Any help is appreciated.

.js file reads:

$(document).ready(function() {
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'));
  // Watch for a form submission:
  $("#form-submit-btn").click(function(event) {
    event.preventDefault();
    $('input[type=submit]').prop('disabled', true);
    var error = false;
    var ccNum = $('#card_number').val(),
        cvcNum = $('#card_code').val(),
        expMonth = $('#card_month').val(),
        expYear = $('#card_year').val();
    if (!error) {
      // Get the Stripe token:
      Stripe.createToken({
        number: ccNum,
        cvc: cvcNum,
        exp_month: expMonth,
        exp_year: expYear
      }, stripeResponseHandler);
    }
    return false;
  }); 

registrations_controller.rb reads:

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    super do |resource|
      if params[:plan]
        resource.plan_id = params[:plan]
        if resource.plan_id == 2
          resource.save_with_payment
        else
          resource.save
        end
      end
    end
  end

end

I installed Stripe as show @ GitHub to no avail.

I do have js.stripe.com referenced in my html:

<!DOCTYPE html>
<html>
<head>
  <title>Dev Match</title>
  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag "https://js.stripe.com/v2/", type: 'text/javascript' %>
  <%= javascript_include_tag 'application' %>
  <%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>
  <%= csrf_meta_tags %>
</head>

Upvotes: 0

Views: 2113

Answers (2)

MrRipley
MrRipley

Reputation: 21

Finally managed to figure this one out.

The error is because the stripe API does not return a token. The ommission of any kind of errorhandling in the script makes this hard to see, let alone figure out why. I added the response from the stripe API to a text field in the form and commented out the submit (both in the users.js) Then the silly error reveals itself. The card is simply not excepted as valid. The codermanual uses 4111111.. as trial number and this is no longer accepted by stripe.

Try 42424242424242 and life will be sweet.

Upvotes: 1

MrRipley
MrRipley

Reputation: 21

I'm running into the same issue, also on the coder manual course. I added debugger; to line 14 (in the ifstatement but before Stripe.createToken of the users.js file to stop the script running (if it was?) just before the API call to stripe.

Adding the debugger stopped the script. So it does seem to run. Letting the script resume leads to same error you mentioned.

Moving the debugger; over to after the API call but still in the if statement also lets the script run without errors till its halted.

Moving the debugger; over yet another line (after the return false;) leads to the error before the code stops executing because of the debugger;

The chrome console gives this error in the log:

POST https://api.stripe.com/v1/tokens 400 (Bad Request) which expands to: Stripe.isDoubleLoaded.c Stripe.isDoubleLoaded.e``Stripe.isDoubleLoaded.a Stripe.isDoubleLoaded.Stripe.xhr``Stripe.a._rawRequest``Stripe.a.request``Stripe.token.a.create``Stripe.card.b.createToken``Stripe.a._channelListener``t.concat.incoming

The problem seems to be in the actual API call. But at least its making the call ;)

Upvotes: 1

Related Questions