We've moved discussions to Discord

Coupon Code & Discounts

Ugurcan Kaya
Is there a way to apply discounts with a coupon code or direct link?

I checked pay gem but could not find any examples there


Chris Oliver
You can do it pretty easily by creating Stripe::Coupon directly and adding the coupon either to the Customer or the Subscription if you only want to apply it to one or the other.

For example, just pass in the coupon option when creating a subscription. Pay will just forward any extra attributes to Stripe::Subscription#create.
https://stripe.com/docs/api/subscriptions/create#create_subscription-coupon

And you can update the customer with a coupon as well if you'd like apply it to any of their future purchases.
Chris Oliver
Also, definitely something I'd like to add to Pay at some point, and then to Jumpstart Pro.

Contributions to Pay are welcome. 😜
Dillon Raphael
Ooo this is pretty important. I also vote to see this in jumpstart pro, or at least an example of how to apply a coupon with a code during signup.
Willard Moore
I agree with having a coupon field as part of the template. You can always turn it off when desired.
I think having the coupon field is a good idea.
Nathan Rofkahr
+1 for this.
Corinn Pope
EDIT: So the error was in my f.text_field in _stripe.html.erb. Changing it to <%= f.text_field :coupon, class: "form-control", placeholder: "Coupon Code", required: false %>fixed it! Hopefully this helps someone else get coupon code fields implemented!

Has anybody been able to add this to their app successfully? I'm getting stuck on how to get the coupon code value from the stripe form. 

I don't know how to get a `coupon` into the subscription_params from the stripe form.

To start, I've added a coupon field to my accounts table and added `:coupon` to the strong params in the subscriptions & accounts controllers. 

I added a coupon code field to my _stripe.html.erb form: 

    <div x-data="{ open: false }" class="">
    <p class="hover:underline text-xs text-neutral-7 pb-4" @click="open = !open">Have a coupon code?</p>
  
    <div x-show="open" id="coupon-field" class="form-group">
      <%= f.label :coupon %>
      <%= f.text_field :coupon, name: nil, class: "form-control", placeholder: "Coupon Code", required: false, data: { target: "stripe.coupon" } %>
    </div>
  </div>


  <%= f.hidden_field :plan, value: @plan.try(:id) %>

  <div class="text-center">
    <%= f.button local_assigns.fetch(:button, 'Subscribe'), id: 'submit-button', class: "btn btn-primary", data: { action: "click->stripe#submit", disable_with: disable_with("Processing...") } %>
  </div>
<% end %>

I also assumed it needed to be passed in subscriptions#create:
coupon = current_account.coupon
current_account.subscribe(plan: processor_id, trial_period_days: @plan.trial_period_days, coupon: coupon) 

But I'm stuck on how to get the coupon from the field to the controller. I thought maybe I could add a line in the handlePaymentMethod on stripe_controller.js (edit: nope! you don't need to change stripe_controller at all)

this.addHiddenField("account[card_token]", coupon)

...but I have no coupon coming through in the params. Any ideas on how to send that data?

:insert "I have no idea what I'm doing dog" meme here:
Chris Oliver
Glad you got it fixed  Corinn Pope 👍

I would also mention that since you already have a field for coupon, you don't need to add a hidden field for coupon. That would just be a duplicate field. The browser will submit the last value, so it would override the first field.
David Larsen
Corin, did you get things sorted out?
Notifications
You’re not receiving notifications from this thread.