belt-pay 0.0.3 → 0.0.5
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 +4 -4
- data/CHANGELOG.md +31 -0
- data/README.md +25 -5
- data/lib/belt/generators/pay_generator.rb +7 -12
- data/lib/belt/pay/checkout.rb +3 -0
- data/lib/belt/pay/cli.rb +133 -3
- data/lib/belt/pay/providers/stripe.rb +53 -10
- data/lib/belt/pay/version.rb +1 -1
- data/lib/belt/pay/webhook_handler.rb +11 -0
- data/lib/belt/pay.rb +5 -1
- data/lib/tasks/pay.rake +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 937d9e1a7ca86c274b1dd20feb2ad16b2f9f9aa8544b0bffd6d72ed1550768f7
|
|
4
|
+
data.tar.gz: f33e95b73bd5f8104d3da45c34ce7e3e4e6ca329224721c80271af3f8e7c85d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b1c7f6bb3f97548a05e060090f47b586aaf0435237931cd1c67b55534efd72eb706cf6af1d7294f07a5210268fdd6c232ef951d410722b00de1bdc56f43c30cc
|
|
7
|
+
data.tar.gz: 73e0341c2f2441a279814645e959df9f1b94c41d88ba70018bac6b3ef10c144583288e945ee0a1d22e955c057361dc5e71858c5e4a63769e9a2fb47af3d4ad38
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.0.5 — 2026-10-31
|
|
4
|
+
|
|
5
|
+
- **`create_checkout` no longer drops Checkout Session options.** The Stripe provider used
|
|
6
|
+
to copy only `line_items`, `mode`, `success_url`, `cancel_url`, `metadata` and
|
|
7
|
+
`subscription_data` into `Stripe::Checkout::Session.create` and silently discard
|
|
8
|
+
everything else. It now forwards every option in
|
|
9
|
+
`Belt::Pay::Providers::Stripe::CHECKOUT_SESSION_OPTIONS` — including
|
|
10
|
+
`allow_promotion_codes`, `discounts`, `automatic_tax`, `payment_intent_data`,
|
|
11
|
+
`setup_intent_data`, `tax_id_collection`, `customer_update`, `locale`, `expires_at`,
|
|
12
|
+
`ui_mode`/`return_url` and the rest of the documented Checkout Session params.
|
|
13
|
+
- Unknown keys raise `ArgumentError` naming the offending option, so the next missing
|
|
14
|
+
param fails loudly instead of vanishing.
|
|
15
|
+
- `extra: { ... }` merges any additional params in unvalidated, for Stripe options the
|
|
16
|
+
list doesn't know about yet.
|
|
17
|
+
- Options given as `nil` are omitted from the request.
|
|
18
|
+
|
|
19
|
+
## 0.0.4 — 2026-10-20
|
|
20
|
+
|
|
21
|
+
- **`belt pay:setup` creates the Stripe webhook endpoint for you.** After the secret-key
|
|
22
|
+
prompt it reads `api_url` from `terraform output`, offers to create
|
|
23
|
+
`<api_url>/pay/webhooks` in Stripe (subscribed to every event Belt Pay handles), and
|
|
24
|
+
stores the returned `whsec_` signing secret directly. No Dashboard clicking.
|
|
25
|
+
- Shows whether the key is test or live mode before creating anything.
|
|
26
|
+
- If Stripe already has an endpoint for that URL, offers to replace it (the old one
|
|
27
|
+
is deleted only after the new secret is saved) or links to it in the Dashboard.
|
|
28
|
+
- Declining, or missing terraform output, prints step-by-step Dashboard instructions
|
|
29
|
+
with the exact URL and events, then prompts for the secret as before.
|
|
30
|
+
- `--webhook-url` / `BELT_PAY_WEBHOOK_URL` and `--webhook-events` /
|
|
31
|
+
`BELT_PAY_WEBHOOK_EVENTS` override the defaults.
|
|
32
|
+
- `Belt::Pay::WebhookHandler::EVENTS` lists the Stripe events the handler acts on.
|
|
33
|
+
|
|
3
34
|
## 0.0.3 — 2026-10-02
|
|
4
35
|
|
|
5
36
|
- Add Belt-native `pay:setup`, `pay:show`, and `pay:edit` tasks through Belt's
|
data/README.md
CHANGED
|
@@ -100,6 +100,18 @@ Belt::Pay.create_checkout(customer,
|
|
|
100
100
|
success_url: 'https://app.example.com/success?session_id={CHECKOUT_SESSION_ID}',
|
|
101
101
|
cancel_url: 'https://app.example.com/cancel')
|
|
102
102
|
|
|
103
|
+
# Any Stripe Checkout Session option passes straight through
|
|
104
|
+
Belt::Pay.create_checkout(customer,
|
|
105
|
+
line_items: [{ price: 'price_xxx', quantity: 1 }],
|
|
106
|
+
success_url: 'https://app.example.com/success?session_id={CHECKOUT_SESSION_ID}',
|
|
107
|
+
cancel_url: 'https://app.example.com/cancel',
|
|
108
|
+
allow_promotion_codes: true, # Dashboard coupons / promo codes
|
|
109
|
+
automatic_tax: { enabled: true }, # Stripe Tax
|
|
110
|
+
payment_intent_data: { metadata: { pass_id: 42 } }) # carried onto the charge, so a
|
|
111
|
+
# Dashboard refund still knows the app record
|
|
112
|
+
# Unknown keys raise ArgumentError instead of being silently dropped.
|
|
113
|
+
# Use `extra: { ... }` to forward a Stripe param this gem doesn't list yet.
|
|
114
|
+
|
|
103
115
|
# Subscribe directly (when payment method is already attached)
|
|
104
116
|
customer.subscribe!(price_id: 'price_xxx')
|
|
105
117
|
|
|
@@ -176,10 +188,17 @@ guided happy path uses `BELT_ENV` to select the environment:
|
|
|
176
188
|
BELT_ENV=dev belt pay:setup
|
|
177
189
|
```
|
|
178
190
|
|
|
179
|
-
It prompts without echo for the Stripe secret key
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
191
|
+
It prompts without echo for the Stripe secret key, then offers to create the webhook
|
|
192
|
+
endpoint in Stripe for you: it reads `api_url` from `terraform output`, creates
|
|
193
|
+
`<api_url>/pay/webhooks` subscribed to every [handled event](#webhook-events), and stores
|
|
194
|
+
the `whsec_` signing secret Stripe returns. Everything goes directly through the Stripe
|
|
195
|
+
API and AWS SDK. Run it after `belt apply` so the URL is known.
|
|
196
|
+
|
|
197
|
+
Decline (or run it before the first apply) and it prints step-by-step Dashboard
|
|
198
|
+
instructions with the exact URL and events, then prompts for the signing secret. Leave
|
|
199
|
+
either prompt blank to preserve its existing value. Use `--webhook-url` /
|
|
200
|
+
`BELT_PAY_WEBHOOK_URL` or `--webhook-events` / `BELT_PAY_WEBHOOK_EVENTS` to override the
|
|
201
|
+
defaults. Test (`sk_test_`) and live (`sk_live_`) keys get separate endpoints and secrets.
|
|
183
202
|
|
|
184
203
|
You can also pass the environment with standard Rake task-argument syntax:
|
|
185
204
|
|
|
@@ -354,7 +373,8 @@ module "pay" {
|
|
|
354
373
|
|
|
355
374
|
## Stripe Dashboard Setup
|
|
356
375
|
|
|
357
|
-
|
|
376
|
+
`BELT_ENV=<env> belt pay:setup` creates the webhook endpoint and stores its signing
|
|
377
|
+
secret for you. To do it by hand instead:
|
|
358
378
|
|
|
359
379
|
1. Go to Stripe Dashboard → Developers → Webhooks
|
|
360
380
|
2. Add endpoint: `https://<your-api-domain>/pay/webhooks`
|
|
@@ -315,20 +315,15 @@ module Belt
|
|
|
315
315
|
# ...
|
|
316
316
|
end
|
|
317
317
|
|
|
318
|
-
3.
|
|
319
|
-
BELT_ENV=<env> belt pay:setup
|
|
320
|
-
|
|
321
|
-
Values are prompted without echo. Leave the webhook secret blank
|
|
322
|
-
until Stripe gives it to you, then run setup again.
|
|
323
|
-
|
|
324
|
-
4. Deploy:
|
|
318
|
+
3. Deploy:
|
|
325
319
|
belt apply <env>
|
|
326
320
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
321
|
+
4. Store your Stripe key and create the webhook endpoint:
|
|
322
|
+
BELT_ENV=<env> belt pay:setup
|
|
323
|
+
|
|
324
|
+
Prompts without echo for the Stripe secret key, then creates
|
|
325
|
+
<api_url>/pay/webhooks in Stripe and stores its signing secret.
|
|
326
|
+
Decline to get step-by-step Dashboard instructions instead.
|
|
332
327
|
|
|
333
328
|
Quick Usage:
|
|
334
329
|
# Create a checkout session
|
data/lib/belt/pay/checkout.rb
CHANGED
|
@@ -12,6 +12,9 @@ module Belt
|
|
|
12
12
|
# @param success_url [String]
|
|
13
13
|
# @param cancel_url [String]
|
|
14
14
|
# @param metadata [Hash]
|
|
15
|
+
# @param options [Hash] Any other Checkout Session option the provider supports
|
|
16
|
+
# (e.g. allow_promotion_codes:, automatic_tax:, payment_intent_data:,
|
|
17
|
+
# subscription_data:). Forwarded verbatim; unknown keys raise ArgumentError.
|
|
15
18
|
# @return [Hash] { url:, session_id: }
|
|
16
19
|
def create(customer, line_items:, mode: 'payment', success_url:, cancel_url:, metadata: {}, **options)
|
|
17
20
|
# Ensure provider customer exists
|
data/lib/belt/pay/cli.rb
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require 'io/console'
|
|
4
4
|
require 'json'
|
|
5
|
+
require 'open3'
|
|
5
6
|
require 'optparse'
|
|
6
7
|
require 'shellwords'
|
|
7
8
|
require 'tempfile'
|
|
8
9
|
require 'aws-sdk-secretsmanager'
|
|
10
|
+
require 'stripe'
|
|
11
|
+
require 'belt/pay/webhook_handler'
|
|
9
12
|
|
|
10
13
|
module Belt
|
|
11
14
|
module Pay
|
|
@@ -13,6 +16,7 @@ module Belt
|
|
|
13
16
|
class Error < StandardError; end
|
|
14
17
|
|
|
15
18
|
COMMANDS = %w[secrets:setup secrets:show secrets:edit].freeze
|
|
19
|
+
WEBHOOK_PATH = '/pay/webhooks'
|
|
16
20
|
|
|
17
21
|
def self.start(args, input: $stdin, out: $stdout, err: $stderr)
|
|
18
22
|
new(args, input: input, out: out, err: err).run
|
|
@@ -24,6 +28,9 @@ module Belt
|
|
|
24
28
|
rescue Aws::Errors::ServiceError => e
|
|
25
29
|
err.puts "AWS error: #{e.message}"
|
|
26
30
|
1
|
|
31
|
+
rescue ::Stripe::StripeError => e
|
|
32
|
+
err.puts "Stripe error: #{e.message}"
|
|
33
|
+
1
|
|
27
34
|
end
|
|
28
35
|
|
|
29
36
|
def initialize(args, input:, out:, err:)
|
|
@@ -78,6 +85,12 @@ module Belt
|
|
|
78
85
|
opts.on('--region REGION', 'Override the AWS region') do |value|
|
|
79
86
|
@options[:region] = value
|
|
80
87
|
end
|
|
88
|
+
opts.on('--webhook-url URL', 'Override the Stripe webhook endpoint URL') do |value|
|
|
89
|
+
@options[:webhook_url] = value
|
|
90
|
+
end
|
|
91
|
+
opts.on('--webhook-events LIST', 'Comma-separated Stripe events for a new endpoint') do |value|
|
|
92
|
+
@options[:webhook_events] = value
|
|
93
|
+
end
|
|
81
94
|
opts.on('-h', '--help', 'Show this help') do
|
|
82
95
|
@out.puts help
|
|
83
96
|
exit 0
|
|
@@ -108,11 +121,122 @@ module Belt
|
|
|
108
121
|
updated = original.dup
|
|
109
122
|
|
|
110
123
|
secret_key = hidden_prompt('Stripe secret key [blank keeps existing]')
|
|
111
|
-
webhook_secret = hidden_prompt('Stripe webhook signing secret [blank keeps existing]')
|
|
112
124
|
updated['stripe_secret_key'] = secret_key unless blank?(secret_key)
|
|
125
|
+
|
|
126
|
+
webhook_secret, replaced = webhook_secret_for(updated['stripe_secret_key'], original['stripe_webhook_secret'])
|
|
113
127
|
updated['stripe_webhook_secret'] = webhook_secret unless blank?(webhook_secret)
|
|
114
128
|
|
|
115
129
|
update_secret(response, original, updated)
|
|
130
|
+
delete_replaced_endpoints(updated['stripe_secret_key'], replaced)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Returns [signing_secret, endpoints_to_delete]. Offers to create the Stripe
|
|
134
|
+
# webhook endpoint (Stripe only returns a signing secret at creation time);
|
|
135
|
+
# otherwise prints Dashboard instructions and prompts for the secret.
|
|
136
|
+
def webhook_secret_for(api_key, existing_secret)
|
|
137
|
+
if blank?(api_key)
|
|
138
|
+
@out.puts 'No Stripe secret key stored yet, so the webhook endpoint cannot be created for you.'
|
|
139
|
+
return [manual_webhook_secret(nil), []]
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
mode = stripe_mode(api_key)
|
|
143
|
+
url = webhook_url
|
|
144
|
+
@out.puts "Stripe account mode: #{mode}"
|
|
145
|
+
@out.puts "Webhook endpoint URL: #{url || '(unknown; run `belt apply` first or pass --webhook-url)'}"
|
|
146
|
+
|
|
147
|
+
create = !url.nil? && confirm("Create the #{mode}-mode webhook endpoint in Stripe now?", default: blank?(existing_secret))
|
|
148
|
+
return [manual_webhook_secret(url, mode: mode), []] unless create
|
|
149
|
+
|
|
150
|
+
existing = stripe_endpoints(api_key).select { |endpoint| endpoint.url == url }
|
|
151
|
+
if existing.any?
|
|
152
|
+
ids = existing.map(&:id).join(', ')
|
|
153
|
+
@out.puts "Stripe already has an endpoint for this URL (#{ids}). Stripe never re-sends a signing secret,"
|
|
154
|
+
@out.puts 'so setup can only replace it with a new endpoint (the old secret stops working).'
|
|
155
|
+
unless confirm('Replace it?', default: false)
|
|
156
|
+
@out.puts "Reveal its signing secret at #{dashboard_url(mode, existing.first.id)}"
|
|
157
|
+
return [hidden_prompt('Stripe webhook signing secret [blank keeps existing]'), []]
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
endpoint = ::Stripe::WebhookEndpoint.create(
|
|
162
|
+
{ url: url, enabled_events: webhook_events, description: "Belt Pay (#{@app_name} #{@environment})" },
|
|
163
|
+
{ api_key: api_key }
|
|
164
|
+
)
|
|
165
|
+
@out.puts "Created #{mode}-mode webhook endpoint #{endpoint.id} for #{webhook_events.size} events."
|
|
166
|
+
[endpoint.secret, existing.map(&:id)]
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def manual_webhook_secret(url, mode: nil)
|
|
170
|
+
@out.puts <<~STEPS
|
|
171
|
+
|
|
172
|
+
To set up the webhook in the Stripe Dashboard#{" (#{mode} mode)" if mode}:
|
|
173
|
+
1. Developers -> Webhooks -> Add destination (#{mode == 'live' ? 'https://dashboard.stripe.com/webhooks' : 'https://dashboard.stripe.com/test/webhooks'})
|
|
174
|
+
2. Events from: Your account. API version: the default.
|
|
175
|
+
3. Events: #{webhook_events.join(', ')}
|
|
176
|
+
4. Destination type: Webhook endpoint
|
|
177
|
+
5. URL: #{url || "your API base URL (`belt output #{@environment}` api_url) + #{WEBHOOK_PATH}"}
|
|
178
|
+
6. Open the new endpoint -> Signing secret -> Reveal, and paste it below.
|
|
179
|
+
Each endpoint (and test vs. live mode) has its own whsec_ secret. Do not use the
|
|
180
|
+
one `stripe listen` prints; it only works for local forwarding.
|
|
181
|
+
|
|
182
|
+
STEPS
|
|
183
|
+
hidden_prompt('Stripe webhook signing secret [blank keeps existing]')
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def delete_replaced_endpoints(api_key, ids)
|
|
187
|
+
ids.each do |id|
|
|
188
|
+
::Stripe::WebhookEndpoint.delete(id, {}, { api_key: api_key })
|
|
189
|
+
@out.puts "Deleted replaced webhook endpoint #{id}"
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def stripe_endpoints(api_key)
|
|
194
|
+
::Stripe::WebhookEndpoint.list({ limit: 100 }, { api_key: api_key }).auto_paging_each.to_a
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def webhook_url
|
|
198
|
+
return @webhook_url if defined?(@webhook_url)
|
|
199
|
+
|
|
200
|
+
explicit = @options[:webhook_url] || ENV['BELT_PAY_WEBHOOK_URL']
|
|
201
|
+
@webhook_url = if blank?(explicit)
|
|
202
|
+
api_url = terraform_output('api_url')
|
|
203
|
+
"#{api_url.chomp('/')}#{WEBHOOK_PATH}" unless blank?(api_url)
|
|
204
|
+
else
|
|
205
|
+
explicit.strip
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def webhook_events
|
|
210
|
+
list = @options[:webhook_events] || ENV['BELT_PAY_WEBHOOK_EVENTS']
|
|
211
|
+
return Belt::Pay::WebhookHandler::EVENTS if blank?(list)
|
|
212
|
+
|
|
213
|
+
list.split(',').map(&:strip).reject(&:empty?)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def terraform_output(name)
|
|
217
|
+
env_dir = File.join(@root, 'infrastructure', @environment)
|
|
218
|
+
stdout, _stderr, status = Open3.capture3('terraform', "-chdir=#{env_dir}", 'output', '-raw', name)
|
|
219
|
+
stdout.strip if status.success?
|
|
220
|
+
rescue SystemCallError
|
|
221
|
+
nil
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def stripe_mode(api_key)
|
|
225
|
+
api_key.match?(/\A[sr]k_live_/) ? 'live' : 'test'
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def dashboard_url(mode, endpoint_id)
|
|
229
|
+
"https://dashboard.stripe.com/#{'test/' unless mode == 'live'}webhooks/#{endpoint_id}"
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def confirm(question, default:)
|
|
233
|
+
@out.print "#{question} #{default ? '[Y/n]' : '[y/N]'} "
|
|
234
|
+
@out.flush
|
|
235
|
+
answer = @input.gets
|
|
236
|
+
@out.puts if answer.nil?
|
|
237
|
+
return default if answer.nil? || answer.strip.empty?
|
|
238
|
+
|
|
239
|
+
answer.strip.downcase.start_with?('y')
|
|
116
240
|
end
|
|
117
241
|
|
|
118
242
|
def show
|
|
@@ -171,7 +295,7 @@ module Belt
|
|
|
171
295
|
@input.gets
|
|
172
296
|
end
|
|
173
297
|
@out.puts
|
|
174
|
-
raise Error, 'input ended before
|
|
298
|
+
raise Error, 'input ended before the Stripe secrets were read' if value.nil?
|
|
175
299
|
|
|
176
300
|
value.chomp
|
|
177
301
|
end
|
|
@@ -264,7 +388,9 @@ module Belt
|
|
|
264
388
|
belt-pay secrets:edit ENV [options]
|
|
265
389
|
|
|
266
390
|
Commands:
|
|
267
|
-
secrets:setup Prompt without echo for Stripe
|
|
391
|
+
secrets:setup Prompt without echo for the Stripe secret key, then create
|
|
392
|
+
the webhook endpoint in Stripe and store its signing
|
|
393
|
+
secret (or walk through creating it in the Dashboard)
|
|
268
394
|
secrets:show Print the decrypted JSON secret
|
|
269
395
|
secrets:edit Edit the secret with $VISUAL or $EDITOR
|
|
270
396
|
|
|
@@ -273,12 +399,16 @@ module Belt
|
|
|
273
399
|
--secret-name NAME Override <app>-<environment>-stripe
|
|
274
400
|
--profile PROFILE Override the environment's AWS profile
|
|
275
401
|
--region REGION Override the detected AWS region
|
|
402
|
+
--webhook-url URL Override <terraform api_url>#{WEBHOOK_PATH}
|
|
403
|
+
--webhook-events L Comma-separated events (default: every event Belt Pay handles)
|
|
276
404
|
-h, --help Show this help
|
|
277
405
|
|
|
278
406
|
Environment:
|
|
279
407
|
BELT_ENV Environment when ENV is omitted
|
|
280
408
|
AWS_PROFILE AWS profile (overrides infrastructure/ENV/belt.rb)
|
|
281
409
|
AWS_REGION AWS region (default: us-east-1)
|
|
410
|
+
BELT_PAY_WEBHOOK_URL Same as --webhook-url
|
|
411
|
+
BELT_PAY_WEBHOOK_EVENTS Same as --webhook-events
|
|
282
412
|
VISUAL, EDITOR Editor command for secrets:edit
|
|
283
413
|
|
|
284
414
|
Examples:
|
|
@@ -67,24 +67,54 @@ module Belt
|
|
|
67
67
|
{ client_secret: setup_intent.client_secret, setup_intent_id: setup_intent.id }
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
+
# Stripe Checkout Session options that #create_checkout_session forwards verbatim
|
|
71
|
+
# when given. Anything else raises ArgumentError so a missing option fails loudly
|
|
72
|
+
# instead of being silently dropped; pass `extra:` to send a key not listed here.
|
|
73
|
+
# @see https://docs.stripe.com/api/checkout/sessions/create
|
|
74
|
+
CHECKOUT_SESSION_OPTIONS = %i[
|
|
75
|
+
subscription_data payment_intent_data setup_intent_data
|
|
76
|
+
allow_promotion_codes discounts automatic_tax tax_id_collection
|
|
77
|
+
customer_update customer_creation client_reference_id currency locale expires_at
|
|
78
|
+
payment_method_types payment_method_collection payment_method_options
|
|
79
|
+
payment_method_configuration saved_payment_method_options
|
|
80
|
+
billing_address_collection shipping_address_collection shipping_options
|
|
81
|
+
phone_number_collection consent_collection custom_fields custom_text submit_type
|
|
82
|
+
invoice_creation after_expiration adaptive_pricing optional_items
|
|
83
|
+
ui_mode return_url redirect_on_completion
|
|
84
|
+
].freeze
|
|
85
|
+
|
|
70
86
|
# Create a Checkout Session.
|
|
87
|
+
#
|
|
88
|
+
# Every key in CHECKOUT_SESSION_OPTIONS (e.g. `allow_promotion_codes:`,
|
|
89
|
+
# `automatic_tax:`, `payment_intent_data:`, `subscription_data:`) is forwarded to
|
|
90
|
+
# Stripe as-is. Keys with a nil value are omitted. Unknown keys raise
|
|
91
|
+
# ArgumentError; use `extra:` for a Stripe option this list doesn't know about yet.
|
|
92
|
+
#
|
|
71
93
|
# @param pay_customer_id [String] Stripe customer ID
|
|
72
|
-
# @param
|
|
94
|
+
# @param line_items [Array<Hash>]
|
|
95
|
+
# @param mode [String] 'payment', 'subscription' or 'setup' (default 'payment')
|
|
96
|
+
# @param success_url [String, nil]
|
|
97
|
+
# @param cancel_url [String, nil]
|
|
98
|
+
# @param metadata [Hash]
|
|
99
|
+
# @param extra [Hash] Additional Checkout Session params merged in last, unvalidated
|
|
100
|
+
# @param options [Hash] Any key in CHECKOUT_SESSION_OPTIONS
|
|
73
101
|
# @return [Hash] { url:, session_id: }
|
|
74
|
-
|
|
102
|
+
# @raise [ArgumentError] when an option is not a known Checkout Session param
|
|
103
|
+
def create_checkout_session(pay_customer_id, line_items:, mode: 'payment', success_url: nil,
|
|
104
|
+
cancel_url: nil, metadata: {}, extra: {}, **options)
|
|
75
105
|
ensure_api_key!
|
|
76
106
|
params = {
|
|
77
107
|
customer: pay_customer_id,
|
|
78
|
-
line_items:
|
|
79
|
-
mode:
|
|
80
|
-
success_url:
|
|
81
|
-
cancel_url:
|
|
82
|
-
metadata:
|
|
108
|
+
line_items: line_items,
|
|
109
|
+
mode: mode || 'payment',
|
|
110
|
+
success_url: success_url,
|
|
111
|
+
cancel_url: cancel_url,
|
|
112
|
+
metadata: metadata || {}
|
|
83
113
|
}
|
|
84
|
-
|
|
85
|
-
params
|
|
114
|
+
params.merge!(checkout_session_options(options))
|
|
115
|
+
params.merge!(extra) if extra
|
|
86
116
|
|
|
87
|
-
session = ::Stripe::Checkout::Session.create(params)
|
|
117
|
+
session = ::Stripe::Checkout::Session.create(params.compact)
|
|
88
118
|
{ url: session.url, session_id: session.id }
|
|
89
119
|
end
|
|
90
120
|
|
|
@@ -178,6 +208,19 @@ module Belt
|
|
|
178
208
|
|
|
179
209
|
private
|
|
180
210
|
|
|
211
|
+
# Validate and compact the pass-through Checkout Session options.
|
|
212
|
+
def checkout_session_options(options)
|
|
213
|
+
unknown = options.keys - CHECKOUT_SESSION_OPTIONS
|
|
214
|
+
if unknown.any?
|
|
215
|
+
raise ArgumentError,
|
|
216
|
+
"Unknown Checkout Session option(s): #{unknown.map(&:inspect).join(', ')}. " \
|
|
217
|
+
"Supported: #{CHECKOUT_SESSION_OPTIONS.join(', ')}. " \
|
|
218
|
+
'Pass `extra: { ... }` to forward a param not listed here.'
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
options.compact
|
|
222
|
+
end
|
|
223
|
+
|
|
181
224
|
def fetch_api_key
|
|
182
225
|
secret_name = Belt::Pay.configuration.secret_name
|
|
183
226
|
return nil unless secret_name
|
data/lib/belt/pay/version.rb
CHANGED
|
@@ -5,6 +5,17 @@ module Belt
|
|
|
5
5
|
# Handles incoming webhooks from the payment provider.
|
|
6
6
|
# Verifies signatures, dispatches events, and records transactions.
|
|
7
7
|
module WebhookHandler
|
|
8
|
+
# Stripe events #dispatch acts on. `belt pay:setup` subscribes new webhook
|
|
9
|
+
# endpoints to exactly this list.
|
|
10
|
+
EVENTS = %w[
|
|
11
|
+
checkout.session.completed
|
|
12
|
+
checkout.session.expired
|
|
13
|
+
invoice.paid
|
|
14
|
+
invoice.payment_failed
|
|
15
|
+
customer.subscription.deleted
|
|
16
|
+
customer.subscription.updated
|
|
17
|
+
].freeze
|
|
18
|
+
|
|
8
19
|
class << self
|
|
9
20
|
# Process a raw webhook request.
|
|
10
21
|
# @param payload [String] Raw request body
|
data/lib/belt/pay.rb
CHANGED
|
@@ -94,7 +94,11 @@ module Belt
|
|
|
94
94
|
|
|
95
95
|
# Create a checkout session for one-time or subscription payments.
|
|
96
96
|
# @param customer [Object] Your app's user/customer model instance
|
|
97
|
-
# @param options [Hash] Checkout options
|
|
97
|
+
# @param options [Hash] Checkout options: line_items, mode, success_url, cancel_url,
|
|
98
|
+
# metadata, plus any Stripe Checkout Session option such as allow_promotion_codes,
|
|
99
|
+
# automatic_tax, payment_intent_data or subscription_data (see
|
|
100
|
+
# Providers::Stripe::CHECKOUT_SESSION_OPTIONS). Unknown keys raise ArgumentError;
|
|
101
|
+
# pass extra: { ... } to forward a param the list doesn't know yet.
|
|
98
102
|
# @return [Hash] { url:, session_id: }
|
|
99
103
|
def create_checkout(customer, **options)
|
|
100
104
|
Checkout.create(customer, **options)
|
data/lib/tasks/pay.rake
CHANGED
|
@@ -11,7 +11,7 @@ run_pay_secret_command = lambda do |command, environment|
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
namespace :pay do
|
|
14
|
-
desc '
|
|
14
|
+
desc 'Store the Stripe key and create the webhook endpoint (BELT_ENV or pay:setup[env])'
|
|
15
15
|
task :setup, [:environment] do |_task, args|
|
|
16
16
|
run_pay_secret_command.call(:setup, args[:environment])
|
|
17
17
|
end
|