Class: SpreeStripe::Gateway
- Inherits:
-
Spree::Gateway
- Object
- Spree::Gateway
- SpreeStripe::Gateway
show all
- Includes:
- PaymentIntents, PaymentSessions, PaymentSetupSessions, Tax
- Defined in:
- app/models/spree_stripe/gateway.rb,
app/models/spree_stripe/gateway/payment_intents.rb,
app/models/spree_stripe/gateway/payment_sessions.rb,
app/models/spree_stripe/gateway/payment_setup_sessions.rb
Defined Under Namespace
Modules: PaymentIntents, PaymentSessions, PaymentSetupSessions
Constant Summary
collapse
- WEBHOOK_EVENT_ACTIONS =
{
'payment_intent.succeeded' => :captured,
'payment_intent.amount_capturable_updated' => :authorized,
'payment_intent.payment_failed' => :failed
}.freeze
PaymentIntents::BANK_PAYMENT_METHOD_TYPES, PaymentIntents::DELAYED_NOTIFICATION_PAYMENT_METHOD_TYPES, PaymentIntents::MANUAL_CAPTURE_METHOD
Instance Method Summary
collapse
-
#api_options ⇒ Object
-
#apple_domain_association_file_content ⇒ Object
-
#attach_customer_to_credit_card(user) ⇒ Object
-
#authorize(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
-
#cancel(payment_intent_id, payment = nil) ⇒ Object
-
#capture(amount_in_cents, payment_intent_id, _gateway_options = {}) ⇒ Object
-
#configuration_guide_partial_name ⇒ Object
-
#create_customer(order: nil, user: nil) ⇒ Stripe::Customer
Creates a Stripe customer based on the order or user.
-
#create_ephemeral_key(customer_id) ⇒ Object
-
#create_profile(payment) ⇒ Object
-
#create_setup_intent(customer_id) ⇒ Object
-
#create_tax_calculation(order) ⇒ Object
-
#create_tax_transaction(payment_intent_id, tax_calculation_id) ⇒ Object
-
#create_webhook_endpoint ⇒ Object
-
#credit(amount_in_cents, _source, payment_intent_id, _gateway_options = {}) ⇒ Object
-
#custom_form_fields_partial_name ⇒ Object
-
#default_name ⇒ Object
-
#description_partial_name ⇒ Object
-
#fetch_or_create_customer(order: nil, user: nil) ⇒ Object
-
#gateway_dashboard_payment_url(payment) ⇒ Object
-
#handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options) ⇒ Object
the behavior for authorize and purchase is the same, so we can use the same method to handle both.
-
#method_type ⇒ Object
-
#parse_webhook_event(raw_body, headers) ⇒ Object
-
#payment_icon_name ⇒ Object
-
#payment_profiles_supported? ⇒ Boolean
-
#provider_class ⇒ Object
-
#purchase(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
-
#retrieve_charge(charge_id) ⇒ Object
-
#send_request {|api_options| ... } ⇒ Object
-
#update_customer(order: nil, user: nil) ⇒ Stripe::Customer
Updates a Stripe customer based on the order or user.
-
#void(response_code, _source, _gateway_options) ⇒ Object
-
#webhook_url ⇒ Object
#complete_payment_setup_session, #create_payment_setup_session, #payment_setup_session_class, #retrieve_setup_intent, #setup_session_supported?
#complete_payment_session, #create_payment_session, #payment_session_class, #session_required?, #update_payment_session
#cancel_payment_intent, #capture_payment_intent, #confirm_payment_intent, #create_payment_intent, #ensure_payment_intent_exists_for_payment, #payment_intent_accepted?, #payment_intent_bank_payment_method?, #payment_intent_charge_not_required?, #payment_intent_delayed_notification?, #payment_intent_manual_capture?, #payment_intent_requires_capture?, #retrieve_payment_intent, #update_payment_intent
Instance Method Details
#api_options ⇒ Object
300
301
302
|
# File 'app/models/spree_stripe/gateway.rb', line 300
def api_options
{ api_key: preferred_secret_key }
end
|
#apple_domain_association_file_content ⇒ Object
252
253
254
|
# File 'app/models/spree_stripe/gateway.rb', line 252
def apple_domain_association_file_content
@apple_domain_association_file_content ||= apple_developer_merchantid_domain_association&.download
end
|
#attach_customer_to_credit_card(user) ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'app/models/spree_stripe/gateway.rb', line 237
def attach_customer_to_credit_card(user)
payment_method_id = user&.default_credit_card&.gateway_payment_profile_id
return if payment_method_id.blank? || user&.default_credit_card&.gateway_customer_profile_id.present?
customer = fetch_or_create_customer(user: user)
return if customer.blank?
send_request { |opts| Stripe::PaymentMethod.attach(payment_method_id, { customer: customer.profile_id }, opts) }
user.default_credit_card.update(gateway_customer_profile_id: customer.profile_id, gateway_customer_id: customer.id)
rescue Stripe::StripeError => e
Rails.error.report(e, context: { payment_method_id: id, user_id: user.id }, source: 'spree_stripe')
nil
end
|
#authorize(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
58
59
60
|
# File 'app/models/spree_stripe/gateway.rb', line 58
def authorize(amount_in_cents, payment_source, gateway_options = {})
handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
end
|
#cancel(payment_intent_id, payment = nil) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'app/models/spree_stripe/gateway.rb', line 133
def cancel(payment_intent_id, payment = nil)
protect_from_error do
if payment&.completed?
amount = payment.credit_allowed
return success(payment_intent_id, {}) if amount.zero?
return success(payment_intent_id, {}) if payment.respond_to?(:for_shipment?) && payment.for_shipment?
refund = payment.refunds.create!(
amount: amount,
reason: Spree::RefundReason.order_canceled_reason,
refunder_id: payment.order.canceler_id
)
success(payment.response_code, refund.response.params)
else
response = cancel_payment_intent(payment_intent_id)
success(response.id, response)
end
end
end
|
#capture(amount_in_cents, payment_intent_id, _gateway_options = {}) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'app/models/spree_stripe/gateway.rb', line 108
def capture(amount_in_cents, payment_intent_id, _gateway_options = {})
protect_from_error do
stripe_payment_intent = retrieve_payment_intent(payment_intent_id)
response = if payment_intent_requires_capture?(stripe_payment_intent)
capture_payment_intent(payment_intent_id, amount_in_cents)
elsif stripe_payment_intent.status == 'succeeded'
stripe_payment_intent
else
raise Spree::Core::GatewayError, "Payment intent status is #{stripe_payment_intent.status}"
end
success(response.id, response)
end
end
|
#configuration_guide_partial_name ⇒ Object
280
281
282
|
# File 'app/models/spree_stripe/gateway.rb', line 280
def configuration_guide_partial_name
'spree_stripe'
end
|
#create_customer(order: nil, user: nil) ⇒ Stripe::Customer
Creates a Stripe customer based on the order or user
170
171
172
173
174
175
176
177
|
# File 'app/models/spree_stripe/gateway.rb', line 170
def create_customer(order: nil, user: nil)
payload = build_customer_payload(order: order, user: user)
response = send_request { |opts| Stripe::Customer.create(payload, opts) }
customer = gateway_customers.build(user: user, profile_id: response.id)
customer.save! if user.present?
customer
end
|
#create_ephemeral_key(customer_id) ⇒ Object
199
200
201
202
203
204
205
|
# File 'app/models/spree_stripe/gateway.rb', line 199
def create_ephemeral_key(customer_id)
protect_from_error do
response = send_request { |opts| Stripe::EphemeralKey.create({ customer: customer_id }, opts.merge(stripe_version: Stripe.api_version)) }
success(response.secret, response)
end
end
|
#create_profile(payment) ⇒ Object
294
295
296
297
298
|
# File 'app/models/spree_stripe/gateway.rb', line 294
def create_profile(payment)
customer = fetch_or_create_customer(order: payment.order)
payment.source.update(gateway_customer_profile_id: customer.profile_id) if payment.source.present? && customer.present?
end
|
#create_setup_intent(customer_id) ⇒ Object
207
208
209
210
211
212
213
|
# File 'app/models/spree_stripe/gateway.rb', line 207
def create_setup_intent(customer_id)
protect_from_error do
response = send_request { |opts| Stripe::SetupIntent.create({ customer: customer_id, automatic_payment_methods: { enabled: true } }, opts) }
success(response.client_secret, response)
end
end
|
#create_tax_calculation(order) ⇒ Object
215
216
217
218
219
220
221
222
223
|
# File 'app/models/spree_stripe/gateway.rb', line 215
def create_tax_calculation(order)
protect_from_error do
send_request do |opts|
Stripe::Tax::Calculation.create(
SpreeStripe::TaxPresenter.new(order: order).call, opts
)
end
end
end
|
#create_tax_transaction(payment_intent_id, tax_calculation_id) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
|
# File 'app/models/spree_stripe/gateway.rb', line 225
def create_tax_transaction(payment_intent_id, tax_calculation_id)
protect_from_error do
payload = {
calculation: tax_calculation_id,
reference: payment_intent_id,
expand: ['line_items']
}
send_request { |opts| Stripe::Tax::Transaction.create_from_calculation(payload, opts) }
end
end
|
#create_webhook_endpoint ⇒ Object
#credit(amount_in_cents, _source, payment_intent_id, _gateway_options = {}) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'app/models/spree_stripe/gateway.rb', line 95
def credit(amount_in_cents, _source, payment_intent_id, _gateway_options = {})
protect_from_error do
payload = {
amount: amount_in_cents,
payment_intent: payment_intent_id
}
response = send_request { |opts| Stripe::Refund.create(payload, opts) }
success(response.id, response)
end
end
|
276
277
278
|
# File 'app/models/spree_stripe/gateway.rb', line 276
def custom_form_fields_partial_name
'spree_stripe'
end
|
#default_name ⇒ Object
260
261
262
|
# File 'app/models/spree_stripe/gateway.rb', line 260
def default_name
'Stripe'
end
|
#description_partial_name ⇒ Object
272
273
274
|
# File 'app/models/spree_stripe/gateway.rb', line 272
def description_partial_name
'spree_stripe'
end
|
#fetch_or_create_customer(order: nil, user: nil) ⇒ Object
158
159
160
161
162
163
|
# File 'app/models/spree_stripe/gateway.rb', line 158
def fetch_or_create_customer(order: nil, user: nil)
user ||= order&.user
return nil unless user
gateway_customers.find_by(user: user) || create_customer(order: order, user: user)
end
|
#gateway_dashboard_payment_url(payment) ⇒ Object
284
285
286
287
288
|
# File 'app/models/spree_stripe/gateway.rb', line 284
def gateway_dashboard_payment_url(payment)
return if payment.transaction_id.blank?
"https://dashboard.stripe.com/payments/#{payment.transaction_id}"
end
|
#handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options) ⇒ Object
the behavior for authorize and purchase is the same, so we can use the same method to handle both
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/models/spree_stripe/gateway.rb', line 70
def handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
order_number, payment_number = gateway_options[:order_id].split('-')
return failure('Order number is invalid') if order_number.blank?
return failure('Payment number is invalid') if payment_number.blank?
order = Spree::Order.where(store_id: stores.ids).find_by(number: order_number)
payment = order.payments.find_by(number: payment_number)
protect_from_error do
payment = ensure_payment_intent_exists_for_payment(payment, amount_in_cents, payment_source)
stripe_payment_intent = retrieve_payment_intent(payment.response_code)
response = if payment_intent_accepted?(stripe_payment_intent)
stripe_payment_intent
else
confirm_payment_intent(stripe_payment_intent.id)
end
success(response.id, response)
end
end
|
#method_type ⇒ Object
264
265
266
|
# File 'app/models/spree_stripe/gateway.rb', line 264
def method_type
'spree_stripe'
end
|
#parse_webhook_event(raw_body, headers) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'app/models/spree_stripe/gateway.rb', line 36
def parse_webhook_event(raw_body, )
event = verify_webhook_signature(raw_body, )
action = WEBHOOK_EVENT_ACTIONS[event.type]
return nil unless action
payment_session = Spree::PaymentSessions::Stripe.find_by(
payment_method: self,
external_id: event.data.object[:id]
)
return nil unless payment_session
{ action: action, payment_session: payment_session, metadata: { stripe_event: event } }
end
|
#payment_icon_name ⇒ Object
268
269
270
|
# File 'app/models/spree_stripe/gateway.rb', line 268
def payment_icon_name
'stripe'
end
|
#payment_profiles_supported? ⇒ Boolean
256
257
258
|
# File 'app/models/spree_stripe/gateway.rb', line 256
def payment_profiles_supported?
true
end
|
#provider_class ⇒ Object
51
52
53
|
# File 'app/models/spree_stripe/gateway.rb', line 51
def provider_class
self.class
end
|
#purchase(amount_in_cents, payment_source, gateway_options = {}) ⇒ Object
65
66
67
|
# File 'app/models/spree_stripe/gateway.rb', line 65
def purchase(amount_in_cents, payment_source, gateway_options = {})
handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
end
|
#retrieve_charge(charge_id) ⇒ Object
195
196
197
|
# File 'app/models/spree_stripe/gateway.rb', line 195
def retrieve_charge(charge_id)
send_request { |opts| Stripe::Charge.retrieve(charge_id, opts) }
end
|
#send_request {|api_options| ... } ⇒ Object
304
305
306
|
# File 'app/models/spree_stripe/gateway.rb', line 304
def send_request
yield(api_options)
end
|
#update_customer(order: nil, user: nil) ⇒ Stripe::Customer
Updates a Stripe customer based on the order or user
184
185
186
187
188
189
190
191
192
193
|
# File 'app/models/spree_stripe/gateway.rb', line 184
def update_customer(order: nil, user: nil)
user ||= order&.user
return if user.blank?
customer = gateway_customers.find_by(user: user)
return if customer.blank?
payload = build_customer_payload(order: order, user: user)
send_request { |opts| Stripe::Customer.update(customer.profile_id, payload, opts) }
end
|
#void(response_code, _source, _gateway_options) ⇒ Object
124
125
126
127
128
129
130
131
|
# File 'app/models/spree_stripe/gateway.rb', line 124
def void(response_code, _source, _gateway_options)
return failure('Response code is blank') if response_code.blank?
protect_from_error do
response = cancel_payment_intent(response_code)
success(response.id, response)
end
end
|
#webhook_url ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'app/models/spree_stripe/gateway.rb', line 25
def webhook_url
store = stores.first
return nil unless store
if SpreeStripe::Config[:use_legacy_webhook_handlers]
StripeEvent::Engine.routes.url_helpers.root_url(host: store.url, protocol: 'https')
else
"#{store.formatted_url}/api/v3/webhooks/payments/#{prefixed_id}"
end
end
|