Class: MerchantSidekick::PurchaseInvoice

Inherits:
Invoice
  • Object
show all
Defined in:
lib/merchant_sidekick/purchase_invoice.rb

Instance Method Summary collapse

Methods inherited from Invoice

#enter_authorized, #enter_paid, #enter_payment_declined, #enter_pending, #enter_refunded, #enter_voided, #evaluate, #exit_authorized, #exit_paid, #exit_payment_declined, #exit_pending, #exit_refunded, #exit_voided, #gross_total, #guard_payment_authorized_from_payment_declined, #guard_payment_authorized_from_pending, #guard_payment_captured_from_authorized, #guard_payment_paid_from_pending, #guard_payment_refunded_from_paid, #guard_payment_voided_from_authorized, #guard_transaction_declined_from_authorized, #guard_transaction_declined_from_payment_declined, #guard_transaction_declined_from_pending, #net_total, #number, #payment_type, #payment_type_display, #tax_total, #total

Instance Method Details

#authorizationObject

overrides accessor and caches authorization



73
74
75
76
77
78
79
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 73

def authorization
  @authorization ||= if auth = self.payments.find_by_action_and_success('authorization', true, :order => 'id ASC')
    auth
  elsif auth = self.payments.find_by_action_and_success('purchase', true, :order => 'id ASC')
    auth
  end
end

#authorization_referenceObject



81
82
83
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 81

def authorization_reference
  authorization.reference if authorization
end

#authorize(payment_object, options = {}) ⇒ Object

authorizes the payment payment_object is the credit_card instance or other payment objects



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 13

def authorize(payment_object, options={})
  transaction do
    authorization = MerchantSidekick::Payment.class_for(payment_object).authorize(
      gross_total,
      payment_object,
      payment_options(options)
    )

    self.push_payment(authorization)

    if authorization.success?
      save(:validate => false)
      payment_authorized!
    else
      # we don't want to save the payment and related objects
      # when the authorization fails
      # transaction_declined!
    end
    authorization
  end
end

#capture(options = {}) ⇒ Object

Captures a previously authorized payment. If the gross amount of the invoice has changed between authorization and capture, the difference in case authorization amount - capture amount > 0 will be refunded to the respective account, otherwise an exception thrown. Only payments can be captured, not deposits.

E.g.

@order = person.purchase(@product)
@payment = @order.authorize(@credit_card)
...
@order.capture
@order.invoice.paid? #=> true


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 49

def capture(options={})
  transaction do
    if authorization
      capture_result = authorization.class.capture(
        gross_total,
        authorization_reference,
        payment_options(authorization.content_attributes.merge(options))
      )

      self.push_payment(capture_result)

      save(:validate => false)

      if capture_result.success?
        payment_captured!
      else
        transaction_declined!
      end
      capture_result
    end
  end
end

#credit(options = {}) ⇒ Object

refunds the entire amount or the amount provided of the invoice

Options:

:card_number must be supplied in the options
:amount => specify the amount to be refunded


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 115

def credit(options={})
  transaction do
    if authorization
      credit_result = authorization.class.credit(
        options[:amount] || gross_total,
        authorization_reference,
        payment_options(authorization.content_attributes.merge(options))
      )
      self.push_payment(credit_result)

      save(:validate => false)

      if credit_result.success?
        payment_refunded!
      else
        transaction_declined!
      end
      capture_result
    end
  end
end

#payment_options(options = {}) ⇒ Object

returns a hash of additional merchant data passed to authorize you want to pass in the following additional options

:ip => ip address of the buyer


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 163

def payment_options(options={})
  { # general
    :buyer            => self.buyer,
    :seller           => self.seller,
    :payable          => self,
    # active merchant relevant
    :customer         => self.buyer ? "#{self.buyer.class.name} (#{self.buyer.id})" : nil,
    :merchant         => self.seller ? "#{self.seller.class.name} (#{self.seller.id})" : nil,
    :email            => self.buyer && self.buyer.respond_to?(:email) ? self.buyer.email : nil,
    :invoice          => self.number,
    :currency         => self.currency,
    :billing_address  => self.billing_address ? self.billing_address.to_merchant_attributes : nil,
    :shipping_address => self.shipping_address ? self.shipping_address.to_merchant_attributes : nil
  }.merge(options)
end

#purchase(payment_object, options = {}) ⇒ Object

Purchase invoice, combines authorization and capture in one step



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 138

def purchase(payment_object, options={})
  transaction do
    purchase_result = MerchantSidekick::Payment.class_for(payment_object).purchase(
      gross_total,
      payment_object,
      payment_options(options)
    )
    self.push_payment(purchase_result)

    save(:validate => false)

    if purchase_result.success?
      payment_paid!
    else
      transaction_declined!
    end
    purchase_result
  end
end

#purchase_invoice?Boolean

overrides superclass

Returns:

  • (Boolean)


7
8
9
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 7

def purchase_invoice?
  true
end

#void(options = {}) ⇒ Object

void a previously authorized payment



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/merchant_sidekick/purchase_invoice.rb', line 86

def void(options={})
  transaction do
    if authorization
      void_result = authorization.class.void(
        gross_total,
        authorization_reference,
        payment_options(authorization.content_attributes.merge(options))
      )
      self.push_payment(void_result)

      save(:validate => false)

      if void_result.success?
        payment_voided!
      else
        transaction_declined!
      end
      void_result
    end
  end
end