RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.

Home Page:http://railsapps.github.io/rails-stripe-membership-saas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding a coupon/discount code feature

daluzluiz opened this issue · comments

I was trying to implement a coupon/discount code to this app, but I'm not sure if doing it right or if I'm missing something. I created a coupon code on stripe and I've tried adding coupon: $('#card_coupon').val() to registrations.js and it now looks like this:

$('.registrations').ready(function() { $.externalScript('https://js.stripe.com/v1/').done(function(script, textStatus) { Stripe.setPublishableKey('pk_test_xxxxxxxxxxxxxxxx'); var subscription = { setupForm: function() { return $('.card_form').submit(function() { $('input[type=submit]').prop('disabled', true); if ($('#card_number').length) { subscription.processCard(); return false; } else { return true; } }); }, processCard: function() { var card; card = { name: $('#user_name').val(), number: $('#card_number').val(), cvc: $('#card_code').val(), expMonth: $('#card_month').val(), expYear: $('#card_year').val() coupon: $('#card_coupon').val() }; return Stripe.createToken(card, subscription.handleStripeResponse); }, handleStripeResponse: function(status, response) { if (status === 200) { $('#user_stripe_token').val(response.id) $('.card_form')[0].submit() } else { $('#stripe_error').text(response.error.message).show(); return $('input[type=submit]').prop('disabled', false); } } }; return subscription.setupForm(); }); });

and added the form field in the registration view. When I try to register a new user, I get and error Stripe token not present. Can't create account. Does anyone know what am I missing here?

Thanks

Are you getting any additional details through an error in the console? Sounds like an issue with setting your Stripe API keys. So if you're doing it through the environment, make sure those are set. If you're doing it locally, do the same or try testing by hardcoding them in.

My keys are set and working perfectly, but I was trying to add a coupon code by adding that line of code in the javascript file and adding the field on my views. But I get that error mentioned above.

Stripe token not present. Can't create account.
Rails.root: /Users/luizdaluz/myapp

Application Trace | Framework Trace | Full Trace
app/models/user.rb:33:in `update_stripe'
Request

Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"scrc/jL365lT2U8qPgGSPte8bBtUK8LTZJDaaakXoSx=",
"plan"=>"gold",
"user"=>{"name"=>"luiz",
"email"=>"luiz4@teste.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"stripe_token"=>""},
"commit"=>"Sign up"}

It can't be set correctly. Look at the params being sent. stripe_token is missing.

"stripe_token"=>""},

I've had issues with this too; it's a headache. Just ensure that however you're setting them (in stripe.rb) is actually set. Default is through your local environment.

My keys are both set in stripe.rb and registrations.js. Originally when I add a user everything works just fine, but i tried to implement the coupon code and thats when I get that error. I dont think the error is because the key is missing (because it's not), but it might be something related to that.

Maybe a piece of code that needs to go somewhere. Not sure

Can you create a user without an example.com email address?

Just noticed your missing a comma right before your coupon parameter is set be getting Val.

expYear: $('#card_year').val()
coupon: $('#card_coupon').val()

Should be:

expYear: $('#card_year').val(),
coupon: $('#card_coupon').val()

Thanks a lot, that did the trick. But somehow the discount was not applied to stripe. The logs show that the token was sent with the coupon, but it didn't apply.

@DanielKehoe Would you know the solution for this? Thanks.

I'm sorry I haven't explored the Stripe coupon option. You're on your own with it for now.

In the future I may extend the tutorial to cover coupons but there is other work to do first.

Fair enough. I will keep trying and will post it when I find the solution. Thank you very much once again.

Stripe has some awesome customer service set up to help through stripe.com/campfire.

I actually wanted to try the same thing, and I was told that the coupon needs to be added as an optional parameter on the subscription creation. So, I think (I've not tried this yet) the coupon code needs to be passed to the Stripe::Customer.create hash.

Hope that helps a bit, I'll be digging into the problem this weekend. I'll post if I find anything.

I tried that as well, but no luck. The coupon shows up on stripe logs but for some reason it does not apply the discount. Not sure if thats a code issue or maybe I need to add something else in order to this coupon to work. I'll try stripe customer service and see if they can help me.

{
key: "pk_test_xxxxxxxxxxx"
card:
exp_month: "1"
exp_year: "2014"
number: "*********_4242"
coupon: "72327"
cvc: "
_"
name: "juliana ferreira"
},
},

@daluzluiz Have you had any luck getting this to work?

@PendragonDevelopment No, I tried yesterday for a couple of hours but no luck. I contacted stripe via their chat, and the guy said that my code looks ok. Might guess is that maybe my :coupon_code is nill.

Did you try anything?

Yeah, I can't seem to figure out how to pass the value. I did discover that the coupon code is actually a hash within the discount object. Not really sure how or if that makes a difference.

Please let me know if you find a solution for this and I'll do the same. Thanks.

Got it! Added a :coupon virtual attribute to the User model (much like the stripe_token), and then passed it from the form to the Stripe::Customer.create hash:

https://github.com/PendragonDevelopment/orion/commit/383afd76f8711c4df9caf3c933e52d3c6cb1bd2e
(not sure if there's a better way to show a diff)

Let me know how this works for you.

fantastic. This was almost exactly what I had, but it didn't work because i had <%= text_field_tag :card_coupon %>
instead of <%= f.input :card_coupon %>. I changed it and that was it.

Awesome. Thanks!!

Great to see you guys worked this out. Can you create a gist containing the files you changed and post a link to the gist here, please?

This issue has been solved and it's now closed. Special thanks to @PendragonDevelopment

To offer a coupon feature, you'll need to modify 2 files. app/models/user.rb and app/views/devise/registrations/new.html.erb Add a coupon to your attr_accessible and attr_accessor and modify your view to capture the coupon. You'll also need to create a coupon within your stripe account.

Here is the link to the modified files: Gist

Thank you.

Great! Thanks for the good work. Open source FTW!

I made some small improvements to the contributed code. Added a label to the form field and made sure there's no error if no coupon code is entered.