1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-13 23:48:55 +01:00
fab-manager/app/services/payments/stripe_service.rb

47 lines
1.5 KiB
Ruby
Raw Normal View History

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)
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'
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'
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')
{ order: order, payment: { error: { statusText: 'payment failed' } } }
2022-08-25 08:52:17 +02:00
end
end
end