mollie_pay 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +426 -0
- data/Rakefile +6 -0
- data/app/controllers/mollie_pay/application_controller.rb +4 -0
- data/app/controllers/mollie_pay/webhook_events_controller.rb +53 -0
- data/app/controllers/mollie_pay/webhooks_controller.rb +20 -0
- data/app/jobs/mollie_pay/application_job.rb +24 -0
- data/app/jobs/mollie_pay/delete_mollie_customer_job.rb +16 -0
- data/app/jobs/mollie_pay/notify_billable_job.rb +17 -0
- data/app/jobs/mollie_pay/process_webhook_event_job.rb +39 -0
- data/app/jobs/mollie_pay/process_webhook_job.rb +56 -0
- data/app/jobs/mollie_pay/refresh_pending_mandate_job.rb +22 -0
- data/app/jobs/mollie_pay/sync_chargebacks_job.rb +19 -0
- data/app/jobs/mollie_pay/sync_subscription_job.rb +18 -0
- data/app/jobs/mollie_pay/sync_subscriptions_job.rb +16 -0
- data/app/models/concerns/mollie_pay/has_uuid.rb +19 -0
- data/app/models/concerns/mollie_pay/syncable.rb +208 -0
- data/app/models/mollie_pay/application_record.rb +45 -0
- data/app/models/mollie_pay/billable.rb +445 -0
- data/app/models/mollie_pay/chargeback.rb +103 -0
- data/app/models/mollie_pay/customer.rb +32 -0
- data/app/models/mollie_pay/mandate.rb +113 -0
- data/app/models/mollie_pay/payment.rb +241 -0
- data/app/models/mollie_pay/refund.rb +61 -0
- data/app/models/mollie_pay/sales_invoice.rb +131 -0
- data/app/models/mollie_pay/subscription.rb +215 -0
- data/config/routes.rb +4 -0
- data/db/migrate/20260315142959_create_mollie_pay_customers.rb +9 -0
- data/db/migrate/20260315143008_create_mollie_pay_mandates.rb +13 -0
- data/db/migrate/20260315143014_create_mollie_pay_subscriptions.rb +15 -0
- data/db/migrate/20260315143020_create_mollie_pay_payments.rb +20 -0
- data/db/migrate/20260315143026_create_mollie_pay_refunds.rb +14 -0
- data/db/migrate/20260315143032_create_mollie_pay_webhook_events.rb +12 -0
- data/db/migrate/20260315143040_add_status_indexes_to_mollie_pay.rb +8 -0
- data/db/migrate/20260315143050_add_checkout_url_to_mollie_pay_payments.rb +5 -0
- data/db/migrate/20260316120000_make_mollie_id_nullable_on_mollie_pay_payments.rb +5 -0
- data/db/migrate/20260317100000_make_webhook_event_mollie_id_unique.rb +6 -0
- data/db/migrate/20260317194500_add_authorized_at_to_mollie_pay_payments.rb +5 -0
- data/db/migrate/20260317194501_add_amount_tracking_to_mollie_pay_payments.rb +9 -0
- data/db/migrate/20260318100000_add_name_to_mollie_pay_subscriptions.rb +12 -0
- data/db/migrate/20260318110000_drop_mollie_pay_webhook_events.rb +19 -0
- data/db/migrate/20260321000001_create_mollie_pay_chargebacks.rb +15 -0
- data/db/migrate/20260326000002_create_mollie_pay_sales_invoices.rb +19 -0
- data/db/migrate/20261115000001_change_mollie_pay_money_to_decimal.rb +38 -0
- data/db/migrate/20261202000001_add_sync_version_to_mollie_pay_tables.rb +13 -0
- data/db/migrate/20261202000002_add_created_at_mollie_to_mollie_pay_payments.rb +5 -0
- data/db/migrate/20261203000001_add_mandate_to_mollie_pay_payments.rb +6 -0
- data/docs/api.md +359 -0
- data/docs/mandates.md +340 -0
- data/docs/request-signing.md +178 -0
- data/docs/testing.md +177 -0
- data/docs/tutorial.md +1340 -0
- data/docs/webhooks.md +231 -0
- data/lib/generators/mollie_pay/install/install_generator.rb +44 -0
- data/lib/generators/mollie_pay/install/templates/initializer.rb.tt +12 -0
- data/lib/mollie/sales_invoice.rb +124 -0
- data/lib/mollie_pay/configuration.rb +101 -0
- data/lib/mollie_pay/engine.rb +31 -0
- data/lib/mollie_pay/errors.rb +12 -0
- data/lib/mollie_pay/request_signature.rb +160 -0
- data/lib/mollie_pay/test_fixtures/customer.json +36 -0
- data/lib/mollie_pay/test_fixtures/payment.json +40 -0
- data/lib/mollie_pay/test_fixtures/payment_billink.json +79 -0
- data/lib/mollie_pay/test_fixtures/payment_with_chargebacks.json +72 -0
- data/lib/mollie_pay/test_fixtures/refund.json +32 -0
- data/lib/mollie_pay/test_fixtures/sales_invoice.json +72 -0
- data/lib/mollie_pay/test_fixtures/subscription.json +48 -0
- data/lib/mollie_pay/test_helper.rb +576 -0
- data/lib/mollie_pay/version.rb +3 -0
- data/lib/mollie_pay/webhook_signature.rb +21 -0
- data/lib/mollie_pay.rb +182 -0
- data/lib/tasks/mollie_pay_tasks.rake +43 -0
- metadata +148 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module MolliePay
|
|
4
|
+
module TestHelper
|
|
5
|
+
MOLLIE_API_BASE = "https://api.mollie.com/v2".freeze
|
|
6
|
+
FIXTURES_PATH = File.expand_path("test_fixtures", __dir__).freeze
|
|
7
|
+
|
|
8
|
+
# ── Method-level stubs ─────────────────────────────────────────────
|
|
9
|
+
#
|
|
10
|
+
# Fast, simple stubs that bypass HTTP entirely. Use these for unit
|
|
11
|
+
# tests where you just need MolliePay methods to return a record.
|
|
12
|
+
|
|
13
|
+
# Stub Mollie::Payment.create and yield.
|
|
14
|
+
#
|
|
15
|
+
# stub_mollie_payment_create do
|
|
16
|
+
# payment = @user.mollie_pay_once(amount: 5000, description: "Test")
|
|
17
|
+
# assert_equal "open", payment.status
|
|
18
|
+
# assert payment.checkout_url.present?
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# With method and metadata:
|
|
22
|
+
#
|
|
23
|
+
# stub_mollie_payment_create do
|
|
24
|
+
# payment = @user.mollie_pay_first(
|
|
25
|
+
# amount: 1000, description: "Setup",
|
|
26
|
+
# redirect_url: billing_url,
|
|
27
|
+
# method: "ideal",
|
|
28
|
+
# metadata: { plan: "yearly" }
|
|
29
|
+
# )
|
|
30
|
+
# end
|
|
31
|
+
#
|
|
32
|
+
def stub_mollie_payment_create(**overrides, &block)
|
|
33
|
+
response = fake_mollie_payment(**overrides)
|
|
34
|
+
Mollie::Payment.stub(:create, response, &block)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Stub Mollie::Customer.create and Mollie::Payment.create together.
|
|
38
|
+
# Use this when testing a billable that has no Mollie customer yet.
|
|
39
|
+
#
|
|
40
|
+
# stub_mollie_customer_and_payment_create do
|
|
41
|
+
# payment = new_user.mollie_pay_once(amount: 1000, description: "First")
|
|
42
|
+
# assert new_user.reload.mollie_customer.present?
|
|
43
|
+
# end
|
|
44
|
+
#
|
|
45
|
+
def stub_mollie_customer_and_payment_create(customer_overrides: {}, payment_overrides: {}, &block)
|
|
46
|
+
customer_response = fake_mollie_customer(**customer_overrides)
|
|
47
|
+
payment_response = fake_mollie_payment(**payment_overrides)
|
|
48
|
+
|
|
49
|
+
Mollie::Customer.stub(:create, customer_response) do
|
|
50
|
+
Mollie::Payment.stub(:create, payment_response, &block)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Stub Mollie::Customer::Subscription.create.
|
|
55
|
+
#
|
|
56
|
+
# Note: mollie_subscribe has an idempotency guard — it returns an existing
|
|
57
|
+
# pending/active subscription without hitting the Mollie API. Cancel or
|
|
58
|
+
# remove existing subscriptions in your test setup if you need to test
|
|
59
|
+
# subscription creation.
|
|
60
|
+
#
|
|
61
|
+
# stub_mollie_subscription_create do
|
|
62
|
+
# subscription = @user.mollie_subscribe(amount: 2500, interval: "1 month", description: "Monthly")
|
|
63
|
+
# assert_equal "active", subscription.status
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# With start_date (for credit card first payments):
|
|
67
|
+
#
|
|
68
|
+
# stub_mollie_subscription_create do
|
|
69
|
+
# subscription = @user.mollie_subscribe(
|
|
70
|
+
# amount: 2500, interval: "1 month",
|
|
71
|
+
# description: "Monthly", start_date: Date.today + 1.month
|
|
72
|
+
# )
|
|
73
|
+
# end
|
|
74
|
+
#
|
|
75
|
+
def stub_mollie_subscription_create(**overrides, &block)
|
|
76
|
+
response = fake_mollie_subscription(**overrides)
|
|
77
|
+
Mollie::Customer::Subscription.stub(:create, response, &block)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Stub Mollie::Customer::Subscription.cancel.
|
|
81
|
+
#
|
|
82
|
+
# stub_mollie_subscription_cancel do
|
|
83
|
+
# @user.mollie_cancel_subscription
|
|
84
|
+
# end
|
|
85
|
+
#
|
|
86
|
+
def stub_mollie_subscription_cancel(&block)
|
|
87
|
+
Mollie::Customer::Subscription.stub(:cancel, nil, &block)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Stub Mollie::Customer::Subscription.update.
|
|
91
|
+
#
|
|
92
|
+
# stub_mollie_subscription_update do
|
|
93
|
+
# @user.mollie_swap_subscription(amount: 4999)
|
|
94
|
+
# end
|
|
95
|
+
#
|
|
96
|
+
def stub_mollie_subscription_update(**overrides, &block)
|
|
97
|
+
response = fake_mollie_subscription(**overrides)
|
|
98
|
+
Mollie::Customer::Subscription.stub(:update, response, &block)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Stub Mollie::Customer.update.
|
|
102
|
+
#
|
|
103
|
+
# stub_mollie_customer_update do
|
|
104
|
+
# @org.mollie_update_customer(name: "New Name")
|
|
105
|
+
# end
|
|
106
|
+
#
|
|
107
|
+
def stub_mollie_customer_update(&block)
|
|
108
|
+
Mollie::Customer.stub(:update, OpenStruct.new, &block)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Stub Mollie::Customer.delete.
|
|
112
|
+
#
|
|
113
|
+
# stub_mollie_customer_delete do
|
|
114
|
+
# @org.mollie_delete_customer
|
|
115
|
+
# end
|
|
116
|
+
#
|
|
117
|
+
def stub_mollie_customer_delete(&block)
|
|
118
|
+
Mollie::Customer.stub(:delete, nil, &block)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Stub Mollie::Customer::Mandate.create.
|
|
122
|
+
#
|
|
123
|
+
# stub_mollie_mandate_create do
|
|
124
|
+
# mandate = @user.mollie_create_mandate(
|
|
125
|
+
# method: "directdebit", consumer_name: "John Doe",
|
|
126
|
+
# consumer_account: "NL55INGB0000000000"
|
|
127
|
+
# )
|
|
128
|
+
# assert_equal "valid", mandate.status
|
|
129
|
+
# end
|
|
130
|
+
#
|
|
131
|
+
def stub_mollie_mandate_create(**overrides, &block)
|
|
132
|
+
response = fake_mollie_mandate(**overrides)
|
|
133
|
+
Mollie::Customer::Mandate.stub(:create, response, &block)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Stub Mollie::Customer::Mandate.delete.
|
|
137
|
+
#
|
|
138
|
+
# stub_mollie_mandate_revoke do
|
|
139
|
+
# @user.mollie_revoke_mandate(mandate)
|
|
140
|
+
# end
|
|
141
|
+
#
|
|
142
|
+
def stub_mollie_mandate_revoke(&block)
|
|
143
|
+
Mollie::Customer::Mandate.stub(:delete, nil, &block)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Stub Mollie::Payment::Refund.create.
|
|
147
|
+
#
|
|
148
|
+
# stub_mollie_refund_create do
|
|
149
|
+
# refund = @user.mollie_refund(payment)
|
|
150
|
+
# assert_equal "queued", refund.status
|
|
151
|
+
# end
|
|
152
|
+
#
|
|
153
|
+
def stub_mollie_refund_create(**overrides, &block)
|
|
154
|
+
response = fake_mollie_refund(**overrides)
|
|
155
|
+
Mollie::Payment::Refund.stub(:create, response, &block)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Stub Mollie::Payment.get (returning an authorized payment) and
|
|
159
|
+
# Mollie::Payment::Capture.create together. Note: Mollie::Payment.get is
|
|
160
|
+
# overridden for the whole block.
|
|
161
|
+
#
|
|
162
|
+
# stub_mollie_capture_create do
|
|
163
|
+
# capture = @user.mollie_capture(payment)
|
|
164
|
+
# assert capture.id.start_with?("cpt_")
|
|
165
|
+
# end
|
|
166
|
+
#
|
|
167
|
+
# Pass `status:` to test the not-authorized guard:
|
|
168
|
+
#
|
|
169
|
+
# stub_mollie_capture_create(status: "open") do
|
|
170
|
+
# assert_raises(MolliePay::PaymentNotAuthorized) { @user.mollie_capture(payment) }
|
|
171
|
+
# end
|
|
172
|
+
#
|
|
173
|
+
def stub_mollie_capture_create(status: "authorized", **overrides, &block)
|
|
174
|
+
response = fake_mollie_capture(**overrides)
|
|
175
|
+
Mollie::Payment.stub(:get, fake_mollie_authorizable_payment(status: status)) do
|
|
176
|
+
Mollie::Payment::Capture.stub(:create, response, &block)
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Stub Mollie::Payment.get with a payment whose release_authorization
|
|
181
|
+
# succeeds. Note: Mollie::Payment.get is overridden for the whole block.
|
|
182
|
+
#
|
|
183
|
+
# stub_mollie_payment_release_authorization do
|
|
184
|
+
# @user.mollie_release_authorization(payment)
|
|
185
|
+
# end
|
|
186
|
+
#
|
|
187
|
+
# Pass `status:` to test the not-authorized guard:
|
|
188
|
+
#
|
|
189
|
+
# stub_mollie_payment_release_authorization(status: "paid") do
|
|
190
|
+
# assert_raises(MolliePay::PaymentNotAuthorized) { @user.mollie_release_authorization(payment) }
|
|
191
|
+
# end
|
|
192
|
+
#
|
|
193
|
+
def stub_mollie_payment_release_authorization(status: "authorized", &block)
|
|
194
|
+
Mollie::Payment.stub(:get, fake_mollie_authorizable_payment(status: status), &block)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# ── Next-Gen Webhook Event helpers ──────────────────────────────
|
|
198
|
+
#
|
|
199
|
+
# Post a signed next-gen webhook event to the engine's webhook_events
|
|
200
|
+
# endpoint. Generates a valid HMAC-SHA256 signature using the provided
|
|
201
|
+
# secret (or the test default).
|
|
202
|
+
#
|
|
203
|
+
# MolliePay.configuration.webhook_signing_secret = WEBHOOK_TEST_SECRET
|
|
204
|
+
# post_signed_webhook_event(event_type: "sales-invoice.paid", entity_id: "invoice_xxx")
|
|
205
|
+
# assert_response :ok
|
|
206
|
+
#
|
|
207
|
+
# Pass `secret: nil` to omit the signature header (for testing unsigned events).
|
|
208
|
+
#
|
|
209
|
+
WEBHOOK_TEST_SECRET = "test_whsec_000000000000000000000000".freeze
|
|
210
|
+
|
|
211
|
+
def post_signed_webhook_event(event_type:, entity_id:, secret: WEBHOOK_TEST_SECRET)
|
|
212
|
+
payload = {
|
|
213
|
+
resource: "event",
|
|
214
|
+
id: "whe_test#{SecureRandom.hex(8)}",
|
|
215
|
+
type: event_type,
|
|
216
|
+
entityId: entity_id,
|
|
217
|
+
createdAt: Time.current.iso8601
|
|
218
|
+
}.to_json
|
|
219
|
+
|
|
220
|
+
headers = { "CONTENT_TYPE" => "application/json" }
|
|
221
|
+
if secret
|
|
222
|
+
signature = "sha256=#{OpenSSL::HMAC.hexdigest('SHA256', secret, payload)}"
|
|
223
|
+
headers["HTTP_X_MOLLIE_SIGNATURE"] = signature
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
post mollie_pay.webhook_events_path, params: payload, headers: headers
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Build a fake Mollie payment response (OpenStruct).
|
|
230
|
+
def fake_mollie_payment(id: nil, status: "open", checkout_url: nil, created_at: nil, mandate_id: nil)
|
|
231
|
+
id ||= "tr_test#{SecureRandom.hex(4)}"
|
|
232
|
+
checkout_url ||= "https://www.mollie.com/payscreen/select-method/#{id}"
|
|
233
|
+
|
|
234
|
+
OpenStruct.new(id: id, status: status, checkout_url: checkout_url, created_at: created_at, mandate_id: mandate_id)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# Build a fake Mollie customer response (OpenStruct).
|
|
238
|
+
def fake_mollie_customer(id: nil)
|
|
239
|
+
id ||= "cst_test#{SecureRandom.hex(4)}"
|
|
240
|
+
OpenStruct.new(id: id)
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Build a fake Mollie mandate response (OpenStruct).
|
|
244
|
+
def fake_mollie_mandate(id: nil, status: "valid", method: "directdebit")
|
|
245
|
+
id ||= "mdt_test#{SecureRandom.hex(4)}"
|
|
246
|
+
OpenStruct.new(id: id, status: status, method: method)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Build a fake Mollie subscription response (OpenStruct).
|
|
250
|
+
def fake_mollie_subscription(id: nil, status: "active")
|
|
251
|
+
id ||= "sub_test#{SecureRandom.hex(4)}"
|
|
252
|
+
OpenStruct.new(id: id, status: status)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Build a fake Mollie capture response (OpenStruct). The SDK capture object
|
|
256
|
+
# exposes id, amount and payment_id.
|
|
257
|
+
def fake_mollie_capture(id: nil, payment_id: nil, amount: nil)
|
|
258
|
+
id ||= "cpt_test#{SecureRandom.hex(4)}"
|
|
259
|
+
OpenStruct.new(id: id, payment_id: payment_id, amount: amount)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# Build a fake Mollie payment that answers authorized? and accepts a
|
|
263
|
+
# release_authorization call. Plain Object because OpenStruct rejects
|
|
264
|
+
# arguments on release_authorization(idempotency_key:).
|
|
265
|
+
def fake_mollie_authorizable_payment(status: "authorized")
|
|
266
|
+
payment = Object.new
|
|
267
|
+
payment.define_singleton_method(:status) { status }
|
|
268
|
+
payment.define_singleton_method(:authorized?) { status == "authorized" }
|
|
269
|
+
payment.define_singleton_method(:release_authorization) { |_options = {}| true }
|
|
270
|
+
payment
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Build a fake Mollie refund response (OpenStruct).
|
|
274
|
+
def fake_mollie_refund(id: nil, status: "queued")
|
|
275
|
+
id ||= "re_test#{SecureRandom.hex(4)}"
|
|
276
|
+
OpenStruct.new(id: id, status: status)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Build a fake Mollie sales invoice response (OpenStruct).
|
|
280
|
+
# Simple version for stub helpers.
|
|
281
|
+
def fake_mollie_sales_invoice(id: nil, status: "draft")
|
|
282
|
+
id ||= "invoice_test#{SecureRandom.hex(4)}"
|
|
283
|
+
OpenStruct.new(
|
|
284
|
+
id: id,
|
|
285
|
+
status: status,
|
|
286
|
+
invoice_number: nil,
|
|
287
|
+
recipient_identifier: nil
|
|
288
|
+
)
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Build a fake Mollie sales invoice response with all fields needed
|
|
292
|
+
# for SalesInvoice.record_from_mollie. Use this when testing Billable
|
|
293
|
+
# methods that persist local records.
|
|
294
|
+
def fake_mollie_sales_invoice_response(id: nil, status: "draft")
|
|
295
|
+
id ||= "invoice_test#{SecureRandom.hex(4)}"
|
|
296
|
+
OpenStruct.new(
|
|
297
|
+
id: id,
|
|
298
|
+
status: status,
|
|
299
|
+
invoice_number: status == "issued" ? "INV-0000001" : nil,
|
|
300
|
+
total_amount: OpenStruct.new(value: BigDecimal("107.69"), currency: "EUR"),
|
|
301
|
+
recipient_identifier: "cust-test",
|
|
302
|
+
memo: "Test invoice",
|
|
303
|
+
issued_at: status == "issued" ? Time.current : nil,
|
|
304
|
+
paid_at: status == "paid" ? Time.current : nil,
|
|
305
|
+
due_at: 30.days.from_now
|
|
306
|
+
)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Stub Mollie::SalesInvoice.create.
|
|
310
|
+
#
|
|
311
|
+
# stub_mollie_sales_invoice_create do
|
|
312
|
+
# invoice = MolliePay.create_sales_invoice(
|
|
313
|
+
# status: "draft",
|
|
314
|
+
# recipient: { type: "consumer", given_name: "Jane" },
|
|
315
|
+
# lines: [{ description: "Item", quantity: 1, vat_rate: "21.00", unit_price: 1000 }]
|
|
316
|
+
# )
|
|
317
|
+
# assert_equal "draft", invoice.status
|
|
318
|
+
# end
|
|
319
|
+
#
|
|
320
|
+
def stub_mollie_sales_invoice_create(**overrides, &block)
|
|
321
|
+
response = fake_mollie_sales_invoice(**overrides)
|
|
322
|
+
Mollie::SalesInvoice.stub(:create, response, &block)
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# Stub Mollie::SalesInvoice.get.
|
|
326
|
+
#
|
|
327
|
+
# stub_mollie_sales_invoice_get do
|
|
328
|
+
# invoice = MolliePay.sales_invoice("invoice_abc123")
|
|
329
|
+
# assert_equal "issued", invoice.status
|
|
330
|
+
# end
|
|
331
|
+
#
|
|
332
|
+
def stub_mollie_sales_invoice_get(**overrides, &block)
|
|
333
|
+
response = fake_mollie_sales_invoice(**overrides)
|
|
334
|
+
Mollie::SalesInvoice.stub(:get, response, &block)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# ── WebMock-based API stubs ────────────────────────────────────────
|
|
338
|
+
#
|
|
339
|
+
# These stub the actual HTTP endpoints that the Mollie SDK calls.
|
|
340
|
+
# The full SDK parsing pipeline runs — JSON is deserialized into real
|
|
341
|
+
# Mollie::Payment, Mollie::Customer, etc. objects. Use these for
|
|
342
|
+
# integration tests or when you want higher-fidelity stubbing.
|
|
343
|
+
#
|
|
344
|
+
# Requires `require "webmock/minitest"` in your test_helper.rb.
|
|
345
|
+
|
|
346
|
+
# Stub POST /v2/payments to return a realistic Mollie API response.
|
|
347
|
+
# The Mollie SDK parses the JSON into a real Mollie::Payment object.
|
|
348
|
+
#
|
|
349
|
+
# webmock_mollie_payment_create do
|
|
350
|
+
# payment = @user.mollie_pay_once(amount: 5000, description: "Test")
|
|
351
|
+
# assert_equal "open", payment.status
|
|
352
|
+
# assert_equal "https://www.mollie.com/payscreen/select-method/test1234AB", payment.checkout_url
|
|
353
|
+
# end
|
|
354
|
+
#
|
|
355
|
+
# Pass overrides to merge into the JSON response:
|
|
356
|
+
#
|
|
357
|
+
# webmock_mollie_payment_create(id: "tr_custom", status: "paid") do
|
|
358
|
+
# ...
|
|
359
|
+
# end
|
|
360
|
+
#
|
|
361
|
+
def webmock_mollie_payment_create(**overrides)
|
|
362
|
+
body = mollie_fixture("payment", **overrides)
|
|
363
|
+
stub_request(:post, "#{MOLLIE_API_BASE}/payments")
|
|
364
|
+
.to_return(status: 201, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
365
|
+
yield
|
|
366
|
+
ensure
|
|
367
|
+
WebMock.reset!
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# Stub POST /v2/customers and POST /v2/payments together.
|
|
371
|
+
#
|
|
372
|
+
# webmock_mollie_customer_and_payment_create do
|
|
373
|
+
# payment = new_user.mollie_pay_once(amount: 1000, description: "First")
|
|
374
|
+
# assert new_user.reload.mollie_customer.present?
|
|
375
|
+
# end
|
|
376
|
+
#
|
|
377
|
+
def webmock_mollie_customer_and_payment_create(customer_overrides: {}, payment_overrides: {})
|
|
378
|
+
customer_body = mollie_fixture("customer", **customer_overrides)
|
|
379
|
+
payment_body = mollie_fixture("payment", **payment_overrides)
|
|
380
|
+
|
|
381
|
+
stub_request(:post, "#{MOLLIE_API_BASE}/customers")
|
|
382
|
+
.to_return(status: 201, body: customer_body, headers: { "Content-Type" => "application/hal+json" })
|
|
383
|
+
stub_request(:post, "#{MOLLIE_API_BASE}/payments")
|
|
384
|
+
.to_return(status: 201, body: payment_body, headers: { "Content-Type" => "application/hal+json" })
|
|
385
|
+
yield
|
|
386
|
+
ensure
|
|
387
|
+
WebMock.reset!
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# Stub POST /v2/customers/:customer_id/subscriptions.
|
|
391
|
+
#
|
|
392
|
+
# webmock_mollie_subscription_create(customer_id: "cst_abc") do
|
|
393
|
+
# subscription = @user.mollie_subscribe(amount: 2500, interval: "1 month", description: "Monthly")
|
|
394
|
+
# assert_equal "active", subscription.status
|
|
395
|
+
# end
|
|
396
|
+
#
|
|
397
|
+
def webmock_mollie_subscription_create(customer_id: nil, **overrides)
|
|
398
|
+
body = mollie_fixture("subscription", **overrides)
|
|
399
|
+
url_pattern = if customer_id
|
|
400
|
+
"#{MOLLIE_API_BASE}/customers/#{customer_id}/subscriptions"
|
|
401
|
+
else
|
|
402
|
+
%r{#{MOLLIE_API_BASE}/customers/cst_\w+/subscriptions}
|
|
403
|
+
end
|
|
404
|
+
stub_request(:post, url_pattern)
|
|
405
|
+
.to_return(status: 201, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
406
|
+
yield
|
|
407
|
+
ensure
|
|
408
|
+
WebMock.reset!
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# Stub DELETE /v2/customers/:customer_id/subscriptions/:id.
|
|
412
|
+
#
|
|
413
|
+
# webmock_mollie_subscription_cancel(customer_id: "cst_abc", subscription_id: "sub_xyz") do
|
|
414
|
+
# @user.mollie_cancel_subscription
|
|
415
|
+
# end
|
|
416
|
+
#
|
|
417
|
+
def webmock_mollie_subscription_cancel(customer_id: nil, subscription_id: "sub_test1234AB")
|
|
418
|
+
url_pattern = if customer_id
|
|
419
|
+
"#{MOLLIE_API_BASE}/customers/#{customer_id}/subscriptions/#{subscription_id}"
|
|
420
|
+
else
|
|
421
|
+
%r{#{MOLLIE_API_BASE}/customers/cst_\w+/subscriptions/#{subscription_id}}
|
|
422
|
+
end
|
|
423
|
+
stub_request(:delete, url_pattern)
|
|
424
|
+
.to_return(status: 204, body: "", headers: {})
|
|
425
|
+
yield
|
|
426
|
+
ensure
|
|
427
|
+
WebMock.reset!
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Stub POST /v2/payments/:payment_id/refunds.
|
|
431
|
+
#
|
|
432
|
+
# webmock_mollie_refund_create(payment_id: "tr_abc") do
|
|
433
|
+
# refund = @user.mollie_refund(payment)
|
|
434
|
+
# assert_equal "queued", refund.status
|
|
435
|
+
# end
|
|
436
|
+
#
|
|
437
|
+
def webmock_mollie_refund_create(payment_id: nil, **overrides)
|
|
438
|
+
body = mollie_fixture("refund", **overrides)
|
|
439
|
+
url_pattern = if payment_id
|
|
440
|
+
"#{MOLLIE_API_BASE}/payments/#{payment_id}/refunds"
|
|
441
|
+
else
|
|
442
|
+
%r{#{MOLLIE_API_BASE}/payments/tr_\w+/refunds}
|
|
443
|
+
end
|
|
444
|
+
stub_request(:post, url_pattern)
|
|
445
|
+
.to_return(status: 201, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
446
|
+
yield
|
|
447
|
+
ensure
|
|
448
|
+
WebMock.reset!
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# Stub GET /v2/payments/:id for webhook processing tests.
|
|
452
|
+
#
|
|
453
|
+
# webmock_mollie_payment_get("tr_abc123", status: "paid") do
|
|
454
|
+
# MolliePay::ProcessWebhookJob.perform_now(event.id)
|
|
455
|
+
# end
|
|
456
|
+
#
|
|
457
|
+
# Pass `fixture:` to load another payment fixture, such as the Billink
|
|
458
|
+
# (manual capture) payment with lines and a billing address:
|
|
459
|
+
#
|
|
460
|
+
# webmock_mollie_payment_get("tr_abc123", fixture: "payment_billink", status: "authorized") do
|
|
461
|
+
# MolliePay::ProcessWebhookJob.perform_now("tr_abc123")
|
|
462
|
+
# end
|
|
463
|
+
#
|
|
464
|
+
def webmock_mollie_payment_get(payment_id, fixture: "payment", **overrides)
|
|
465
|
+
body = mollie_fixture(fixture, id: payment_id, **overrides)
|
|
466
|
+
stub_request(:get, "#{MOLLIE_API_BASE}/payments/#{payment_id}")
|
|
467
|
+
.to_return(status: 200, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
468
|
+
yield
|
|
469
|
+
ensure
|
|
470
|
+
WebMock.reset!
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
# Stub POST /v2/sales-invoices.
|
|
474
|
+
#
|
|
475
|
+
# webmock_mollie_sales_invoice_create do
|
|
476
|
+
# invoice = MolliePay.create_sales_invoice(...)
|
|
477
|
+
# end
|
|
478
|
+
#
|
|
479
|
+
def webmock_mollie_sales_invoice_create(**overrides)
|
|
480
|
+
body = mollie_fixture("sales_invoice", **overrides)
|
|
481
|
+
stub_request(:post, "#{MOLLIE_API_BASE}/sales-invoices")
|
|
482
|
+
.to_return(status: 201, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
483
|
+
yield
|
|
484
|
+
ensure
|
|
485
|
+
WebMock.reset!
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
# Stub GET /v2/sales-invoices/:id.
|
|
489
|
+
#
|
|
490
|
+
# webmock_mollie_sales_invoice_get("invoice_abc123") do
|
|
491
|
+
# invoice = MolliePay.sales_invoice("invoice_abc123")
|
|
492
|
+
# end
|
|
493
|
+
#
|
|
494
|
+
def webmock_mollie_sales_invoice_get(invoice_id, **overrides)
|
|
495
|
+
body = mollie_fixture("sales_invoice", id: invoice_id, **overrides)
|
|
496
|
+
stub_request(:get, "#{MOLLIE_API_BASE}/sales-invoices/#{invoice_id}")
|
|
497
|
+
.to_return(status: 200, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
498
|
+
yield
|
|
499
|
+
ensure
|
|
500
|
+
WebMock.reset!
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
# Stub GET /v2/payments/:id with embedded chargebacks for chargeback
|
|
504
|
+
# detection tests. Uses the payment_with_chargebacks fixture which
|
|
505
|
+
# includes _embedded.chargebacks matching the Mollie API structure.
|
|
506
|
+
#
|
|
507
|
+
# webmock_mollie_payment_get_with_chargebacks("tr_abc123") do
|
|
508
|
+
# MolliePay::ProcessWebhookJob.perform_now("tr_abc123")
|
|
509
|
+
# end
|
|
510
|
+
#
|
|
511
|
+
def webmock_mollie_payment_get_with_chargebacks(payment_id, **overrides)
|
|
512
|
+
body = mollie_fixture("payment_with_chargebacks", id: payment_id, **overrides)
|
|
513
|
+
stub_request(:get, "#{MOLLIE_API_BASE}/payments/#{payment_id}")
|
|
514
|
+
.to_return(status: 200, body: body, headers: { "Content-Type" => "application/hal+json" })
|
|
515
|
+
yield
|
|
516
|
+
ensure
|
|
517
|
+
WebMock.reset!
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
private
|
|
521
|
+
|
|
522
|
+
# Load a JSON fixture file and merge in overrides.
|
|
523
|
+
# Top-level keys are replaced directly. Nested "amount" is handled
|
|
524
|
+
# specially so you can pass `amount_value: "50.00"`.
|
|
525
|
+
def mollie_fixture(name, **overrides)
|
|
526
|
+
json = JSON.parse(File.read(File.join(FIXTURES_PATH, "#{name}.json")))
|
|
527
|
+
explicit_links = overrides.key?(:_links)
|
|
528
|
+
|
|
529
|
+
# Handle amount shorthand: amount_value and amount_currency
|
|
530
|
+
if overrides.key?(:amount_value) || overrides.key?(:amount_currency)
|
|
531
|
+
json["amount"] ||= {}
|
|
532
|
+
json["amount"]["value"] = overrides.delete(:amount_value) if overrides.key?(:amount_value)
|
|
533
|
+
json["amount"]["currency"] = overrides.delete(:amount_currency) if overrides.key?(:amount_currency)
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
# Handle checkout_url shorthand — update the _links.checkout.href
|
|
537
|
+
if overrides.key?(:checkout_url)
|
|
538
|
+
checkout_url = overrides.delete(:checkout_url)
|
|
539
|
+
json["_links"] ||= {}
|
|
540
|
+
json["_links"]["checkout"] = { "href" => checkout_url, "type" => "text/html" }
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
# Merge remaining overrides as camelCase keys (Mollie API format)
|
|
544
|
+
overrides.each do |key, value|
|
|
545
|
+
json[camelize(key.to_s)] = value
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
rewrite_subscription_links!(json) if name.to_s == "subscription" && !explicit_links
|
|
549
|
+
|
|
550
|
+
json.to_json
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
def rewrite_subscription_links!(json)
|
|
554
|
+
links = json["_links"]
|
|
555
|
+
return unless links.is_a?(Hash)
|
|
556
|
+
|
|
557
|
+
customer_id = json["customerId"]
|
|
558
|
+
subscription_id = json["id"]
|
|
559
|
+
return unless customer_id.present? && subscription_id.present?
|
|
560
|
+
|
|
561
|
+
customer_url = "#{MOLLIE_API_BASE}/customers/#{customer_id}"
|
|
562
|
+
subscription_url = "#{customer_url}/subscriptions/#{subscription_id}"
|
|
563
|
+
links["self"]["href"] = subscription_url if links["self"].is_a?(Hash)
|
|
564
|
+
links["customer"]["href"] = customer_url if links["customer"].is_a?(Hash)
|
|
565
|
+
links["payments"]["href"] = "#{subscription_url}/payments" if links["payments"].is_a?(Hash)
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
def camelize(snake_str)
|
|
569
|
+
prefix = snake_str[/\A_+/].to_s
|
|
570
|
+
parts = snake_str.delete_prefix(prefix).split("_")
|
|
571
|
+
return prefix if parts.empty?
|
|
572
|
+
|
|
573
|
+
prefix + parts[0] + parts[1..].map(&:capitalize).join
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "openssl"
|
|
2
|
+
|
|
3
|
+
module MolliePay
|
|
4
|
+
module WebhookSignature
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def verify!(payload, signature_header, secrets)
|
|
8
|
+
raise MolliePay::InvalidSignature, "Missing signature" if signature_header.blank?
|
|
9
|
+
raise MolliePay::InvalidSignature, "Invalid signature format" unless signature_header.start_with?("sha256=")
|
|
10
|
+
|
|
11
|
+
provided = signature_header.delete_prefix("sha256=")
|
|
12
|
+
|
|
13
|
+
verified = Array(secrets).any? do |secret|
|
|
14
|
+
calculated = OpenSSL::HMAC.hexdigest("SHA256", secret, payload)
|
|
15
|
+
ActiveSupport::SecurityUtils.secure_compare(calculated, provided)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
raise MolliePay::InvalidSignature, "Invalid signature" unless verified
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|