Class: SpreePaypalCheckout::CreatePayment

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CreatePayment.



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

def initialize(paypal_order:, order: nil, gateway: nil, amount: nil)
  @paypal_order = paypal_order
  @order = order || paypal_order.order
  @gateway = gateway || paypal_order.gateway
  @amount = amount || paypal_order.amount
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/spree_paypal_checkout/create_payment.rb', line 10

def call
  source = SpreePaypalCheckout::CreateSource.new(
    paypal_payment_source: paypal_order.payment_source,
    gateway: gateway,
    order: order
  ).call

  # 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: paypal_order.paypal_payment_id,
    amount: amount
  )

  payment.source = source if source.present?
  payment.state = 'completed' # we're creating the payment record after the order is captured in PayPal API
  payment.save!
  payment
end