@@ -5,34 +5,33 @@ class PaymentsController < ApplicationController
5
5
load_and_authorize_resource
6
6
load_resource :conference , find_by : :short_title
7
7
authorize_resource :conference_registrations , class : Registration
8
- before_action :load_currency_conversions , only : [ :new , :create ]
9
8
10
9
def index
11
10
@payments = current_user . payments
12
11
end
13
12
14
13
def new
15
- # todo : use "base currency"
14
+ # TODO : use "base currency"
16
15
session [ :selected_currency ] = params [ :currency ] if params [ :currency ] . present?
17
- selected_currency = session [ :selected_currency ] || 'USD'
18
- from_currency = "USD"
16
+ selected_currency = session [ :selected_currency ] || @conference . tickets . first . price_currency
17
+ from_currency = @conference . tickets . first . price_currency
19
18
20
- @total_amount_to_pay = convert_currency ( Ticket . total_price ( @conference , current_user , paid : false ) . amount , from_currency , selected_currency )
19
+ @total_amount_to_pay = CurrencyConversion . convert_currency ( @conference , Ticket . total_price ( @conference , current_user , paid : false ) , from_currency , selected_currency )
21
20
raise CanCan ::AccessDenied . new ( 'Nothing to pay for!' , :new , Payment ) if @total_amount_to_pay . zero?
21
+ raise CanCan ::AccessDenied . new ( 'Selected currency is invalid!' , :new , Payment ) if @total_amount_to_pay . negative?
22
22
23
23
@has_registration_ticket = params [ :has_registration_ticket ]
24
24
@unpaid_ticket_purchases = current_user . ticket_purchases . unpaid . by_conference ( @conference )
25
- #a way to display the currency values in the view, but there might be a better way to do this.
25
+
26
26
@converted_prices = { }
27
27
@unpaid_ticket_purchases . each do |ticket_purchase |
28
- #hardcoded
29
- @converted_prices [ ticket_purchase . id ] = convert_currency ( ticket_purchase . price . amount , 'USD' , selected_currency )
28
+ @converted_prices [ ticket_purchase . id ] = ticket_purchase . amount_paid
30
29
end
31
30
@currency = selected_currency
32
31
end
33
32
34
33
def create
35
- @payment = Payment . new ( payment_params )
34
+ @payment = Payment . new payment_params
36
35
37
36
if @payment . purchase && @payment . save
38
37
update_purchased_ticket_purchases
@@ -52,7 +51,7 @@ def create
52
51
notice : 'Thanks! Your ticket is booked successfully.'
53
52
end
54
53
else
55
- @total_amount_to_pay = convert_currency ( Ticket . total_price ( @conference , current_user , paid : false ) . amount , from_currency , selected_currency )
54
+ @total_amount_to_pay = convert_currency ( @conference , Ticket . total_price ( @conference , current_user , paid : false ) , from_currency , selected_currency )
56
55
@unpaid_ticket_purchases = current_user . ticket_purchases . unpaid . by_conference ( @conference )
57
56
flash . now [ :error ] = @payment . errors . full_messages . to_sentence + ' Please try again with correct credentials.'
58
57
render :new
@@ -73,27 +72,4 @@ def update_purchased_ticket_purchases
73
72
ticket_purchase . pay ( @payment )
74
73
end
75
74
end
76
-
77
- def load_currency_conversions
78
- @currency_conversions = @conference . currency_conversions
79
- end
80
-
81
- def convert_currency ( amount , from_currency , to_currency )
82
- update_rate ( from_currency , to_currency )
83
-
84
- money_amount = Money . from_amount ( amount , from_currency )
85
- converted_amount = money_amount . exchange_to ( to_currency )
86
- converted_amount
87
- end
88
-
89
- def update_rate ( from_currency , to_currency )
90
- conversion = @currency_conversions . find_by ( from_currency : from_currency , to_currency : to_currency )
91
- if conversion
92
- Money . add_rate ( from_currency , to_currency , conversion . rate )
93
- else
94
- #If no conversion is found. Typically only possible if base to base. Maybe make this error out.
95
- Money . add_rate ( from_currency , to_currency , 1 ) unless from_currency == to_currency
96
- end
97
- end
98
-
99
75
end
0 commit comments