Skip to content

Commit 343eda1

Browse files
committed
Refactor Pay.sync to allow customization by plugins or apps
1 parent b7044fa commit 343eda1

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
### Unreleased
44

55
* [Breaking] Remove default `type` for Stripe Merchant Account creation
6+
* Refactor `Pay.sync(params)` into a Hash for easily extending
7+
8+
```ruby
9+
Pay::SYNC_HANDLERS[:foo] = ->(id) { Pay::Foo.sync_checkout(id) }
10+
# Pay.sync(params) calls lambda if params[:foo] is present
11+
```
612

713
### 8.3.0
814

lib/pay.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,20 @@ def self.resolve_option(option, *remaining_args)
132132
end
133133
end
134134

135+
SYNC_HANDLERS = {
136+
lemon_squeezy_order_id: ->(param) { Pay::LemonSqueezy.sync_order(param) },
137+
paddle_billing_transaction_id: ->(param) { Pay::PaddleBilling.sync_transaction(param) },
138+
stripe_checkout_session_id: ->(param) { Pay::Stripe.sync_checkout_session(param) },
139+
transaction_id: ->(param) { Pay::PaddleBilling.sync_transaction(param) },
140+
session_id: ->(param) { Pay::Stripe.sync_checkout_session(param) }
141+
}
142+
135143
def self.sync(params)
136-
if (session_id = params[:stripe_checkout_session_id] || params[:session_id])
137-
Pay::Stripe.sync_checkout_session(session_id)
138-
elsif (transaction_id = params[:paddle_billing_transaction_id] || params[:transaction_id])
139-
Pay::PaddleBilling.sync_transaction(transaction_id)
140-
elsif (order_id = params[:lemon_squeezy_order_id])
141-
Pay::LemonSqueezy.sync_order(order_id)
144+
SYNC_HANDLERS.each do |param_name, handler|
145+
if (param = params[param_name])
146+
return handler.call(param)
147+
end
142148
end
149+
nil
143150
end
144151
end

0 commit comments

Comments
 (0)