2022-08-25 08:52:17 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for pay cart by Stripe
|
|
|
|
class Payments::StripeService
|
|
|
|
require 'stripe/service'
|
|
|
|
include Payments::PaymentConcern
|
|
|
|
|
|
|
|
def payment(order, payment_id)
|
|
|
|
amount = debit_amount(order)
|
2022-08-25 11:46:14 +02:00
|
|
|
|
|
|
|
raise Cart::ZeroPriceError if amount.zero?
|
|
|
|
|
2022-08-25 08:52:17 +02:00
|
|
|
# Create the PaymentIntent
|
|
|
|
intent = Stripe::PaymentIntent.create(
|
|
|
|
{
|
|
|
|
payment_method: payment_id,
|
|
|
|
amount: Stripe::Service.new.stripe_amount(amount),
|
|
|
|
currency: Setting.get('stripe_currency'),
|
|
|
|
confirmation_method: 'manual',
|
|
|
|
confirm: true,
|
|
|
|
customer: order.statistic_profile.user.payment_gateway_object.gateway_object_id
|
|
|
|
}, { api_key: Setting.get('stripe_secret_key') }
|
|
|
|
)
|
|
|
|
|
|
|
|
if intent&.status == 'succeeded'
|
2022-08-26 15:30:51 +02:00
|
|
|
o = payment_success(order, 'card', intent.id, intent.class.name)
|
2022-08-25 08:52:17 +02:00
|
|
|
return { order: o }
|
|
|
|
end
|
|
|
|
|
|
|
|
if intent&.status == 'requires_action' && intent&.next_action&.type == 'use_stripe_sdk'
|
|
|
|
{ order: order, payment: { requires_action: true, payment_intent_client_secret: intent.client_secret,
|
|
|
|
type: 'payment' } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_payment(order, payment_id)
|
|
|
|
intent = Stripe::PaymentIntent.confirm(payment_id, {}, { api_key: Setting.get('stripe_secret_key') })
|
|
|
|
if intent&.status == 'succeeded'
|
2022-08-26 15:30:51 +02:00
|
|
|
o = payment_success(order, 'card', intent.id, intent.class.name)
|
2022-08-25 08:52:17 +02:00
|
|
|
{ order: o }
|
|
|
|
else
|
|
|
|
order.update(payment_state: 'failed')
|
2022-08-26 15:30:51 +02:00
|
|
|
{ order: order, payment: { error: { statusText: 'payment failed' } } }
|
2022-08-25 08:52:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|