Topic: Before_save callback fails to create Resource

I am using Stripe for CC processing.  I have a method in my User model like this:

def save_with_payment
    if valid?
      customer = Stripe::Customer.create(description: email, plan: subscription_id, card: stripe_card_token)
      self.stripe_customer_token = customer.id
      self.last_four = customer.active_card.last4
      save!
    end
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating user: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

Right now that method is accessed by the controller in the create method:

      if @user.save_with_payment

I want to refactor this into the model entirely.  So I would change the @user.save_with_payment to just @user.save

And then call the model method from a before_save callback.

When I do this Rails fails to save the resource.  The error passed is:

CACHE (0.0ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'tj@ravennainteractive.com' LIMIT 1
Stripe error while creating user: You cannot use a stripe token more than once: tok_thO7zZwoRBL7Qk

When Stripe processes the CC it does so before the form is actually trigged by using js.  So I don't see how it thinks I am using it twice.  Any ideas?

thanks

Re: Before_save callback fails to create Resource

It is probably the Stripe::Customer.create(description: email, plan: subscription_id, card: stripe_card_token) that is getting called more than once.  The token you get back from the JS call to Stripe can only be used once with a server side API call.  Also I would switch it to a before_validation callback.

Re: Before_save callback fails to create Resource

themusicman wrote:

It is probably the Stripe::Customer.create(description: email, plan: subscription_id, card: stripe_card_token) that is getting called more than once.  The token you get back from the JS call to Stripe can only be used once with a server side API call.  Also I would switch it to a before_validation callback.

thank you its also solve my problem
http://www.livetv.pk/indus_vision.html

Last edited by sabilamalik (2012-06-12 02:39:49)

Re: Before_save callback fails to create Resource

i have one application for which i want to create .exe file , can anyone help me out by telling me how to create .exe for a ruby application .and the thing is that i have created .rb file for that aplication , now iam left with only creating .exe using that .rb file.please help me out ...


thanx in advance for your help and suport