mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-01 12:24:28 +01:00
pay subscription by wallet
This commit is contained in:
parent
31d5c6d3b3
commit
2dbc026db1
@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
Application.Controllers.controller "PlansIndexController", ["$scope", "$rootScope", "$state", '$uibModal', 'Auth', 'dialogs', 'growl', 'plansPromise', 'groupsPromise', 'Subscription', 'Member', 'subscriptionExplicationsPromise', '_t'
|
||||
, ($scope, $rootScope, $state, $uibModal, Auth, dialogs, growl, plansPromise, groupsPromise, Subscription, Member, subscriptionExplicationsPromise, _t) ->
|
||||
Application.Controllers.controller "PlansIndexController", ["$scope", "$rootScope", "$state", '$uibModal', 'Auth', 'dialogs', 'growl', 'plansPromise', 'groupsPromise', 'Subscription', 'Member', 'subscriptionExplicationsPromise', '_t', 'Wallet', 'helpers'
|
||||
, ($scope, $rootScope, $state, $uibModal, Auth, dialogs, growl, plansPromise, groupsPromise, Subscription, Member, subscriptionExplicationsPromise, _t, Wallet, helpers) ->
|
||||
|
||||
|
||||
|
||||
@ -72,10 +72,13 @@ Application.Controllers.controller "PlansIndexController", ["$scope", "$rootScop
|
||||
# Callback to trigger the payment process of the subscription
|
||||
##
|
||||
$scope.openSubscribePlanModal = ->
|
||||
if $scope.currentUser.role isnt 'admin'
|
||||
payByStripe()
|
||||
else
|
||||
payOnSite()
|
||||
Wallet.getWalletByUser {user_id: $scope.ctrl.member.id}, (wallet) ->
|
||||
amountToPay = helpers.getAmountToPay($scope.selectedPlan.amount, wallet.amount)
|
||||
if $scope.currentUser.role isnt 'admin' and amountToPay > 0
|
||||
payByStripe()
|
||||
else
|
||||
if $scope.currentUser.role is 'admin' or amountToPay is 0
|
||||
payOnSite()
|
||||
|
||||
|
||||
|
||||
@ -162,9 +165,17 @@ Application.Controllers.controller "PlansIndexController", ["$scope", "$rootScop
|
||||
resolve:
|
||||
selectedPlan: -> $scope.selectedPlan
|
||||
member: -> $scope.ctrl.member
|
||||
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'Subscription', 'CustomAsset', ($scope, $uibModalInstance, $state, selectedPlan, member, Subscription, CustomAsset) ->
|
||||
$scope.amount = selectedPlan.amount
|
||||
wallet: ->
|
||||
Wallet.getWalletByUser({user_id: $scope.ctrl.member.id}).$promise
|
||||
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'Subscription', 'CustomAsset', 'wallet', 'helpers', '$locale', ($scope, $uibModalInstance, $state, selectedPlan, member, Subscription, CustomAsset, wallet, helpers, $locale) ->
|
||||
# user wallet amount
|
||||
$scope.walletAmount = wallet.amount
|
||||
|
||||
$scope.amount = helpers.getAmountToPay(selectedPlan.amount, wallet.amount)
|
||||
|
||||
$scope.selectedPlan = selectedPlan
|
||||
|
||||
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
|
||||
# retrieve the CGV
|
||||
CustomAsset.get {name: 'cgv-file'}, (cgv) ->
|
||||
$scope.cgv = cgv.custom_asset
|
||||
@ -203,9 +214,31 @@ Application.Controllers.controller "PlansIndexController", ["$scope", "$rootScop
|
||||
resolve:
|
||||
selectedPlan: -> $scope.selectedPlan
|
||||
member: -> $scope.ctrl.member
|
||||
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'Subscription', ($scope, $uibModalInstance, $state, selectedPlan, member, Subscription) ->
|
||||
wallet: ->
|
||||
Wallet.getWalletByUser({user_id: $scope.ctrl.member.id}).$promise
|
||||
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'Subscription', 'wallet', 'helpers', '$locale', '$filter', ($scope, $uibModalInstance, $state, selectedPlan, member, Subscription, wallet, helpers, $locale, $filter) ->
|
||||
# user wallet amount
|
||||
$scope.walletAmount = wallet.amount
|
||||
|
||||
$scope.price = selectedPlan.amount
|
||||
|
||||
# price to pay
|
||||
$scope.amount = helpers.getAmountToPay($scope.price, wallet.amount)
|
||||
|
||||
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
|
||||
|
||||
$scope.plan = selectedPlan
|
||||
$scope.member = member
|
||||
|
||||
# Button label
|
||||
if $scope.amount > 0
|
||||
$scope.validButtonName = "#{_t('confirm_my_payment_of_')} #{$filter('currency')($scope.amount)}"
|
||||
else
|
||||
if $scope.price > 0
|
||||
$scope.validButtonName = "#{_t('confirm_my_payment_of_')} #{$filter('currency')($scope.price)}"
|
||||
else
|
||||
$scope.validButtonName = _t('confirm')
|
||||
|
||||
$scope.ok = ->
|
||||
$scope.attempting = true
|
||||
Subscription.save
|
||||
|
@ -473,7 +473,7 @@ angular.module('application.router', ['ui.router']).
|
||||
Group.query().$promise
|
||||
]
|
||||
translations: [ 'Translations', (Translations) ->
|
||||
Translations.query(['app.public.plans', 'app.shared.member_select', 'app.shared.stripe']).$promise
|
||||
Translations.query(['app.public.plans', 'app.shared.member_select', 'app.shared.stripe', 'app.shared.wallet']).$promise
|
||||
]
|
||||
|
||||
# events
|
||||
|
@ -3,10 +3,14 @@
|
||||
<h1 translate>{{ 'subscription_confirmation' }}</h1>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<h3 ng-if="walletAmount > 0 && price > 0">{{ 'you_have_amount_in_wallet' | translate:{ amount: walletAmount, currency: currencySymbol } }}</h3>
|
||||
<p ng-if="walletAmount > 0 && price > 0 && amount === 0" class="text-italic">{{'wallet_pay_reservation' | translate}}</p>
|
||||
<p ng-if="walletAmount > 0 && amount !== 0" class="text-italic">{{'credit_amount_for_pay_reservation' | translate:{ amount: amount, currency: currencySymbol } }}</p>
|
||||
|
||||
<p>{{ 'here_is_the_NAME_subscription_summary' | translate:{NAME:member.name} }}</p>
|
||||
<p>{{ plan | humanReadablePlanName }}</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-info" ng-click="ok()" ng-disabled="attempting" translate>{{ 'confirm_(payment_on_site)' }}</button>
|
||||
<button class="btn btn-info" ng-click="ok()" ng-disabled="attempting">{{validButtonName}}</button>
|
||||
<button class="btn btn-default" ng-click="cancel()" translate>{{ 'cancel' }}</button>
|
||||
</div>
|
||||
|
@ -18,8 +18,10 @@ class API::SubscriptionsController < API::ApiController
|
||||
@subscription.attributes = subscription_params
|
||||
is_subscribe = @subscription.save_with_local_payment(!User.find(subscription_params[:user_id]).invoicing_disabled?)
|
||||
else
|
||||
member = User.find(subscription_params[:user_id])
|
||||
plan = Plan.find(subscription_params[:plan_id])
|
||||
@subscription = Subscription.find_or_initialize_by(user_id: current_user.id)
|
||||
if valid_card_token?(subscription_params[:card_token])
|
||||
if valid_card_token?(subscription_params[:card_token]) or (member.wallet.amount >= plan.amount / 100.0)
|
||||
@subscription.update_column(:expired_at, nil) unless @subscription.new_record? # very important
|
||||
@subscription.attributes = subscription_params.merge(user_id: current_user.id)
|
||||
is_subscribe = @subscription.save_with_payment
|
||||
|
@ -17,7 +17,7 @@ class Reservation < ActiveRecord::Base
|
||||
after_commit :notify_member_create_reservation, on: :create
|
||||
after_commit :notify_admin_member_create_reservation, on: :create
|
||||
after_save :update_event_nb_free_places, if: Proc.new { |reservation| reservation.reservable_type == 'Event' }
|
||||
after_save :debit_user_wallet
|
||||
after_create :debit_user_wallet
|
||||
|
||||
#
|
||||
# Generate an array of {Stripe::InvoiceItem} with the elements in the current reservation, price included.
|
||||
@ -125,10 +125,8 @@ class Reservation < ActiveRecord::Base
|
||||
|
||||
end
|
||||
|
||||
total = invoice_items.map(&:amount).map(&:to_i).reduce(:+) || 0
|
||||
wallet_amount = (user.wallet.amount * 100).to_i
|
||||
@wallet_amount_debit = wallet_amount >= total ? total : wallet_amount
|
||||
if @wallet_amount_debit != 0
|
||||
@wallet_amount_debit = get_wallet_amount_debit
|
||||
if @wallet_amount_debit != 0 and !on_site
|
||||
invoice_items << Stripe::InvoiceItem.create(
|
||||
customer: user.stp_customer_id,
|
||||
amount: -@wallet_amount_debit,
|
||||
@ -264,6 +262,14 @@ class Reservation < ActiveRecord::Base
|
||||
def save_with_local_payment
|
||||
if user.invoicing_disabled?
|
||||
if valid?
|
||||
|
||||
### generate invoice only for calcul price, to refactoring!!
|
||||
build_invoice(user: user)
|
||||
generate_invoice_items(true)
|
||||
@wallet_amount_debit = get_wallet_amount_debit
|
||||
self.invoice = nil
|
||||
###
|
||||
|
||||
save!
|
||||
UsersCredits::Manager.new(reservation: self).update_credits
|
||||
return true
|
||||
@ -346,6 +352,15 @@ class Reservation < ActiveRecord::Base
|
||||
reservable.update_columns(nb_free_places: nb_free_places)
|
||||
end
|
||||
|
||||
def get_wallet_amount_debit
|
||||
total = self.invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+) or 0
|
||||
if plan_id.present?
|
||||
total += plan.amount
|
||||
end
|
||||
wallet_amount = (user.wallet.amount * 100).to_i
|
||||
return wallet_amount >= total ? total : wallet_amount
|
||||
end
|
||||
|
||||
def debit_user_wallet
|
||||
if @wallet_amount_debit.present? and @wallet_amount_debit != 0
|
||||
amount = @wallet_amount_debit / 100.0
|
||||
|
@ -16,13 +16,27 @@ class Subscription < ActiveRecord::Base
|
||||
after_save :notify_member_subscribed_plan, if: :is_new?
|
||||
after_save :notify_admin_subscribed_plan, if: :is_new?
|
||||
after_save :notify_partner_subscribed_plan, if: :of_partner_plan?
|
||||
after_save :debit_user_wallet
|
||||
|
||||
# Stripe subscription payment
|
||||
def save_with_payment(invoice = true)
|
||||
if valid?
|
||||
customer = Stripe::Customer.retrieve(user.stp_customer_id)
|
||||
begin
|
||||
new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, card: card_token)
|
||||
# dont add a wallet invoice item if pay subscription by reservation
|
||||
if invoice
|
||||
@wallet_amount_debit = get_wallet_amount_debit
|
||||
if @wallet_amount_debit != 0
|
||||
Stripe::InvoiceItem.create(
|
||||
customer: user.stp_customer_id,
|
||||
amount: -@wallet_amount_debit,
|
||||
currency: Rails.application.secrets.stripe_currency,
|
||||
description: "wallet -#{@wallet_amount_debit / 100.0}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
new_subscription = customer.subscriptions.create(plan: plan.stp_plan_id, source: card_token)
|
||||
self.stp_subscription_id = new_subscription.id
|
||||
self.canceled_at = nil
|
||||
self.expired_at = Time.at(new_subscription.current_period_end)
|
||||
@ -73,6 +87,8 @@ class Subscription < ActiveRecord::Base
|
||||
|
||||
def save_with_local_payment(invoice = true)
|
||||
if valid?
|
||||
@wallet_amount_debit = get_wallet_amount_debit if invoice
|
||||
|
||||
self.stp_subscription_id = nil
|
||||
self.canceled_at = nil
|
||||
set_expired_at
|
||||
@ -209,4 +225,16 @@ class Subscription < ActiveRecord::Base
|
||||
plan.is_a?(PartnerPlan)
|
||||
end
|
||||
|
||||
def get_wallet_amount_debit
|
||||
total = plan.amount
|
||||
wallet_amount = (user.wallet.amount * 100).to_i
|
||||
return wallet_amount >= total ? total : wallet_amount
|
||||
end
|
||||
|
||||
def debit_user_wallet
|
||||
if @wallet_amount_debit.present? and @wallet_amount_debit != 0
|
||||
amount = @wallet_amount_debit / 100.0
|
||||
WalletService.new(user: user, wallet: user.wallet).debit(amount, self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -160,6 +160,7 @@ fr:
|
||||
_the_general_terms_and_conditions: "les conditions générales de vente."
|
||||
enter_your_card_number: "Saisissez votre numéro de carte"
|
||||
confirm_my_payment_of_: "Valider mon paiement de" # contexte : valider mon paiement de 20,00 €
|
||||
credit_amount_for_pay_reservation: "Vous devez créditer {{amount}} {{currency}} pour valider votre réservation"
|
||||
|
||||
valid_reservation_modal:
|
||||
# fenêtre de paiement sur place d'une réservation
|
||||
@ -310,3 +311,5 @@ fr:
|
||||
a_problem_occurred_for_wallet_credit: "Il y a eu un problème lors de chargement au porte-monnaie d'utilisateur."
|
||||
amount_is_required: "Le montant est obligatoire"
|
||||
amount_minimum_1: "Le montant minimum est d'1"
|
||||
you_have_amount_in_wallet: "Vous avez {{amount}} {{currency}} sur votre porte-monnaie"
|
||||
wallet_pay_reservation: "Vous pouvez effectuer directement votre paiement de réservation"
|
||||
|
Loading…
Reference in New Issue
Block a user