Class: SpreePaypalCheckout::Order

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree_paypal_checkout/order.rb

Defined Under Namespace

Classes: AlreadyCapturedError, NotCapturedError

Instance Method Summary collapse

Instance Method Details

#capture!SpreePaypalCheckout::Order

Capture the PayPal order in PayPal API



42
43
44
45
46
# File 'app/models/spree_paypal_checkout/order.rb', line 42

def capture!
  raise AlreadyCapturedError if completed?

  CaptureOrder.new(paypal_order: self).call
end

#completed?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/spree_paypal_checkout/order.rb', line 61

def completed?
  status == 'COMPLETED'
end

#create_payment!Spree::Payment

Create a Spree::Payment record for this PayPal order

Returns:

  • (Spree::Payment)

Raises:



29
30
31
32
33
34
35
36
37
38
# File 'app/models/spree_paypal_checkout/order.rb', line 29

def create_payment!
  raise NotCapturedError unless completed?

  SpreePaypalCheckout::CreatePayment.new(
    order: order,
    paypal_order: self,
    gateway: payment_method,
    amount: amount
  ).call
end

#paypal_orderPaypalServerSdk::ApiResponse

gets the PayPal order from PayPal API

Returns:

  • (PaypalServerSdk::ApiResponse)


57
58
59
# File 'app/models/spree_paypal_checkout/order.rb', line 57

def paypal_order
  @paypal_order ||= gateway.client.orders.get_order({ 'id' => paypal_id })
end

#paypal_payment_idString

gets the PayPal payment ID from the PayPal order only available for authorized or captured orders

Returns:

  • (String)


51
52
53
# File 'app/models/spree_paypal_checkout/order.rb', line 51

def paypal_payment_id
  @paypal_payment_id ||= data.dig('purchase_units', 0, 'payments', 'captures', 0, 'id')
end