belt-pay 0.0.4 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 225d94bd8d7f1b25849f237c4bb8432ce5b485b3a7ca5b88aee833d928481c4d
4
- data.tar.gz: 7cabb1843e20857030506f0a3efe42a26aa375563f65c8ffb14334d84dc39b07
3
+ metadata.gz: df0a213a964e2b9d6e20019719612a03ae5a38fdafaade9f75bdb8b853f2ca87
4
+ data.tar.gz: 0a539c50414142e25667909c2858a45a00056e9afeb6b5f3ed824d50d7b50e30
5
5
  SHA512:
6
- metadata.gz: ccf4f5972eabe4ffa0aa4c4d4be3207ac640970cea1ddaae857043bdf7d81a6df507932cc8c7704020326f578c46ee73ef0ea8e978b1443f761ca34f59deb960
7
- data.tar.gz: 91f585c4b462664b2f193d8575fd5b551e969d613456ada465c4d3fa83387b4577b910fa64dd6e49f51b903082788ad8e48532a84174a76003368b5471ab40d1
6
+ metadata.gz: a8ece6c8fc7785233a665207bb5d68a7f652e3097d1706dd18fcb614b0b049a14d71b06f161fcda0acbd706ce01617061465c92b332f34c7f1f77021d2dafdd2
7
+ data.tar.gz: 5fae3025eff2bbc50cac637a22621d1f4de3b259a487653f5a3aeb2b6fe062312b6c4f9740088caa42288cc17d8b327bebaf698bc51fa819dc63a6d3767a98f4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.6 — 2026-11-02
4
+
5
+ - **`pay:setup` works with a restricted runtime key.** The stored key is what the app
6
+ runs with, so it should be a restricted key (`rk_`) holding only what checkout and the
7
+ portal need. Setup used to manage the webhook endpoint with that same key, so a
8
+ restricted key without Webhook Endpoints access failed with a Stripe permission error.
9
+ When Stripe refuses the stored key, setup now asks once for a key that can manage
10
+ endpoints (an `sk_` secret key, or a restricted key with Webhook Endpoints: Write),
11
+ uses it for the endpoint list/create/delete calls in that run only, and never stores
12
+ it. A key from the other Stripe mode is refused. Leaving the prompt blank, or a second
13
+ refusal, falls back to the Dashboard steps; the stored key is still saved either way.
14
+
15
+ ## 0.0.5 — 2026-10-31
16
+
17
+ - **`create_checkout` no longer drops Checkout Session options.** The Stripe provider used
18
+ to copy only `line_items`, `mode`, `success_url`, `cancel_url`, `metadata` and
19
+ `subscription_data` into `Stripe::Checkout::Session.create` and silently discard
20
+ everything else. It now forwards every option in
21
+ `Belt::Pay::Providers::Stripe::CHECKOUT_SESSION_OPTIONS` — including
22
+ `allow_promotion_codes`, `discounts`, `automatic_tax`, `payment_intent_data`,
23
+ `setup_intent_data`, `tax_id_collection`, `customer_update`, `locale`, `expires_at`,
24
+ `ui_mode`/`return_url` and the rest of the documented Checkout Session params.
25
+ - Unknown keys raise `ArgumentError` naming the offending option, so the next missing
26
+ param fails loudly instead of vanishing.
27
+ - `extra: { ... }` merges any additional params in unvalidated, for Stripe options the
28
+ list doesn't know about yet.
29
+ - Options given as `nil` are omitted from the request.
30
+
3
31
  ## 0.0.4 — 2026-10-20
4
32
 
5
33
  - **`belt pay:setup` creates the Stripe webhook endpoint for you.** After the secret-key
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
 
@@ -188,6 +200,13 @@ either prompt blank to preserve its existing value. Use `--webhook-url` /
188
200
  `BELT_PAY_WEBHOOK_URL` or `--webhook-events` / `BELT_PAY_WEBHOOK_EVENTS` to override the
189
201
  defaults. Test (`sk_test_`) and live (`sk_live_`) keys get separate endpoints and secrets.
190
202
 
