Skip to content

Commit 90a324b

Browse files
committed
feat: update to mollie v3 api
1 parent f5f1c1d commit 90a324b

File tree

6 files changed

+47
-49
lines changed

6 files changed

+47
-49
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ gem 'impressionist', github: 'charlotte-ruby/impressionist'
2727
# rests calls for mailgun
2828
gem 'rest-client'
2929

30+
# mollie
31+
gem 'mollie-api-ruby'
32+
3033
gem 'responders'
3134

3235
# pagination

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ GEM
179179
mini_mime (1.1.2)
180180
mini_portile2 (2.7.1)
181181
minitest (5.15.0)
182+
mollie-api-ruby (4.13.0)
182183
netrc (0.11.0)
183184
nio4r (2.5.8)
184185
nokogiri (1.13.1)
@@ -352,6 +353,7 @@ DEPENDENCIES
352353
impressionist!
353354
listen
354355
mimemagic (= 0.3.9)
356+
mollie-api-ruby
355357
pagy
356358
pg
357359
pg_search

app/models/payment.rb

+27-47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#:nodoc:
22
class Payment < ApplicationRecord
3-
require 'request'
4-
53
self.primary_key = :token
64

75
attr_accessor :issuer, :payment_uri, :message
@@ -12,6 +10,7 @@ class Payment < ApplicationRecord
1210
validates :payment_type, presence: true
1311

1412
enum status: { failed: 0, in_progress: 1, successful: 2 }
13+
1514
# Keep payconiq_online because it is still present in the database
1615
enum payment_type: { ideal: 0, payconiq_online: 1, pin: 3 }
1716
enum transaction_type: { checkout: 0, activity: 1 }
@@ -26,6 +25,7 @@ class Payment < ApplicationRecord
2625
after_validation :request_payment, on: :create
2726

2827
include PgSearch::Model
28+
2929
pg_search_scope :search_by_name,
3030
against: [:trxid],
3131
associated_against: {
@@ -47,54 +47,38 @@ def request_payment
4747

4848
case payment_type.to_sym
4949
when :ideal
50-
http = ConstipatedKoala::Request.new(ENV['MOLLIE_DOMAIN'])
5150
self.token = Digest::SHA256.hexdigest("#{ member.id }#{ Time.now.to_f }#{ redirect_uri }")
5251

5352
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
7457

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+
)
7764

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']
8067
self.status = :in_progress
81-
# pin payment shouldn't have any extra work
68+
69+
# pin payment shouldn't have any extra work
8270
when :pin
8371
end
8472
end
8573

8674
def update_transaction!
8775
case payment_type.to_sym
8876
when :ideal
89-
http = ConstipatedKoala::Request.new(ENV['MOLLIE_DOMAIN'])
9077
@status = status
9178

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)
9680

97-
status_update(response.status)
81+
status_update(payment.status)
9882

9983
save!
10084

@@ -161,13 +145,9 @@ def self.ideal_issuers
161145
return [] if ENV['MOLLIE_TOKEN'].blank?
162146

163147
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')
165149

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"]] }
171151
end
172152
end
173153

@@ -179,12 +159,12 @@ def activities
179159

180160
def status_update(new_status)
181161
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
189169
end
190170
end

config/application.rb

+5
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@ class Application < Rails::Application
7575
config.middleware.use(I18n::JS::Middleware)
7676
end
7777
end
78+
79+
# Mollie configuration
80+
Mollie::Client.configure do |config|
81+
config.api_key = ENV['MOLLIE_TOKEN']
82+
end

gemset.nix

+10
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,16 @@
713713
};
714714
version = "5.15.0";
715715
};
716+
mollie-api-ruby = {
717+
groups = ["default"];
718+
platforms = [];
719+
source = {
720+
remotes = ["https://rubygems.org"];
721+
sha256 = "0z4z0cf5lq3bmdbjsdzjj2spvg351b32nzwrn9rf3zqm9rldai6w";
722+
type = "gem";
723+
};
724+
version = "4.13.0";
725+
};
716726
netrc = {
717727
groups = ["default"];
718728
platforms = [];

sample.env

-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ MAILCHIMP_TEACHER_ID=
7676
NGROK_HOST=http
7777

7878
# MOLLIE credentials for the iDEAL integration.
79-
MOLLIE_DOMAIN=https://api.mollie.nl
80-
MOLLIE_VERSION=v1
8179
MOLLIE_TOKEN=
8280

8381
# Secret for error reporting.

0 commit comments

Comments
 (0)