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.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +426 -0
  4. data/Rakefile +6 -0
  5. data/app/controllers/mollie_pay/application_controller.rb +4 -0
  6. data/app/controllers/mollie_pay/webhook_events_controller.rb +53 -0
  7. data/app/controllers/mollie_pay/webhooks_controller.rb +20 -0
  8. data/app/jobs/mollie_pay/application_job.rb +24 -0
  9. data/app/jobs/mollie_pay/delete_mollie_customer_job.rb +16 -0
  10. data/app/jobs/mollie_pay/notify_billable_job.rb +17 -0
  11. data/app/jobs/mollie_pay/process_webhook_event_job.rb +39 -0
  12. data/app/jobs/mollie_pay/process_webhook_job.rb +56 -0
  13. data/app/jobs/mollie_pay/refresh_pending_mandate_job.rb +22 -0
  14. data/app/jobs/mollie_pay/sync_chargebacks_job.rb +19 -0
  15. data/app/jobs/mollie_pay/sync_subscription_job.rb +18 -0
  16. data/app/jobs/mollie_pay/sync_subscriptions_job.rb +16 -0
  17. data/app/models/concerns/mollie_pay/has_uuid.rb +19 -0
  18. data/app/models/concerns/mollie_pay/syncable.rb +208 -0
  19. data/app/models/mollie_pay/application_record.rb +45 -0
  20. data/app/models/mollie_pay/billable.rb +445 -0
  21. data/app/models/mollie_pay/chargeback.rb +103 -0
  22. data/app/models/mollie_pay/customer.rb +32 -0
  23. data/app/models/mollie_pay/mandate.rb +113 -0
  24. data/app/models/mollie_pay/payment.rb +241 -0
  25. data/app/models/mollie_pay/refund.rb +61 -0
  26. data/app/models/mollie_pay/sales_invoice.rb +131 -0
  27. data/app/models/mollie_pay/subscription.rb +215 -0
  28. data/config/routes.rb +4 -0
  29. data/db/migrate/20260315142959_create_mollie_pay_customers.rb +9 -0
  30. data/db/migrate/20260315143008_create_mollie_pay_mandates.rb +13 -0
  31. data/db/migrate/20260315143014_create_mollie_pay_subscriptions.rb +15 -0
  32. data/db/migrate/20260315143020_create_mollie_pay_payments.rb +20 -0
  33. data/db/migrate/20260315143026_create_mollie_pay_refunds.rb +14 -0
  34. data/db/migrate/20260315143032_create_mollie_pay_webhook_events.rb +12 -0
  35. data/db/migrate/20260315143040_add_status_indexes_to_mollie_pay.rb +8 -0
  36. data/db/migrate/20260315143050_add_checkout_url_to_mollie_pay_payments.rb +5 -0
  37. data/db/migrate/20260316120000_make_mollie_id_nullable_on_mollie_pay_payments.rb +5 -0
  38. data/db/migrate/20260317100000_make_webhook_event_mollie_id_unique.rb +6 -0
  39. data/db/migrate/20260317194500_add_authorized_at_to_mollie_pay_payments.rb +5 -0
  40. data/db/migrate/20260317194501_add_amount_tracking_to_mollie_pay_payments.rb +9 -0
  41. data/db/migrate/20260318100000_add_name_to_mollie_pay_subscriptions.rb +12 -0
  42. data/db/migrate/20260318110000_drop_mollie_pay_webhook_events.rb +19 -0
  43. data/db/migrate/20260321000001_create_mollie_pay_chargebacks.rb +15 -0
  44. data/db/migrate/20260326000002_create_mollie_pay_sales_invoices.rb +19 -0
  45. data/db/migrate/20261115000001_change_mollie_pay_money_to_decimal.rb +38 -0
  46. data/db/migrate/20261202000001_add_sync_version_to_mollie_pay_tables.rb +13 -0
  47. data/db/migrate/20261202000002_add_created_at_mollie_to_mollie_pay_payments.rb +5 -0
  48. data/db/migrate/20261203000001_add_mandate_to_mollie_pay_payments.rb +6 -0
  49. data/docs/api.md +359 -0
  50. data/docs/mandates.md +340 -0
  51. data/docs/request-signing.md +178 -0
  52. data/docs/testing.md +177 -0
  53. data/docs/tutorial.md +1340 -0
  54. data/docs/webhooks.md +231 -0
  55. data/lib/generators/mollie_pay/install/install_generator.rb +44 -0
  56. data/lib/generators/mollie_pay/install/templates/initializer.rb.tt +12 -0
  57. data/lib/mollie/sales_invoice.rb +124 -0
  58. data/lib/mollie_pay/configuration.rb +101 -0
  59. data/lib/mollie_pay/engine.rb +31 -0
  60. data/lib/mollie_pay/errors.rb +12 -0
  61. data/lib/mollie_pay/request_signature.rb +160 -0
  62. data/lib/mollie_pay/test_fixtures/customer.json +36 -0
  63. data/lib/mollie_pay/test_fixtures/payment.json +40 -0
  64. data/lib/mollie_pay/test_fixtures/payment_billink.json +79 -0
  65. data/lib/mollie_pay/test_fixtures/payment_with_chargebacks.json +72 -0
  66. data/lib/mollie_pay/test_fixtures/refund.json +32 -0
  67. data/lib/mollie_pay/test_fixtures/sales_invoice.json +72 -0
  68. data/lib/mollie_pay/test_fixtures/subscription.json +48 -0
  69. data/lib/mollie_pay/test_helper.rb +576 -0
  70. data/lib/mollie_pay/version.rb +3 -0
  71. data/lib/mollie_pay/webhook_signature.rb +21 -0
  72. data/lib/mollie_pay.rb +182 -0
  73. data/lib/tasks/mollie_pay_tasks.rake +43 -0
  74. metadata +148 -0
