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
|
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
def payment(order, coupon_code, payment_id)
|
|
|
|
amount = debit_amount(order, coupon_code)
|
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 20:10:21 +02:00
|
|
|
o = payment_success(order, coupon_code, '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
|
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
def confirm_payment(order, coupon_code, payment_id)
|
2022-08-25 08:52:17 +02:00
|
|
|
intent = Stripe::PaymentIntent.confirm(payment_id, {}, { api_key: Setting.get('stripe_secret_key') })
|
|
|
|
if intent&.status == 'succeeded'
|
2022-08-26 20:10:21 +02:00
|
|
|
o = payment_success(order, coupon_code, 'card', intent.id, intent.class.name)
|
2022-08-25 08:52:17 +02:00
|
|
|
{ order: o }
|
|
|
|
else
|
2022-09-14 19:54:24 +02:00
|
|
|
order.update(state: 'payment_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
|