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,445 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
module Billable
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
has_one :mollie_customer,
|
|
7
|
+
class_name: "MolliePay::Customer",
|
|
8
|
+
as: :owner,
|
|
9
|
+
dependent: :destroy
|
|
10
|
+
|
|
11
|
+
has_many :mollie_subscriptions,
|
|
12
|
+
through: :mollie_customer,
|
|
13
|
+
source: :subscriptions,
|
|
14
|
+
class_name: "MolliePay::Subscription"
|
|
15
|
+
|
|
16
|
+
has_many :mollie_payments,
|
|
17
|
+
through: :mollie_customer,
|
|
18
|
+
source: :payments,
|
|
19
|
+
class_name: "MolliePay::Payment"
|
|
20
|
+
|
|
21
|
+
has_many :mollie_mandates,
|
|
22
|
+
through: :mollie_customer,
|
|
23
|
+
source: :mandates,
|
|
24
|
+
class_name: "MolliePay::Mandate"
|
|
25
|
+
|
|
26
|
+
has_many :mollie_sales_invoices,
|
|
27
|
+
through: :mollie_customer,
|
|
28
|
+
source: :sales_invoices,
|
|
29
|
+
class_name: "MolliePay::SalesInvoice"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# === Customer ===
|
|
33
|
+
|
|
34
|
+
def mollie_customer!
|
|
35
|
+
mollie_customer || create_mollie_customer_on_mollie
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def mollie_update_customer(name: nil, email: nil, locale: nil, metadata: nil)
|
|
39
|
+
raise MolliePay::Error, "No Mollie customer exists" if mollie_customer.nil?
|
|
40
|
+
|
|
41
|
+
params = {}
|
|
42
|
+
params[:name] = name if name
|
|
43
|
+
params[:email] = email if email
|
|
44
|
+
params[:locale] = locale if locale
|
|
45
|
+
params[:metadata] = metadata if metadata
|
|
46
|
+
return mollie_customer if params.empty?
|
|
47
|
+
|
|
48
|
+
Mollie::Customer.update(mollie_customer.mollie_id, params)
|
|
49
|
+
mollie_customer
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def mollie_delete_customer
|
|
53
|
+
raise MolliePay::Error, "No Mollie customer exists" if mollie_customer.nil?
|
|
54
|
+
|
|
55
|
+
customer = mollie_customer
|
|
56
|
+
Mollie::Customer.delete(customer.mollie_id)
|
|
57
|
+
customer.destroy!
|
|
58
|
+
reset_mollie_customer
|
|
59
|
+
customer
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# === Payments ===
|
|
63
|
+
|
|
64
|
+
def mollie_pay_once(amount:, description:, redirect_url: nil, method: nil, metadata: nil,
|
|
65
|
+
lines: nil, billing_address: nil, shipping_address: nil, capture_mode: nil, locale: nil)
|
|
66
|
+
extra_params = MolliePay.build_payment_params(lines:, billing_address:, shipping_address:, capture_mode:, locale:)
|
|
67
|
+
create_mollie_payment(amount:, description:, redirect_url:, method:, metadata:, sequence_type: "oneoff", extra_params:)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def mollie_pay_first(amount:, description:, redirect_url: nil, method: nil, metadata: nil)
|
|
71
|
+
create_mollie_payment(amount:, description:, redirect_url:, method:, metadata:, sequence_type: "first")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# === Mandates ===
|
|
75
|
+
|
|
76
|
+
def mollie_create_mandate(method:, consumer_name:, consumer_account:, **options)
|
|
77
|
+
customer = mollie_customer!
|
|
78
|
+
|
|
79
|
+
mollie_mandate = Mollie::Customer::Mandate.create(
|
|
80
|
+
customer_id: customer.mollie_id,
|
|
81
|
+
method: method,
|
|
82
|
+
consumerName: consumer_name,
|
|
83
|
+
consumerAccount: consumer_account,
|
|
84
|
+
**options,
|
|
85
|
+
idempotency_key: SecureRandom.uuid
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
Mandate.record_from_mollie_mandate(mollie_mandate, customer)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def mollie_revoke_mandate(mandate)
|
|
92
|
+
verify_mandate_ownership!(mandate)
|
|
93
|
+
|
|
94
|
+
Mollie::Customer::Mandate.delete(
|
|
95
|
+
mandate.mollie_id,
|
|
96
|
+
customer_id: mollie_customer.mollie_id
|
|
97
|
+
)
|
|
98
|
+
mandate.mark_invalid!
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# === Subscriptions ===
|
|
102
|
+
|
|
103
|
+
def mollie_subscribe(amount:, interval:, description:, start_date: nil, name: "default", mandate: nil)
|
|
104
|
+
mandate_mollie_id = verify_subscription_mandate!(mandate)
|
|
105
|
+
|
|
106
|
+
existing = mollie_subscriptions.where(status: Subscription::ACTIVE_STATUSES, name: name).first
|
|
107
|
+
return existing if existing
|
|
108
|
+
|
|
109
|
+
customer = mollie_customer!
|
|
110
|
+
params = {
|
|
111
|
+
customer_id: customer.mollie_id,
|
|
112
|
+
amount: mollie_amount(amount),
|
|
113
|
+
interval: interval,
|
|
114
|
+
description: description,
|
|
115
|
+
webhook_url: MolliePay.configuration.webhook_url,
|
|
116
|
+
metadata: { mollie_pay_name: name }
|
|
117
|
+
}
|
|
118
|
+
params[:start_date] = start_date.to_s if start_date
|
|
119
|
+
params[:mandate_id] = mandate_mollie_id if mandate_mollie_id
|
|
120
|
+
ms = Mollie::Customer::Subscription.create(**params, idempotency_key: SecureRandom.uuid)
|
|
121
|
+
Subscription.create!(
|
|
122
|
+
customer: customer,
|
|
123
|
+
mollie_id: ms.id,
|
|
124
|
+
status: ms.status,
|
|
125
|
+
amount: amount,
|
|
126
|
+
currency: MolliePay.configuration.currency,
|
|
127
|
+
interval: interval,
|
|
128
|
+
name: name
|
|
129
|
+
)
|
|
130
|
+
rescue ActiveRecord::RecordNotUnique
|
|
131
|
+
Mollie::Customer::Subscription.cancel(ms.id, customer_id: customer.mollie_id)
|
|
132
|
+
mollie_subscriptions.where(status: Subscription::ACTIVE_STATUSES, name: name).first!
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def mollie_cancel_subscription(name: "default")
|
|
136
|
+
subscription = mollie_subscriptions.where(status: Subscription::ACTIVE_STATUSES).named(name).first
|
|
137
|
+
raise MolliePay::SubscriptionNotFound, "No active subscription" unless subscription
|
|
138
|
+
|
|
139
|
+
cancel_subscription_on_mollie(subscription)
|
|
140
|
+
subscription.mark_canceled!
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def mollie_swap_subscription(name: "default", amount: nil, interval: nil)
|
|
144
|
+
subscription = mollie_subscriptions.where(status: Subscription::ACTIVE_STATUSES).named(name).first
|
|
145
|
+
raise MolliePay::SubscriptionNotFound, "No active subscription" unless subscription
|
|
146
|
+
|
|
147
|
+
params = {}
|
|
148
|
+
params[:amount] = mollie_amount(amount) if amount && amount != subscription.amount
|
|
149
|
+
params[:interval] = interval if interval && interval != subscription.interval
|
|
150
|
+
return subscription if params.empty?
|
|
151
|
+
|
|
152
|
+
previous_amount = subscription.amount
|
|
153
|
+
previous_interval = subscription.interval
|
|
154
|
+
|
|
155
|
+
Mollie::Customer::Subscription.update(
|
|
156
|
+
subscription.mollie_id,
|
|
157
|
+
customer_id: mollie_customer.mollie_id,
|
|
158
|
+
**params
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
subscription.record_swap!(
|
|
162
|
+
amount: amount || subscription.amount,
|
|
163
|
+
interval: interval || subscription.interval
|
|
164
|
+
) do
|
|
165
|
+
on_mollie_subscription_swapped(
|
|
166
|
+
subscription,
|
|
167
|
+
previous_amount: previous_amount,
|
|
168
|
+
previous_interval: previous_interval
|
|
169
|
+
)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
subscription
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def mollie_update_payment(payment, description: nil, redirect_url: nil, metadata: nil)
|
|
176
|
+
verify_payment_ownership!(payment)
|
|
177
|
+
|
|
178
|
+
params = {}
|
|
179
|
+
params[:description] = description if description
|
|
180
|
+
params[:redirectUrl] = redirect_url if redirect_url
|
|
181
|
+
params[:metadata] = metadata if metadata
|
|
182
|
+
return payment if params.empty?
|
|
183
|
+
|
|
184
|
+
Mollie::Payment.update(payment.mollie_id, params)
|
|
185
|
+
payment
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def mollie_cancel_payment(payment)
|
|
189
|
+
verify_payment_ownership!(payment)
|
|
190
|
+
|
|
191
|
+
mollie_payment = Mollie::Payment.get(payment.mollie_id)
|
|
192
|
+
raise MolliePay::PaymentNotCancelable, "Payment #{payment.mollie_id} is not cancelable" unless mollie_payment.cancelable?
|
|
193
|
+
|
|
194
|
+
Mollie::Payment.delete(payment.mollie_id)
|
|
195
|
+
payment.mark_canceled!
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Capture an authorized payment (manual-capture methods such as Billink).
|
|
199
|
+
# Captures the full authorized amount unless `amount` is given. The payment
|
|
200
|
+
# becomes "paid" through the subsequent webhook; nothing is written locally.
|
|
201
|
+
def mollie_capture(payment, amount: nil)
|
|
202
|
+
verify_payment_ownership!(payment)
|
|
203
|
+
verify_payment_authorized!(payment)
|
|
204
|
+
|
|
205
|
+
params = { payment_id: payment.mollie_id, idempotency_key: SecureRandom.uuid }
|
|
206
|
+
params[:amount] = mollie_amount(amount) if amount
|
|
207
|
+
Mollie::Payment::Capture.create(**params)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Ask Mollie to release an authorization. Mollie processes the release
|
|
211
|
+
# asynchronously; the payment becomes "canceled" through the subsequent
|
|
212
|
+
# webhook, which also fires on_mollie_payment_canceled.
|
|
213
|
+
def mollie_release_authorization(payment)
|
|
214
|
+
verify_payment_ownership!(payment)
|
|
215
|
+
mollie_payment = verify_payment_authorized!(payment)
|
|
216
|
+
|
|
217
|
+
mollie_payment.release_authorization(idempotency_key: SecureRandom.uuid)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def mollie_refund(payment, amount: nil)
|
|
221
|
+
verify_payment_ownership!(payment)
|
|
222
|
+
refund_amount = amount || payment.amount
|
|
223
|
+
mr = Mollie::Payment::Refund.create(
|
|
224
|
+
payment_id: payment.mollie_id,
|
|
225
|
+
amount: mollie_amount(refund_amount),
|
|
226
|
+
idempotency_key: SecureRandom.uuid
|
|
227
|
+
)
|
|
228
|
+
Refund.create!(
|
|
229
|
+
payment: payment,
|
|
230
|
+
mollie_id: mr.id,
|
|
231
|
+
status: mr.status,
|
|
232
|
+
amount: refund_amount,
|
|
233
|
+
currency: payment.currency
|
|
234
|
+
)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# === State queries ===
|
|
238
|
+
|
|
239
|
+
def mollie_subscribed?(name: "default")
|
|
240
|
+
mollie_subscriptions.active.named(name).exists?
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def mollie_mandated?
|
|
244
|
+
mollie_mandates.valid_status.exists?
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def mollie_subscription(name: "default")
|
|
248
|
+
mollie_subscriptions.active.named(name).first
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def mollie_mandate
|
|
252
|
+
mollie_mandates.valid_status.first
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def mollie_payment_methods(**options)
|
|
256
|
+
MolliePay.payment_methods(**options)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# === Sales Invoices (beta) ===
|
|
260
|
+
|
|
261
|
+
def mollie_create_sales_invoice(lines:, status: "draft", recipient: nil, **options)
|
|
262
|
+
customer = mollie_customer!
|
|
263
|
+
resolved_recipient = recipient || build_sales_invoice_recipient
|
|
264
|
+
mollie_si = MolliePay.create_sales_invoice(status: status, recipient: resolved_recipient, lines: lines, **options)
|
|
265
|
+
SalesInvoice.record_from_mollie(mollie_si, customer)
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def mollie_mark_invoice_paid(invoice, source: "manual")
|
|
269
|
+
verify_sales_invoice_ownership!(invoice)
|
|
270
|
+
|
|
271
|
+
raise MolliePay::InvoiceNotUpdatable, "Only issued invoices can be marked as paid" unless invoice.issued?
|
|
272
|
+
|
|
273
|
+
MolliePay.update_sales_invoice(
|
|
274
|
+
invoice.mollie_id,
|
|
275
|
+
status: "paid",
|
|
276
|
+
payment_details: { source: source }
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
invoice.mark_paid! do
|
|
280
|
+
on_mollie_sales_invoice_paid(invoice)
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def mollie_sales_invoice(id)
|
|
285
|
+
MolliePay.sales_invoice(id)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# === Hooks — override these in your model ===
|
|
289
|
+
|
|
290
|
+
def on_mollie_payment_paid(payment) ; end
|
|
291
|
+
def on_mollie_payment_authorized(payment) ; end
|
|
292
|
+
def on_mollie_payment_failed(payment) ; end
|
|
293
|
+
def on_mollie_payment_canceled(payment) ; end
|
|
294
|
+
def on_mollie_payment_expired(payment) ; end
|
|
295
|
+
def on_mollie_first_payment_paid(payment) ; end
|
|
296
|
+
def on_mollie_subscription_charged(payment) ; end
|
|
297
|
+
def on_mollie_subscription_canceled(subscription) ; end
|
|
298
|
+
def on_mollie_subscription_suspended(subscription); end
|
|
299
|
+
def on_mollie_subscription_completed(subscription); end
|
|
300
|
+
def on_mollie_mandate_created(mandate) ; end
|
|
301
|
+
def on_mollie_refund_processed(refund) ; end
|
|
302
|
+
def on_mollie_chargeback_received(chargeback) ; end
|
|
303
|
+
def on_mollie_chargeback_reversed(chargeback) ; end
|
|
304
|
+
def on_mollie_subscription_swapped(subscription, previous_amount:, previous_interval:) ; end
|
|
305
|
+
def on_mollie_sales_invoice_issued(sales_invoice) ; end
|
|
306
|
+
def on_mollie_sales_invoice_paid(sales_invoice) ; end
|
|
307
|
+
|
|
308
|
+
private
|
|
309
|
+
|
|
310
|
+
def verify_mandate_ownership!(mandate)
|
|
311
|
+
raise MolliePay::Error, "Mandate does not belong to this customer" unless mollie_mandates.exists?(id: mandate.id)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def verify_subscription_mandate!(mandate)
|
|
315
|
+
if mandate.nil?
|
|
316
|
+
raise MolliePay::MandateRequired, "No valid mandate on file" unless mollie_mandated?
|
|
317
|
+
nil
|
|
318
|
+
else
|
|
319
|
+
unless mandate.is_a?(Mandate) && mandate.persisted?
|
|
320
|
+
raise MolliePay::Error, "Mandate does not belong to this customer"
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
status, mollie_id = mollie_mandates.where(id: mandate.id).pick(:status, :mollie_id)
|
|
324
|
+
if status.nil?
|
|
325
|
+
raise MolliePay::Error, "Mandate does not belong to this customer"
|
|
326
|
+
elsif status != "valid"
|
|
327
|
+
raise MolliePay::MandateRequired, "Mandate is not valid"
|
|
328
|
+
else
|
|
329
|
+
mollie_id
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def verify_payment_ownership!(payment)
|
|
335
|
+
raise MolliePay::Error, "Payment does not belong to this customer" unless mollie_payments.exists?(id: payment.id)
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# Fetches the live payment so a delayed webhook cannot allow or block the
|
|
339
|
+
# operation wrongly.
|
|
340
|
+
def verify_payment_authorized!(payment)
|
|
341
|
+
mollie_payment = payment.mollie_record
|
|
342
|
+
raise MolliePay::PaymentNotAuthorized, "Payment #{payment.mollie_id} is not authorized" unless mollie_payment.authorized?
|
|
343
|
+
|
|
344
|
+
mollie_payment
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
def create_mollie_payment(amount:, description:, redirect_url:, method:, metadata:, sequence_type:, extra_params: {})
|
|
348
|
+
customer = mollie_customer!
|
|
349
|
+
|
|
350
|
+
payment = Payment.new(
|
|
351
|
+
id: SecureRandom.uuid_v7,
|
|
352
|
+
customer: customer,
|
|
353
|
+
amount: amount,
|
|
354
|
+
currency: MolliePay.configuration.currency,
|
|
355
|
+
sequence_type: sequence_type
|
|
356
|
+
)
|
|
357
|
+
payment.validate!
|
|
358
|
+
|
|
359
|
+
resolved_redirect_url = redirect_url || MolliePay.configuration.redirect_url_for(payment)
|
|
360
|
+
raise MolliePay::ConfigurationError, "No redirect_url provided and default_redirect_path is not configured" if resolved_redirect_url.blank?
|
|
361
|
+
|
|
362
|
+
mp = Mollie::Payment.create(
|
|
363
|
+
amount: mollie_amount(amount),
|
|
364
|
+
description: description,
|
|
365
|
+
redirectUrl: resolved_redirect_url,
|
|
366
|
+
webhookUrl: MolliePay.configuration.webhook_url,
|
|
367
|
+
customerId: customer.mollie_id,
|
|
368
|
+
sequenceType: sequence_type,
|
|
369
|
+
method: method,
|
|
370
|
+
metadata: metadata,
|
|
371
|
+
**extra_params,
|
|
372
|
+
idempotency_key: SecureRandom.uuid
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
persist_created_mollie_payment(payment, mp)
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def persist_created_mollie_payment(payment, mp)
|
|
379
|
+
payment.assign_attributes(
|
|
380
|
+
mollie_id: mp.id,
|
|
381
|
+
status: mp.status,
|
|
382
|
+
checkout_url: mp.checkout_url,
|
|
383
|
+
created_at_mollie: mp.try(:created_at)
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
saved_payment, result = Payment.save_new_mollie_record(payment)
|
|
387
|
+
if result == :inserted
|
|
388
|
+
saved_payment
|
|
389
|
+
else
|
|
390
|
+
saved_payment.attach_checkout_url!(mp.checkout_url)
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# Mollie stops a subscription itself after failed payments and sends no
|
|
395
|
+
# webhook for it, so a subscription stored as running may already be gone
|
|
396
|
+
# (404) or cancelled (422) there. Both leave it cancelled, as asked.
|
|
397
|
+
def cancel_subscription_on_mollie(subscription)
|
|
398
|
+
Mollie::Customer::Subscription.cancel(subscription.mollie_id, customer_id: mollie_customer.mollie_id)
|
|
399
|
+
rescue Mollie::ResourceNotFoundError
|
|
400
|
+
nil
|
|
401
|
+
rescue Mollie::RequestError => error
|
|
402
|
+
raise unless error.status == 422 && error.detail.to_s.match?(/has been cancell?ed/i)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# Two calls that both find no customer each create one at Mollie. The call
|
|
406
|
+
# that saves second returns the customer the first one saved and has its
|
|
407
|
+
# own deleted at Mollie by a job, so the owner is left with one customer
|
|
408
|
+
# there even when that delete has to be retried.
|
|
409
|
+
def create_mollie_customer_on_mollie
|
|
410
|
+
mc = Mollie::Customer.create(
|
|
411
|
+
name: respond_to?(:name) ? name : nil,
|
|
412
|
+
email: respond_to?(:email) ? email : nil,
|
|
413
|
+
idempotency_key: SecureRandom.uuid
|
|
414
|
+
)
|
|
415
|
+
create_mollie_customer!(mollie_id: mc.id)
|
|
416
|
+
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
|
|
417
|
+
customer = reload_mollie_customer || raise
|
|
418
|
+
DeleteMollieCustomerJob.perform_later(mc.id) unless customer.mollie_id == mc.id
|
|
419
|
+
customer
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
def verify_sales_invoice_ownership!(invoice)
|
|
423
|
+
raise MolliePay::Error, "Sales invoice does not belong to this customer" unless mollie_sales_invoices.exists?(id: invoice.id)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
def build_sales_invoice_recipient
|
|
427
|
+
if respond_to?(:organization_name) && organization_name.present?
|
|
428
|
+
{ type: "business", organization_name: organization_name }.tap do |r|
|
|
429
|
+
r[:email] = email if respond_to?(:email) && email.present?
|
|
430
|
+
r[:vat_number] = vat_number if respond_to?(:vat_number) && vat_number.present?
|
|
431
|
+
end
|
|
432
|
+
else
|
|
433
|
+
{}.tap do |r|
|
|
434
|
+
r[:type] = "consumer"
|
|
435
|
+
r[:given_name] = name if respond_to?(:name) && name.present?
|
|
436
|
+
r[:email] = email if respond_to?(:email) && email.present?
|
|
437
|
+
end
|
|
438
|
+
end
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
def mollie_amount(amount)
|
|
442
|
+
MolliePay.mollie_amount(amount)
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class Chargeback < ApplicationRecord
|
|
3
|
+
belongs_to :payment
|
|
4
|
+
|
|
5
|
+
validates :mollie_id, presence: true, uniqueness: true
|
|
6
|
+
validates :amount, presence: true, numericality: { greater_than: 0 }
|
|
7
|
+
validate :amount_within_precision
|
|
8
|
+
validates :currency, presence: true
|
|
9
|
+
|
|
10
|
+
def self.sync_for_payment(payment)
|
|
11
|
+
mollie_chargebacks = payment.mollie_record.chargebacks
|
|
12
|
+
return if mollie_chargebacks.blank?
|
|
13
|
+
|
|
14
|
+
billable = payment.customer.owner
|
|
15
|
+
events = []
|
|
16
|
+
|
|
17
|
+
mollie_chargebacks.each do |mc|
|
|
18
|
+
event = sync_chargeback(payment, mc)
|
|
19
|
+
events << event if event
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
events.each do |type, chargeback|
|
|
23
|
+
case type
|
|
24
|
+
when :received then NotifyBillableJob.perform_later(billable, :on_mollie_chargeback_received, chargeback)
|
|
25
|
+
when :reversed then NotifyBillableJob.perform_later(billable, :on_mollie_chargeback_reversed, chargeback)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def reversed?
|
|
31
|
+
reversed_at.present?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def mollie_record
|
|
35
|
+
Mollie::Payment::Chargeback.get(mollie_id, payment_id: payment.mollie_id)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def mollie_amount
|
|
39
|
+
MolliePay.mollie_amount(amount, currency: currency)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private_class_method def self.sync_chargeback(payment, mc)
|
|
43
|
+
chargeback = find_or_initialize_by(mollie_id: mc.id)
|
|
44
|
+
attrs = {
|
|
45
|
+
payment_id: payment.id,
|
|
46
|
+
amount: mollie_value_to_decimal(mc.amount),
|
|
47
|
+
currency: mc.amount.currency,
|
|
48
|
+
reason: extract_reason(mc),
|
|
49
|
+
created_at_mollie: type_for_attribute("created_at_mollie").cast(mc.created_at)
|
|
50
|
+
}
|
|
51
|
+
reversed_at = type_for_attribute("reversed_at").cast(mc.reversed_at)
|
|
52
|
+
|
|
53
|
+
if chargeback.new_record?
|
|
54
|
+
chargeback.assign_attributes(attrs.merge(payment: payment, reversed_at: reversed_at))
|
|
55
|
+
chargeback, insert_result = save_new_mollie_record(chargeback)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
if insert_result == :inserted
|
|
59
|
+
[ :received, chargeback ]
|
|
60
|
+
else
|
|
61
|
+
update_existing_chargeback(chargeback, payment: payment, attrs: attrs, reversed_at: reversed_at)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private_class_method def self.update_existing_chargeback(chargeback, payment:, attrs:, reversed_at:)
|
|
66
|
+
chargeback.assign_attributes(
|
|
67
|
+
attrs.merge(
|
|
68
|
+
payment: payment,
|
|
69
|
+
reversed_at: reversed_at.nil? ? chargeback.reversed_at : reversed_at
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
chargeback.validate!
|
|
73
|
+
|
|
74
|
+
update_attrs = attrs.merge(updated_at: Time.current)
|
|
75
|
+
|
|
76
|
+
if reversed_at.present? && chargeback.reversed_at_in_database.nil? &&
|
|
77
|
+
where(id: chargeback.id, reversed_at: nil).update_all(update_attrs.merge(reversed_at: reversed_at)) == 1
|
|
78
|
+
chargeback.reload
|
|
79
|
+
[ :reversed, chargeback ]
|
|
80
|
+
else
|
|
81
|
+
where(id: chargeback.id).update_all(update_attrs)
|
|
82
|
+
chargeback.reload
|
|
83
|
+
nil
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The Mollie API returns reason as an object: {"code": "AM04", "description": "Insufficient funds"}
|
|
88
|
+
# The mollie-api-ruby SDK does not expose this via attr_accessor. Access
|
|
89
|
+
# via the raw attributes hash instead.
|
|
90
|
+
private_class_method def self.extract_reason(mollie_chargeback)
|
|
91
|
+
reason = (mollie_chargeback.try(:attributes) || {})["reason"]
|
|
92
|
+
return nil if reason.nil?
|
|
93
|
+
|
|
94
|
+
if reason.is_a?(Hash)
|
|
95
|
+
description = reason["description"]
|
|
96
|
+
code = reason["code"]
|
|
97
|
+
code ? "#{description} (#{code})" : description
|
|
98
|
+
else
|
|
99
|
+
reason.to_s
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class Customer < ApplicationRecord
|
|
3
|
+
belongs_to :owner, polymorphic: true
|
|
4
|
+
|
|
5
|
+
has_many :payments, dependent: :destroy
|
|
6
|
+
has_many :subscriptions, dependent: :destroy
|
|
7
|
+
has_many :mandates, dependent: :destroy
|
|
8
|
+
has_many :sales_invoices, dependent: :destroy
|
|
9
|
+
|
|
10
|
+
validates :mollie_id, presence: true, uniqueness: true
|
|
11
|
+
|
|
12
|
+
def mollie_record
|
|
13
|
+
Mollie::Customer.get(mollie_id)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def active_subscription
|
|
17
|
+
subscriptions.active.first
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def valid_mandate
|
|
21
|
+
mandates.valid_status.first
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def subscribed?
|
|
25
|
+
subscriptions.active.exists?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def mandated?
|
|
29
|
+
mandates.valid_status.exists?
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class Mandate < ApplicationRecord
|
|
3
|
+
include MolliePay::Syncable
|
|
4
|
+
|
|
5
|
+
STATUSES = %w[ pending valid invalid ].freeze
|
|
6
|
+
ALLOWED_FROM = {
|
|
7
|
+
"pending" => [].freeze,
|
|
8
|
+
"valid" => %w[ pending ].freeze,
|
|
9
|
+
"invalid" => %w[ pending valid ].freeze
|
|
10
|
+
}.freeze
|
|
11
|
+
|
|
12
|
+
belongs_to :customer
|
|
13
|
+
has_many :payments, dependent: :nullify
|
|
14
|
+
|
|
15
|
+
validates :mollie_id, presence: true, uniqueness: true
|
|
16
|
+
validates :status, inclusion: { in: STATUSES }
|
|
17
|
+
validates :method, presence: true
|
|
18
|
+
|
|
19
|
+
scope :valid_status, -> { where(status: "valid") }
|
|
20
|
+
scope :pending, -> { where(status: "pending") }
|
|
21
|
+
|
|
22
|
+
def self.record_from_mollie_mandate(mollie_mandate, customer, payment: nil)
|
|
23
|
+
mandate = find_or_initialize_by(mollie_id: mollie_mandate.id)
|
|
24
|
+
|
|
25
|
+
mandate, outcome = sync_from_mollie(
|
|
26
|
+
mandate,
|
|
27
|
+
target_status: mollie_mandate.status,
|
|
28
|
+
attributes: {
|
|
29
|
+
customer_id: customer.id,
|
|
30
|
+
method: mollie_mandate.method
|
|
31
|
+
},
|
|
32
|
+
coalesce_attributes: mollie_mandate.status == "valid" ? { mandated_at: Time.current } : {},
|
|
33
|
+
association_attributes: { customer: customer }
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
if payment
|
|
37
|
+
begin
|
|
38
|
+
payment.attach_mandate!(mandate)
|
|
39
|
+
rescue ActiveRecord::RecordNotFound
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
if %i[ inserted transitioned ].include?(outcome) && mollie_mandate.status == "valid"
|
|
45
|
+
NotifyBillableJob.perform_later(customer.owner, :on_mollie_mandate_created, mandate)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
mandate
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.record_from_mollie_payment(payment, mollie_payment = nil)
|
|
52
|
+
mollie_payment ||= payment.mollie_record
|
|
53
|
+
mollie_mandate_id = mollie_payment.try(:mandate_id)
|
|
54
|
+
return if mollie_mandate_id.blank?
|
|
55
|
+
|
|
56
|
+
customer = payment.customer
|
|
57
|
+
mollie_mandate = Mollie::Customer::Mandate.get(mollie_mandate_id, customer_id: customer.mollie_id)
|
|
58
|
+
mandate = record_from_mollie_mandate(mollie_mandate, customer, payment: payment)
|
|
59
|
+
RefreshPendingMandateJob.perform_later(mandate) if mandate.pending?
|
|
60
|
+
mandate
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Mollie has no mandate webhooks. A mandate that was still pending when its
|
|
64
|
+
# first payment was paid is re-fetched by RefreshPendingMandateJob, and on
|
|
65
|
+
# later webhooks for that payment when Mollie sends any.
|
|
66
|
+
def self.refresh_pending_from_mollie_payment(payment, mollie_payment)
|
|
67
|
+
mollie_mandate_id = mollie_payment.try(:mandate_id)
|
|
68
|
+
return if mollie_mandate_id.blank?
|
|
69
|
+
|
|
70
|
+
if pending.exists?(customer: payment.customer, mollie_id: mollie_mandate_id)
|
|
71
|
+
record_from_mollie_payment(payment, mollie_payment)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def mollie_record
|
|
76
|
+
Mollie::Customer::Mandate.get(mollie_id, customer_id: customer.mollie_id)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Stores a status Mollie reported and returns whether this call moved the
|
|
80
|
+
# mandate to valid. The move is one conditional UPDATE, so when the refresh
|
|
81
|
+
# job and a payment webhook both saw the mandate pending, only one of them
|
|
82
|
+
# fires on_mollie_mandate_created. Mollie never takes a valid mandate back
|
|
83
|
+
# to pending or revives an invalid mandate, so stale answers are ignored.
|
|
84
|
+
def record_status(mollie_status)
|
|
85
|
+
assign_attributes(status: mollie_status)
|
|
86
|
+
validate!
|
|
87
|
+
|
|
88
|
+
coalesce_attributes = mollie_status == "valid" ? { mandated_at: Time.current } : {}
|
|
89
|
+
outcome = sync_record_update(
|
|
90
|
+
target_status: mollie_status,
|
|
91
|
+
coalesce_attributes: coalesce_attributes
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
outcome == :transitioned && mollie_status == "valid"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def mark_invalid!
|
|
98
|
+
conditional_sync_update!(
|
|
99
|
+
from_status: ALLOWED_FROM.fetch("invalid"),
|
|
100
|
+
attributes: { status: "invalid" }
|
|
101
|
+
)
|
|
102
|
+
self
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def valid_status?
|
|
106
|
+
status == "valid"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def pending?
|
|
110
|
+
status == "pending"
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|