Class: SpreePaypalCheckout::CaptureOrder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paypal_order:) ⇒ CaptureOrder

Returns a new instance of CaptureOrder.



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

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

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



10
11
12
# File 'app/services/spree_paypal_checkout/capture_order.rb', line 10

def amount
  @amount
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



10
11
12
# File 'app/services/spree_paypal_checkout/capture_order.rb', line 10

def gateway
  @gateway
end

#orderObject (readonly)

Returns the value of attribute order.



10
11
12
# File 'app/services/spree_paypal_checkout/capture_order.rb', line 10

def order
  @order
end

#paypal_orderObject (readonly)

Returns the value of attribute paypal_order.



10
11
12
# File 'app/services/spree_paypal_checkout/capture_order.rb', line 10

def paypal_order
  @paypal_order
end

Instance Method Details

#callObject



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
# File 'app/services/spree_paypal_checkout/capture_order.rb', line 12

def call
  return paypal_order if order.completed? || order.canceled?

  # capture the order in PayPal API
  gateway_response = gateway.capture(
    Money.new(amount, order.currency).cents,
    paypal_order.paypal_id,
    {
      order_id: order.number
    }
  )

  order.with_lock do
    # gateway_response.params is a JSON response from PayPal APIs
    paypal_order.update!(data: gateway_response.params)

    # create the Spree::Payment record
    paypal_order.create_payment!

    # complete the order in Spree
    Spree::Dependencies.checkout_complete_service.constantize.call(order: order)
  end

  paypal_order
end