RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.
http://railsapps.github.io/rails-stripe-membership-saas
1.14k stars 232 forks source link

Last 4 Digits don't update after free trial #85

Closed albertpak closed 10 years ago

albertpak commented 10 years ago

So I've followed #47 - created free subscription - but now i run into an issue that when user adds CC# to their profile while in "trial" mode, Stripe gets updated, i checked user record there and new CC# is in, but when i checked ROR app's DB record for that user - that field "last_4_digits" is empty...I'm not even sure where to look, or which part of the code to post?

I've tested "Plan upgrade" and it does work - user gets upgraded and roles change, just can't figure out why that field doesn't update with the last 4...

Thank you in advance

billvieux commented 10 years ago

Look about 8 lines from the bottom of the update_stripe method given in #47. This line skips setting the last 4 digits for users in the 'free' role.

self.last_4_digits = customer.active_card.last4 unless roles.first.name == 'free'
albertpak commented 10 years ago

@billvieux Thank you :) I was looking at earlier too, but wasn't sure, so I've replaced that line with a line with the one from original tutorial:

self.last_4_digits = customer.cards.data.first["last4"]

So that worked, once user tried to put in their credit card number and I saw that field updated, but then it messed up my sign up process for FREE subscription since user doesn't submit any CC# with the form - how can I "bypass" that, or what would you suggest would be a good approach?

Thank you in advance.

albertpak commented 10 years ago

In a meantime, I've come up with a "solution" for now that seems to be working.

First created a helper defaultcc, i.e. keeping it blank:

module DefaultccHelper
    def defaultcc
        "----"
    end
end

and then added helper :defaultcc to Application Controller.

Lastly, replaced self.last_4_digits = customer.active_card.last4 unless roles.first.name == 'free' with the following piece:

if customer.cards.data.blank?
    self.last_4_digits = @defaultcc
else
    self.last_4_digits = customer.cards.data.first["last4"]
end

So far seems to be working - use is able to sing up for a free account, create a free 90 day trial and then after 90 days, it'll ask them to add CC and upgrade.

Hope this helps someone else :)

billvieux commented 10 years ago

Glad you found a way to make it work. It might be cleaner not to use a helper or a controller from the model. You can just write

self.last_4_digits = customer.cards.first["last4"] if customer.cards.any?

If you really need to initialize last_4_digits, it is probably better to do that on the model (e.g., using after_initialize) instead of relying on the update_stripe method to set a default value.

You can consider closing this issue if you are good to go.

albertpak commented 10 years ago

Thank you, @billvieux :) Learning something new everyday :)

billvieux commented 10 years ago

Me too! :)

albertpak commented 10 years ago

@billvieux Just tried your solution and works great :) Thank you for your help :)