1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

fix stripe subscription w/ coupon + wallet

This commit is contained in:
Sylvain 2020-12-30 12:19:33 +01:00
parent b8319a5124
commit dc00df4afc
8 changed files with 31 additions and 20 deletions

View File

@ -47,15 +47,18 @@ Application.Directives.directive('coupon', [ '$rootScope', 'Coupon', '_t', funct
$scope.messages = [];
if ($scope.couponCode === '') {
$scope.status = 'pending';
return $scope.coupon = null;
$scope.coupon = null;
} else {
return Coupon.validate({ code: $scope.couponCode, user_id: $scope.userId, amount: $scope.total }, function (res) {
Coupon.validate({ code: $scope.couponCode, user_id: $scope.userId, amount: $scope.total }, function (res) {
$scope.status = 'valid';
$scope.coupon = res;
if (res.type === 'percent_off') {
return $scope.messages.push({ type: 'success', message: _t('app.shared.coupon_input.the_coupon_has_been_applied_you_get_PERCENT_discount', { PERCENT: res.percent_off }) });
$scope.messages.push({ type: 'success', message: _t('app.shared.coupon_input.the_coupon_has_been_applied_you_get_PERCENT_discount', { PERCENT: res.percent_off }) });
} else {
return $scope.messages.push({ type: 'success', message: _t('app.shared.coupon_input.the_coupon_has_been_applied_you_get_AMOUNT_CURRENCY', { AMOUNT: res.amount_off, CURRENCY: $rootScope.currencySymbol }) });
$scope.messages.push({ type: 'success', message: _t('app.shared.coupon_input.the_coupon_has_been_applied_you_get_AMOUNT_CURRENCY', { AMOUNT: res.amount_off, CURRENCY: $rootScope.currencySymbol }) });
}
if (res.validity_per_user === 'once') {
$scope.messages.push({ type: 'warning', message: _t('app.shared.coupon_input.coupon_validity_once') });
}
}
, function (err) {

View File

@ -145,15 +145,13 @@ class Price < ApplicationRecord
total_amount = cs.apply(total_amount, cp)
# == generate PaymentSchedule (if applicable) ===
schedule = if options[:payment_schedule] && plan.monthly_payment
schedule = if options[:payment_schedule] && plan&.monthly_payment
PaymentScheduleService.new.compute(plan, _amount_no_coupon, cp)
else
nil
end
if schedule
total_amount -= schedule[:payment_schedule].total
total_amount += schedule[:items][0].amount
end
total_amount = schedule[:items][0].amount if schedule
# return result
{

View File

@ -10,13 +10,8 @@ class PaymentScheduleService
##
def compute(plan, total, coupon = nil)
other_items = total - plan.amount
price = if coupon
cs = CouponService.new
other_items = cs.ventilate(total, other_items, coupon)
cs.ventilate(total, plan.amount, coupon)
else
plan.amount
end
# base monthly price of the plan
price = plan.amount
ps = PaymentSchedule.new(scheduled: plan, total: price + other_items, coupon: coupon)
deadlines = plan.duration / 1.month
per_month = (price / deadlines).truncate
@ -36,6 +31,13 @@ class PaymentScheduleService
else
per_month
end
if coupon
cs = CouponService.new
if (coupon.validity_per_user == 'once' && i.zero?) || coupon.validity_per_user == 'forever'
details[:without_coupon] = amount
amount = cs.apply(amount, coupon)
end
end
items.push PaymentScheduleItem.new(
amount: amount,
due_date: date,

View File

@ -80,7 +80,11 @@ class WalletService
# @param coupon {Coupon|String} Coupon object or code
##
def self.wallet_amount_debit(payment, user, coupon = nil)
total = payment.total
total = if payment.class == PaymentSchedule
payment.payment_schedule_items.first.amount
else
payment.total
end
total = CouponService.new.apply(total, coupon, user.id) if coupon
wallet_amount = (user.wallet.amount * 100).to_i

View File

@ -1,2 +1,4 @@
json.extract! @coupon, :id, :code, :type, :percent_off
json.amount_off (@coupon.amount_off / 100.00) unless @coupon.amount_off.nil?
# frozen_string_literal: true
json.extract! @coupon, :id, :code, :type, :percent_off, :validity_per_user
json.amount_off (@coupon.amount_off / 100.00) unless @coupon.amount_off.nil?

View File

@ -371,6 +371,7 @@ en:
code_: "Code:"
the_coupon_has_been_applied_you_get_PERCENT_discount: "The coupon has been applied. You get a {PERCENT}% discount."
the_coupon_has_been_applied_you_get_AMOUNT_CURRENCY: "The coupon has been applied. You get a discount of {AMOUNT} {CURRENCY}."
coupon_validity_once: "This coupon is valid only once. In case of payment schedule, only for the first deadline."
unable_to_apply_the_coupon_because_disabled: "Unable to apply the coupon: this code was disabled."
unable_to_apply_the_coupon_because_expired: "Unable to apply the coupon: this code has expired."
unable_to_apply_the_coupon_because_sold_out: "Unable to apply the coupon: this code reached its quota."

View File

@ -371,6 +371,7 @@ fr:
code_: "Code :"
the_coupon_has_been_applied_you_get_PERCENT_discount: "Le code promo a bien été appliqué. Vous bénéficiez d'une remise de {PERCENT} %."
the_coupon_has_been_applied_you_get_AMOUNT_CURRENCY: "Le code promo a bien été appliqué. Vous bénéficiez d'une remise de {AMOUNT} {CURRENCY}."
coupon_validity_once: "Ce code promo n'est valide qu'une seule fois. En cas de paiement échelonné, seulement pour la première échéance."
unable_to_apply_the_coupon_because_disabled: "Impossible d'appliquer la réduction : ce code promo a été désactivé."
unable_to_apply_the_coupon_because_expired: "Impossible d'appliquer la réduction : ce code promo a expiré."
unable_to_apply_the_coupon_because_sold_out: "Impossible d'appliquer la réduction : ce code promo a atteint son quota."

View File

@ -181,7 +181,7 @@ You can subscribe to [this atom feed](https://github.com/sleede/fab-manager/rele
4. remove old assets
`rm -Rf public/assets/`
`rm -Rf public/packs/`
5. compile new assets