Class: PaymentRecipes::PayPal::REST::Transaction

Inherits:
Object
  • Object
show all
Includes:
Utils::Converters
Defined in:
lib/payment_recipes/paypal/rest/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Converters

#convert_to_money, #convert_to_string, #convert_to_symbol, #convert_to_time

Constructor Details

#initialize(paypal_transaction, payment:, expand: false) ⇒ Transaction

Returns a new instance of Transaction.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 30

def initialize(paypal_transaction, payment:, expand: false)
  unless paypal_transaction.is_a?(::PayPal::SDK::REST::DataTypes::Transaction)
    raise Exception, "#{ self.class.name } must be initialized with a PayPal Transaction" 
  end

  unless payment.is_a?(::PaymentRecipes::PayPal::REST::Payment)
    raise Exception, "Parameter payment must be a PaymentRecipes::PayPal::REST::Payment"
  end

  @currency           = convert_to_string(paypal_transaction.amount.currency)
  @total              = convert_to_money(amount: paypal_transaction.amount.total, currency: @currency)
  @description        = convert_to_string(paypal_transaction.description)
  @payment            = payment
  @payment_id         = payment.id

  @subtotal           = convert_to_money(amount: paypal_transaction.amount.details.subtotal, currency: @currency)
  @tax                = convert_to_money(amount: paypal_transaction.amount.details.tax, currency: @currency)
  @fee                = convert_to_money(amount: paypal_transaction.amount.details.fee, currency: @currency)
  @shipping           = convert_to_money(amount: paypal_transaction.amount.details.shipping, currency: @currency)
  @handling_fee       = convert_to_money(amount: paypal_transaction.amount.details.handling_fee, currency: @currency)
  @insurance          = convert_to_money(amount: paypal_transaction.amount.details.insurance, currency: @currency)
  @shipping_discount  = convert_to_money(amount: paypal_transaction.amount.details.shipping_discount, currency: @currency)
  @insurance          = convert_to_money(amount: paypal_transaction.amount.details.insurance, currency: @currency)
  @gift_wrap          = convert_to_money(amount: paypal_transaction.amount.details.gift_wrap, currency: @currency)
  @raw_transaction    = paypal_transaction

  @sales              = []
  @authorizations     = []
  @captures           = []
  @refunds            = []

  paypal_transaction.related_resources.each do |paypal_related_resource|
    if paypal_related_resource.sale.id 
      sale = ::PaymentRecipes::PayPal::REST::Sale.new(paypal_related_resource.sale, payment: payment)
      sale.reload! if expand

      @sales << sale
    end

    if paypal_related_resource.authorization.id
      authorization = ::PaymentRecipes::PayPal::REST::Authorization.new(paypal_related_resource.authorization, payment: payment)
      authorization.reload! if expand

      @authorizations << authorization
    end

    if paypal_related_resource.capture.id
      capture = ::PaymentRecipes::PayPal::REST::Capture.new(paypal_related_resource.capture, payment: payment)
      @captures << capture
    end

    if paypal_related_resource.refund.id
      refund = ::PaymentRecipes::PayPal::REST::Refund.new(paypal_related_resource.refund, payment: payment)
      @refunds << refund
    end
  end
end

Instance Attribute Details

#authorizationsObject (readonly)

Returns the value of attribute authorizations


22
23
24
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 22

def authorizations
  @authorizations
end

#capturesObject (readonly)

Returns the value of attribute captures


23
24
25
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 23

def captures
  @captures
end

#currencyObject (readonly)

Returns the value of attribute currency


5
6
7
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 5

def currency
  @currency
end

#descriptionObject (readonly)

Returns the value of attribute description


9
10
11
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 9

def description
  @description
end

#feeObject (readonly)

Returns the value of attribute fee


13
14
15
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 13

def fee
  @fee
end

#gift_wrapObject (readonly)

Returns the value of attribute gift_wrap


19
20
21
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 19

def gift_wrap
  @gift_wrap
end

#handling_feeObject (readonly)

Returns the value of attribute handling_fee


15
16
17
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 15

def handling_fee
  @handling_fee
end

#insuranceObject (readonly)

Returns the value of attribute insurance


16
17
18
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 16

def insurance
  @insurance
end

#paymentObject (readonly)

Returns the value of attribute payment


7
8
9
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 7

def payment
  @payment
end

#payment_idObject (readonly)

Returns the value of attribute payment_id


8
9
10
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 8

def payment_id
  @payment_id
end

#raw_transactionObject (readonly)

Returns the value of attribute raw_transaction


26
27
28
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 26

def raw_transaction
  @raw_transaction
end

#refundsObject (readonly)

Returns the value of attribute refunds


24
25
26
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 24

def refunds
  @refunds
end

#salesObject (readonly)

Returns the value of attribute sales


21
22
23
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 21

def sales
  @sales
end

#shippingObject (readonly)

Returns the value of attribute shipping


14
15
16
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 14

def shipping
  @shipping
end

#shipping_discountObject (readonly)

Returns the value of attribute shipping_discount


17
18
19
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 17

def shipping_discount
  @shipping_discount
end

#subtotalObject (readonly)

Returns the value of attribute subtotal


11
12
13
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 11

def subtotal
  @subtotal
end

#taxObject (readonly)

Returns the value of attribute tax


12
13
14
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 12

def tax
  @tax
end

#totalObject (readonly)

Returns the value of attribute total


6
7
8
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 6

def total
  @total
end

Instance Method Details

#authorizationObject


92
93
94
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 92

def authorization
  @authorizations.first
end

#captureObject


96
97
98
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 96

def capture
  @captures.first
end

#inspectObject


104
105
106
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 104

def inspect
  to_str
end

#refundObject


100
101
102
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 100

def refund
  @refunds.first
end

#saleObject


88
89
90
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 88

def sale
  @sales.first
end

#to_sObject


108
109
110
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 108

def to_s
  to_str
end

#to_strObject


112
113
114
# File 'lib/payment_recipes/paypal/rest/transaction.rb', line 112

def to_str
  "<#{ self.class.name } total=#{ @total.format } sales=#{ @sales.size } authorizations=#{ @authorizations.size } captures=#{ @captures.size }>"
end