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
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2819570726154747ea42dd13019c101a66de6f0ac43d2939da0d7903a8e0d823
|
|
4
|
+
data.tar.gz: f3104b14b903e3d15a54e5b5b367041055597335833294829e6b820463e72da1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e372b5f8367d929e08db771fed707a0e9467a2de5628ff19b245de84a042f2a1d0aed63629d55907d77a7be65e04da8d1da2e61ad3502ceca01a10796f2a267b
|
|
7
|
+
data.tar.gz: 8ef5f6688bb02f712801578e1ab4f6d5741b7798bcf7cca6f7141695dcea4c36d7f8f17943097f4acfc90bb98524bc3f2fa5668ac15fb6038bce2d0d5c34a2a8
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright Peter Berkenbosch
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
# MolliePay
|
|
2
|
+
|
|
3
|
+
A mountable Rails engine for Mollie payments in SaaS applications. Handles
|
|
4
|
+
customers, mandates, subscriptions, one-off payments, refunds, chargebacks
|
|
5
|
+
and webhooks. You write the business logic; MolliePay handles the Mollie
|
|
6
|
+
plumbing.
|
|
7
|
+
|
|
8
|
+
Rails 8.1+, Mollie-native.
|
|
9
|
+
|
|
10
|
+
## Philosophy
|
|
11
|
+
|
|
12
|
+
- **Headless.** No views, no UI opinions. One mounted webhook endpoint, nothing else.
|
|
13
|
+
- **Lean data model.** Only business-critical fields are stored locally. Everything else is fetched from Mollie on demand via `record.mollie_record`.
|
|
14
|
+
- **Hooks, not events.** Override plain Ruby methods in your model. No event bus, no pub/sub, no callbacks to register.
|
|
15
|
+
- **Boring Rails.** Models do the work. No service objects, no form objects, no interactors.
|
|
16
|
+
- **Idempotent.** Hooks fire only on actual state transitions. Duplicate webhooks are handled safely.
|
|
17
|
+
- **BigDecimal, never cents.** All amounts are `BigDecimal`, stored in `decimal(15,4)` columns. Conversion to Mollie's wire format happens only at the API boundary.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem "mollie_pay"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
bundle install
|
|
27
|
+
bin/rails generate mollie_pay:install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Mount the engine in `config/routes.rb`:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
mount MolliePay::Engine => "/mollie_pay"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Configure in `config/initializers/mollie_pay.rb`:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
MolliePay.configure do |config|
|
|
40
|
+
config.api_key = ENV["MOLLIE_API_KEY"]
|
|
41
|
+
config.host = ENV["MOLLIE_HOST"]
|
|
42
|
+
config.default_redirect_path = "/payments/:id"
|
|
43
|
+
config.currency = "EUR"
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Request signing
|
|
48
|
+
|
|
49
|
+
Mollie can require every API request to be signed with an Ed25519 key
|
|
50
|
+
([request signing](https://docs.mollie.com/reference/request-signing)). Once a
|
|
51
|
+
public key is registered on an API key, Mollie rejects unsigned requests made
|
|
52
|
+
with it with `401`.
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
bin/rails mollie_pay:request_signing:generate # prints the public key and the private key, once
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
config.request_signing_key = ENV["MOLLIE_REQUEST_SIGNING_KEY"]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Paste the public key into the API key in the Mollie dashboard, store the
|
|
63
|
+
private key as a secret, and check the result from the deployed app:
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
bin/rails mollie_pay:request_signing:verify
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
[docs/request-signing.md](docs/request-signing.md) says where to keep the
|
|
70
|
+
private key, what to do when it leaks or is lost, and how to rotate it.
|
|
71
|
+
|
|
72
|
+
## Setup
|
|
73
|
+
|
|
74
|
+
Include `MolliePay::Billable` in your paying model:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
class Organization < ApplicationRecord
|
|
78
|
+
include MolliePay::Billable
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Usage
|
|
83
|
+
|
|
84
|
+
### First payment (establishes mandate)
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
payment = current_organization.mollie_pay_first(
|
|
88
|
+
amount: BigDecimal("10.00"), description: "Activation fee"
|
|
89
|
+
)
|
|
90
|
+
redirect_to payment.checkout_url
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Subscribe (requires mandate)
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
current_organization.mollie_subscribe(
|
|
97
|
+
amount: BigDecimal("25.00"), interval: "1 month", description: "Monthly plan"
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Named subscriptions for multiple concurrent plans:
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
current_organization.mollie_subscribe(
|
|
105
|
+
amount: BigDecimal("10.00"), interval: "1 month", description: "Analytics",
|
|
106
|
+
name: "analytics_addon"
|
|
107
|
+
)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Pin a subscription to a specific valid mandate (for example, inside
|
|
111
|
+
`on_mollie_mandate_created(mandate)` when a customer has more than one valid
|
|
112
|
+
mandate):
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
current_organization.mollie_subscribe(
|
|
116
|
+
amount: BigDecimal("25.00"), interval: "1 month", description: "Monthly plan",
|
|
117
|
+
mandate: mandate
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### One-off payment
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
payment = current_organization.mollie_pay_once(
|
|
125
|
+
amount: BigDecimal("75.00"), description: "Extra service"
|
|
126
|
+
)
|
|
127
|
+
redirect_to payment.checkout_url
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Pay later with Billink (beta)
|
|
131
|
+
|
|
132
|
+
Billink is a buy-now-pay-later method for consumers in the Netherlands,
|
|
133
|
+
Belgium and Germany. It needs order lines, a billing address and manual
|
|
134
|
+
capture; the customer pays Billink within 14 days of capture.
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
payment = current_organization.mollie_pay_once(
|
|
138
|
+
amount: BigDecimal("121.00"), description: "Order 1001",
|
|
139
|
+
method: "billink", capture_mode: "manual", locale: "nl_NL",
|
|
140
|
+
billing_address: {
|
|
141
|
+
given_name: "Jan", family_name: "Jansen", street_and_number: "Keizersgracht 126",
|
|
142
|
+
postal_code: "1015 CW", city: "Amsterdam", country: "NL", email: "jan@example.com"
|
|
143
|
+
},
|
|
144
|
+
lines: [ {
|
|
145
|
+
description: "Widget", quantity: 1, unit_price: BigDecimal("121.00"),
|
|
146
|
+
total_amount: BigDecimal("121.00"), vat_rate: "21.00", vat_amount: BigDecimal("21.00")
|
|
147
|
+
} ]
|
|
148
|
+
)
|
|
149
|
+
redirect_to payment.checkout_url
|
|
150
|
+
|
|
151
|
+
# Later, once the payment is authorized and the order has shipped:
|
|
152
|
+
current_organization.mollie_capture(payment)
|
|
153
|
+
|
|
154
|
+
# Or, to cancel the order before shipping:
|
|
155
|
+
current_organization.mollie_release_authorization(payment)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
See [docs/api.md](docs/api.md#pay-later-with-billink) for the full lifecycle.
|
|
159
|
+
|
|
160
|
+
### Upgrade or downgrade
|
|
161
|
+
|
|
162
|
+
```ruby
|
|
163
|
+
current_organization.mollie_swap_subscription(amount: BigDecimal("49.99"))
|
|
164
|
+
current_organization.mollie_swap_subscription(amount: BigDecimal("49.99"), interval: "1 year")
|
|
165
|
+
current_organization.mollie_swap_subscription(name: "analytics_addon", amount: BigDecimal("19.99"))
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Changes take effect on the next billing cycle. See [docs/api.md](docs/api.md)
|
|
169
|
+
for proration guidance.
|
|
170
|
+
|
|
171
|
+
### Mandates
|
|
172
|
+
|
|
173
|
+
Create SEPA Direct Debit mandates directly (for mandate migrations or
|
|
174
|
+
pre-existing bank relationships):
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
mandate = current_organization.mollie_create_mandate(
|
|
178
|
+
method: "directdebit",
|
|
179
|
+
consumer_name: "Jane Doe",
|
|
180
|
+
consumer_account: "NL55INGB0000000000",
|
|
181
|
+
signature_date: Date.today
|
|
182
|
+
)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
> **Important:** Direct mandate creation requires prior customer authorization.
|
|
186
|
+
> See [docs/mandates.md](docs/mandates.md) for SEPA compliance requirements.
|
|
187
|
+
> For most applications, use `mollie_pay_first` instead — Mollie's checkout
|
|
188
|
+
> handles consent and bank authentication automatically.
|
|
189
|
+
|
|
190
|
+
Revoke a mandate:
|
|
191
|
+
|
|
192
|
+
```ruby
|
|
193
|
+
current_organization.mollie_revoke_mandate(mandate)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Customer management
|
|
197
|
+
|
|
198
|
+
```ruby
|
|
199
|
+
current_organization.mollie_update_customer(name: "New Name", email: "new@example.com")
|
|
200
|
+
current_organization.mollie_delete_customer # cascades to all local records
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Cancel, update, and refund
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
current_organization.mollie_cancel_subscription
|
|
207
|
+
current_organization.mollie_cancel_payment(payment)
|
|
208
|
+
current_organization.mollie_update_payment(payment, description: "Updated")
|
|
209
|
+
current_organization.mollie_refund(payment) # full
|
|
210
|
+
current_organization.mollie_refund(payment, amount: BigDecimal("5.00")) # partial
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Payment methods
|
|
214
|
+
|
|
215
|
+
```ruby
|
|
216
|
+
MolliePay.payment_methods # all enabled methods
|
|
217
|
+
MolliePay.payment_methods(amount: BigDecimal("10.00")) # filtered by amount
|
|
218
|
+
MolliePay.payment_method("ideal") # single method details
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Sales Invoices (beta)
|
|
222
|
+
|
|
223
|
+
Create, retrieve, and manage sales invoices through Mollie's Sales Invoices API.
|
|
224
|
+
`mollie_create_sales_invoice` creates the invoice at Mollie and stores a local
|
|
225
|
+
`MolliePay::SalesInvoice` with its status, amount, currency, invoice number,
|
|
226
|
+
recipient identifier, memo, and issued, paid and due dates.
|
|
227
|
+
`mollie_mark_invoice_paid` marks an issued invoice paid at Mollie and on the
|
|
228
|
+
local record. To have the local record follow a status change made at Mollie,
|
|
229
|
+
such as the customer paying the invoice, subscribe a next-gen webhook to the
|
|
230
|
+
`sales-invoice.*` events (see [docs/webhooks.md](docs/webhooks.md)).
|
|
231
|
+
`invoice.mollie_record` fetches the full, current invoice from Mollie.
|
|
232
|
+
|
|
233
|
+
```ruby
|
|
234
|
+
# Create a draft invoice (returns a MolliePay::SalesInvoice)
|
|
235
|
+
invoice = current_organization.mollie_create_sales_invoice(
|
|
236
|
+
lines: [{ description: "Pro plan", quantity: 1, vat_rate: "21.00", unit_price: BigDecimal("89.00") }]
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
# Create and send immediately
|
|
240
|
+
invoice = current_organization.mollie_create_sales_invoice(
|
|
241
|
+
status: "issued",
|
|
242
|
+
lines: [{ description: "Pro plan", quantity: 1, vat_rate: "21.00", unit_price: BigDecimal("89.00") }],
|
|
243
|
+
email_details: { subject: "Your invoice", body: "Please pay within 30 days" }
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
# With explicit recipient (overrides auto-populated fields)
|
|
247
|
+
invoice = current_organization.mollie_create_sales_invoice(
|
|
248
|
+
recipient: { type: "business", organization_name: "Acme B.V.", email: "billing@acme.nl" },
|
|
249
|
+
lines: [{ description: "Consulting", quantity: 10, vat_rate: "21.00", unit_price: BigDecimal("150.00") }],
|
|
250
|
+
payment_term: "30 days",
|
|
251
|
+
memo: "Thank you for your business!"
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
# Local records
|
|
255
|
+
current_organization.mollie_sales_invoices # ActiveRecord relation
|
|
256
|
+
current_organization.mollie_sales_invoices.issued # also .draft, .paid, .overdue
|
|
257
|
+
issued = current_organization.mollie_sales_invoices.issued.first
|
|
258
|
+
current_organization.mollie_mark_invoice_paid(issued) # issued invoices only
|
|
259
|
+
issued.mollie_record # => Mollie::SalesInvoice
|
|
260
|
+
|
|
261
|
+
# Straight to Mollie: these return Mollie::SalesInvoice objects and do not
|
|
262
|
+
# create or change a local record
|
|
263
|
+
mollie_si = MolliePay.sales_invoice("invoice_abc123")
|
|
264
|
+
mollie_sis = MolliePay.sales_invoices
|
|
265
|
+
MolliePay.update_sales_invoice("invoice_abc123", memo: "Updated memo")
|
|
266
|
+
MolliePay.delete_sales_invoice("invoice_abc123") # draft only
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Line item `unit_price` accepts a `BigDecimal` and is converted automatically.
|
|
270
|
+
The Billable convenience method auto-populates the recipient from your model's
|
|
271
|
+
`name` and `email` attributes.
|
|
272
|
+
|
|
273
|
+
### Query state
|
|
274
|
+
|
|
275
|
+
```ruby
|
|
276
|
+
org.mollie_subscribed? # => true/false
|
|
277
|
+
org.mollie_mandated? # => true/false
|
|
278
|
+
org.mollie_subscription # => MolliePay::Subscription or nil
|
|
279
|
+
org.mollie_mandate # => MolliePay::Mandate or nil
|
|
280
|
+
org.mollie_payments.paid # ActiveRecord relation
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Webhooks
|
|
284
|
+
|
|
285
|
+
Point Mollie to `POST https://yourapp.com/mollie_pay/webhooks`. MolliePay
|
|
286
|
+
validates the ID, enqueues a background job, and responds `200 OK` immediately.
|
|
287
|
+
The job fetches the current state from Mollie and upserts the local record.
|
|
288
|
+
Chargebacks are detected automatically via payment amount comparison.
|
|
289
|
+
|
|
290
|
+
See [docs/webhooks.md](docs/webhooks.md) for details on verification,
|
|
291
|
+
idempotency, chargeback detection, Active Job configuration, and rate limiting.
|
|
292
|
+
|
|
293
|
+
### Keeping subscriptions in sync
|
|
294
|
+
|
|
295
|
+
Mollie does not send webhooks when a subscription's own status changes — for
|
|
296
|
+
example, when Mollie cancels a subscription after exhausted payment retries or
|
|
297
|
+
a bank rejection, suspends it because a mandate became invalid, or completes it
|
|
298
|
+
after `times` payments. Schedule `MolliePay::SyncSubscriptionsJob` to keep local
|
|
299
|
+
subscriptions and missed recurring payments in sync:
|
|
300
|
+
|
|
301
|
+
```yaml
|
|
302
|
+
# config/recurring.yml (Solid Queue)
|
|
303
|
+
production:
|
|
304
|
+
mollie_pay_sync_subscriptions:
|
|
305
|
+
class: MolliePay::SyncSubscriptionsJob
|
|
306
|
+
schedule: every day at 3am
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
If you use another scheduler (cron, Sidekiq-Cron, GoodJob), enqueue
|
|
310
|
+
`MolliePay::SyncSubscriptionsJob.perform_later` on a schedule.
|
|
311
|
+
|
|
312
|
+
The sweep selects local subscriptions in `MolliePay::Subscription::ACTIVE_STATUSES`
|
|
313
|
+
(`pending` and `active`) and enqueues one `MolliePay::SyncSubscriptionJob` per
|
|
314
|
+
subscription on the same queue (so scheduling the sweep on a dedicated queue
|
|
315
|
+
isolates sync traffic from `:default` webhooks). Each job reconciles missed or
|
|
316
|
+
updated subscription payments before recording the subscription's status so hooks
|
|
317
|
+
fire, and marks a subscription `canceled` locally (firing
|
|
318
|
+
`on_mollie_subscription_canceled`) if Mollie answers `404 Not Found` for the
|
|
319
|
+
subscription.
|
|
320
|
+
|
|
321
|
+
- **Do not run with a mismatched API key:** Mollie isolates `test_` and `live_`
|
|
322
|
+
resources and answers `404` across modes. Running the sync with a `test_` key
|
|
323
|
+
against a database of `live_` subscriptions (for example, a staging copy of
|
|
324
|
+
production) will cancel those subscriptions locally.
|
|
325
|
+
- **Suspended subscriptions:** The sweep only walks `pending` and `active`
|
|
326
|
+
subscriptions. To re-check a `suspended` subscription on demand, enqueue
|
|
327
|
+
`MolliePay::SyncSubscriptionJob.perform_later(subscription)` or call
|
|
328
|
+
`subscription.sync_with_mollie`.
|
|
329
|
+
- **Unbackfilled subscriptions:** If you imported existing Mollie subscriptions
|
|
330
|
+
without their historical `MolliePay::Payment` rows, the first sync inserts
|
|
331
|
+
those payments and enqueues their payment hooks — keep hooks idempotent.
|
|
332
|
+
|
|
333
|
+
## Reacting to events
|
|
334
|
+
|
|
335
|
+
Override hooks in your billable model. Each hook runs in a background job after
|
|
336
|
+
the change is saved, and that job retries a hook that raises, so a hook can run
|
|
337
|
+
more than once. Make hooks idempotent.
|
|
338
|
+
|
|
339
|
+
```ruby
|
|
340
|
+
class Organization < ApplicationRecord
|
|
341
|
+
include MolliePay::Billable
|
|
342
|
+
|
|
343
|
+
def on_mollie_mandate_created(mandate)
|
|
344
|
+
# Mandate is valid and linked via mandate.payments — safe to subscribe with mandate: mandate
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
def on_mollie_payment_paid(payment)
|
|
348
|
+
# One-off payment confirmed
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def on_mollie_payment_authorized(payment)
|
|
352
|
+
# Pay-later payment authorized — ship, then mollie_capture(payment)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def on_mollie_subscription_charged(payment)
|
|
356
|
+
# Recurring payment succeeded
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def on_mollie_subscription_canceled(subscription)
|
|
360
|
+
# Subscription ended (fires via scheduled MolliePay::SyncSubscriptionsJob)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def on_mollie_refund_processed(refund)
|
|
364
|
+
# Refund completed
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def on_mollie_chargeback_received(chargeback)
|
|
368
|
+
# Chargeback filed — take action
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
def on_mollie_subscription_swapped(subscription, previous_amount:, previous_interval:)
|
|
372
|
+
# Plan changed — calculate proration if needed
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
All hooks: `on_mollie_payment_paid`, `on_mollie_payment_authorized`,
|
|
378
|
+
`on_mollie_payment_failed`, `on_mollie_payment_canceled`,
|
|
379
|
+
`on_mollie_payment_expired`, `on_mollie_first_payment_paid`,
|
|
380
|
+
`on_mollie_subscription_charged`, `on_mollie_subscription_canceled`,
|
|
381
|
+
`on_mollie_subscription_suspended`, `on_mollie_subscription_completed`,
|
|
382
|
+
`on_mollie_mandate_created`, `on_mollie_refund_processed`,
|
|
383
|
+
`on_mollie_chargeback_received`, `on_mollie_chargeback_reversed`,
|
|
384
|
+
`on_mollie_subscription_swapped`, `on_mollie_sales_invoice_issued`,
|
|
385
|
+
`on_mollie_sales_invoice_paid`.
|
|
386
|
+
|
|
387
|
+
## Testing
|
|
388
|
+
|
|
389
|
+
```ruby
|
|
390
|
+
# test/test_helper.rb
|
|
391
|
+
require "mollie_pay/test_helper"
|
|
392
|
+
|
|
393
|
+
class ActiveSupport::TestCase
|
|
394
|
+
include MolliePay::TestHelper
|
|
395
|
+
end
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
```ruby
|
|
399
|
+
stub_mollie_payment_create do
|
|
400
|
+
payment = @org.mollie_pay_once(amount: BigDecimal("50.00"), description: "Test")
|
|
401
|
+
assert_equal "open", payment.status
|
|
402
|
+
end
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
See [docs/testing.md](docs/testing.md) for the full helper reference.
|
|
406
|
+
|
|
407
|
+
## Documentation
|
|
408
|
+
|
|
409
|
+
- [Tutorial](docs/tutorial.md) — step-by-step guide
|
|
410
|
+
- [API Reference](docs/api.md) — data model, statuses, scopes, errors, configuration
|
|
411
|
+
- [Webhooks](docs/webhooks.md) — webhook flow, chargeback detection, idempotency, Active Job
|
|
412
|
+
- [Request signing](docs/request-signing.md) — generating the key pair, storing the private key, verifying, rotating
|
|
413
|
+
- [Mandates](docs/mandates.md) — SEPA DD mandate consent, compliance, web signature collection
|
|
414
|
+
- [Testing](docs/testing.md) — stub helpers and WebMock helpers
|
|
415
|
+
- [Releasing](https://github.com/peterberkenbosch/mollie_pay/blob/master/docs/RELEASING.md) — release process
|
|
416
|
+
|
|
417
|
+
## Requirements
|
|
418
|
+
|
|
419
|
+
- Ruby 3.3+
|
|
420
|
+
- Rails 8.1+
|
|
421
|
+
- Active Job (any queue adapter)
|
|
422
|
+
- A [Mollie](https://my.mollie.com/dashboard/signup/7878281?lang=nl) account and API key
|
|
423
|
+
|
|
424
|
+
## License
|
|
425
|
+
|
|
426
|
+
MIT
|
data/Rakefile
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class WebhookEventsController < ApplicationController
|
|
3
|
+
skip_forgery_protection
|
|
4
|
+
|
|
5
|
+
# Without a signing secret an event cannot be verified, so the endpoint
|
|
6
|
+
# acts as if it is not there.
|
|
7
|
+
before_action :require_signing_secret
|
|
8
|
+
|
|
9
|
+
def create
|
|
10
|
+
raw_body = request.raw_post
|
|
11
|
+
head :bad_request and return if raw_body.blank?
|
|
12
|
+
|
|
13
|
+
verify_signature!(raw_body)
|
|
14
|
+
|
|
15
|
+
event = JSON.parse(raw_body)
|
|
16
|
+
|
|
17
|
+
if valid_event?(event)
|
|
18
|
+
ProcessWebhookEventJob.perform_later(event)
|
|
19
|
+
head :ok
|
|
20
|
+
else
|
|
21
|
+
head :unprocessable_entity
|
|
22
|
+
end
|
|
23
|
+
rescue JSON::ParserError
|
|
24
|
+
head :bad_request
|
|
25
|
+
rescue MolliePay::InvalidSignature => e
|
|
26
|
+
Rails.logger.warn("[MolliePay] Webhook signature verification failed: #{e.message}")
|
|
27
|
+
head :bad_request
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def require_signing_secret
|
|
33
|
+
head :not_found if MolliePay.configuration.webhook_signing_secrets.nil?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def verify_signature!(raw_body)
|
|
37
|
+
MolliePay::WebhookSignature.verify!(
|
|
38
|
+
raw_body,
|
|
39
|
+
request.headers["X-Mollie-Signature"],
|
|
40
|
+
MolliePay.configuration.webhook_signing_secrets
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Mollie's event object always has resource "event" and an id, type and
|
|
45
|
+
# entityId. The type is not checked against a list, so an event type
|
|
46
|
+
# Mollie adds later is still accepted.
|
|
47
|
+
def valid_event?(event)
|
|
48
|
+
event.is_a?(Hash) &&
|
|
49
|
+
event["resource"] == "event" &&
|
|
50
|
+
event.values_at("id", "type", "entityId").all? { |value| value.is_a?(String) && value.present? }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class WebhooksController < ApplicationController
|
|
3
|
+
MOLLIE_ID_FORMAT = /\A(tr|sub|re|stl)_[a-zA-Z0-9]{1,64}\z/
|
|
4
|
+
|
|
5
|
+
skip_forgery_protection
|
|
6
|
+
|
|
7
|
+
def create
|
|
8
|
+
mollie_id = params.expect(:id)
|
|
9
|
+
|
|
10
|
+
if mollie_id.match?(MOLLIE_ID_FORMAT)
|
|
11
|
+
ProcessWebhookJob.perform_later(mollie_id)
|
|
12
|
+
head :ok
|
|
13
|
+
else
|
|
14
|
+
head :unprocessable_entity
|
|
15
|
+
end
|
|
16
|
+
rescue ActionController::ParameterMissing
|
|
17
|
+
head :unprocessable_entity
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class ApplicationJob < ActiveJob::Base
|
|
3
|
+
MAX_SYNC_ATTEMPTS = 3
|
|
4
|
+
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def with_stale_sync_retry(max_attempts: MAX_SYNC_ATTEMPTS)
|
|
8
|
+
attempts = 0
|
|
9
|
+
|
|
10
|
+
MolliePay::ApplicationRecord.uncached do
|
|
11
|
+
begin
|
|
12
|
+
attempts += 1
|
|
13
|
+
yield
|
|
14
|
+
rescue MolliePay::StaleSyncError
|
|
15
|
+
if attempts < max_attempts
|
|
16
|
+
retry
|
|
17
|
+
else
|
|
18
|
+
raise
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# Deletes a customer at Mollie that no local customer points to: the one a
|
|
3
|
+
# call created while losing the race to save this owner's customer. The
|
|
4
|
+
# job's argument is the only record of that customer's id, so a failed
|
|
5
|
+
# delete is retried here rather than lost.
|
|
6
|
+
class DeleteMollieCustomerJob < ApplicationJob
|
|
7
|
+
queue_as :default
|
|
8
|
+
|
|
9
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
10
|
+
discard_on Mollie::ResourceNotFoundError
|
|
11
|
+
|
|
12
|
+
def perform(mollie_id)
|
|
13
|
+
Mollie::Customer.delete(mollie_id)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# Calls one on_mollie_* hook on the host's billable model. The record whose
|
|
3
|
+
# status changed is saved before this job is enqueued, so when a hook
|
|
4
|
+
# raises, this job retries the hook. The webhook job's own retry would find
|
|
5
|
+
# no transition left and never call it again. Hooks therefore run at least
|
|
6
|
+
# once and must be idempotent.
|
|
7
|
+
class NotifyBillableJob < ApplicationJob
|
|
8
|
+
queue_as :default
|
|
9
|
+
|
|
10
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
11
|
+
discard_on ActiveJob::DeserializationError
|
|
12
|
+
|
|
13
|
+
def perform(billable, hook, record)
|
|
14
|
+
billable.public_send(hook, record)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# Syncs local records from next-gen webhook events. Only sales invoice events
|
|
3
|
+
# are handled: payments and refunds already sync through classic webhooks,
|
|
4
|
+
# which Mollie still recommends for them. Every other event is logged and
|
|
5
|
+
# ignored, so a type Mollie adds later never fails the job.
|
|
6
|
+
class ProcessWebhookEventJob < ApplicationJob
|
|
7
|
+
queue_as :default
|
|
8
|
+
|
|
9
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
10
|
+
retry_on MolliePay::StaleSyncError, wait: :polynomially_longer, attempts: 2
|
|
11
|
+
discard_on Mollie::ResourceNotFoundError
|
|
12
|
+
|
|
13
|
+
def perform(event)
|
|
14
|
+
case event["type"]
|
|
15
|
+
when /\Asales-invoice\./ then sync_sales_invoice(event["entityId"])
|
|
16
|
+
else
|
|
17
|
+
Rails.logger.info("[MolliePay] Ignoring webhook event: #{event['type']} (#{event['entityId']})")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
# The event carries no customer, so only an invoice the engine stored
|
|
24
|
+
# can be synced. Its current state is fetched from Mollie, as for
|
|
25
|
+
# classic webhooks, rather than read from the event.
|
|
26
|
+
def sync_sales_invoice(mollie_id)
|
|
27
|
+
with_stale_sync_retry do
|
|
28
|
+
invoice = SalesInvoice.includes(customer: :owner).find_by!(mollie_id: mollie_id)
|
|
29
|
+
SalesInvoice.record_from_mollie(
|
|
30
|
+
MolliePay.sales_invoice(mollie_id),
|
|
31
|
+
invoice.customer,
|
|
32
|
+
expected_version: invoice.sync_version
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
rescue ActiveRecord::RecordNotFound
|
|
36
|
+
Rails.logger.info("[MolliePay] Discarding sales invoice event, local record not found: #{mollie_id}")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|