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
data/lib/mollie_pay.rb ADDED
@@ -0,0 +1,182 @@
1
+ require "mollie-api-ruby"
2
+ require "mollie/sales_invoice"
3
+ require "mollie_pay/version"
4
+ require "mollie_pay/errors"
5
+ require "mollie_pay/configuration"
6
+ require "mollie_pay/webhook_signature"
7
+ require "mollie_pay/request_signature"
8
+ require "mollie_pay/engine"
9
+
10
+ module MolliePay
11
+ PAYMENT_LINE_MONEY_FIELDS = %i[ unitPrice totalAmount vatAmount discountAmount ].freeze
12
+ private_constant :PAYMENT_LINE_MONEY_FIELDS
13
+
14
+ # Currencies Mollie expects without decimals ("1000", not "1000.00").
15
+ ZERO_DECIMAL_CURRENCIES = %w[ ISK JPY ].freeze
16
+
17
+ # Money is a decimal(15,4) column and a BigDecimal in Ruby. SQLite keeps a
18
+ # decimal as a REAL, exact to fifteen significant digits, so amounts stop at
19
+ # a hundred million: four digits below the edge, which leaves room for sums.
20
+ MAX_AMOUNT = BigDecimal("100_000_000")
21
+ MONEY_SCALE = 4
22
+
23
+ # Convert an exact amount to Mollie's wire format { currency:, value: "10.00" }.
24
+ # This is the one place an amount is rounded: half up, to the currency's
25
+ # minor unit, in BigDecimal and never through a Float.
26
+ #
27
+ # MolliePay.mollie_amount(BigDecimal("10.125")) # => { currency: "EUR", value: "10.13" }
28
+ # MolliePay.mollie_amount(BigDecimal("1000"), currency: "JPY") # => { currency: "JPY", value: "1000" }
29
+ #
30
+ def self.mollie_amount(amount, currency: configuration.currency)
31
+ decimal = BigDecimal(amount.to_s)
32
+ raise InvalidAmount, "#{amount} is not an amount Mollie can take" unless amount_within_precision?(decimal)
33
+
34
+ places = ZERO_DECIMAL_CURRENCIES.include?(currency) ? 0 : 2
35
+ rounded = decimal.round(places, BigDecimal::ROUND_HALF_UP)
36
+ rounded = BigDecimal("0") if rounded.zero?
37
+ whole, fraction = rounded.to_s("F").split(".")
38
+
39
+ { currency: currency, value: places.zero? ? whole : "#{whole}.#{fraction.ljust(places, "0")}" }
40
+ end
41
+
42
+ def self.amount_within_precision?(decimal)
43
+ decimal.finite? && decimal.abs <= MAX_AMOUNT && decimal.scale <= MONEY_SCALE
44
+ end
45
+
46
+ # List enabled payment methods. Optionally filter by amount and currency.
47
+ # Returns Mollie SDK objects directly (Mollie::List of Mollie::Method).
48
+ #
49
+ # MolliePay.payment_methods
50
+ # MolliePay.payment_methods(amount: BigDecimal("10.00"), locale: "nl_NL")
51
+ # MolliePay.payment_methods(amount: BigDecimal("10.00"), currency: "EUR", include: "pricing")
52
+ #
53
+ def self.payment_methods(amount: nil, currency: nil, locale: nil, **options)
54
+ params = options
55
+ if amount
56
+ params[:amount] = mollie_amount(amount, currency: currency || configuration.currency)
57
+ end
58
+ params[:locale] = locale if locale
59
+ Mollie::Method.all(params)
60
+ end
61
+
62
+ # Get a single payment method by its ID.
63
+ # Returns a Mollie::Method object.
64
+ #
65
+ # MolliePay.payment_method("ideal")
66
+ # MolliePay.payment_method("creditcard", locale: "nl_NL")
67
+ #
68
+ def self.payment_method(id, **options)
69
+ Mollie::Method.get(id, options)
70
+ end
71
+
72
+ # Create a sales invoice on Mollie (beta).
73
+ # Accepts snake_case Ruby hashes — keys are camelized before sending.
74
+ # Line item unit_price accepts a BigDecimal and is converted to Mollie format.
75
+ #
76
+ # MolliePay.create_sales_invoice(
77
+ # status: "issued",
78
+ # recipient: { type: "consumer", given_name: "Jane", family_name: "Doe", email: "jane@example.com" },
79
+ # lines: [{ description: "Pro plan", quantity: 1, vat_rate: "21.00", unit_price: BigDecimal("89.00") }],
80
+ # email_details: { subject: "Your invoice", body: "Please pay" }
81
+ # )
82
+ #
83
+ def self.create_sales_invoice(status:, recipient:, lines:, **options)
84
+ params = deep_camelize_keys(options)
85
+ params[:status] = status
86
+ params[:recipient] = deep_camelize_keys(recipient)
87
+ params[:lines] = lines.map { |line| build_sales_invoice_line(line) }
88
+ Mollie::SalesInvoice.create(params, idempotency_key: SecureRandom.uuid)
89
+ end
90
+
91
+ # Get a single sales invoice by ID.
92
+ #
93
+ # MolliePay.sales_invoice("invoice_abc123")
94
+ #
95
+ def self.sales_invoice(id)
96
+ Mollie::SalesInvoice.get(id)
97
+ end
98
+
99
+ # List all sales invoices.
100
+ #
101
+ # MolliePay.sales_invoices
102
+ #
103
+ def self.sales_invoices(**options)
104
+ Mollie::SalesInvoice.all(options)
105
+ end
106
+
107
+ # Update a sales invoice.
108
+ #
109
+ # MolliePay.update_sales_invoice("invoice_abc123", memo: "Updated memo")
110
+ #
111
+ def self.update_sales_invoice(id, **attrs)
112
+ params = deep_camelize_keys(attrs.except(:recipient, :lines))
113
+ params[:recipient] = deep_camelize_keys(attrs[:recipient]) if attrs[:recipient]
114
+ params[:lines] = attrs[:lines].map { |line| build_sales_invoice_line(line) } if attrs[:lines]
115
+ Mollie::SalesInvoice.update(id, params)
116
+ end
117
+
118
+ # Delete a draft sales invoice.
119
+ #
120
+ # MolliePay.delete_sales_invoice("invoice_abc123")
121
+ #
122
+ def self.delete_sales_invoice(id)
123
+ Mollie::SalesInvoice.delete(id)
124
+ end
125
+
126
+ # Build the optional Create Payment parameters for order lines, addresses,
127
+ # capture mode and locale. Accepts snake_case Ruby hashes — keys are
128
+ # camelized before sending. Line money fields (unit_price, total_amount,
129
+ # vat_amount, discount_amount) accept a BigDecimal and are converted to
130
+ # Mollie format; a wire-format hash passes through unchanged.
131
+ # Only the keys that were given appear in the result.
132
+ #
133
+ # MolliePay.build_payment_params(
134
+ # capture_mode: "manual",
135
+ # billing_address: { given_name: "Jan", family_name: "Jansen", street_and_number: "Keizersgracht 126",
136
+ # postal_code: "1015 CW", city: "Amsterdam", country: "NL", email: "jan@example.com" },
137
+ # lines: [{ description: "Widget", quantity: 1, unit_price: BigDecimal("121.00"),
138
+ # total_amount: BigDecimal("121.00"), vat_rate: "21.00", vat_amount: BigDecimal("21.00") }]
139
+ # )
140
+ #
141
+ def self.build_payment_params(lines: nil, billing_address: nil, shipping_address: nil, capture_mode: nil, locale: nil)
142
+ params = {}
143
+ params[:captureMode] = capture_mode if capture_mode
144
+ params[:locale] = locale if locale
145
+ params[:billingAddress] = deep_camelize_keys(billing_address) if billing_address
146
+ params[:shippingAddress] = deep_camelize_keys(shipping_address) if shipping_address
147
+ params[:lines] = lines.map { |line| build_payment_line(line) } if lines
148
+ params
149
+ end
150
+
151
+ def self.build_payment_line(line)
152
+ built = deep_camelize_keys(line)
153
+ PAYMENT_LINE_MONEY_FIELDS.each do |field|
154
+ built[field] = mollie_amount(built[field]) if built[field].is_a?(Numeric)
155
+ end
156
+ built
157
+ end
158
+ private_class_method :build_payment_line
159
+
160
+ def self.build_sales_invoice_line(line)
161
+ built = deep_camelize_keys(line)
162
+ if built[:unitPrice].is_a?(Numeric)
163
+ built[:unitPrice] = mollie_amount(built[:unitPrice])
164
+ end
165
+ built
166
+ end
167
+ private_class_method :build_sales_invoice_line
168
+
169
+ def self.deep_camelize_keys(hash)
170
+ hash.each_with_object({}) do |(key, value), result|
171
+ camelized_key = Mollie::Util.camelize(key)
172
+ result[camelized_key.to_sym] = if value.is_a?(Hash)
173
+ deep_camelize_keys(value)
174
+ elsif value.is_a?(Array)
175
+ value.map { |v| v.is_a?(Hash) ? deep_camelize_keys(v) : v }
176
+ else
177
+ value
178
+ end
179
+ end
180
+ end
181
+ private_class_method :deep_camelize_keys
182
+ end
@@ -0,0 +1,43 @@
1
+ namespace :mollie_pay do
2
+ namespace :request_signing do
3
+ desc "Generate an Ed25519 key pair for request signing and print both keys, once"
4
+ task :generate do
5
+ private_key, public_key = MolliePay::RequestSignature.generate_key_pair
6
+
7
+ puts "Public key. Paste it into the \"Public key\" field of the API key in the"
8
+ puts "Mollie dashboard (Developers > API keys). It is not a secret."
9
+ puts
10
+ puts public_key
11
+ puts
12
+ puts "Private key. Store it as a secret and nowhere else: it is shown once and"
13
+ puts "is not saved. Set it as MOLLIE_REQUEST_SIGNING_KEY, or put it in the"
14
+ puts "encrypted credentials. See https://github.com/peterberkenbosch/mollie_pay/blob/master/docs/request-signing.md"
15
+ puts
16
+ puts MolliePay::RequestSignature.single_line(private_key)
17
+ end
18
+
19
+ desc "Print the public key that belongs to the configured private key"
20
+ task public_key: :environment do
21
+ public_key = MolliePay.configuration.request_signing_public_key
22
+
23
+ abort "No request_signing_key is configured." if public_key.nil?
24
+ puts public_key
25
+ end
26
+
27
+ desc "Ask Mollie whether it enforces signing for the API key and accepts this app's signature"
28
+ task verify: :environment do
29
+ case MolliePay::RequestSignature.status
30
+ when :accepted
31
+ puts "OK: Mollie refuses unsigned requests for this API key and accepts this app's signature."
32
+ when :not_enforced
33
+ abort "Not enforced: Mollie accepts unsigned requests for this API key, so no public key is registered on it. " \
34
+ "Register the output of mollie_pay:request_signing:public_key in the Mollie dashboard."
35
+ when :refused
36
+ abort "Refused: Mollie enforces signing and does not accept this app's signature. The public key registered " \
37
+ "at Mollie is not this private key's, the API key is wrong, or this machine's clock is more than 5 minutes off."
38
+ when :not_configured
39
+ abort "No request_signing_key is configured."
40
+ end
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mollie_pay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Berkenbosch
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 8.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 8.1.2
28
+ - !ruby/object:Gem::Dependency
29
+ name: mollie-api-ruby
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ -
33
+ - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 4.19.0
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ -
41
+ - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.19.0
44
+ description: "A headless Rails engine that bridges Mollie payments with SaaS billing patterns. Subscriptions, mandates, one-off payments and webhooks."
45
+ email:
46
+ - info@peterberkenbosch.nl
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - MIT-LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - app/controllers/mollie_pay/application_controller.rb
55
+ - app/controllers/mollie_pay/webhook_events_controller.rb
56
+ - app/controllers/mollie_pay/webhooks_controller.rb
57
+ - app/jobs/mollie_pay/application_job.rb
58
+ - app/jobs/mollie_pay/delete_mollie_customer_job.rb
59
+ - app/jobs/mollie_pay/notify_billable_job.rb
60
+ - app/jobs/mollie_pay/process_webhook_event_job.rb
61
+ - app/jobs/mollie_pay/process_webhook_job.rb
62
+ - app/jobs/mollie_pay/refresh_pending_mandate_job.rb
63
+ - app/jobs/mollie_pay/sync_chargebacks_job.rb
64
+ - app/jobs/mollie_pay/sync_subscription_job.rb
65
+ - app/jobs/mollie_pay/sync_subscriptions_job.rb
66
+ - app/models/concerns/mollie_pay/has_uuid.rb
67
+ - app/models/concerns/mollie_pay/syncable.rb
68
+ - app/models/mollie_pay/application_record.rb
69
+ - app/models/mollie_pay/billable.rb
70
+ - app/models/mollie_pay/chargeback.rb
71
+ - app/models/mollie_pay/customer.rb
72
+ - app/models/mollie_pay/mandate.rb
73
+ - app/models/mollie_pay/payment.rb
74
+ - app/models/mollie_pay/refund.rb
75
+ - app/models/mollie_pay/sales_invoice.rb
76
+ - app/models/mollie_pay/subscription.rb
77
+ - config/routes.rb
78
+ - db/migrate/20260315142959_create_mollie_pay_customers.rb
79
+ - db/migrate/20260315143008_create_mollie_pay_mandates.rb
80
+ - db/migrate/20260315143014_create_mollie_pay_subscriptions.rb
81
+ - db/migrate/20260315143020_create_mollie_pay_payments.rb
82
+ - db/migrate/20260315143026_create_mollie_pay_refunds.rb
83
+ - db/migrate/20260315143032_create_mollie_pay_webhook_events.rb
84
+ - db/migrate/20260315143040_add_status_indexes_to_mollie_pay.rb
85
+ - db/migrate/20260315143050_add_checkout_url_to_mollie_pay_payments.rb
86
+ - db/migrate/20260316120000_make_mollie_id_nullable_on_mollie_pay_payments.rb
87
+ - db/migrate/20260317100000_make_webhook_event_mollie_id_unique.rb
88
+ - db/migrate/20260317194500_add_authorized_at_to_mollie_pay_payments.rb
89
+ - db/migrate/20260317194501_add_amount_tracking_to_mollie_pay_payments.rb
90
+ - db/migrate/20260318100000_add_name_to_mollie_pay_subscriptions.rb
91
+ - db/migrate/20260318110000_drop_mollie_pay_webhook_events.rb
92
+ - db/migrate/20260321000001_create_mollie_pay_chargebacks.rb
93
+ - db/migrate/20260326000002_create_mollie_pay_sales_invoices.rb
94
+ - db/migrate/20261115000001_change_mollie_pay_money_to_decimal.rb
95
+ - db/migrate/20261202000001_add_sync_version_to_mollie_pay_tables.rb
96
+ - db/migrate/20261202000002_add_created_at_mollie_to_mollie_pay_payments.rb
97
+ - db/migrate/20261203000001_add_mandate_to_mollie_pay_payments.rb
98
+ - docs/api.md
99
+ - docs/mandates.md
100
+ - docs/request-signing.md
101
+ - docs/testing.md
102
+ - docs/tutorial.md
103
+ - docs/webhooks.md
104
+ - lib/generators/mollie_pay/install/install_generator.rb
105
+ - lib/generators/mollie_pay/install/templates/initializer.rb.tt
106
+ - lib/mollie/sales_invoice.rb
107
+ - lib/mollie_pay.rb
108
+ - lib/mollie_pay/configuration.rb
109
+ - lib/mollie_pay/engine.rb
110
+ - lib/mollie_pay/errors.rb
111
+ - lib/mollie_pay/request_signature.rb
112
+ - lib/mollie_pay/test_fixtures/customer.json
113
+ - lib/mollie_pay/test_fixtures/payment.json
114
+ - lib/mollie_pay/test_fixtures/payment_billink.json
115
+ - lib/mollie_pay/test_fixtures/payment_with_chargebacks.json
116
+ - lib/mollie_pay/test_fixtures/refund.json
117
+ - lib/mollie_pay/test_fixtures/sales_invoice.json
118
+ - lib/mollie_pay/test_fixtures/subscription.json
119
+ - lib/mollie_pay/test_helper.rb
120
+ - lib/mollie_pay/version.rb
121
+ - lib/mollie_pay/webhook_signature.rb
122
+ - lib/tasks/mollie_pay_tasks.rake
123
+ homepage: "https://github.com/peterberkenbosch/mollie_pay"
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ homepage_uri: "https://github.com/peterberkenbosch/mollie_pay"
128
+ source_code_uri: "https://github.com/peterberkenbosch/mollie_pay"
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ -
135
+ - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: "3.3"
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ -
141
+ - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: "0"
144
+ requirements: []
145
+ rubygems_version: 4.1.0
146
+ specification_version: 4
147
+ summary: Mollie payments engine for Rails SaaS applications
148
+ test_files: []