-
Notifications
You must be signed in to change notification settings - Fork 100
/
stripe_service.rb
223 lines (181 loc) · 6.99 KB
/
stripe_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# frozen_string_literal: true
module Invoices
module Payments
class StripeService < BaseService
include Customers::PaymentProviderFinder
PENDING_STATUSES = %w[processing requires_capture requires_action requires_confirmation requires_payment_method]
.freeze
SUCCESS_STATUSES = %w[succeeded].freeze
FAILED_STATUSES = %w[canceled].freeze
def initialize(invoice = nil)
@invoice = invoice
super
end
def update_payment_status(organization_id:, status:, stripe_payment:)
payment = if stripe_payment.metadata[:payment_type] == "one-time"
create_payment(stripe_payment)
else
Payment.find_by(provider_payment_id: stripe_payment.id)
end
unless payment
handle_missing_payment(organization_id, stripe_payment)
return result unless result.payment
payment = result.payment
end
result.payment = payment
result.invoice = payment.payable
return result if payment.payable.payment_succeeded?
payment.update!(status:)
update_invoice_payment_status(
payment_status: invoice_payment_status(status),
processing: status == "processing"
)
result
rescue BaseService::FailedResult => e
result.fail_with_error!(e)
end
def generate_payment_url
return result unless should_process_payment?
res = ::Stripe::Checkout::Session.create(
payment_url_payload,
{
api_key: stripe_api_key
}
)
result.payment_url = res["url"]
result
rescue ::Stripe::CardError, ::Stripe::InvalidRequestError, ::Stripe::AuthenticationError, Stripe::PermissionError => e
deliver_error_webhook(e)
result.single_validation_failure!(error_code: "payment_provider_error")
end
private
attr_accessor :invoice
delegate :organization, :customer, to: :invoice
def create_payment(stripe_payment, invoice: nil)
@invoice = invoice || Invoice.find_by(id: stripe_payment.metadata[:lago_invoice_id])
unless @invoice
result.not_found_failure!(resource: "invoice")
return
end
increment_payment_attempts
payment = Payment.find_or_initialize_by(
payable: @invoice,
payment_provider_id: stripe_payment_provider.id,
payment_provider_customer_id: customer.stripe_customer.id,
amount_cents: @invoice.total_amount_cents,
amount_currency: @invoice.currency,
status: "pending"
)
status = invoice_payment_status(stripe_payment.status)
status = (status.to_sym == :pending) ? :processing : status
payment.provider_payment_id = stripe_payment.id
payment.status = stripe_payment.status
payment.payable_payment_status = status
payment.save!
payment
end
def success_redirect_url
stripe_payment_provider.success_redirect_url.presence ||
::PaymentProviders::StripeProvider::SUCCESS_REDIRECT_URL
end
def should_process_payment?
return false if invoice.payment_succeeded? || invoice.voided?
return false if stripe_payment_provider.blank?
customer&.stripe_customer&.provider_customer_id
end
def stripe_api_key
stripe_payment_provider.secret_key
end
def payment_url_payload
{
line_items: [
{
quantity: 1,
price_data: {
currency: invoice.currency.downcase,
unit_amount: invoice.total_amount_cents,
product_data: {
name: invoice.number
}
}
}
],
mode: "payment",
success_url: success_redirect_url,
customer: customer.stripe_customer.provider_customer_id,
payment_method_types: customer.stripe_customer.provider_payment_methods,
payment_intent_data: {
description:,
metadata: {
lago_customer_id: customer.id,
lago_invoice_id: invoice.id,
invoice_issuing_date: invoice.issuing_date.iso8601,
invoice_type: invoice.invoice_type,
payment_type: "one-time"
}
}
}
end
def description
"#{organization.name} - Invoice #{invoice.number}"
end
def invoice_payment_status(payment_status)
return :pending if PENDING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)
payment_status&.to_sym
end
def update_invoice_payment_status(payment_status:, deliver_webhook: true, processing: false)
result = Invoices::UpdateService.call(
invoice: invoice.presence || @result.invoice,
params: {
payment_status:,
# NOTE: A proper `processing` payment status should be introduced for invoices
ready_for_payment_processing: !processing && payment_status.to_sym != :succeeded
},
webhook_notification: deliver_webhook
)
result.raise_if_error!
end
def increment_payment_attempts
invoice.update!(payment_attempts: invoice.payment_attempts + 1)
end
def deliver_error_webhook(stripe_error)
DeliverErrorWebhookService.call_async(invoice, {
provider_customer_id: customer.stripe_customer.provider_customer_id,
provider_error: {
message: stripe_error.message,
error_code: stripe_error.code
}
})
end
def handle_missing_payment(organization_id, stripe_payment)
# NOTE: Payment was not initiated by lago
return result unless stripe_payment.metadata&.key?(:lago_invoice_id)
# NOTE: Invoice does not belong to this lago organization
# It means the same Stripe secret key is used for multiple organizations
invoice = Invoice.find_by(id: stripe_payment.metadata[:lago_invoice_id], organization_id:)
return result if invoice.nil?
# NOTE: Invoice exists but payment status is failed
return result if invoice.payment_failed?
# NOTE: For some reason payment is missing in the database... (killed sidekiq job, etc.)
# We have to recreate it from the received data
result.payment = create_payment(stripe_payment, invoice:)
result
end
# NOTE: Due to RBI limitation, all indians payment should be off_session
# to permit 3D secure authentication
# https://docs.stripe.com/india-recurring-payments
def off_session?
invoice.customer.country != "IN"
end
# NOTE: Same as off_session?
def error_on_requires_action?
invoice.customer.country != "IN"
end
def stripe_payment_provider
@stripe_payment_provider ||= payment_provider(customer)
end
end
end
end