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
|
# File 'app/services/spree_stripe/create_payment_intent.rb', line 3
def call(order, gateway, stripe_payment_method_id: nil, off_session: false)
total = order.total_minus_store_credits
amount = Spree::Money.new(total).cents
return nil if amount.zero?
payment_intent = SpreeStripe::PaymentIntent.find_by(order: order, payment_method: gateway)
return payment_intent if payment_intent.present?
response = gateway.create_payment_intent(
amount,
order,
payment_method_id: stripe_payment_method_id,
off_session: off_session
)
customer = gateway.fetch_or_create_customer(user: order.user) if order.user.present?
ephemeral_key_response = gateway.create_ephemeral_key(customer.profile_id) if customer.present?
ephemeral_key_secret = ephemeral_key_response&.params['secret'] if ephemeral_key_response.present?
SpreeStripe::PaymentIntent.create!(
order: order,
payment_method: gateway,
amount: total,
stripe_id: response.authorization,
client_secret: response.params['client_secret'],
customer_id: customer&.profile_id,
stripe_payment_method_id: stripe_payment_method_id,
ephemeral_key_secret: ephemeral_key_secret
)
end
|