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,43 @@ 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
57
+ redirect_url = Rails . application . routes . url_helpers . payment_redirect_url ( token : token )
58
58
59
- request = http . post ( "/#{ ENV [ 'MOLLIE_VERSION' ] } /payments" ,
60
- amount : amount ,
61
- description : description ,
62
-
63
- method : 'ideal' ,
64
- issuer : issuer ,
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
+ )
65
66
66
- metadata : {
67
- member : member . name ,
68
- transaction_type : transaction_type ,
69
- transaction_id : transaction_id
67
+ self . trxid = payment . id
70
68
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
69
+ # The host of this url is `www.mollie.com` so it will redirect to the mollie payment page
70
+ # if this ever chanes, the redirect_uri whitelist in the controller should be updated
71
+ self . payment_uri = payment . _links [ 'checkout' ] [ 'href' ]
80
72
self . status = :in_progress
81
- # pin payment shouldn't have any extra work
73
+
74
+ # pin payment shouldn't have any extra work
82
75
when :pin
83
76
end
84
77
end
85
78
86
79
def update_transaction!
87
80
case payment_type . to_sym
88
81
when :ideal
89
- http = ConstipatedKoala ::Request . new ( ENV [ 'MOLLIE_DOMAIN' ] )
90
82
@status = status
91
83
92
- request = http . get ( "/#{ ENV [ 'MOLLIE_VERSION' ] } /payments/#{ trxid } " )
93
- request [ 'Authorization' ] = "Bearer #{ ENV [ 'MOLLIE_TOKEN' ] } "
84
+ payment = Mollie ::Payment . get ( trxid )
94
85
95
- response = http . send! ( request )
96
-
97
- status_update ( response . status )
86
+ status_update ( payment . status )
98
87
99
88
save!
100
89
@@ -156,32 +145,17 @@ def transaction_fee
156
145
end
157
146
end
158
147
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
148
def activities
175
149
Activity . find ( transaction_id ) if activity?
176
150
end
177
151
178
152
private
179
153
180
154
def status_update ( new_status )
181
- self . status = case new_status . downcase
182
- when "succeeded " , "paid "
155
+ self . status = case new_status
156
+ when "paid " , "authorized "
183
157
:successful
184
- when "expired" , "canceled" , " failed", "cancelled" , "authorization_failed "
158
+ when "expired" , "failed" , "canceled "
185
159
:failed
186
160
else
187
161
:in_progress
0 commit comments