1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

fix coupon discount if reservation+subscription when pay by stripe

This commit is contained in:
Sylvain 2016-09-01 11:36:51 +02:00
parent cb42b46790
commit c3505c1419
3 changed files with 14 additions and 3 deletions

View File

@ -14,5 +14,5 @@
<p class="text-center"><button ng-click="ok()" class="btn btn-warning-full rounded width-70" translate>{{ 'register_for_the_training' }}</button></p>
</div>
<div class="modal-footer">
<p class="text-center">{{ 'i_dont_want_to_register_now' | translate }} <br> <a class="text-u-l" href="#" ng-click="cancel($event)">{{ 'close' }}</a></p>
<p class="text-center">{{ 'i_dont_want_to_register_now' | translate }} <br> <a class="text-u-l" href="#" ng-click="cancel($event)" translate>{{ 'close' }}</a></p>
</div>

View File

@ -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)

View File

@ -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)