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,56 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class ProcessWebhookJob < ApplicationJob
|
|
3
|
+
queue_as :default
|
|
4
|
+
|
|
5
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
6
|
+
retry_on MolliePay::StaleSyncError, wait: :polynomially_longer, attempts: 2
|
|
7
|
+
discard_on Mollie::ResourceNotFoundError
|
|
8
|
+
|
|
9
|
+
def perform(mollie_id)
|
|
10
|
+
case mollie_id
|
|
11
|
+
when /\Atr_/ then sync_payment(mollie_id)
|
|
12
|
+
when /\Asub_/ then sync_subscription(mollie_id)
|
|
13
|
+
when /\Are_/ then sync_refund(mollie_id)
|
|
14
|
+
when /\Astl_/ then sync_settlement(mollie_id)
|
|
15
|
+
else
|
|
16
|
+
Rails.logger.warn("[MolliePay] Unknown webhook prefix for mollie_id: #{mollie_id}")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def sync_payment(mollie_id)
|
|
23
|
+
with_stale_sync_retry do
|
|
24
|
+
expected_version = Payment.where(mollie_id: mollie_id).pick(:sync_version)
|
|
25
|
+
Payment.record_from_mollie(Mollie::Payment.get(mollie_id), expected_version: expected_version)
|
|
26
|
+
end
|
|
27
|
+
rescue ActiveRecord::RecordNotFound
|
|
28
|
+
Rails.logger.info("[MolliePay] Discarding payment webhook — local record not found: #{mollie_id}")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def sync_subscription(mollie_id)
|
|
32
|
+
with_stale_sync_retry do
|
|
33
|
+
subscription = Subscription.includes(:customer).find_by!(mollie_id: mollie_id)
|
|
34
|
+
expected_version = subscription.sync_version
|
|
35
|
+
ms = Mollie::Customer::Subscription.get(mollie_id, customer_id: subscription.customer.mollie_id)
|
|
36
|
+
Subscription.record_from_mollie(ms, expected_version: expected_version)
|
|
37
|
+
end
|
|
38
|
+
rescue ActiveRecord::RecordNotFound
|
|
39
|
+
Rails.logger.info("[MolliePay] Discarding subscription webhook — local record not found: #{mollie_id}")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def sync_refund(mollie_id)
|
|
43
|
+
refund = Refund.find_by!(mollie_id: mollie_id)
|
|
44
|
+
Refund.record_from_mollie(
|
|
45
|
+
Mollie::Payment::Refund.get(mollie_id, payment_id: refund.payment.mollie_id)
|
|
46
|
+
)
|
|
47
|
+
rescue ActiveRecord::RecordNotFound
|
|
48
|
+
Rails.logger.info("[MolliePay] Discarding refund webhook — local record not found: #{mollie_id}")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def sync_settlement(mollie_id)
|
|
52
|
+
settlement = Mollie::Settlement.get(mollie_id)
|
|
53
|
+
ActiveSupport::Notifications.instrument("mollie_pay.settlement_received", settlement: settlement)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# Mollie sends no webhook for a mandate, and none for a first payment once it
|
|
3
|
+
# is paid. A mandate that was still pending at that moment is re-fetched here
|
|
4
|
+
# until Mollie reports it valid or invalid. Mandate.record_from_mollie_mandate
|
|
5
|
+
# fires on_mollie_mandate_created on the transition to valid.
|
|
6
|
+
class RefreshPendingMandateJob < ApplicationJob
|
|
7
|
+
queue_as :default
|
|
8
|
+
|
|
9
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
10
|
+
retry_on MandatePending, wait: :polynomially_longer, attempts: 10
|
|
11
|
+
discard_on ActiveJob::DeserializationError
|
|
12
|
+
discard_on Mollie::ResourceNotFoundError
|
|
13
|
+
|
|
14
|
+
def perform(mandate)
|
|
15
|
+
refreshed = Mandate.record_from_mollie_mandate(mandate.mollie_record, mandate.customer)
|
|
16
|
+
|
|
17
|
+
if refreshed.pending?
|
|
18
|
+
raise MandatePending, "Mandate #{refreshed.mollie_id} is still pending"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# Reconciles chargebacks for a payment asynchronously after Payment.record_from_mollie
|
|
3
|
+
# persists an amount_charged_back change, so transient Mollie API errors retry
|
|
4
|
+
# independently without being skipped by payment convergence checks.
|
|
5
|
+
class SyncChargebacksJob < ApplicationJob
|
|
6
|
+
queue_as :default
|
|
7
|
+
|
|
8
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
9
|
+
discard_on ActiveJob::DeserializationError
|
|
10
|
+
discard_on Mollie::ResourceNotFoundError
|
|
11
|
+
|
|
12
|
+
def perform(payment)
|
|
13
|
+
reloaded = Payment.includes(customer: :owner).find(payment.id)
|
|
14
|
+
Chargeback.sync_for_payment(reloaded)
|
|
15
|
+
rescue ActiveRecord::RecordNotFound
|
|
16
|
+
Rails.logger.info("[MolliePay] Discarding chargeback sync — local payment not found: #{payment.mollie_id}")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class SyncSubscriptionJob < ApplicationJob
|
|
3
|
+
queue_as :default
|
|
4
|
+
|
|
5
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
6
|
+
retry_on MolliePay::StaleSyncError, wait: :polynomially_longer, attempts: 2
|
|
7
|
+
discard_on ActiveJob::DeserializationError
|
|
8
|
+
|
|
9
|
+
def perform(subscription)
|
|
10
|
+
with_stale_sync_retry do
|
|
11
|
+
reloaded = Subscription.includes(:customer).find(subscription.id)
|
|
12
|
+
reloaded.sync_with_mollie(expected_version: reloaded.sync_version)
|
|
13
|
+
end
|
|
14
|
+
rescue ActiveRecord::RecordNotFound
|
|
15
|
+
Rails.logger.info("[MolliePay] Discarding subscription sync — local record not found: #{subscription.mollie_id}")
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# Mollie sends no webhook when a subscription changes status on its own.
|
|
3
|
+
# Schedule this job (for example nightly) so running subscriptions and any
|
|
4
|
+
# missed subscription payments stay in sync with Mollie.
|
|
5
|
+
class SyncSubscriptionsJob < ApplicationJob
|
|
6
|
+
queue_as :default
|
|
7
|
+
|
|
8
|
+
retry_on StandardError, wait: :polynomially_longer, attempts: 5
|
|
9
|
+
|
|
10
|
+
def perform
|
|
11
|
+
Subscription.where(status: Subscription::ACTIVE_STATUSES).select(:id).find_each do |subscription|
|
|
12
|
+
SyncSubscriptionJob.set(queue: queue_name).perform_later(subscription)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
# UUIDv7 string primary keys for all MolliePay models. Copied verbatim across
|
|
3
|
+
# the engine family (reservr, hunion) per the engine-family charter — the
|
|
4
|
+
# engines share the convention, never a gem. UUIDv7 is time-ordered, so it
|
|
5
|
+
# indexes well as a PK while staying merge/replica-safe.
|
|
6
|
+
module HasUuid
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
before_create :set_uuid
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def set_uuid
|
|
16
|
+
self.id ||= SecureRandom.uuid_v7
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
module Syncable
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
# Distinguishes untracked direct callers from a tracked webhook job that
|
|
6
|
+
# found no local row before fetching from Mollie (expected_version: nil).
|
|
7
|
+
UNTRACKED = Object.new.freeze
|
|
8
|
+
|
|
9
|
+
class_methods do
|
|
10
|
+
def strictly_stale?(current_status, target_status)
|
|
11
|
+
current_status != target_status && !self::ALLOWED_FROM.fetch(target_status).include?(current_status)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sync_versioned?
|
|
15
|
+
column_names.include?("sync_version")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def sync_from_mollie(record, target_status:, attributes:, coalesce_attributes: {}, insert_attributes: {}, association_attributes: {}, expected_version: UNTRACKED, transition_groups: nil)
|
|
19
|
+
if record.new_record?
|
|
20
|
+
guard_mid_flight_deletion!(expected_version)
|
|
21
|
+
|
|
22
|
+
extra_insert = insert_attributes.respond_to?(:call) ? insert_attributes.call : insert_attributes
|
|
23
|
+
resolved_coalesce = coalesce_attributes.respond_to?(:call) ? coalesce_attributes.call(record) : coalesce_attributes
|
|
24
|
+
insert_attrs = attributes
|
|
25
|
+
.merge(resolved_coalesce.compact)
|
|
26
|
+
.merge(extra_insert)
|
|
27
|
+
.merge(association_attributes)
|
|
28
|
+
.merge(status: target_status)
|
|
29
|
+
|
|
30
|
+
record.assign_attributes(insert_attrs)
|
|
31
|
+
record, insert_result = save_new_mollie_record(record)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
if insert_result == :inserted
|
|
35
|
+
[ record, :inserted ]
|
|
36
|
+
else
|
|
37
|
+
yield record if block_given?
|
|
38
|
+
|
|
39
|
+
resolved_coalesce = coalesce_attributes.respond_to?(:call) ? coalesce_attributes.call(record) : coalesce_attributes
|
|
40
|
+
record.assign_attributes(attributes.merge(association_attributes).merge(status: target_status))
|
|
41
|
+
record.validate!
|
|
42
|
+
|
|
43
|
+
outcome = sync_existing_record(
|
|
44
|
+
record,
|
|
45
|
+
target_status: target_status,
|
|
46
|
+
attributes: attributes,
|
|
47
|
+
coalesce_attributes: resolved_coalesce,
|
|
48
|
+
expected_version: expected_version,
|
|
49
|
+
transition_groups: transition_groups
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
[ record, outcome ]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def guard_mid_flight_deletion!(expected_version)
|
|
59
|
+
if expected_version.is_a?(Integer)
|
|
60
|
+
raise ActiveRecord::RecordNotFound, "#{name} was deleted during sync"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def sync_existing_record(record, target_status:, attributes:, coalesce_attributes: {}, expected_version: UNTRACKED, transition_groups: nil)
|
|
65
|
+
tracked = !expected_version.equal?(UNTRACKED)
|
|
66
|
+
|
|
67
|
+
if tracked && (expected_version.nil? || (sync_versioned? && record.sync_version_in_database != expected_version))
|
|
68
|
+
return resolve_sync_miss(
|
|
69
|
+
record,
|
|
70
|
+
target_status: target_status,
|
|
71
|
+
attributes: attributes,
|
|
72
|
+
coalesce_attributes: coalesce_attributes,
|
|
73
|
+
tracked: true
|
|
74
|
+
)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
base_scope = where(id: record.id)
|
|
78
|
+
base_scope = base_scope.where(sync_version: expected_version) if tracked
|
|
79
|
+
|
|
80
|
+
outcome = if record.status_in_database == target_status
|
|
81
|
+
apply_same_status_update(base_scope, record, target_status: target_status, attributes: attributes, coalesce_attributes: coalesce_attributes) ||
|
|
82
|
+
apply_transition_update(base_scope, record, target_status: target_status, attributes: attributes, coalesce_attributes: coalesce_attributes, transition_groups: transition_groups)
|
|
83
|
+
else
|
|
84
|
+
apply_transition_update(base_scope, record, target_status: target_status, attributes: attributes, coalesce_attributes: coalesce_attributes, transition_groups: transition_groups) ||
|
|
85
|
+
apply_same_status_update(base_scope, record, target_status: target_status, attributes: attributes, coalesce_attributes: coalesce_attributes)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
outcome || resolve_sync_miss(
|
|
89
|
+
record,
|
|
90
|
+
target_status: target_status,
|
|
91
|
+
attributes: attributes,
|
|
92
|
+
coalesce_attributes: coalesce_attributes,
|
|
93
|
+
tracked: tracked
|
|
94
|
+
)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def apply_transition_update(base_scope, record, target_status:, attributes:, coalesce_attributes:, transition_groups:)
|
|
98
|
+
groups = (transition_groups || [ { from: self::ALLOWED_FROM.fetch(target_status), outcome: :transitioned } ]).reject { |group| group[:from].empty? }
|
|
99
|
+
return nil if groups.empty?
|
|
100
|
+
|
|
101
|
+
sql = build_sync_update_sql(
|
|
102
|
+
attributes: attributes.merge(status: target_status),
|
|
103
|
+
coalesce_attributes: coalesce_attributes
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
matched = groups.find { |group| base_scope.where(status: group[:from]).update_all(sql) == 1 }
|
|
107
|
+
if matched
|
|
108
|
+
record.reload
|
|
109
|
+
matched[:outcome]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def apply_same_status_update(base_scope, record, target_status:, attributes:, coalesce_attributes:)
|
|
114
|
+
sql = build_sync_update_sql(
|
|
115
|
+
attributes: attributes,
|
|
116
|
+
coalesce_attributes: coalesce_attributes
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
if base_scope.where(status: target_status).update_all(sql) == 1
|
|
120
|
+
record.reload
|
|
121
|
+
:updated
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def build_sync_update_sql(attributes:, coalesce_attributes: {}, increment_version: sync_versioned?, now: Time.current)
|
|
126
|
+
assignments = []
|
|
127
|
+
values = []
|
|
128
|
+
|
|
129
|
+
attributes.each do |column, value|
|
|
130
|
+
assignments << "#{connection.quote_column_name(column)} = ?"
|
|
131
|
+
values << cast_sync_value(column, value)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
coalesce_attributes.each do |column, value|
|
|
135
|
+
cast_value = cast_sync_value(column, value)
|
|
136
|
+
next if cast_value.nil?
|
|
137
|
+
|
|
138
|
+
quoted = connection.quote_column_name(column)
|
|
139
|
+
assignments << "#{quoted} = COALESCE(#{quoted}, ?)"
|
|
140
|
+
values << cast_value
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
assignments << "sync_version = sync_version + 1" if increment_version
|
|
144
|
+
assignments << "updated_at = ?"
|
|
145
|
+
values << now
|
|
146
|
+
|
|
147
|
+
[ assignments.join(", "), *values ]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def resolve_sync_miss(record, target_status:, attributes:, coalesce_attributes:, tracked:)
|
|
151
|
+
record.reload
|
|
152
|
+
|
|
153
|
+
if strictly_stale?(record.status, target_status)
|
|
154
|
+
:stale
|
|
155
|
+
elsif record.status == target_status &&
|
|
156
|
+
attributes_match?(record, attributes) &&
|
|
157
|
+
coalesce_attributes_settled?(record, coalesce_attributes)
|
|
158
|
+
:converged
|
|
159
|
+
elsif tracked
|
|
160
|
+
raise MolliePay::StaleSyncError, "Concurrent update detected on #{name}##{record.id}"
|
|
161
|
+
else
|
|
162
|
+
:stale
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def attributes_match?(record, attributes)
|
|
167
|
+
attributes.all? do |column, expected|
|
|
168
|
+
record.read_attribute(column) == cast_sync_value(column, expected)
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def coalesce_attributes_settled?(record, coalesce_attributes)
|
|
173
|
+
coalesce_attributes.all? do |column, expected|
|
|
174
|
+
cast_sync_value(column, expected).nil? || !record.read_attribute(column).nil?
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def cast_sync_value(column, value)
|
|
179
|
+
type_for_attribute(column.to_s).cast(value)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
private
|
|
184
|
+
|
|
185
|
+
def conditional_sync_update!(from_status: nil, scope: nil, attributes: {}, coalesce_attributes: {}, now: Time.current)
|
|
186
|
+
relation = scope || self.class.where(id: id, status: from_status)
|
|
187
|
+
sql = self.class.send(
|
|
188
|
+
:build_sync_update_sql,
|
|
189
|
+
attributes: attributes,
|
|
190
|
+
coalesce_attributes: coalesce_attributes,
|
|
191
|
+
now: now
|
|
192
|
+
)
|
|
193
|
+
rows = relation.update_all(sql)
|
|
194
|
+
reload
|
|
195
|
+
rows == 1
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def sync_record_update(target_status:, attributes: {}, coalesce_attributes: {})
|
|
199
|
+
self.class.send(
|
|
200
|
+
:sync_existing_record,
|
|
201
|
+
self,
|
|
202
|
+
target_status: target_status,
|
|
203
|
+
attributes: attributes,
|
|
204
|
+
coalesce_attributes: coalesce_attributes
|
|
205
|
+
)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module MolliePay
|
|
2
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
3
|
+
self.abstract_class = true
|
|
4
|
+
|
|
5
|
+
include MolliePay::HasUuid
|
|
6
|
+
|
|
7
|
+
# Mollie sends amounts as { currency:, value: "10.00" } (a decimal string).
|
|
8
|
+
# Parse straight to exact BigDecimal; never through cents or Float.
|
|
9
|
+
def self.mollie_value_to_decimal(mollie_amount)
|
|
10
|
+
BigDecimal(mollie_amount.value)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.save_new_mollie_record(record)
|
|
14
|
+
record.save!
|
|
15
|
+
[ record, :inserted ]
|
|
16
|
+
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => error
|
|
17
|
+
if error.is_a?(ActiveRecord::RecordNotUnique) || mollie_id_taken_error?(error)
|
|
18
|
+
existing = find_by(mollie_id: record.mollie_id)
|
|
19
|
+
if existing
|
|
20
|
+
[ existing, :conflict ]
|
|
21
|
+
else
|
|
22
|
+
raise
|
|
23
|
+
end
|
|
24
|
+
else
|
|
25
|
+
raise
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private_class_method def self.mollie_id_taken_error?(error)
|
|
30
|
+
errors = error.record.errors
|
|
31
|
+
errors.attribute_names == [ :mollie_id ] && errors.of_kind?(:mollie_id, :taken)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
# The column would round a fifth decimal away on write. Refuse it
|
|
36
|
+
# instead, by looking at the amount as it was given.
|
|
37
|
+
def amount_within_precision
|
|
38
|
+
given = BigDecimal(amount_before_type_cast.to_s, exception: false)
|
|
39
|
+
|
|
40
|
+
if given && !MolliePay.amount_within_precision?(given)
|
|
41
|
+
errors.add(:amount, "must be at most #{MolliePay::MAX_AMOUNT.to_i} with at most #{MolliePay::MONEY_SCALE} decimal places")
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|