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,38 @@ 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
- "#{ ENV [ 'NGROK_HOST' ] } /api/hook/mollie"
55
- else
56
- Rails . application . routes . url_helpers . mollie_hook_url
57
- 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 ) )
53
+ "#{ ENV [ 'NGROK_HOST' ] } /api/hook/mollie"
54
+ else
55
+ Rails . application . routes . url_helpers . mollie_hook_url
56
+ end
74
57
75
- request [ 'Authorization' ] = "Bearer #{ ENV [ 'MOLLIE_TOKEN' ] } "
76
- response = http . send! ( request )
58
+ payment = Mollie ::Payment . create (
59
+ amount : { value : amount . to_s , currency : 'EUR' } ,
60
+ description : description ,
61
+ webhookUrl : webhook_url ,
62
+ redirectUrl : Rails . application . routes . url_helpers . payment_redirect_url ( token : token ) ,
63
+ )
77
64
78
- self . trxid = response . id
79
- self . payment_uri = response . links . paymentUrl
65
+ self . trxid = payment . id
66
+ self . payment_uri = payment . _links [ 'checkout' ] [ 'href' ]
80
67
self . status = :in_progress
81
- # pin payment shouldn't have any extra work
68
+
69
+ # pin payment shouldn't have any extra work
82
70
when :pin
83
71
end
84
72
end
85
73
86
74
def update_transaction!
87
75
case payment_type . to_sym
88
76
when :ideal
89
- http = ConstipatedKoala ::Request . new ( ENV [ 'MOLLIE_DOMAIN' ] )
90
77
@status = status
91
78
92
- request = http . get ( "/#{ ENV [ 'MOLLIE_VERSION' ] } /payments/#{ trxid } " )
93
- request [ 'Authorization' ] = "Bearer #{ ENV [ 'MOLLIE_TOKEN' ] } "
94
-
95
- response = http . send! ( request )
79
+ payment = Mollie ::Payment . get ( trxid )
96
80
97
- status_update ( response . status )
81
+ status_update ( payment . status )
98
82
99
83
save!
100
84
@@ -161,13 +145,9 @@ def self.ideal_issuers
161
145
return [ ] if ENV [ 'MOLLIE_TOKEN' ] . blank?
162
146
163
147
Rails . cache . fetch ( 'mollie_issuers' , expires_in : 12 . hours ) do
164
- http = ConstipatedKoala :: Request . new ( ENV [ 'MOLLIE_DOMAIN' ] )
148
+ method = Mollie :: Method . get ( 'ideal' , include : 'issuers' )
165
149
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 ] }
150
+ method . issuers . map { |issuer | [ issuer [ "name" ] , issuer [ "id" ] ] }
171
151
end
172
152
end
173
153
@@ -179,12 +159,12 @@ def activities
179
159
180
160
def status_update ( new_status )
181
161
self . status = case new_status . downcase
182
- when "succeeded" , "paid"
183
- :successful
184
- when "expired" , "canceled" , "failed" , "cancelled" , "authorization_failed"
185
- :failed
186
- else
187
- :in_progress
188
- end
162
+ when "succeeded" , "paid"
163
+ :successful
164
+ when "expired" , "canceled" , "failed" , "cancelled" , "authorization_failed"
165
+ :failed
166
+ else
167
+ :in_progress
168
+ end
189
169
end
190
170
end
0 commit comments