Class: SpreeStripe::CreatePayment

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_stripe/create_payment.rb

Instance Method Summary collapse

Constructor Details

#initialize(order:, payment_intent:, gateway: nil, tax_transaction: nil, amount: nil) ⇒ CreatePayment

Returns a new instance of CreatePayment.



3
4
5
6
7
8
9
# File 'app/services/spree_stripe/create_payment.rb', line 3

def initialize(order:, payment_intent:, gateway: nil, tax_transaction: nil, amount: nil)
  @order = order
  @gateway = gateway || order.store.stripe_gateway
  @payment_intent = payment_intent
  @tax_transaction = tax_transaction
  @amount = amount || order.total_minus_store_credits
end

Instance Method Details

#callObject



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
40
41
42
43
44
45
46
# File 'app/services/spree_stripe/create_payment.rb', line 11

def call
  stripe_charge = payment_intent.stripe_charge

  if stripe_charge.present?
    source = SpreeStripe::CreateSource.new(
      order: order,
      stripe_payment_method_details: stripe_charge.payment_method_details,
      stripe_payment_method_id: stripe_charge.payment_method,
      stripe_billing_details: stripe_charge.billing_details,
      gateway: gateway
    ).call
  elsif payment_intent.charge_not_required?
    stripe_payment_intent = payment_intent.stripe_payment_intent
    source = SpreeStripe::CreateSource.new(
      order: order,
      stripe_payment_method_details: stripe_payment_intent.payment_method,
      stripe_payment_method_id: stripe_payment_intent.payment_method.id,
      stripe_billing_details: nil,
      gateway: gateway
    ).call
  end

  # sometimes a job is re-tried and creates a double payment record so we need to avoid it!
  payment = order.payments.find_or_initialize_by(
    payment_method_id: gateway.id,
    response_code: payment_intent.stripe_id,
    amount: amount
  )

  payment.source = source if source.present?
  payment.stripe_tax_transaction_id = tax_transaction
  payment.stripe_charge_id = stripe_charge&.id

  payment.save!
  payment
end