203
+ Prefer a [restricted key](https://docs.stripe.com/keys#limit-access) (`rk_`) as the
204
+ stored key, scoped to what your app calls at runtime (typically Customers, Checkout
205
+ Sessions, Customer portal and Subscriptions). It doesn't need Webhook Endpoints access:
206
+ when Stripe refuses it for the endpoint step, setup asks once for a key that can manage
207
+ endpoints (your `sk_` secret key, or a restricted key with Webhook Endpoints: Write),
208
+ uses it for that step only, and doesn't store it.
209
+
191
210
  You can also pass the environment with standard Rake task-argument syntax:
192
211
 
193
212
  ```bash
@@ -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
@@ -14,6 +14,7 @@ module Belt
14
14
  module Pay
15
15
  class CLI
16
16
  class Error < StandardError; end
17
+ class EndpointKeyUnavailable < StandardError; end
17
18
 
18
19
  COMMANDS = %w[secrets:setup secrets:show secrets:edit].freeze
19
20
  WEBHOOK_PATH = '/pay/webhooks'
@@ -127,7 +128,7 @@ module Belt
127
128
  updated['stripe_webhook_secret'] = webhook_secret unless blank?(webhook_secret)
128
129
 
129
130
  update_secret(response, original, updated)
130
- delete_replaced_endpoints(updated['stripe_secret_key'], replaced)
131
+ delete_replaced_endpoints(replaced)
131
132
  end
132
133
 
133
134
  # Returns [signing_secret, endpoints_to_delete]. Offers to create the Stripe
@@ -147,7 +148,8 @@ module Belt
147
148
  create = !url.nil? && confirm("Create the #{mode}-mode webhook endpoint in Stripe now?", default: blank?(existing_secret))
148
149
  return [manual_webhook_secret(url, mode: mode), []] unless create
149
150
 
150
- existing = stripe_endpoints(api_key).select { |endpoint| endpoint.url == url }
151
+ @endpoint_key = api_key
152
+ existing = with_endpoint_key(mode) { |key| stripe_endpoints(key) }.select { |endpoint| endpoint.url == url }
151
153
  if existing.any?
152
154
  ids = existing.map(&:id).join(', ')
153
155
  @out.puts "Stripe already has an endpoint for this URL (#{ids}). Stripe never re-sends a signing secret,"
@@ -158,12 +160,17 @@ module Belt
158
160
  end
159
161
  end
160
162
 
161
- endpoint = ::Stripe::WebhookEndpoint.create(
162
- { url: url, enabled_events: webhook_events, description: "Belt Pay (#{@app_name} #{@environment})" },
163
- { api_key: api_key }
164
- )
163
+ endpoint = with_endpoint_key(mode) do |key|
164
+ ::Stripe::WebhookEndpoint.create(
165
+ { url: url, enabled_events: webhook_events, description: "Belt Pay (#{@app_name} #{@environment})" },
166
+ { api_key: key }
167
+ )
168
+ end
165
169
  @out.puts "Created #{mode}-mode webhook endpoint #{endpoint.id} for #{webhook_events.size} events."
166
170
  [endpoint.secret, existing.map(&:id)]
171
+ rescue EndpointKeyUnavailable => e
172
+ @out.puts e.message
173
+ [manual_webhook_secret(url, mode: mode), []]
167
174
  end
168
175
 
169
176
  def manual_webhook_secret(url, mode: nil)
@@ -183,13 +190,38 @@ module Belt
183
190
  hidden_prompt('Stripe webhook signing secret [blank keeps existing]')
184
191
  end
185
192
 
186
- def delete_replaced_endpoints(api_key, ids)
193
+ def delete_replaced_endpoints(ids)
187
194
  ids.each do |id|
188
- ::Stripe::WebhookEndpoint.delete(id, {}, { api_key: api_key })
195
+ ::Stripe::WebhookEndpoint.delete(id, {}, { api_key: @endpoint_key })
189
196
  @out.puts "Deleted replaced webhook endpoint #{id}"
190
197
  end
191
198
  end
192
199
 
200
+ # The stored key is what the app runs with, so it is usually a restricted key
201
+ # (rk_) without Webhook Endpoints access. When Stripe refuses it, ask once for a
202
+ # key that can manage endpoints (an sk_ secret key, or a restricted key with
203
+ # Webhook Endpoints: Write), use it for the endpoint calls in this run only, and
204
+ # never store it.
205
+ def with_endpoint_key(mode)
206
+ yield @endpoint_key
207
+ rescue ::Stripe::PermissionError => e
208
+ raise EndpointKeyUnavailable, "Stripe refused that key too: #{e.message}" if @endpoint_key_borrowed
209
+
210
+ @out.puts 'The stored Stripe key cannot manage webhook endpoints (a restricted key usually cannot).'
211
+ @out.puts 'Setup can use a second key for this step only; it is not stored.'
212
+ key = hidden_prompt("Stripe #{mode} secret key, or restricted key with Webhook Endpoints write [not stored]")
213
+ raise EndpointKeyUnavailable, 'No key given.' if blank?(key)
214
+
215
+ key = key.strip
216
+ unless stripe_mode(key) == mode
217
+ raise EndpointKeyUnavailable, "That is a #{stripe_mode(key)}-mode key; the stored key is #{mode} mode."
218
+ end
219
+
220
+ @endpoint_key = key
221
+ @endpoint_key_borrowed = true
222
+ retry
223
+ end
224
+
193
225
  def stripe_endpoints(api_key)
194
226
  ::Stripe::WebhookEndpoint.list({ limit: 100 }, { api_key: api_key }).auto_paging_each.to_a
195
227
  end
@@ -419,7 +451,8 @@ module Belt
419
451
 
420
452
  Security:
421
453
  secrets:setup reads values without echo and sends them directly through the
422
- AWS SDK. secrets:show writes the complete secret to stdout. secrets:edit uses
454
+ AWS SDK. If the stored key is a restricted key without Webhook Endpoints
455
+ access, it asks for a second key for the endpoint step and never stores it. secrets:show writes the complete secret to stdout. secrets:edit uses
423
456
  a mode-0600 temporary file, validates JSON, and removes the file afterward.
424
457
  HELP
425
458
  end
@@ -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 options [Hash] line_items, mode, success_url, cancel_url, metadata
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
- def create_checkout_session(pay_customer_id, **options)
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: options[:line_items],
79
- mode: options[:mode] || 'payment',
80
- success_url: options[:success_url],
81
- cancel_url: options[:cancel_url],
82
- metadata: options[: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
- # For subscription mode, allow trial periods
85
- params[:subscription_data] = options[:subscription_data] if options[:subscription_data]
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Belt
4
4
  module Pay
5
- VERSION = '0.0.4'
5
+ VERSION = '0.0.6'
6
6
  end
7
7
  end
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 (line_items, mode, success_url, cancel_url, metadata)
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: belt-pay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stowzilla