mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +01:00
fix coupon discount if reservation+subscription when pay by stripe
This commit is contained in:
parent
cb42b46790
commit
c3505c1419
@ -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>
|
<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>
|
||||||
<div class="modal-footer">
|
<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>
|
</div>
|
||||||
|
@ -165,7 +165,7 @@ class Reservation < ActiveRecord::Base
|
|||||||
if plan_id
|
if plan_id
|
||||||
self.subscription = Subscription.find_or_initialize_by(user_id: user.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}
|
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.stp_invoice_id = invoice_items.first.refresh.invoice
|
||||||
self.invoice.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)
|
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)
|
||||||
|
@ -39,11 +39,22 @@ class Subscription < ActiveRecord::Base
|
|||||||
total = plan.amount
|
total = plan.amount
|
||||||
Stripe::InvoiceItem.create(
|
Stripe::InvoiceItem.create(
|
||||||
customer: user.stp_customer_id,
|
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,
|
currency: Rails.application.secrets.stripe_currency,
|
||||||
description: "coupon #{cp.code}"
|
description: "coupon #{cp.code}"
|
||||||
)
|
)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, source: card_token)
|
new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, source: card_token)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user