1
1
#:nodoc:
2
2
class Payment < ApplicationRecord
3
- require 'request'
4
-
5
3
self . primary_key = :token
6
4
7
5
attr_accessor :issuer , :payment_uri , :message
@@ -12,6 +10,7 @@ class Payment < ApplicationRecord
12
10
validates :payment_type , presence : true
13
11
14
12
enum status : { failed : 0 , in_progress : 1 , successful : 2 }
13
+
15
14
# Keep payconiq_online because it is still present in the database
16
15
enum payment_type : { ideal : 0 , payconiq_online : 1 , pin : 3 }
17
16
enum transaction_type : { checkout : 0 , activity : 1 }
@@ -26,6 +25,7 @@ class Payment < ApplicationRecord
26
25
after_validation :request_payment , on : :create
27
26
28
27
include PgSearch ::Model
28
+
29
29
pg_search_scope :search_by_name ,
30
30
against : [ :trxid ] ,
31
31
associated_against : {
@@ -47,54 +47,40 @@ def request_payment
47
47
48
48
case payment_type . to_sym
49
49
when :ideal
50
- http = ConstipatedKoala ::Request . new ( ENV [ 'MOLLIE_DOMAIN' ] )
51
50
self . token = Digest ::SHA256 . hexdigest ( "#{ member . id } #{ Time . now . to_f } #{ redirect_uri } " )
52
51
53
52
webhook_url = if Rails . env . development?
54
53
"#{ ENV [ 'NGROK_HOST' ] } /api/hook/mollie"
55
54
else
56
55
Rails . application . routes . url_helpers . mollie_hook_url
57
56
end
58
-
59
- request = http . post ( "/#{ ENV [ 'MOLLIE_VERSION' ] } /payments" ,
60
- amount : amount ,
61
- description : description ,
62
-
63
- method : 'ideal' ,
64
- issuer : issuer ,
65
-
66
- metadata : {
67
- member : member . name ,
68
- transaction_type : transaction_type ,
69
- transaction_id : transaction_id
70
-
71
- } ,
72
- webhookUrl : webhook_url ,
73
- redirectUrl : Rails . application . routes . url_helpers . payment_redirect_url ( token : token ) )
74
-
75
- request [ 'Authorization' ] = "Bearer #{ ENV [ 'MOLLIE_TOKEN' ] } "
76
- response = http . send! ( request )
77
-
78
- self . trxid = response . id
79
- self . payment_uri = response . links . paymentUrl
57
+ redirect_url = Rails . application . routes . url_helpers . payment_redirect_url ( token : token )
58
+
59
+ payment = Mollie ::Payment . create (
60
+ amount : { value : format ( '%.2f' , amount ) , currency : 'EUR' } ,
61
+ method : 'ideal' , # only ideal for now
62
+ description : description ,
63
+ webhook_url : webhook_url ,
64
+ redirect_url : redirect_url
65
+ )
66
+
67
+ self . trxid = payment . id
68
+ self . payment_uri = payment . _links [ 'checkout' ] [ 'href' ]
80
69
self . status = :in_progress
81
- # pin payment shouldn't have any extra work
70
+
71
+ # pin payment shouldn't have any extra work
82
72
when :pin
83
73
end
84
74
end
85
75
86
76
def update_transaction!
87
77
case payment_type . to_sym
88
78
when :ideal
89
- http = ConstipatedKoala ::Request . new ( ENV [ 'MOLLIE_DOMAIN' ] )
90
79
@status = status
91
80
92
- request = http . get ( "/#{ ENV [ 'MOLLIE_VERSION' ] } /payments/#{ trxid } " )
93
- request [ 'Authorization' ] = "Bearer #{ ENV [ 'MOLLIE_TOKEN' ] } "
94
-
95
- response = http . send! ( request )
81
+ payment = Mollie ::Payment . get ( trxid )
96
82
97
- status_update ( response . status )
83
+ status_update ( payment . status )
98
84
99
85
save!
100
86
@@ -156,32 +142,17 @@ def transaction_fee
156
142
end
157
143
end
158
144
159
- def self . ideal_issuers
160
- # cache the payment issuers for 12 hours, don't request it to often. Stored in tmp/cache
161
- return [ ] if ENV [ 'MOLLIE_TOKEN' ] . blank?
162
-
163
- Rails . cache . fetch ( 'mollie_issuers' , expires_in : 12 . hours ) do
164
- http = ConstipatedKoala ::Request . new ( ENV [ 'MOLLIE_DOMAIN' ] )
165
-
166
- request = http . get ( "/#{ ENV [ 'MOLLIE_VERSION' ] } /issuers" )
167
- request [ 'Authorization' ] = "Bearer #{ ENV [ 'MOLLIE_TOKEN' ] } "
168
-
169
- response = http . send! ( request )
170
- response . data . map { |issuer | [ issuer . name , issuer . id ] }
171
- end
172
- end
173
-
174
145
def activities
175
146
Activity . find ( transaction_id ) if activity?
176
147
end
177
148
178
149
private
179
150
180
151
def status_update ( new_status )
181
- self . status = case new_status . downcase
182
- when "succeeded " , "paid "
152
+ self . status = case new_status
153
+ when "paid " , "authorized "
183
154
:successful
184
- when "expired" , "canceled" , " failed", "cancelled" , "authorization_failed "
155
+ when "expired" , "failed" , "canceled "
185
156
:failed
186
157
else
187
158
:in_progress
0 commit comments