PSCampbell
PSCampbell

Reputation: 868

Stripe - not updating User's information in database

I'm working on my first project using stripe. I've got a subscription product and I've got the basic stripe funcationality working, but I need to update my user's record in the database as being "subscribed" so that I may use it as a validation later on in development. I've seen several tutorials online which show adding a column to your model called subscribed or something along those lines and updating it during the stripe customer creation process. I've got that working, except it is not updating my user model (in this case, supplier). Here's my controller for the stripe processs:

    class ChargesController < ApplicationController
    before_filter :authenticate_supplier!

    def new
    end

    def create
      # Amount in cents
      token = params[:stripeToken]

      customer = Stripe::Customer.create(
          :source => token, # obtained from Stripe.js
          :plan => "Free_month_annual",
          :email => current_supplier.email,
          #:coupon => "coupon_ID"
        )

      current_supplier.subscribed = true
      #current_supplier.stripe_id = token

      redirect_to orders_path

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

end

As you can see, there are two things commented out - a coupon, which I plan to work on later, and stripe_id update because I ran into an error for having already used the token once (can't use it twice). I've updated my supplier model, there is an appropriate column in the database, the params have been updated to allow supplier.subscribed. I'm sure this is a quick answer for many, but I need help spotting my problem.

Edit: subscribed is a boolean field - fyi

Upvotes: 0

Views: 118

Answers (1)

The Fabio
The Fabio

Reputation: 6260

It seems you forgot to save the current_supplier record.

The solution should be adding something like:

current_supplier.subscribed = true
#current_supplier.stripe_id = token

current_supplier.save!

redirect_to orders_path

Upvotes: 1

Related Questions