@@ -0,0 +1,20 @@
1
+ class CreateMolliePayPayments < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :mollie_pay_payments, id: :string do |t|
4
+ t.references :customer, null: false, type: :string,
5
+ foreign_key: { to_table: :mollie_pay_customers }
6
+ t.references :subscription, type: :string,
7
+ foreign_key: { to_table: :mollie_pay_subscriptions }
8
+ t.string :mollie_id, null: false, index: { unique: true }
9
+ t.string :status, null: false, default: "open"
10
+ t.string :amount, null: false, default: "0" # money as text until ChangeMolliePayMoneyToDecimal makes it decimal(15,4)
11
+ t.string :currency, null: false, default: "EUR"
12
+ t.string :sequence_type, null: false, default: "oneoff"
13
+ t.datetime :paid_at
14
+ t.datetime :failed_at
15
+ t.datetime :canceled_at
16
+ t.datetime :expired_at
17
+ t.timestamps
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ class CreateMolliePayRefunds < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :mollie_pay_refunds, id: :string do |t|
4
+ t.references :payment, null: false, type: :string,
5
+ foreign_key: { to_table: :mollie_pay_payments }
6
+ t.string :mollie_id, null: false, index: { unique: true }
7
+ t.string :status, null: false, default: "queued"
8
+ t.string :amount, null: false, default: "0" # money as text until ChangeMolliePayMoneyToDecimal makes it decimal(15,4)
9
+ t.string :currency, null: false, default: "EUR"
10
+ t.datetime :refunded_at
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ class CreateMolliePayWebhookEvents < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :mollie_pay_webhook_events do |t|
4
+ t.string :mollie_id, null: false, index: true
5
+ t.string :resource_type
6
+ t.datetime :processed_at
7
+ t.datetime :failed_at
8
+ t.text :error
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ class AddStatusIndexesToMolliePay < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_index :mollie_pay_payments, [ :customer_id, :status ]
4
+ add_index :mollie_pay_subscriptions, [ :customer_id, :status ]
5
+ add_index :mollie_pay_mandates, [ :customer_id, :status ]
6
+ add_index :mollie_pay_webhook_events, :processed_at
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class AddCheckoutUrlToMolliePayPayments < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :mollie_pay_payments, :checkout_url, :string
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class MakeMollieIdNullableOnMolliePayPayments < ActiveRecord::Migration[8.1]
2
+ def change
3
+ change_column_null :mollie_pay_payments, :mollie_id, true
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class MakeWebhookEventMollieIdUnique < ActiveRecord::Migration[8.1]
2
+ def change
3
+ remove_index :mollie_pay_webhook_events, :mollie_id
4
+ add_index :mollie_pay_webhook_events, :mollie_id, unique: true
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class AddAuthorizedAtToMolliePayPayments < ActiveRecord::Migration[8.0]
2
+ def change
3
+ add_column :mollie_pay_payments, :authorized_at, :datetime
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class AddAmountTrackingToMolliePayPayments < ActiveRecord::Migration[8.0]
2
+ def change
3
+ # money as text until ChangeMolliePayMoneyToDecimal makes it decimal(15,4)
4
+ add_column :mollie_pay_payments, :amount_refunded, :string, default: "0"
5
+ add_column :mollie_pay_payments, :amount_remaining, :string
6
+ add_column :mollie_pay_payments, :amount_captured, :string, default: "0"
7
+ add_column :mollie_pay_payments, :amount_charged_back, :string, default: "0"
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class AddNameToMolliePaySubscriptions < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :mollie_pay_subscriptions, :name, :string, null: false, default: "default"
4
+
5
+ # Partial unique index: one active/pending subscription per name per customer.
6
+ # Must match Subscription::ACTIVE_STATUSES constant.
7
+ add_index :mollie_pay_subscriptions, [ :customer_id, :name ],
8
+ unique: true,
9
+ where: "status IN ('pending', 'active')",
10
+ name: "idx_mollie_subs_unique_active_per_customer_name"
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ class DropMolliePayWebhookEvents < ActiveRecord::Migration[8.1]
2
+ def up
3
+ drop_table :mollie_pay_webhook_events
4
+ end
5
+
6
+ def down
7
+ create_table :mollie_pay_webhook_events do |t|
8
+ t.string :mollie_id, null: false
9
+ t.string :resource_type
10
+ t.datetime :processed_at
11
+ t.datetime :failed_at
12
+ t.text :error
13
+ t.timestamps
14
+ end
15
+
16
+ add_index :mollie_pay_webhook_events, :mollie_id, unique: true
17
+ add_index :mollie_pay_webhook_events, :processed_at
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ class CreateMolliePayChargebacks < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :mollie_pay_chargebacks, id: :string do |t|
4
+ t.references :payment, null: false, type: :string,
5
+ foreign_key: { to_table: :mollie_pay_payments }
6
+ t.string :mollie_id, null: false, index: { unique: true }
7
+ t.string :amount, null: false, default: "0" # money as text until ChangeMolliePayMoneyToDecimal makes it decimal(15,4)
8
+ t.string :currency, null: false, default: "EUR"
9
+ t.string :reason
10
+ t.datetime :created_at_mollie
11
+ t.datetime :reversed_at
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ class CreateMolliePaySalesInvoices < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :mollie_pay_sales_invoices, id: :string do |t|
4
+ t.references :customer, null: false, type: :string,
5
+ foreign_key: { to_table: :mollie_pay_customers }
6
+ t.string :mollie_id, null: false, index: { unique: true }
7
+ t.string :status, null: false, default: "draft"
8
+ t.string :invoice_number
9
+ t.string :amount # money as text until ChangeMolliePayMoneyToDecimal makes it decimal(15,4)
10
+ t.string :currency
11
+ t.string :recipient_identifier
12
+ t.text :memo
13
+ t.datetime :issued_at
14
+ t.datetime :paid_at
15
+ t.datetime :due_at
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ # Money is decimal(15,4): four decimals is what accounting software keeps, and
2
+ # fifteen significant digits is what SQLite holds exactly, since it stores a
3
+ # decimal as a REAL. See engine-family ADR 006. The amounts were text; SQLite
4
+ # converts the stored values when the table is rebuilt.
5
+ class ChangeMolliePayMoneyToDecimal < ActiveRecord::Migration[8.1]
6
+ COLUMNS = {
7
+ mollie_pay_subscriptions: { amount: { null: false, default: 0 } },
8
+ mollie_pay_refunds: { amount: { null: false, default: 0 } },
9
+ mollie_pay_chargebacks: { amount: { null: false, default: 0 } },
10
+ mollie_pay_sales_invoices: { amount: {} },
11
+ mollie_pay_payments: {
12
+ amount: { null: false, default: 0 },
13
+ amount_refunded: { default: 0 },
14
+ amount_captured: { default: 0 },
15
+ amount_charged_back: { default: 0 },
16
+ amount_remaining: {}
17
+ }
18
+ }.freeze
19
+
20
+ def up
21
+ each_column do |table, column, options|
22
+ change_column table, column, :decimal, precision: 15, scale: 4, **options
23
+ end
24
+ end
25
+
26
+ def down
27
+ each_column do |table, column, options|
28
+ change_column table, column, :string, **options.merge(options.key?(:default) ? { default: "0" } : {})
29
+ end
30
+ end
31
+
32
+ private
33
+ def each_column
34
+ COLUMNS.each do |table, columns|
35
+ columns.each { |column, options| yield table, column, options }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ class AddSyncVersionToMolliePayTables < ActiveRecord::Migration[8.1]
2
+ TABLES = %i[
3
+ mollie_pay_payments
4
+ mollie_pay_subscriptions
5
+ mollie_pay_sales_invoices
6
+ ].freeze
7
+
8
+ def change
9
+ TABLES.each do |table|
10
+ add_column table, :sync_version, :integer, default: 0, null: false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ class AddCreatedAtMollieToMolliePayPayments < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :mollie_pay_payments, :created_at_mollie, :datetime
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class AddMandateToMolliePayPayments < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_reference :mollie_pay_payments, :mandate, type: :string,
4
+ foreign_key: { to_table: :mollie_pay_mandates, on_delete: :nullify }
5
+ end
6
+ end
data/docs/api.md ADDED
@@ -0,0 +1,359 @@
1
+ # API Reference
2
+
3
+ ## Data model
4
+
5
+ | Table | Key columns | Purpose |
6
+ |---|---|---|
7
+ | `mollie_pay_customers` | `mollie_id`, `owner` (polymorphic) | Links your model to a Mollie customer |
8
+ | `mollie_pay_mandates` | `mollie_id`, `status`, `method` | Stored payment methods (SEPA, card, etc.) |
9
+ | `mollie_pay_subscriptions` | `mollie_id`, `status`, `amount`, `interval` | Recurring billing agreements |
10
+ | `mollie_pay_payments` | `mollie_id`, `status`, `amount`, `sequence_type`, `mandate_id` | Individual payment records |
11
+ | `mollie_pay_refunds` | `mollie_id`, `status`, `amount` | Refunds against payments |
12
+ | `mollie_pay_chargebacks` | `mollie_id`, `amount`, `reason` | Chargebacks against payments |
13
+
14
+ Only fields needed for business logic and state queries are stored locally.
15
+ Display data lives in Mollie and is fetched via `mollie_record`.
16
+
17
+ ### Relationships
18
+
19
+ ```
20
+ Owner (your model)
21
+ └── Customer (1:1, polymorphic)
22
+ ├── Payments (1:N)
23
+ │ ├── Refunds (1:N)
24
+ │ └── Chargebacks (1:N)
25
+ ├── Subscriptions (1:N)
26
+ │ └── Payments (1:N, optional)
27
+ └── Mandates (1:N)
28
+ └── Payments (1:N, optional)
29
+ ```
30
+
31
+ ### Statuses
32
+
33
+ | Model | Valid statuses |
34
+ |---|---|
35
+ | Payment | `open`, `pending`, `authorized`, `paid`, `failed`, `canceled`, `expired` |
36
+ | Subscription | `pending`, `active`, `suspended`, `canceled`, `completed` |
37
+ | Mandate | `pending`, `valid`, `invalid` |
38
+ | Refund | `queued`, `pending`, `processing`, `refunded`, `failed`, `canceled` |
39
+ | Sales invoice | `draft`, `issuing`, `issued`, `pending-payment`, `paid`, `overdue`, `payment-reversed`, `cancelled`, `expired`, `failed` |
40
+
41
+ ### Scopes
42
+
43
+ ```ruby
44
+ MolliePay::Payment.paid # status: paid
45
+ MolliePay::Payment.failed # status: failed
46
+ MolliePay::Payment.open # status: open
47
+ MolliePay::Payment.authorized # status: authorized (awaiting capture)
48
+ MolliePay::Payment.recurring # sequence_type: recurring
49
+ MolliePay::Payment.first_payments # sequence_type: first
50
+
51
+ MolliePay::Subscription.active
52
+ MolliePay::Subscription.canceled
53
+ MolliePay::Subscription.suspended
54
+
55
+ MolliePay::Mandate.valid_status
56
+ MolliePay::Refund.refunded
57
+ ```
58
+
59
+ ## Through associations
60
+
61
+ `mollie_payments`, `mollie_subscriptions`, and `mollie_mandates` are real
62
+ `has_many :through` associations — they support full ActiveRecord chaining:
63
+
64
+ ```ruby
65
+ org.mollie_subscriptions # all subscriptions
66
+ org.mollie_subscriptions.active # active only
67
+ org.mollie_payments # all payments
68
+ org.mollie_payments.paid # paid only
69
+ org.mollie_payments.recurring # recurring payments
70
+ org.mollie_mandates # all mandates
71
+ org.mollie_mandates.valid_status # valid only
72
+
73
+ # Eager loading for admin views (avoids N+1)
74
+ User.includes(mollie_customer: :subscriptions).find_each do |user|
75
+ user.mollie_subscriptions.active # no extra query
76
+ end
77
+ ```
78
+
79
+ ## Fetching live Mollie data
80
+
81
+ Every local record exposes `mollie_record`, which fetches the full object from
82
+ the Mollie API on demand:
83
+
84
+ ```ruby
85
+ subscription.mollie_record.next_payment_date
86
+ mandate.mollie_record.details
87
+ customer.mollie_record.locale
88
+ ```
89
+
90
+ This is a live API call — don't use it in loops or list views.
91
+
92
+ ## Amounts
93
+
94
+ All amounts in MolliePay are exact **`BigDecimal`** (stored as text, never cents).
95
+
96
+ ```ruby
97
+ payment.amount # => BigDecimal("25.00")
98
+ payment.mollie_amount # => { currency: "EUR", value: "25.00" } (Mollie wire format)
99
+ ```
100
+
101
+ The same methods are available on `Subscription` and `Refund`. Pass amounts to the
102
+ public API as `BigDecimal` (e.g. `BigDecimal("25.00")`); conversion to Mollie's wire
103
+ format happens only at the API boundary.
104
+
105
+ ## Payment methods
106
+
107
+ List available payment methods from the Mollie API. These are fetched live —
108
+ no local model or migration is involved.
109
+
110
+ ```ruby
111
+ MolliePay.payment_methods # all enabled methods
112
+ MolliePay.payment_methods(amount: BigDecimal("10.00")) # filtered by amount
113
+ MolliePay.payment_methods(amount: BigDecimal("10.00"), currency: "USD") # with currency override
114
+ MolliePay.payment_methods(locale: "nl_NL") # localized descriptions
115
+ MolliePay.payment_methods(include: "pricing") # include pricing details
116
+
117
+ MolliePay.payment_method("ideal") # single method details
118
+ MolliePay.payment_method("creditcard", locale: "nl_NL") # with locale
119
+ ```
120
+
121
+ A convenience method is available on Billable:
122
+
123
+ ```ruby
124
+ organization.mollie_payment_methods(amount: 2500, locale: "nl_NL")
125
+ ```
126
+
127
+ Both return Mollie SDK objects (`Mollie::Method`) with `id`, `description`,
128
+ `minimum_amount`, `maximum_amount`, `image`, and `status`.
129
+
130
+ ### Caching
131
+
132
+ Payment methods rarely change. Cache them in your host app to avoid hitting
133
+ the Mollie API on every checkout page load:
134
+
135
+ ```ruby
136
+ # app/models/organization.rb
137
+ def available_payment_methods(amount: nil)
138
+ cache_key = ["mollie_payment_methods", amount, MolliePay.configuration.currency]
139
+ Rails.cache.fetch(cache_key, expires_in: 1.hour) do
140
+ mollie_payment_methods(amount: amount).map do |method|
141
+ {
142
+ id: method.id,
143
+ description: method.description,
144
+ image: method.image["svg"],
145
+ status: method.status
146
+ }
147
+ end
148
+ end
149
+ end
150
+ ```
151
+
152
+ > **Important:** Cache the serialized data (hashes/arrays), not the
153
+ > `Mollie::Method` objects themselves. SDK objects hold network references
154
+ > and are not safe for cache serialization.
155
+
156
+ **Cache invalidation tips:**
157
+
158
+ - Use `expires_in: 1.hour` — methods change infrequently, but Mollie can
159
+ enable/disable methods at any time
160
+ - Include `amount` in the cache key if you filter by amount, since different
161
+ amounts may yield different available methods
162
+ - Bust the cache manually when you change payment method settings in the
163
+ Mollie dashboard: `Rails.cache.delete_matched("mollie_payment_methods*")`
164
+
165
+ ## Pay later with Billink
166
+
167
+ [Billink](https://docs.mollie.com/docs/billink) is a buy-now-pay-later method
168
+ on Mollie's Payments API. The consumer receives the order first and pays
169
+ Billink within 14 days of capture; Mollie settles to you regardless.
170
+
171
+ Constraints from Mollie's documentation:
172
+
173
+ - Beta. Mollie must activate Billink on your account (contact support or your
174
+ account manager). Code support alone does not enable it.
175
+ - EUR only, consumers in NL, BE and DE, merchants in NL and BE.
176
+ - Amount between 0.01 and 2,500.00.
177
+ - Manual capture only: the payment is **authorized** first, you capture it after
178
+ shipping. The authorization expires after 28 days; an uncaptured payment then
179
+ becomes `expired`.
180
+ - Full capture only. Full and partial refunds after capture.
181
+ - No recurring payments, so only `mollie_pay_once` applies.
182
+
183
+ ### Creating the payment
184
+
185
+ `mollie_pay_once` accepts five optional arguments for pay-later methods:
186
+
187
+ ```ruby
188
+ payment = organization.mollie_pay_once(
189
+ amount: BigDecimal("121.00"), description: "Order 1001",
190
+ redirect_url: "https://yourapp.com/orders/1001",
191
+ method: "billink",
192
+ capture_mode: "manual", # required by Billink; the engine does not infer it
193
+ locale: "nl_NL", # optional, recommended by Mollie
194
+ billing_address: { # required by Billink
195
+ given_name: "Jan", family_name: "Jansen",
196
+ street_and_number: "Keizersgracht 126", postal_code: "1015 CW",
197
+ city: "Amsterdam", country: "NL", email: "jan@example.com"
198
+ },
199
+ shipping_address: { ... }, # optional, same fields
200
+ lines: [ # required by Billink
201
+ {
202
+ description: "Widget",
203
+ quantity: 1,
204
+ unit_price: BigDecimal("121.00"),
205
+ total_amount: BigDecimal("121.00"),
206
+ vat_rate: "21.00",
207
+ vat_amount: BigDecimal("21.00"),
208
+ type: "physical" # optional: physical, digital, shipping_fee, discount, ...
209
+ }
210
+ ]
211
+ )
212
+ ```
213
+
214
+ Lines and addresses are snake_case Ruby hashes; the engine camelizes the keys
215
+ and converts the money fields `unit_price`, `total_amount`, `vat_amount` and
216
+ `discount_amount` from `BigDecimal` to Mollie's wire format. The engine does
217
+ not derive totals. Apply Mollie's formulas yourself:
218
+
219
+ - `total_amount = unit_price × quantity − discount_amount`
220
+ - `vat_amount = total_amount × vat_rate / (100 + vat_rate)`, rounded to two decimals
221
+ - the `total_amount` of all lines must equal the payment `amount`
222
+
223
+ Mollie rejects a mismatch with a 422; the local payment record is rolled back.
224
+
225
+ The conversion is also available directly as
226
+ `MolliePay.build_payment_params(lines:, billing_address:, shipping_address:, capture_mode:, locale:)`,
227
+ which returns the camelized hash the engine sends.
228
+
229
+ ### Lifecycle
230
+
231
+ ```
232
+ open → authorized → (mollie_capture) → paid on_mollie_payment_authorized, on_mollie_payment_paid
233
+ open → authorized → (mollie_release_authorization) → canceled on_mollie_payment_canceled
234
+ open → authorized → (no capture within 28 days) → expired on_mollie_payment_expired
235
+ open → canceled / failed / expired consumer abandoned or was rejected
236
+ ```
237
+
238
+ Every local state change arrives through the normal payment webhook. The two
239
+ operations below only ask Mollie to act; they never write local state.
240
+
241
+ ### Capturing
242
+
243
+ ```ruby
244
+ organization.mollie_capture(payment) # full amount (Billink)
245
+ organization.mollie_capture(payment, amount: BigDecimal("50.00")) # partial, for methods that allow it
246
+ ```
247
+
248
+ `mollie_capture` fetches the live payment from Mollie and raises
249
+ `MolliePay::PaymentNotAuthorized` unless its status is `authorized`. It returns
250
+ the SDK capture object (`Mollie::Payment::Capture`); its status is in
251
+ `capture.attributes["status"]` (`pending`, `succeeded`, `failed`). The payment
252
+ becomes `paid` through the next webhook, which fires `on_mollie_payment_paid`.
253
+ If a capture call times out, check `payment.mollie_record.amount_captured`
254
+ before retrying.
255
+
256
+ The capture deadline is in `payment.mollie_record.attributes["capture_before"]`
257
+ (the SDK exposes no accessor for it).
258
+
259
+ ### Releasing
260
+
261
+ ```ruby
262
+ organization.mollie_release_authorization(payment)
263
+ ```
264
+
265
+ Same ownership and live-status checks as capture. Mollie processes the release
266
+ asynchronously and does not guarantee it succeeds. When nothing was captured,
267
+ the payment becomes `canceled` through the next webhook, which fires
268
+ `on_mollie_payment_canceled`. When a capture already succeeded (a partial
269
+ capture on methods that allow it; Billink is full-capture only), Mollie moves
270
+ the payment to `paid` instead and `on_mollie_payment_paid` fires. If Mollie does
271
+ not honor the release, the payment stays `authorized` and you can retry or
272
+ capture.
273
+
274
+ Refunds (`mollie_refund`) apply only after capture.
275
+
276
+ ## Subscription plan swap (upgrade/downgrade)
277
+
278
+ Change a customer's subscription amount and/or interval without canceling:
279
+
280
+ ```ruby
281
+ organization.mollie_swap_subscription(amount: 4999) # change amount only
282
+ organization.mollie_swap_subscription(interval: "1 year") # change interval only
283
+ organization.mollie_swap_subscription(amount: 4999, interval: "1 year") # change both
284
+ organization.mollie_swap_subscription(name: "addon", amount: 1999) # named subscription
285
+ ```
286
+
287
+ This uses the Mollie Update Subscription API (`PATCH`) to modify the existing
288
+ subscription in place. The change takes effect on the **next billing cycle** —
289
+ Mollie does not calculate proration.
290
+
291
+ - Returns the existing subscription unchanged if the values are already the same (no API call)
292
+ - Raises `SubscriptionNotFound` if no active or pending subscription exists for the name
293
+ - Fires `on_mollie_subscription_swapped(subscription, previous_amount:, previous_interval:)`
294
+
295
+ ### Proration
296
+
297
+ Mollie does not handle proration. If you need to charge the upgrade difference
298
+ immediately, use the swap hook:
299
+
300
+ ```ruby
301
+ def on_mollie_subscription_swapped(subscription, previous_amount:, previous_interval:)
302
+ if subscription.amount > previous_amount
303
+ difference = subscription.amount - previous_amount
304
+ mollie_pay_once(
305
+ amount: difference,
306
+ description: "Plan upgrade proration",
307
+ redirect_url: billing_url
308
+ )
309
+ end
310
+ end
311
+ ```
312
+
313
+ ## Syncing subscriptions
314
+
315
+ Mollie sends no webhook when it cancels, suspends, or completes a subscription
316
+ on its own. Schedule `MolliePay::SyncSubscriptionsJob` in your app's recurring
317
+ job runner (Solid Queue `config/recurring.yml`, cron, Sidekiq-Cron, GoodJob):
318
+
319
+ ```ruby
320
+ # Scheduled sweep — enqueues one SyncSubscriptionJob per pending/active subscription
321
+ # on the sweep job's queue_name (default :default)
322
+ MolliePay::SyncSubscriptionsJob.perform_later
323
+
324
+ # Single subscription (also works for a suspended subscription on demand)
325
+ MolliePay::SyncSubscriptionJob.perform_later(subscription)
326
+
327
+ # Synchronous single-subscription sync (reloads and returns the subscription)
328
+ subscription.sync_with_mollie
329
+ ```
330
+
331
+ Each sync fetches `GET /v2/customers/:customer_id/subscriptions/:id`, reconciles
332
+ all pages of that subscription's payments (`GET .../payments?limit=250`,
333
+ skipping unchanged local payments), and records the subscription status through
334
+ `Subscription.record_from_mollie` so `on_mollie_subscription_*` and payment
335
+ hooks fire. A `404 Not Found` on the subscription fetch itself marks the
336
+ subscription `canceled` locally and enqueues `on_mollie_subscription_canceled`
337
+ once via `NotifyBillableJob`.
338
+
339
+ ## Errors
340
+
341
+ | Error | Raised when |
342
+ |---|---|
343
+ | `MolliePay::ConfigurationError` | `api_key` or `host` is missing at boot, or no `redirect_url` is available |
344
+ | `MolliePay::MandateRequired` | `mollie_subscribe` is called without a valid mandate, or with a `mandate:` whose status is not `"valid"` |
345
+ | `MolliePay::SubscriptionNotFound` | `mollie_cancel_subscription` or `mollie_swap_subscription` is called without an active or pending subscription |
346
+ | `MolliePay::PaymentNotCancelable` | `mollie_cancel_payment` is called on a payment Mollie says is not cancelable |
347
+ | `MolliePay::PaymentNotAuthorized` | `mollie_capture` or `mollie_release_authorization` is called on a payment whose live Mollie status is not `authorized` |
348
+
349
+ All inherit from `MolliePay::Error < StandardError`.
350
+
351
+ ## Configuration
352
+
353
+ | Option | Required | Default | Description |
354
+ |---|---|---|---|
355
+ | `api_key` | Yes | — | Your Mollie API key (`test_*` or `live_*`) |
356
+ | `host` | Yes | — | Your application's public URL (e.g. `https://yourapp.com`) |
357
+ | `default_redirect_path` | No | — | Path where Mollie sends customers back after payment (e.g. `/payments/:id`) |
358
+ | `currency` | No | `"EUR"` | ISO 4217 currency code for new payments/subscriptions |
359
+ | `request_signing_key` | No | — | Ed25519 private key that signs every request to Mollie, as PEM or as its single line of base64. Required once a public key is registered on the API key; see [request signing](request-signing.md) |