Skip to content

Commit df98a2d

Browse files
[STA-6] misc(payment): Migrate to TypedResult
1 parent 7845580 commit df98a2d

30 files changed

Lines changed: 359 additions & 277 deletions

app/services/invoices/payments/adyen_service.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ module Payments
55
class AdyenService < BaseService
66
include Lago::Adyen::ErrorHandlable
77
include Customers::PaymentProviderFinder
8+
include TypedResults
89

910
PROVIDER_NAME = "Adyen"
1011

11-
def initialize(invoice = nil)
12-
@invoice = invoice
12+
RESULTS = {
13+
update_payment_status: BaseResult[:payment, :invoice],
14+
generate_payment_url: BaseResult[:payment_url]
15+
}.freeze
1316

14-
super
15-
end
17+
private
1618

1719
def update_payment_status(provider_payment_id:, status:, amount_cents: nil, metadata: {})
1820
payment = if metadata[:payment_type] == "one-time"
@@ -43,7 +45,8 @@ def update_payment_status(provider_payment_id:, status:, amount_cents: nil, meta
4345
result.fail_with_error!(e)
4446
end
4547

46-
def generate_payment_url(payment_intent)
48+
def generate_payment_url(invoice, payment_intent)
49+
@invoice = invoice
4750
res = client.checkout.payment_links_api.payment_links(
4851
Lago::Adyen::Params.new(payment_url_params(payment_intent)).to_h,
4952
headers: {"Idempotency-Key" => payment_intent.id}
@@ -61,8 +64,6 @@ def generate_payment_url(payment_intent)
6164
result.third_party_failure!(third_party: PROVIDER_NAME, error_code: e.code, error_message: e.msg)
6265
end
6366

64-
private
65-
6667
attr_accessor :invoice
6768

6869
delegate :organization, :customer, to: :invoice
@@ -126,6 +127,8 @@ def payment_url_params(payment_intent)
126127
end
127128

128129
def update_invoice_payment_status(payment_status:, deliver_webhook: true)
130+
@invoice = result.invoice
131+
129132
params = {
130133
payment_status:,
131134
ready_for_payment_processing: payment_status.to_sym != :succeeded

app/services/invoices/payments/cashfree_service.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module Invoices
44
module Payments
55
class CashfreeService < BaseService
66
include Customers::PaymentProviderFinder
7+
include TypedResults
78

89
PROVIDER_NAME = "Cashfree"
910

10-
def initialize(invoice = nil)
11-
@invoice = invoice
11+
RESULTS = {
12+
update_payment_status: BaseResult[:payment, :invoice],
13+
generate_payment_url: BaseResult[:payment_url]
14+
}.freeze
1215

13-
super
14-
end
16+
private
1517

1618
def update_payment_status(organization_id:, status:, cashfree_payment:, amount_cents: nil)
1719
payment = if cashfree_payment.metadata[:payment_type] == "one-time"
@@ -42,7 +44,8 @@ def update_payment_status(organization_id:, status:, cashfree_payment:, amount_c
4244
result.fail_with_error!(e)
4345
end
4446

45-
def generate_payment_url(payment_intent)
47+
def generate_payment_url(invoice, payment_intent)
48+
@invoice = invoice
4649
payment_link_response = create_payment_link(payment_url_params(payment_intent))
4750
result.payment_url = JSON.parse(payment_link_response.body)["link_url"]
4851

@@ -51,8 +54,6 @@ def generate_payment_url(payment_intent)
5154
result.third_party_failure!(third_party: PROVIDER_NAME, error_code: e.error_code, error_message: e.error_body)
5255
end
5356

54-
private
55-
5657
attr_accessor :invoice
5758

5859
delegate :organization, :customer, to: :invoice

app/services/invoices/payments/flutterwave_service.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module Invoices
44
module Payments
55
class FlutterwaveService < BaseService
66
include Customers::PaymentProviderFinder
7+
include TypedResults
78

89
PROVIDER_NAME = "Flutterwave"
910

10-
def initialize(invoice = nil)
11-
@invoice = invoice
11+
RESULTS = {
12+
update_payment_status: BaseResult[:payment, :invoice],
13+
generate_payment_url: BaseResult[:payment_url]
14+
}.freeze
1215

13-
super
14-
end
16+
private
1517

1618
def update_payment_status(organization_id:, status:, flutterwave_payment:, amount_cents: nil)
1719
payment = if flutterwave_payment.metadata[:payment_type] == "one-time"
@@ -42,15 +44,14 @@ def update_payment_status(organization_id:, status:, flutterwave_payment:, amoun
4244
result.fail_with_error!(e)
4345
end
4446

45-
def generate_payment_url(payment_intent)
47+
def generate_payment_url(invoice, payment_intent)
48+
@invoice = invoice
4649
result.payment_url = payment_url
4750
result
4851
rescue LagoHttpClient::HttpError => e
4952
result.third_party_failure!(third_party: PROVIDER_NAME, error_code: e.error_code, error_message: e.error_body)
5053
end
5154

52-
private
53-
5455
attr_accessor :invoice
5556

5657
delegate :organization, :customer, to: :invoice

app/services/invoices/payments/gocardless_service.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ module Invoices
44
module Payments
55
class GocardlessService < BaseService
66
include Customers::PaymentProviderFinder
7+
include TypedResults
78

8-
def initialize(invoice = nil)
9-
@invoice = invoice
9+
RESULTS = {
10+
update_payment_status: BaseResult[:payment, :invoice]
11+
}.freeze
1012

11-
super
12-
end
13+
private
1314

1415
def update_payment_status(provider_payment_id:, status:)
1516
payment = Payment.find_by(provider_payment_id:)
@@ -36,8 +37,6 @@ def update_payment_status(provider_payment_id:, status:)
3637
result.fail_with_error!(e)
3738
end
3839

39-
private
40-
4140
attr_accessor :invoice
4241

4342
delegate :organization, :customer, to: :invoice

app/services/invoices/payments/moneyhash_service.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ module Invoices
44
module Payments
55
class MoneyhashService < BaseService
66
include Customers::PaymentProviderFinder
7+
include TypedResults
78

8-
def initialize(invoice = nil)
9-
@invoice = invoice
9+
RESULTS = {
10+
update_payment_status: BaseResult[:payment, :invoice],
11+
generate_payment_url: BaseResult[:payment_url]
12+
}.freeze
1013

11-
super(nil)
12-
end
14+
private
1315

1416
def update_payment_status(organization_id:, provider_payment_id:, status:, amount_cents: nil, metadata: {})
1517
payment_obj = Payment.find_or_initialize_by(provider_payment_id: provider_payment_id)
@@ -39,7 +41,8 @@ def update_payment_status(organization_id:, provider_payment_id:, status:, amoun
3941
result.fail_with_error!(e)
4042
end
4143

42-
def generate_payment_url(payment_intent)
44+
def generate_payment_url(invoice, payment_intent)
45+
@invoice = invoice
4346
return result unless should_process_payment?
4447

4548
response = client.post_with_response(
@@ -58,8 +61,6 @@ def generate_payment_url(payment_intent)
5861
result.service_failure!(code: e.error_code, message: e.message)
5962
end
6063

61-
private
62-
6364
attr_accessor :invoice
6465

6566
delegate :organization, :customer, to: :invoice

app/services/invoices/payments/payment_providers/factory.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Invoices
44
module Payments
55
module PaymentProviders
66
class Factory
7-
def self.new_instance(invoice:)
8-
service_class(invoice.customer&.payment_provider).new(invoice)
7+
def self.for(invoice)
8+
service_class(invoice.customer&.payment_provider)
99
end
1010

1111
def self.service_class(payment_provider)

app/services/invoices/payments/stripe_service.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ module Invoices
44
module Payments
55
class StripeService < BaseService
66
include Customers::PaymentProviderFinder
7+
include TypedResults
78

89
PROVIDER_NAME = "Stripe"
910

10-
def initialize(invoice = nil)
11-
@invoice = invoice
11+
RESULTS = {
12+
update_payment_status: BaseResult[:payment, :invoice],
13+
generate_payment_url: BaseResult[:payment_url, :provider_session_id],
14+
expire_payment_url: BaseResult
15+
}.freeze
1216

13-
super
14-
end
17+
private
1518

1619
def update_payment_status(organization_id:, status:, stripe_payment:, amount_cents: nil)
1720
payment = Payment.find_by(provider_payment_id: stripe_payment.id)
@@ -60,7 +63,8 @@ def update_payment_status(organization_id:, status:, stripe_payment:, amount_cen
6063
result.fail_with_error!(e)
6164
end
6265

63-
def generate_payment_url(payment_intent)
66+
def generate_payment_url(invoice, payment_intent)
67+
@invoice = invoice
6468
res = ::Stripe::Checkout::Session.create(
6569
payment_url_payload(payment_intent),
6670
{
@@ -78,7 +82,8 @@ def generate_payment_url(payment_intent)
7882
end
7983

8084
# NOTE: Expires the hosted Stripe Checkout open Session so it can no longer be paid.
81-
def expire_payment_url(payment_intent)
85+
def expire_payment_url(invoice, payment_intent)
86+
@invoice = invoice
8287
return result if payment_intent.provider_session_id.blank?
8388

8489
session = ::Stripe::Checkout::Session.retrieve(
@@ -99,8 +104,6 @@ def expire_payment_url(payment_intent)
99104
result
100105
end
101106

102-
private
103-
104107
attr_accessor :invoice
105108

106109
delegate :organization, :customer, to: :invoice

app/services/payment_intents/expire_service.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ def call
1515
payment_intents.find_each do |payment_intent|
1616
if payment_intent.provider_session_id.present?
1717
Invoices::Payments::PaymentProviders::Factory
18-
.new_instance(invoice:)
19-
.expire_payment_url(payment_intent)
20-
.raise_if_error!
18+
.for(invoice)
19+
.call!(:expire_payment_url, invoice, payment_intent)
2120
end
2221

2322
payment_intent.expired!

app/services/payment_intents/fetch_service.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ def call
1717

1818
if payment_intent.payment_url.blank?
1919
payment_url_result = Invoices::Payments::PaymentProviders::Factory
20-
.new_instance(invoice:)
21-
.generate_payment_url(payment_intent)
22-
23-
payment_url_result.raise_if_error!
20+
.for(invoice)
21+
.call!(:generate_payment_url, invoice, payment_intent)
2422

2523
if payment_url_result.payment_url.blank?
2624
return result.single_validation_failure!(error_code: "payment_provider_error")

app/services/payment_providers/adyen/handle_event_service.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ def call
5050
return result unless payment
5151

5252
metadata = {lago_payable_type: payment.payable_type}
53-
update_result = payment_service_klass(metadata)
54-
.new
55-
.update_payment_status(provider_payment_id:, status: "Cancelled", metadata:)
53+
klass = payment_service_klass(metadata)
54+
args = {provider_payment_id:, status: "Cancelled", metadata:}
55+
56+
# NOTE: Temporary branch until PaymentRequests::Payments services are migrated to
57+
# TypedResults too. Once they are, call `klass.call(:update_payment_status, **args)` directly.
58+
update_result = if klass.include?(TypedResults)
59+
klass.call(:update_payment_status, **args)
60+
else
61+
klass.new.update_payment_status(**args)
62+
end
5663
update_result.raise_if_error!
5764
when "REFUND"
5865
service = CreditNotes::Refunds::AdyenService.new
@@ -97,12 +104,21 @@ def update_payment_status(payment_type)
97104
lago_payable_type: event.dig("additionalData", "metadata.lago_payable_type")
98105
}
99106

100-
payment_service_klass(metadata).new.update_payment_status(
107+
klass = payment_service_klass(metadata)
108+
args = {
101109
provider_payment_id:,
102110
status:,
103111
amount_cents: event.dig("amount", "value"),
104112
metadata:
105-
)
113+
}
114+
115+
# NOTE: Temporary branch until PaymentRequests::Payments services are migrated to
116+
# TypedResults too. Once they are, call `klass.call(:update_payment_status, **args)` directly.
117+
if klass.include?(TypedResults)
118+
klass.call(:update_payment_status, **args)
119+
else
120+
klass.new.update_payment_status(**args)
121+
end
106122
end
107123

108124
def payment_service_klass(metadata)

0 commit comments

Comments
 (0)