diff --git a/app/assets/templates/machines/request_training_modal.html.erb b/app/assets/templates/machines/request_training_modal.html.erb index 552796a9b..660f2e9e8 100644 --- a/app/assets/templates/machines/request_training_modal.html.erb +++ b/app/assets/templates/machines/request_training_modal.html.erb @@ -14,5 +14,5 @@

diff --git a/app/models/reservation.rb b/app/models/reservation.rb index d43cb9d72..53a78fe4b 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -165,7 +165,7 @@ class Reservation < ActiveRecord::Base if plan_id self.subscription = Subscription.find_or_initialize_by(user_id: user.id) self.subscription.attributes = {plan_id: plan_id, user_id: user.id, card_token: card_token, expired_at: nil} - if subscription.save_with_payment(false) + if subscription.save_with_payment(false, coupon_code) self.stp_invoice_id = invoice_items.first.refresh.invoice self.invoice.stp_invoice_id = invoice_items.first.refresh.invoice self.invoice.invoice_items.push InvoiceItem.new(amount: subscription.plan.amount, stp_invoice_item_id: subscription.stp_subscription_id, description: subscription.plan.name, subscription_id: subscription.id) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 558d217d0..a25f9e70f 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -39,11 +39,22 @@ class Subscription < ActiveRecord::Base total = plan.amount Stripe::InvoiceItem.create( customer: user.stp_customer_id, - amount: -(total * cp.percent_off / 100.0), + amount: -(total * cp.percent_off / 100.0).to_i, currency: Rails.application.secrets.stripe_currency, description: "coupon #{cp.code}" ) end + elsif coupon_code != nil + # this case applies if a subscription was took in addition of a reservation, so we create a second + # stripe coupon to apply the discount on the subscription item for the stripe's invoice. + cp = Coupon.find_by_code(coupon_code) + total = plan.amount + Stripe::InvoiceItem.create( + customer: user.stp_customer_id, + amount: -(total * cp.percent_off / 100.0).to_i, + currency: Rails.application.secrets.stripe_currency, + description: "coupon #{cp.code}" + ) end new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, source: card_token)