Class: PaymentRecipes::PayPal::REST::Sale

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Equality

#==

Methods included from Utils::Converters

#convert_to_money, #convert_to_string, #convert_to_symbol, #convert_to_time

Constructor Details

#initialize(paypal_sale, payment: nil, expanded: false) ⇒ Sale

Returns a new instance of Sale.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 25

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

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

    @payment = payment
    @payment_id = payment.id
  end

  extract_and_store(paypal_sale)
  @expanded = expanded
end

Instance Attribute Details

#create_timeObject (readonly)

Returns the value of attribute create_time.



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

def create_time
  @create_time
end

#currencyObject (readonly)

Returns the value of attribute currency.



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

def currency
  @currency
end

#expandedObject (readonly)

Returns the value of attribute expanded.



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

def expanded
  @expanded
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#paymentObject (readonly)

Returns the value of attribute payment.



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

def payment
  @payment
end

#payment_idObject (readonly)

Returns the value of attribute payment_id.



10
11
12
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 10

def payment_id
  @payment_id
end

#payment_modeObject (readonly)

Returns the value of attribute payment_mode.



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

def payment_mode
  @payment_mode
end

#protection_eligibilityObject (readonly)

Returns the value of attribute protection_eligibility.



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

def protection_eligibility
  @protection_eligibility
end

#protection_eligibility_typeObject (readonly)

Returns the value of attribute protection_eligibility_type.



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

def protection_eligibility_type
  @protection_eligibility_type
end

#raw_saleObject (readonly)

Returns the value of attribute raw_sale.



20
21
22
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 20

def raw_sale
  @raw_sale
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

#transaction_feeObject (readonly)

Returns the value of attribute transaction_fee.



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

def transaction_fee
  @transaction_fee
end

#update_timeObject (readonly)

Returns the value of attribute update_time.



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

def update_time
  @update_time
end

Class Method Details

.find(id) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 134

def find(id)
  paypal_sale = find_raw(id)

  if paypal_sale
    new(paypal_sale, payment: nil, expanded: true)
  else
    nil
  end
end

.find_raw(id) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 144

def find_raw(id)
  begin
    ::PayPal::SDK::REST::Sale.find(id)

  rescue ::PayPal::SDK::Core::Exceptions::ResourceNotFound
    nil
  end
end

Instance Method Details

#can_be_refunded?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 109

def can_be_refunded?
  completed?
end

#completed?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 113

def completed?
  @state == :completed
end

#denied?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 129

def denied?
  @state == :denied
end

#extract_and_store(paypal_sale) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 154

def extract_and_store(paypal_sale)
  @currency                     = convert_to_string(paypal_sale.amount.currency)
  @total                        = convert_to_money(amount: paypal_sale.amount.total, currency: @currency)
  @create_time                  = convert_to_time(paypal_sale.create_time)
  @update_time                  = convert_to_time(paypal_sale.update_time)
  @id                           = convert_to_string(paypal_sale.id)
  @payment_id                   = convert_to_string(paypal_sale.parent_payment)
  @state                        = convert_to_symbol(paypal_sale.state)
  @raw_sale                     = paypal_sale

  @transaction_fee              = convert_to_money(amount: paypal_sale.transaction_fee.value, currency: paypal_sale.transaction_fee.currency)
  @payment_mode                 = convert_to_string(paypal_sale.payment_mode)
  @protection_eligibility       = convert_to_string(paypal_sale.protection_eligibility)
  @protection_eligibility_type  = convert_to_string(paypal_sale.protection_eligibility_type)
end

#inspectObject



97
98
99
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 97

def inspect
  to_str
end

#partially_refunded?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 117

def partially_refunded?
  @state == :partially_refunded
end

#pending?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 121

def pending?
  @state == :pending
end

#refundObject



93
94
95
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 93

def refund
  refunds.first
end

#refunded?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 125

def refunded?
  @state == :refunded
end

#refundsObject



87
88
89
90
91
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 87

def refunds
  payment.refunds.select do |refund|
    refund.sale_id == @id
  end
end

#reload!Object



43
44
45
46
47
48
49
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 43

def reload!
  paypal_sale = self.class.find_raw(@id)
  extract_and_store(paypal_sale)
  @expanded = true

  self
end

#reload_payment!Object



51
52
53
54
55
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 51

def reload_payment!
  @payment = ::PaymentRecipes::PayPal::REST::Payment.find(@payment_id) 

  @payment
end

#to_sObject



101
102
103
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 101

def to_s
  to_str
end

#to_strObject



105
106
107
# File 'lib/payment_recipes/paypal/rest/sale.rb', line 105

def to_str
  "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
end