class PaymentRecipes::PayPal::REST::Sale

Attributes

create_time[R]
currency[R]
expanded[R]
id[R]
payment[R]
payment_id[R]
payment_mode[R]
protection_eligibility[R]
protection_eligibility_type[R]
raw_sale[R]
state[R]
total[R]
transaction_fee[R]
update_time[R]

Public Class Methods

find(id) click to toggle source
# 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) click to toggle source
# 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
new(paypal_sale, payment: nil, expanded: false) click to toggle source
# 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

Public Instance Methods

can_be_refunded?() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 109
def can_be_refunded?
  completed?
end
completed?() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 113
def completed?
  @state == :completed
end
denied?() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 129
def denied?
  @state == :denied
end
extract_and_store(paypal_sale) click to toggle source
# 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
inspect() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 97
def inspect
  to_str
end
partially_refunded?() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 117
def partially_refunded?
  @state == :partially_refunded
end
pending?() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 121
def pending?
  @state == :pending
end
refund() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 93
def refund
  refunds.first
end
refunded?() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 125
def refunded?
  @state == :refunded
end
refunds() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 87
def refunds
  payment.refunds.select do |refund|
    refund.sale_id == @id
  end
end
reload!() click to toggle source
# 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!() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 51
def reload_payment!
  @payment = ::PaymentRecipes::PayPal::REST::Payment.find(@payment_id) 

  @payment
end
to_s() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 101
def to_s
  to_str
end
to_str() click to toggle source
# File lib/payment_recipes/paypal/rest/sale.rb, line 105
def to_str
  "<#{ self.class.name } total=#{ @total.format } state=#{ @state } id=#{ @id }>"
end