belt-pay 0.0.6 → 0.0.7
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 +156 -0
- data/README.md +97 -16
- data/lib/belt/pay/billable.rb +13 -4
- data/lib/belt/pay/checkout.rb +15 -1
- data/lib/belt/pay/cli.rb +35 -2
- data/lib/belt/pay/configuration.rb +13 -1
- data/lib/belt/pay/controllers/webhooks_controller.rb +4 -2
- data/lib/belt/pay/providers/stripe.rb +82 -19
- data/lib/belt/pay/subscription.rb +10 -1
- data/lib/belt/pay/templates/controllers/pay_webhooks_controller.rb.erb +3 -2
- data/lib/belt/pay/templates/terraform/main.tf.erb +2 -1
- data/lib/belt/pay/templates/terraform/variables.tf.erb +4 -4
- data/lib/belt/pay/transaction.rb +59 -15
- data/lib/belt/pay/version.rb +1 -1
- data/lib/belt/pay/webhook_handler.rb +341 -76
- data/lib/belt/pay.rb +43 -0
- metadata +1 -1
|
@@ -9,13 +9,24 @@ module Belt
|
|
|
9
9
|
# endpoints to exactly this list.
|
|
10
10
|
EVENTS = %w[
|
|
11
11
|
checkout.session.completed
|
|
12
|
+
checkout.session.async_payment_succeeded
|
|
13
|
+
checkout.session.async_payment_failed
|
|
12
14
|
checkout.session.expired
|
|
13
15
|
invoice.paid
|
|
14
16
|
invoice.payment_failed
|
|
15
|
-
customer.subscription.
|
|
17
|
+
customer.subscription.created
|
|
16
18
|
customer.subscription.updated
|
|
19
|
+
customer.subscription.deleted
|
|
20
|
+
charge.refunded
|
|
21
|
+
charge.dispute.created
|
|
22
|
+
charge.dispute.closed
|
|
17
23
|
].freeze
|
|
18
24
|
|
|
25
|
+
# Checkout Session payment_status values that mean the money is in. A session
|
|
26
|
+
# paid by a delayed method (bank debit, voucher) completes as `unpaid`; its
|
|
27
|
+
# result arrives later as checkout.session.async_payment_succeeded / _failed.
|
|
28
|
+
PAID_STATUSES = %w[paid no_payment_required].freeze
|
|
29
|
+
|
|
19
30
|
class << self
|
|
20
31
|
# Process a raw webhook request.
|
|
21
32
|
# @param payload [String] Raw request body
|
|
@@ -29,135 +40,389 @@ module Belt
|
|
|
29
40
|
|
|
30
41
|
private
|
|
31
42
|
|
|
43
|
+
# Does the gem's bookkeeping for the event, then runs the app's Belt::Pay.on
|
|
44
|
+
# hooks with the event and the transaction it touched (nil when none).
|
|
45
|
+
# Stripe delivers at least once, so both halves are safe to repeat: the
|
|
46
|
+
# bookkeeping changes nothing the second time, and hooks should check
|
|
47
|
+
# `transaction.status` rather than assume the event is news.
|
|
32
48
|
def dispatch(event)
|
|
33
49
|
event_type = event['type'] || event.type
|
|
34
50
|
|
|
35
51
|
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: received event', event_type: event_type)
|
|
36
52
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
transaction =
|
|
54
|
+
case event_type
|
|
55
|
+
when 'checkout.session.completed'
|
|
56
|
+
handle_checkout_completed(event.data.object)
|
|
57
|
+
when 'checkout.session.async_payment_succeeded'
|
|
58
|
+
handle_checkout_async_payment_succeeded(event.data.object)
|
|
59
|
+
when 'checkout.session.async_payment_failed'
|
|
60
|
+
handle_checkout_async_payment_failed(event.data.object)
|
|
61
|
+
when 'checkout.session.expired'
|
|
62
|
+
handle_checkout_expired(event.data.object)
|
|
63
|
+
when 'invoice.paid'
|
|
64
|
+
handle_invoice_paid(event.data.object)
|
|
65
|
+
when 'invoice.payment_failed'
|
|
66
|
+
handle_invoice_payment_failed(event.data.object)
|
|
67
|
+
when 'customer.subscription.deleted'
|
|
68
|
+
handle_subscription_deleted(event.data.object)
|
|
69
|
+
when 'customer.subscription.created', 'customer.subscription.updated'
|
|
70
|
+
handle_subscription_updated(event.data.object)
|
|
71
|
+
when 'charge.refunded'
|
|
72
|
+
handle_charge_refunded(event.data.object)
|
|
73
|
+
when 'charge.dispute.created'
|
|
74
|
+
handle_dispute_created(event.data.object)
|
|
75
|
+
when 'charge.dispute.closed'
|
|
76
|
+
handle_dispute_closed(event.data.object)
|
|
77
|
+
else
|
|
78
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: unhandled event type', event_type: event_type)
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
Belt::Pay.run_hooks(event_type, event, transaction)
|
|
53
83
|
end
|
|
54
84
|
|
|
85
|
+
# The session is finished, which is not the same as paid: only a paid session
|
|
86
|
+
# (PAID_STATUSES) completes the transaction. An unpaid one stays pending until
|
|
87
|
+
# checkout.session.async_payment_succeeded or _failed says how it went.
|
|
55
88
|
def handle_checkout_completed(session)
|
|
56
|
-
|
|
57
|
-
|
|
89
|
+
payment_status = value(session, :payment_status).to_s
|
|
90
|
+
paid = PAID_STATUSES.include?(payment_status)
|
|
91
|
+
transaction = settle_checkout(session, paid ? 'completed' : 'pending')
|
|
58
92
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
93
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: checkout completed',
|
|
94
|
+
customer_id: app_customer_id(session), session_id: value(session, :id),
|
|
95
|
+
amount_cents: value(session, :amount_total), payment_status: payment_status)
|
|
96
|
+
transaction
|
|
97
|
+
end
|
|
64
98
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
Transaction.record_checkout(
|
|
72
|
-
customer_id: customer_id,
|
|
73
|
-
session_id: session.id,
|
|
74
|
-
amount_cents: session.amount_total || 0,
|
|
75
|
-
metadata: { payment_status: session.payment_status }
|
|
76
|
-
)
|
|
77
|
-
end
|
|
99
|
+
def handle_checkout_async_payment_succeeded(session)
|
|
100
|
+
transaction = settle_checkout(session, 'completed')
|
|
101
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: checkout payment succeeded',
|
|
102
|
+
customer_id: app_customer_id(session), session_id: value(session, :id))
|
|
103
|
+
transaction
|
|
104
|
+
end
|
|
78
105
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
106
|
+
def handle_checkout_async_payment_failed(session)
|
|
107
|
+
transaction = settle_checkout(session, 'failed', description: 'Checkout payment failed')
|
|
108
|
+
Belt::Pay.log(:warn, 'Belt::Pay::WebhookHandler: checkout payment failed',
|
|
109
|
+
customer_id: app_customer_id(session), session_id: value(session, :id))
|
|
110
|
+
transaction
|
|
82
111
|
end
|
|
83
112
|
|
|
84
113
|
def handle_checkout_expired(session)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
).first
|
|
90
|
-
|
|
91
|
-
if pending&.status == 'pending'
|
|
92
|
-
pending.status = 'failed'
|
|
93
|
-
pending.description = 'Checkout session expired'
|
|
94
|
-
pending.save!
|
|
95
|
-
end
|
|
114
|
+
transaction = settle_checkout(session, 'failed', description: 'Checkout session expired', create: false)
|
|
115
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: checkout expired', session_id: value(session, :id))
|
|
116
|
+
transaction
|
|
117
|
+
end
|
|
96
118
|
|
|
97
|
-
|
|
119
|
+
# Move a session's transaction to `status`, recording it first when the
|
|
120
|
+
# webhook is the first to hear of the session. Only a pending transaction
|
|
121
|
+
# moves, so a redelivered event, or one sent again from the Dashboard after
|
|
122
|
+
# a refund, changes nothing and never adds a row.
|
|
123
|
+
# @return [Transaction, nil]
|
|
124
|
+
def settle_checkout(session, status, description: nil, create: true)
|
|
125
|
+
session_id = value(session, :id)
|
|
126
|
+
transaction = Transaction.for_session(session_id)
|
|
127
|
+
transaction ||= record_checkout(session, status) if create
|
|
128
|
+
return transaction unless transaction && transaction.status == 'pending' && status != 'pending'
|
|
129
|
+
|
|
130
|
+
transaction.status = status
|
|
131
|
+
transaction.amount_cents = value(session, :amount_total) if status == 'completed'
|
|
132
|
+
transaction.currency = value(session, :currency) || transaction.currency
|
|
133
|
+
transaction.provider_payment_intent_id = reference_id(value(session, :payment_intent)) ||
|
|
134
|
+
transaction.provider_payment_intent_id
|
|
135
|
+
transaction.description = description if description
|
|
136
|
+
transaction.save!
|
|
137
|
+
transaction
|
|
98
138
|
end
|
|
99
139
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
140
|
+
# Webhook-first flow: no pending row for the session. Keyed on the session
|
|
141
|
+
# id; when another delivery got there first, that row is the answer.
|
|
142
|
+
def record_checkout(session, status)
|
|
143
|
+
customer_id = app_customer_id(session)
|
|
144
|
+
return nil unless customer_id
|
|
103
145
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
customer_id
|
|
107
|
-
|
|
146
|
+
session_id = value(session, :id)
|
|
147
|
+
Transaction.record_checkout(
|
|
148
|
+
customer_id: customer_id,
|
|
149
|
+
session_id: session_id,
|
|
150
|
+
amount_cents: value(session, :amount_total) || 0,
|
|
151
|
+
currency: value(session, :currency) || 'usd',
|
|
152
|
+
status: status == 'completed' ? 'completed' : 'pending',
|
|
153
|
+
payment_intent_id: reference_id(value(session, :payment_intent)),
|
|
154
|
+
metadata: { payment_status: value(session, :payment_status) }
|
|
155
|
+
) || Transaction.lookup(session_id)
|
|
156
|
+
end
|
|
108
157
|
|
|
158
|
+
def handle_invoice_paid(invoice)
|
|
159
|
+
subscription_id = invoice_subscription_id(invoice)
|
|
160
|
+
# Try to resolve customer from subscription metadata if not on invoice
|
|
161
|
+
customer_id = app_customer_id(invoice) || resolve_customer_from_subscription(subscription_id)
|
|
109
162
|
return unless customer_id && subscription_id
|
|
110
163
|
|
|
111
|
-
|
|
164
|
+
invoice_id = value(invoice, :id)
|
|
165
|
+
# Keyed on the invoice id: a redelivered invoice.paid finds the row it made.
|
|
166
|
+
transaction = Transaction.record_renewal(
|
|
167
|
+
id: invoice_id,
|
|
112
168
|
customer_id: customer_id,
|
|
113
169
|
subscription_id: subscription_id,
|
|
114
|
-
amount_cents: invoice
|
|
115
|
-
|
|
116
|
-
|
|
170
|
+
amount_cents: value(invoice, :amount_paid) || 0,
|
|
171
|
+
currency: value(invoice, :currency) || 'usd',
|
|
172
|
+
metadata: { invoice_id: invoice_id, billing_reason: value(invoice, :billing_reason) }
|
|
173
|
+
) || Transaction.lookup(invoice_id)
|
|
117
174
|
|
|
118
175
|
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: invoice paid',
|
|
119
176
|
customer_id: customer_id, subscription_id: subscription_id,
|
|
120
|
-
amount_cents: invoice
|
|
177
|
+
amount_cents: value(invoice, :amount_paid))
|
|
178
|
+
transaction
|
|
121
179
|
end
|
|
122
180
|
|
|
123
181
|
def handle_invoice_payment_failed(invoice)
|
|
124
|
-
customer_id = invoice
|
|
125
|
-
|
|
126
|
-
customer_id = resolve_customer_from_subscription(invoice.subscription)
|
|
127
|
-
end
|
|
182
|
+
customer_id = app_customer_id(invoice) ||
|
|
183
|
+
resolve_customer_from_subscription(invoice_subscription_id(invoice))
|
|
128
184
|
|
|
129
185
|
Belt::Pay.log(:warn, 'Belt::Pay::WebhookHandler: invoice payment failed',
|
|
130
|
-
customer_id: customer_id, invoice_id: invoice
|
|
186
|
+
customer_id: customer_id, invoice_id: value(invoice, :id))
|
|
187
|
+
nil
|
|
131
188
|
end
|
|
132
189
|
|
|
133
190
|
def handle_subscription_deleted(subscription)
|
|
134
|
-
customer_id =
|
|
191
|
+
customer_id = app_customer_id(subscription)
|
|
135
192
|
return unless customer_id
|
|
136
193
|
|
|
137
194
|
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: subscription deleted',
|
|
138
|
-
customer_id: customer_id, subscription_id: subscription
|
|
195
|
+
customer_id: customer_id, subscription_id: value(subscription, :id))
|
|
196
|
+
sync_subscription(subscription)
|
|
139
197
|
end
|
|
140
198
|
|
|
141
199
|
def handle_subscription_updated(subscription)
|
|
142
|
-
customer_id =
|
|
200
|
+
customer_id = app_customer_id(subscription)
|
|
143
201
|
return unless customer_id
|
|
144
202
|
|
|
145
203
|
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: subscription updated',
|
|
146
|
-
customer_id: customer_id, subscription_id: subscription
|
|
147
|
-
status: subscription
|
|
204
|
+
customer_id: customer_id, subscription_id: value(subscription, :id),
|
|
205
|
+
status: value(subscription, :status))
|
|
206
|
+
sync_subscription(subscription)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Bring the customer's plan and the subscription's transaction in step with
|
|
210
|
+
# the subscription. The event says what was true when it was sent, and
|
|
211
|
+
# events can arrive late, out of order or again, so Stripe is asked what is
|
|
212
|
+
# true now. A Stripe error is raised and the event retried.
|
|
213
|
+
# @return [Transaction, nil]
|
|
214
|
+
def sync_subscription(event_subscription)
|
|
215
|
+
subscription = Belt::Pay.provider.retrieve_subscription(value(event_subscription, :id))
|
|
216
|
+
customer_id = app_customer_id(subscription) || app_customer_id(event_subscription)
|
|
217
|
+
status = value(subscription, :status).to_s
|
|
218
|
+
|
|
219
|
+
sync_customer_plan(subscription, customer_id, status)
|
|
220
|
+
settle_subscription(value(subscription, :id), status)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# `pay_plan` is what Billable#plan, #plan_allows? and #within_limit? read, so
|
|
224
|
+
# it is set when the subscription is active or trialing and taken back when
|
|
225
|
+
# it is canceled, unpaid or expired before its first payment. Other statuses
|
|
226
|
+
# (incomplete, past_due, paused) leave it as it is.
|
|
227
|
+
def sync_customer_plan(subscription, customer_id, status)
|
|
228
|
+
active = Subscription::ACTIVE_STATUSES.include?(status)
|
|
229
|
+
return unless active || Subscription::ENDED_STATUSES.include?(status)
|
|
230
|
+
|
|
231
|
+
customer = find_customer(customer_id)
|
|
232
|
+
return unless customer.respond_to?(:pay_plan=) && customer.respond_to?(:pay_subscription_id=)
|
|
233
|
+
|
|
234
|
+
subscription_id = value(subscription, :id)
|
|
235
|
+
current = customer.pay_subscription_id.to_s
|
|
236
|
+
# Only the customer's own subscription moves their plan: not an older one
|
|
237
|
+
# ending after they took out a new one.
|
|
238
|
+
return if !current.empty? && current != subscription_id
|
|
239
|
+
return if !active && current.empty?
|
|
240
|
+
|
|
241
|
+
plan_key = subscription_plan_key(subscription)
|
|
242
|
+
before = [customer.pay_subscription_id, customer.pay_plan]
|
|
243
|
+
if active
|
|
244
|
+
customer.pay_subscription_id = subscription_id
|
|
245
|
+
customer.pay_plan = plan_key if plan_key
|
|
246
|
+
else
|
|
247
|
+
# Take back the plan this subscription granted, not one held another way.
|
|
248
|
+
customer.pay_plan = nil if plan_key.nil? || customer.pay_plan.to_s == plan_key
|
|
249
|
+
customer.pay_subscription_id = nil unless status == 'unpaid'
|
|
250
|
+
end
|
|
251
|
+
return if before == [customer.pay_subscription_id, customer.pay_plan]
|
|
252
|
+
|
|
253
|
+
customer.save(validate: false)
|
|
254
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: customer plan updated',
|
|
255
|
+
customer_id: customer_id, subscription_id: subscription_id,
|
|
256
|
+
status: status, pay_plan: customer.pay_plan)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# The transaction Subscription.create recorded under the subscription id:
|
|
260
|
+
# completed once the subscription is active, failed if it expired unpaid,
|
|
261
|
+
# canceled when it ends.
|
|
262
|
+
def settle_subscription(subscription_id, status)
|
|
263
|
+
transaction = Transaction.lookup(subscription_id)
|
|
264
|
+
return nil unless transaction
|
|
265
|
+
|
|
266
|
+
target = if Subscription::ACTIVE_STATUSES.include?(status) then 'completed'
|
|
267
|
+
elsif status == 'incomplete_expired' then 'failed'
|
|
268
|
+
elsif status == 'canceled' then 'canceled'
|
|
269
|
+
end
|
|
270
|
+
movable = target == 'canceled' ? %w[pending completed] : %w[pending]
|
|
271
|
+
if target && movable.include?(transaction.status)
|
|
272
|
+
transaction.status = target
|
|
273
|
+
transaction.save!
|
|
274
|
+
end
|
|
275
|
+
transaction
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# The declared plan a subscription is for: the plan declared for its price,
|
|
279
|
+
# which stays right when the customer changes plan in the billing portal;
|
|
280
|
+
# else the `plan` Belt::Pay.subscribe stamped on its metadata.
|
|
281
|
+
def subscription_plan_key(subscription)
|
|
282
|
+
items = value(value(subscription, :items), :data)
|
|
283
|
+
price_id = reference_id(value(Array(items).first, :price))
|
|
284
|
+
declared = Belt::Pay.plan_for_price(price_id)
|
|
285
|
+
return declared.key.to_s if declared
|
|
286
|
+
|
|
287
|
+
stamped = value(value(subscription, :metadata), :plan).to_s
|
|
288
|
+
stamped.empty? ? nil : stamped
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def find_customer(customer_id)
|
|
292
|
+
model = Belt::Pay.configuration.customer_model
|
|
293
|
+
unless model
|
|
294
|
+
Belt::Pay.log(:error,
|
|
295
|
+
'Belt::Pay::WebhookHandler: customer_class is not configured, so pay_plan cannot ' \
|
|
296
|
+
'follow the subscription; set Belt::Pay.configuration.customer_class',
|
|
297
|
+
customer_id: customer_id)
|
|
298
|
+
return nil
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
model.find(customer_id)
|
|
302
|
+
rescue ActiveItem::RecordNotFound
|
|
303
|
+
Belt::Pay.log(:warn, 'Belt::Pay::WebhookHandler: subscription for an unknown customer',
|
|
304
|
+
customer_id: customer_id)
|
|
305
|
+
nil
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# Money went back. The transaction keeps how much (`amount_refunded_cents`,
|
|
309
|
+
# Stripe's running total for the charge) and becomes `refunded` once all of
|
|
310
|
+
# it has; a part refund leaves it `completed`.
|
|
311
|
+
def handle_charge_refunded(charge)
|
|
312
|
+
transaction = transaction_for_payment(charge)
|
|
313
|
+
return unless transaction
|
|
314
|
+
|
|
315
|
+
transaction.amount_refunded_cents = value(charge, :amount_refunded)
|
|
316
|
+
if value(charge, :refunded) == true && %w[completed disputed].include?(transaction.status)
|
|
317
|
+
transaction.status = 'refunded'
|
|
318
|
+
transaction.description = 'Refunded'
|
|
319
|
+
end
|
|
320
|
+
transaction.save!
|
|
321
|
+
|
|
322
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: charge refunded',
|
|
323
|
+
customer_id: transaction.customer_id, transaction_id: transaction.id,
|
|
324
|
+
amount_refunded_cents: transaction.amount_refunded_cents, status: transaction.status)
|
|
325
|
+
transaction
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# The buyer told their bank the payment wasn't theirs. The money is held by
|
|
329
|
+
# Stripe from now until the dispute closes.
|
|
330
|
+
def handle_dispute_created(dispute)
|
|
331
|
+
transaction = transaction_for_payment(dispute)
|
|
332
|
+
return unless transaction
|
|
333
|
+
|
|
334
|
+
if transaction.status == 'completed'
|
|
335
|
+
transaction.status = 'disputed'
|
|
336
|
+
transaction.description = "Payment disputed (#{value(dispute, :id)})"
|
|
337
|
+
transaction.save!
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
Belt::Pay.log(:warn, 'Belt::Pay::WebhookHandler: payment disputed',
|
|
341
|
+
customer_id: transaction.customer_id, transaction_id: transaction.id,
|
|
342
|
+
dispute_id: value(dispute, :id))
|
|
343
|
+
transaction
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# A dispute won (or an inquiry closed without a chargeback) gives the money
|
|
347
|
+
# back to the seller: `completed` again. One lost is money gone: `refunded`.
|
|
348
|
+
def handle_dispute_closed(dispute)
|
|
349
|
+
transaction = transaction_for_payment(dispute)
|
|
350
|
+
return unless transaction
|
|
351
|
+
|
|
352
|
+
outcome = value(dispute, :status).to_s
|
|
353
|
+
if outcome == 'lost' && %w[completed disputed].include?(transaction.status)
|
|
354
|
+
# Also from completed: the dispute's opening event may never have arrived.
|
|
355
|
+
transaction.status = 'refunded'
|
|
356
|
+
transaction.description = "Dispute lost (#{value(dispute, :id)})"
|
|
357
|
+
transaction.save!
|
|
358
|
+
elsif %w[won warning_closed].include?(outcome) && transaction.status == 'disputed'
|
|
359
|
+
transaction.status = 'completed'
|
|
360
|
+
transaction.description = "Dispute #{outcome == 'won' ? 'won' : 'closed'} (#{value(dispute, :id)})"
|
|
361
|
+
transaction.save!
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
Belt::Pay.log(:info, 'Belt::Pay::WebhookHandler: dispute closed',
|
|
365
|
+
customer_id: transaction.customer_id, transaction_id: transaction.id,
|
|
366
|
+
dispute_id: value(dispute, :id), outcome: outcome, status: transaction.status)
|
|
367
|
+
transaction
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
# The transaction a charge or dispute belongs to. They name only the payment,
|
|
371
|
+
# so a renewal is found by its invoice id and a checkout by asking Stripe
|
|
372
|
+
# which Checkout Session the payment came from. Nil for a payment this app
|
|
373
|
+
# has no transaction for; a Stripe error is raised so the event is retried.
|
|
374
|
+
def transaction_for_payment(object)
|
|
375
|
+
renewal = Transaction.lookup(reference_id(value(object, :invoice)))
|
|
376
|
+
return renewal if renewal
|
|
377
|
+
|
|
378
|
+
payment_intent_id = reference_id(value(object, :payment_intent))
|
|
379
|
+
return nil unless payment_intent_id
|
|
380
|
+
|
|
381
|
+
Transaction.for_session(Belt::Pay.provider.checkout_session_id_for(payment_intent_id))
|
|
148
382
|
end
|
|
149
383
|
|
|
150
384
|
def resolve_customer_from_subscription(subscription_id)
|
|
151
385
|
return nil unless subscription_id
|
|
152
386
|
|
|
153
387
|
Belt::Pay.provider.ensure_api_key!
|
|
154
|
-
|
|
155
|
-
sub.metadata&.app_customer_id || sub.metadata&.[]('app_customer_id')
|
|
388
|
+
app_customer_id(::Stripe::Subscription.retrieve(subscription_id))
|
|
156
389
|
rescue StandardError => e
|
|
157
390
|
Belt::Pay.log(:warn, 'Belt::Pay::WebhookHandler: failed to resolve customer from subscription',
|
|
158
391
|
subscription_id: subscription_id, error: e.message)
|
|
159
392
|
nil
|
|
160
393
|
end
|
|
394
|
+
|
|
395
|
+
# The invoice's subscription id. Stripe API versions from 2025-03-31 moved
|
|
396
|
+
# it under parent.subscription_details.
|
|
397
|
+
def invoice_subscription_id(invoice)
|
|
398
|
+
direct = reference_id(value(invoice, :subscription))
|
|
399
|
+
return direct if direct
|
|
400
|
+
|
|
401
|
+
reference_id(value(value(value(invoice, :parent), :subscription_details), :subscription))
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# The app's customer id, stamped on the object's metadata as app_customer_id.
|
|
405
|
+
def app_customer_id(object)
|
|
406
|
+
id = value(value(object, :metadata), :app_customer_id)
|
|
407
|
+
id.to_s.empty? ? nil : id
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# Read an attribute that may not be there. A Stripe object raises
|
|
411
|
+
# NoMethodError for a field the event doesn't carry, and which fields it
|
|
412
|
+
# carries depends on the endpoint's API version.
|
|
413
|
+
def value(object, key)
|
|
414
|
+
return nil if object.nil?
|
|
415
|
+
return object.public_send(key) if object.respond_to?(key)
|
|
416
|
+
return nil unless object.respond_to?(:[])
|
|
417
|
+
|
|
418
|
+
object[key.to_s]
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# A reference is an id, or the object itself when the field was expanded.
|
|
422
|
+
def reference_id(reference)
|
|
423
|
+
id = reference.is_a?(String) ? reference : value(reference, :id)
|
|
424
|
+
id.to_s.empty? ? nil : id
|
|
425
|
+
end
|
|
161
426
|
end
|
|
162
427
|
end
|
|
163
428
|
end
|
data/lib/belt/pay.rb
CHANGED
|
@@ -68,6 +68,45 @@ module Belt
|
|
|
68
68
|
@plans = PlanRegistry.new
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
+
# --- Webhook hooks ---
|
|
72
|
+
|
|
73
|
+
# Run a block when the webhook receives a Stripe event of one of the given
|
|
74
|
+
# types, after Belt Pay's own bookkeeping. The block gets the Stripe event and
|
|
75
|
+
# the Belt::Pay::Transaction it touched (nil when the event has none).
|
|
76
|
+
#
|
|
77
|
+
# Stripe delivers events at least once, and a hook that raises fails the
|
|
78
|
+
# webhook so Stripe sends the event again. Make hooks safe to repeat, and read
|
|
79
|
+
# `transaction.status` to decide what the customer has: `completed` is paid,
|
|
80
|
+
# `refunded` and `disputed` are not.
|
|
81
|
+
#
|
|
82
|
+
# @example
|
|
83
|
+
# Belt::Pay.on('checkout.session.completed', 'checkout.session.async_payment_succeeded') do |event, transaction|
|
|
84
|
+
# Orders.fulfil(transaction) if transaction&.status == 'completed'
|
|
85
|
+
# end
|
|
86
|
+
# Belt::Pay.on('charge.refunded', 'charge.dispute.created') do |event, transaction|
|
|
87
|
+
# Orders.revoke(transaction) if transaction && transaction.status != 'completed'
|
|
88
|
+
# end
|
|
89
|
+
#
|
|
90
|
+
# @param event_types [Array<String>] Stripe event types, e.g. 'charge.refunded'
|
|
91
|
+
def on(*event_types, &block)
|
|
92
|
+
raise ArgumentError, 'Belt::Pay.on needs a block' unless block
|
|
93
|
+
raise ArgumentError, 'Belt::Pay.on needs at least one event type' if event_types.empty?
|
|
94
|
+
|
|
95
|
+
event_types.flatten.each { |type| hooks[type.to_s] << block }
|
|
96
|
+
nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Run the hooks registered for an event type. Called by the webhook handler.
|
|
100
|
+
def run_hooks(event_type, event, transaction = nil)
|
|
101
|
+
hooks.fetch(event_type.to_s, []).each { |hook| hook.call(event, transaction) }
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Remove every registered hook (useful for tests).
|
|
106
|
+
def reset_hooks!
|
|
107
|
+
@hooks = nil
|
|
108
|
+
end
|
|
109
|
+
|
|
71
110
|
# --- Convenience API (provider-agnostic names) ---
|
|
72
111
|
|
|
73
112
|
# Ensure a payment provider customer exists for this user.
|
|
@@ -159,6 +198,10 @@ module Belt
|
|
|
159
198
|
|
|
160
199
|
private
|
|
161
200
|
|
|
201
|
+
def hooks
|
|
202
|
+
@hooks ||= Hash.new { |registry, type| registry[type] = [] }
|
|
203
|
+
end
|
|
204
|
+
|
|
162
205
|
# Resolve a plan key (+ interval) to its Stripe price ID.
|
|
163
206
|
def resolve_plan_price(plan_key, interval)
|
|
164
207
|
return nil if plan_key.nil?
|