Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe Payment Works on LocalHost but Does not work on Heroku

Hope all is good I am baffled by why my stripe payment in ruby on rails works on my localhost which is c9.io account but when I deployed my code in Heroku, it gives me this error:

Cannot charge a customer that has no active card

my orders.coffee file:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  payment.setupForm()

payment =
  setupForm: ->
    $('#new_order').submit ->
      $('input[type=submit]').attr('disabled', true)
      obj = Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
      alert(JSON.stringify(obj));

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#new_order').append($('<input type="hidden" name="stripeToken" />').val(response.id))
      $('#new_order')[0].submit()
    else
      $('#stripe_error').text(response.error.message).show()
      $('input[type=submit]').attr('disabled', false)

out come of my orders.coffee in localhost:

enter image description here

my application.html.erb header section

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

my orders_controller.rb stripe section:

Stripe.api_key = ENV["stripe_api_key"]
    #flash[:notice] = Stripe.api_key
    #puts "stripe api key is " + Stripe.api_key
    token = params[:stripeToken]

    begin
      customer = Stripe::Customer.create(
        :source  => token,
        :description => "Customer.create"
        )
      charge = Stripe::Charge.create(
        :amount => (@listing.price * 100).floor,
        :description => 'charge.create',
        :currency => "usd",
        :customer    => customer.id
        )
      flash[:notice] = "Thanks for ordering!"
    rescue Stripe::CardError => e
       flash[:danger] = e.message
    end

my application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

my stripe dashboard for a sample where I get from heroku

enter image description here

Could anyone let me know why I have such an odd issue? left one is from heroku and right one is from localhost enter image description here If there is more info needed, please ask it away.

like image 459
Kick Buttowski Avatar asked Jan 06 '23 03:01

Kick Buttowski


1 Answers

So this error means that the charge creation request (Stripe::Charge.create(...)) failed because the customer has no payment source.

This in turn means that when you created the customer:

customer = Stripe::Customer.create(
  :source  => token,
  :description => "Customer.create"
)

token must have been either nil or an empty string, and as a result no source parameter was sent to Stripe. You can check this by looking at the log entry for the customer creation request in your Stripe dashboard.

This in turn means that params[:stripeToken] was itself nil or an empty string.

Unfortunately I'm not sure why that's the case. I'd recommend adding some logs, both in the stripeResponseHandler callback in your CoffeeScript file and in the Rails controller, to check whether the token was created and submitted successfully.

like image 99
Ywain Avatar answered Jan 13 '23 09:01

Ywain