1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

pay totalement/partialement reservation by wallet

This commit is contained in:
Peng DU 2016-07-07 15:57:06 +02:00
parent fc7eaaab94
commit 31d5c6d3b3
14 changed files with 161 additions and 67 deletions

View File

@ -132,8 +132,8 @@ Application.Controllers.controller "EventsController", ["$scope", "$state", 'Eve
Application.Controllers.controller "ShowEventController", ["$scope", "$state", "$stateParams", "Event", '$uibModal', 'Member', 'Reservation', 'Price', 'CustomAsset', 'eventPromise', 'reducedAmountAlert', 'growl', '_t'
($scope, $state, $stateParams, Event, $uibModal, Member, Reservation, Price, CustomAsset, eventPromise, reducedAmountAlert, growl, _t) ->
Application.Controllers.controller "ShowEventController", ["$scope", "$state", "$stateParams", "Event", '$uibModal', 'Member', 'Reservation', 'Price', 'CustomAsset', 'eventPromise', 'reducedAmountAlert', 'growl', '_t', 'Wallet', 'helpers',
($scope, $state, $stateParams, Event, $uibModal, Member, Reservation, Price, CustomAsset, eventPromise, reducedAmountAlert, growl, _t, Wallet, helpers) ->
@ -242,11 +242,13 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
if Object.keys($scope.ctrl.member).length > 0
reservation = mkReservation($scope.ctrl.member, $scope.reserve, $scope.event)
if $scope.currentUser.role isnt 'admin' and $scope.reserve.amountTotal > 0
payByStripe(reservation)
else
if $scope.currentUser.role is 'admin' or $scope.reserve.amountTotal is 0
payOnSite(reservation)
Wallet.getWalletByUser {user_id: $scope.ctrl.member.id}, (wallet) ->
amountToPay = helpers.getAmountToPay($scope.reserve.amountTotal, wallet.amount)
if $scope.currentUser.role isnt 'admin' and amountToPay > 0
payByStripe(reservation)
else
if $scope.currentUser.role is 'admin' or amountToPay is 0
payOnSite(reservation)
else
# otherwise we alert, this error musn't occur when the current user is not admin
growl.error(_t('please_select_a_member_first'))
@ -461,15 +463,20 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
reservation
price: ->
Price.compute({reservation: reservation}).$promise
wallet: ->
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
cgv: ->
CustomAsset.get({name: 'cgv-file'}).$promise
objectToPay: ->
eventToReserve: $scope.event
reserve: $scope.reserve
member: $scope.ctrl.member
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'growl', ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, growl) ->
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'growl', 'wallet', 'helpers', '$locale', ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, growl, wallet, helpers, $locale) ->
# user wallet amount
$scope.walletAmount = wallet.amount
# Price
$scope.amount = price.price
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
# CGV
$scope.cgv = cgv.custom_asset
@ -477,6 +484,8 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
# Reservation
$scope.reservation = reservation
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
# Callback for the stripe payment authorization
$scope.payment = (status, response) ->
if response.error
@ -511,13 +520,23 @@ Application.Controllers.controller "ShowEventController", ["$scope", "$state", "
reservation
price: ->
Price.compute({reservation: reservation}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation) ->
wallet: ->
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'wallet', '$locale', 'helpers', ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation, wallet, $locale, helpers) ->
# user wallet amount
$scope.walletAmount = wallet.amount
# Price
$scope.amount = price.price
$scope.price = price.price
# price to pay
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
# Reservation
$scope.reservation = reservation
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
# Button label
if $scope.amount > 0
$scope.validButtonName = _t('confirm_(payment_on_site)')

View File

@ -268,8 +268,8 @@ Application.Controllers.controller "ShowMachineController", ['$scope', '$state',
# This controller workflow is pretty similar to the trainings reservation controller.
##
Application.Controllers.controller "ReserveMachineController", ["$scope", "$state", '$stateParams', "$uibModal", '_t', "moment", 'Machine', 'Auth', 'dialogs', '$timeout', 'Price', 'Member', 'Availability', 'Slot', 'Setting', 'CustomAsset', 'plansPromise', 'groupsPromise', 'growl', 'settingsPromise', 'uiCalendarConfig', 'CalendarConfig'
($scope, $state, $stateParams, $uibModal, _t, moment, Machine, Auth, dialogs, $timeout, Price, Member, Availability, Slot, Setting, CustomAsset, plansPromise, groupsPromise, growl, settingsPromise, uiCalendarConfig, CalendarConfig) ->
Application.Controllers.controller "ReserveMachineController", ["$scope", "$state", '$stateParams', "$uibModal", '_t', "moment", 'Machine', 'Auth', 'dialogs', '$timeout', 'Price', 'Member', 'Availability', 'Slot', 'Setting', 'CustomAsset', 'plansPromise', 'groupsPromise', 'growl', 'settingsPromise', 'Wallet', 'helpers', 'uiCalendarConfig', 'CalendarConfig',
($scope, $state, $stateParams, $uibModal, _t, moment, Machine, Auth, dialogs, $timeout, Price, Member, Availability, Slot, Setting, CustomAsset, plansPromise, groupsPromise, growl, settingsPromise, Wallet, helpers, uiCalendarConfig, CalendarConfig) ->
@ -529,11 +529,13 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat
if Object.keys($scope.ctrl.member).length > 0
reservation = mkReservation($scope.ctrl.member, $scope.eventsReserved, $scope.selectedPlan)
if $scope.currentUser.role isnt 'admin' and $scope.amountTotal > 0
payByStripe(reservation)
else
if $scope.currentUser.role is 'admin' or $scope.amountTotal is 0
payOnSite(reservation)
Wallet.getWalletByUser {user_id: $scope.ctrl.member.id}, (wallet) ->
amountToPay = helpers.getAmountToPay($scope.amountTotal, wallet.amount)
if $scope.currentUser.role isnt 'admin' and amountToPay > 0
payByStripe(reservation)
else
if $scope.currentUser.role is 'admin' or amountToPay is 0
payOnSite(reservation)
else
# otherwise we alert, this error musn't occur when the current user is not admin
growl.error(_t('please_select_a_member_first'))
@ -752,11 +754,16 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat
reservation
price: ->
Price.compute({reservation: reservation}).$promise
wallet: ->
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
cgv: ->
CustomAsset.get({name: 'cgv-file'}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation) ->
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'wallet', 'helpers', '$locale', ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, wallet, helpers, $locale) ->
# user wallet amount
$scope.walletAmount = wallet.amount
# Price
$scope.amount = price.price
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
# CGV
$scope.cgv = cgv.custom_asset
@ -764,6 +771,8 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat
# Reservation
$scope.reservation = reservation
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
##
# Callback to process the payment with Stripe, triggered on button click
##
@ -800,19 +809,31 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat
reservation
price: ->
Price.compute({reservation: reservation}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation) ->
wallet: ->
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', 'wallet', 'helpers', '$filter', '$locale', ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation, wallet, helpers, $filter, $locale) ->
# user wallet amount
$scope.walletAmount = wallet.amount
# Price
$scope.amount = price.price
$scope.price = price.price
# price to pay
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
# Reservation
$scope.reservation = reservation
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
# Button label
if $scope.amount > 0
$scope.validButtonName = _t('confirm_(payment_on_site)')
$scope.validButtonName = "#{_t('confirm_my_payment_of_')} #{$filter('currency')($scope.amount)}"
else
$scope.validButtonName = _t('confirm')
if price.price > 0
$scope.validButtonName = "#{_t('confirm_my_payment_of_')} #{$filter('currency')(price.price)}"
else
$scope.validButtonName = _t('confirm')
##
# Callback to process the local payment, triggered on button click

View File

@ -51,8 +51,8 @@ Application.Controllers.controller "ShowTrainingController", ['$scope', '$state'
# training can be reserved during the reservation process (the shopping cart may contains only one training and a subscription).
##
Application.Controllers.controller "ReserveTrainingController", ["$scope", "$state", '$stateParams', '$filter', '$compile', "$uibModal", 'Auth', 'dialogs', '$timeout', 'Price', 'Availability', 'Slot', 'Member', 'Setting', 'CustomAsset', 'availabilityTrainingsPromise', 'plansPromise', 'groupsPromise', 'growl', 'settingsPromise', 'trainingPromise', '_t', 'uiCalendarConfig', 'CalendarConfig'
($scope, $state, $stateParams, $filter, $compile, $uibModal, Auth, dialogs, $timeout, Price, Availability, Slot, Member, Setting, CustomAsset, availabilityTrainingsPromise, plansPromise, groupsPromise, growl, settingsPromise, trainingPromise, _t, uiCalendarConfig, CalendarConfig) ->
Application.Controllers.controller "ReserveTrainingController", ["$scope", "$state", '$stateParams', '$filter', '$compile', "$uibModal", 'Auth', 'dialogs', '$timeout', 'Price', 'Availability', 'Slot', 'Member', 'Setting', 'CustomAsset', 'availabilityTrainingsPromise', 'plansPromise', 'groupsPromise', 'growl', 'settingsPromise', 'trainingPromise', '_t', 'Wallet', 'helpers', 'uiCalendarConfig', 'CalendarConfig'
($scope, $state, $stateParams, $filter, $compile, $uibModal, Auth, dialogs, $timeout, Price, Availability, Slot, Member, Setting, CustomAsset, availabilityTrainingsPromise, plansPromise, groupsPromise, growl, settingsPromise, trainingPromise, _t, Wallet, helpers, uiCalendarConfig, CalendarConfig) ->
@ -188,11 +188,13 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta
if Object.keys($scope.ctrl.member).length > 0
reservation = mkReservation($scope.ctrl.member, $scope.selectedTraining, $scope.selectedPlan)
if $scope.currentUser.role isnt 'admin' and $scope.amountTotal > 0
payByStripe(reservation)
else
if $scope.currentUser.role is 'admin' or $scope.amountTotal is 0
payOnSite(reservation)
Wallet.getWalletByUser {user_id: $scope.ctrl.member.id}, (wallet) ->
amountToPay = helpers.getAmountToPay($scope.amountTotal, wallet.amount)
if $scope.currentUser.role isnt 'admin' and amountToPay > 0
payByStripe(reservation)
else
if $scope.currentUser.role is 'admin' or amountToPay is 0
payOnSite(reservation)
else
# otherwise we alert, this error musn't occur when the current user is not admin
growl.error(_t('please_select_a_member_first'))
@ -474,11 +476,16 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta
reservation
price: ->
Price.compute({reservation: reservation}).$promise
wallet: ->
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
cgv: ->
CustomAsset.get({name: 'cgv-file'}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation) ->
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'wallet', 'cgv', 'Auth', 'Reservation', '$locale', 'helpers', ($scope, $uibModalInstance, $state, reservation, price, wallet, cgv, Auth, Reservation, $locale, helpers) ->
# user wallet amount
$scope.walletAmount = wallet.amount
# Price
$scope.amount = price.price
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
# CGV
$scope.cgv = cgv.custom_asset
@ -486,6 +493,8 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta
# Reservation
$scope.reservation = reservation
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
##
# Callback to process the payment with Stripe, triggered on button click
##
@ -525,18 +534,31 @@ Application.Controllers.controller "ReserveTrainingController", ["$scope", "$sta
reservation
price: ->
Price.compute({reservation: reservation}).$promise
controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'Auth', 'Reservation', ($scope, $uibModalInstance, $state, reservation, price, Auth, Reservation) ->
wallet: ->
Wallet.getWalletByUser({user_id: reservation.user_id}).$promise
controller: ['$scope', '$uibModalInstance', '$state', '$filter', 'reservation', 'price', 'wallet', 'Auth', 'Reservation', '$locale', 'helpers', ($scope, $uibModalInstance, $state, $filter, reservation, price, wallet, Auth, Reservation, $locale, helpers) ->
# user wallet amount
$scope.walletAmount = wallet.amount
# Price
$scope.amount = price.price
$scope.price = price.price
# price to pay
$scope.amount = helpers.getAmountToPay(price.price, wallet.amount)
# Reservation
$scope.reservation = reservation
$scope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM
# Button label
if $scope.amount > 0
$scope.validButtonName = _t('confirm_(payment_on_site)')
$scope.validButtonName = "#{_t('confirm_my_payment_of_')} #{$filter('currency')($scope.amount)}"
else
$scope.validButtonName = _t('confirm')
if price.price > 0
$scope.validButtonName = "#{_t('confirm_my_payment_of_')} #{$filter('currency')(price.price)}"
else
$scope.validButtonName = _t('confirm')
##
# Callback to process the local payment, triggered on button click

View File

@ -204,8 +204,8 @@ angular.module('application.router', ['ui.router']).
templateUrl: '<%= asset_path "dashboard/wallet.html" %>'
controller: 'WalletController'
resolve:
walletPromise: ['Wallet', (Wallet)->
Wallet.my().$promise
walletPromise: ['Wallet', 'currentUser', (Wallet, currentUser)->
Wallet.getWalletByUser(user_id: currentUser.id).$promise
]
transactionsPromise: ['Wallet', 'walletPromise', (Wallet, walletPromise)->
Wallet.transactions(id: walletPromise.id).$promise
@ -363,7 +363,7 @@ angular.module('application.router', ['ui.router']).
]
translations: [ 'Translations', (Translations) ->
Translations.query(['app.logged.machines_reserve', 'app.shared.plan_subscribe', 'app.shared.member_select',
'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal']).$promise
'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal', 'app.shared.wallet']).$promise
]
.state 'app.admin.machines_edit',
url: '/machines/:id/edit'
@ -440,7 +440,7 @@ angular.module('application.router', ['ui.router']).
]
translations: [ 'Translations', (Translations) ->
Translations.query(['app.logged.trainings_reserve', 'app.shared.plan_subscribe', 'app.shared.member_select',
'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal']).$promise
'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal', 'app.shared.wallet']).$promise
]
# notifications
.state 'app.logged.notifications',
@ -510,7 +510,7 @@ angular.module('application.router', ['ui.router']).
Setting.get(name: 'event_reduced_amount_alert').$promise
]
translations: [ 'Translations', (Translations) ->
Translations.query(['app.public.events_show', 'app.shared.member_select', 'app.shared.stripe', 'app.shared.valid_reservation_modal']).$promise
Translations.query(['app.public.events_show', 'app.shared.member_select', 'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.wallet']).$promise
]
# calendar global (trainings, machines and events)

View File

@ -0,0 +1,6 @@
'use strict'
Application.Services.factory 'helpers', [()->
getAmountToPay: (price, walletAmount)->
if walletAmount > price then 0 else price - walletAmount
]

View File

@ -3,10 +3,6 @@
Application.Services.factory 'Wallet', ["$resource", ($resource)->
$resource "/api/wallet",
{},
my:
method: 'GET'
url: '/api/wallet/my'
isArray: false
getWalletByUser:
method: 'GET'
url: '/api/wallet/by_user/:user_id'

View File

@ -3,6 +3,10 @@
<h1 translate>{{ 'booking_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 translate>{{ 'here_is_the_summary_of_the_slots_to_book_for_the_current_user' }}</p>
<ul ng-repeat="slot in reservation.slots_attributes">
<li><strong>{{slot.start_at | amDateFormat: 'LL'}} : {{slot.start_at | amDateFormat:'LT'}} - {{slot.end_at | amDateFormat:'LT'}}</strong></li>

View File

@ -19,6 +19,9 @@
</div>
</div>
<h3 ng-if="walletAmount">{{ 'you_have_amount_in_wallet' | translate:{ amount: walletAmount, currency: currencySymbol } }}</h3>
<p ng-if="walletAmount > 0 && amount !== 0" class="text-italic">{{'credit_amount_for_pay_reservation' | translate:{ amount: amount, currency: currencySymbol } }}</p>
<div class="form-group" ng-class="{'has-error': stripeForm.number.$dirty && stripeForm.number.$invalid}">
<div class="col-sm-12">
<input autocomplete="off" class="form-control card-number" placeholder="{{ 'enter_your_card_number' | translate }}" type="text" data-stripe="number" name="number" ng-model="cardNumero" required ng-minlength="14" ng-maxlength="16" ng-pattern="/^\d+$/">

View File

@ -1,14 +1,9 @@
class API::WalletController < API::ApiController
before_action :authenticate_user!
def my
@wallet = current_user.wallet
render :show
end
def by_user
authorize Wallet
@wallet = Wallet.find_by(user_id: params[:user_id])
authorize @wallet
render :show
end

View File

@ -17,6 +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
#
# Generate an array of {Stripe::InvoiceItem} with the elements in the current reservation, price included.
@ -124,6 +125,18 @@ 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
invoice_items << 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
# let's return the resulting array of items
invoice_items
end
@ -170,49 +183,49 @@ class Reservation < ActiveRecord::Base
#
# IMPORTANT NOTE: here, we have to create an invoice manually and pay it to pay all waiting stripe invoice items
#
invoice = Stripe::Invoice.create(
stp_invoice = Stripe::Invoice.create(
customer: user.stp_customer_id,
)
invoice.pay
stp_invoice.pay
card.delete if card
self.stp_invoice_id = invoice.id
self.invoice.stp_invoice_id = invoice.id
self.invoice.total = invoice.total
self.stp_invoice_id = stp_invoice.id
self.invoice.stp_invoice_id = stp_invoice.id
self.invoice.total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
save!
rescue Stripe::CardError => card_error
clear_payment_info(card, invoice, invoice_items)
clear_payment_info(card, stp_invoice, invoice_items)
logger.info card_error
errors[:card] << card_error.message
return false
rescue Stripe::InvalidRequestError => e
# Invalid parameters were supplied to Stripe's API
clear_payment_info(card, invoice, invoice_items)
clear_payment_info(card, stp_invoice, invoice_items)
logger.error e
errors[:payment] << e.message
return false
rescue Stripe::AuthenticationError => e
# Authentication with Stripe's API failed
# (maybe you changed API keys recently)
clear_payment_info(card, invoice, invoice_items)
clear_payment_info(card, stp_invoice, invoice_items)
logger.error e
errors[:payment] << e.message
return false
rescue Stripe::APIConnectionError => e
# Network communication with Stripe failed
clear_payment_info(card, invoice, invoice_items)
clear_payment_info(card, stp_invoice, invoice_items)
logger.error e
errors[:payment] << e.message
return false
rescue Stripe::StripeError => e
# Display a very generic error to the user, and maybe send
# yourself an email
clear_payment_info(card, invoice, invoice_items)
clear_payment_info(card, stp_invoice, invoice_items)
logger.error e
errors[:payment] << e.message
return false
rescue => e
# Something else happened, completely unrelated to Stripe
clear_payment_info(card, invoice, invoice_items)
clear_payment_info(card, stp_invoice, invoice_items)
logger.error e
errors[:payment] << e.message
return false
@ -332,4 +345,11 @@ class Reservation < ActiveRecord::Base
end
reservable.update_columns(nb_free_places: nb_free_places)
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

View File

@ -1,6 +1,6 @@
class WalletPolicy < ApplicationPolicy
def by_user?
user.is_admin?
user.is_admin? or user == record.user
end
def transactions?

View File

@ -16,5 +16,14 @@ class WalletService
attached_object: transaction
return true
end
return false
end
def debit(amount, transactable)
if @wallet.debit(amount)
WalletTransaction.create(user: @user, wallet: @wallet, transaction_type: 'debit', amount: amount, transactable: transactable)
return true
end
return false
end
end

View File

@ -48,8 +48,7 @@ Rails.application.routes.draw do
resources :notifications, only: [:index, :show, :update] do
match :update_all, path: '/', via: [:put, :patch], on: :collection
end
resources :wallet do
get :my, on: :collection
resources :wallet, only: [] do
get '/by_user/:user_id', action: 'by_user', on: :collection
get :transactions, on: :member
put :credit, on: :member

View File

@ -15,7 +15,7 @@ class WalletsTest < ActionDispatch::IntegrationTest
end
test 'get my wallet' do
get '/api/wallet/my'
get "/api/wallet/by_user/#{@kdumas.id}"
assert_equal 200, response.status
assert_equal Mime::JSON, response.content_type
wallet = json_response(response.body)