Class: SpreeExactorConnector::SpreeExactorConnector

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/spree_exactor_connector.rb

Constant Summary collapse

PLUGIN_VERSION =
"20130729"
TEST_TRANSACTION_NAME =
"Spree Test Transaction"
TEST_ITEM_DESRIPTION =
"Exactor Account Verification. Plug-in version:"
PLUGIN_NAME =
"SpreeCommerce"
SHIPPING_EUC_NO_HANDLING =
"EUC-13010204"
SHIPPING_EUC_WITH_HANDLING =
"EUC-13010101"
DISCOUNT_EUC =
"EUC-99010102"
NON_TAXABLE_EUC =
"EUC-99030101"
MARKUP_EUC =
"EUC-99010202"

Instance Method Summary collapse

Constructor Details

#initializeSpreeExactorConnector

Returns a new instance of SpreeExactorConnector.



18
19
20
# File 'app/helpers/spree_exactor_connector.rb', line 18

def initialize
  @settings = Spree::ExactorSetting.get_settings()
end

Instance Method Details

#calculate_hash(order) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/spree_exactor_connector.rb', line 52

def calculate_hash(order)
  if @settings.nil?
    return ''
  end

  taxRequest = create_invoice_request_for_order(order)

  invoice = taxRequest.InvoiceRequest
  hash = '|' + invoice.ExemptionId.to_s
  hash += '|' + calculate_hash_for_address(invoice.ShipFrom)
  hash += '|' + calculate_hash_for_address(invoice.ShipTo)
  hash += '|' + calculate_hash_for_address(invoice.BillTo)
  hash += '|' + calculate_hash_for_address(invoice.BillTo)
  invoice.LineItem.each do |line|
    hash += '|' + calculate_hash_for_line_item(line)
  end
  Digest::MD5.hexdigest(hash)
end

#calculate_tax_for_order(order) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/spree_exactor_connector.rb', line 41

def calculate_tax_for_order(order)
  response = nil
  if @settings.nil?
    response = create_tax_response(0)
    return response
  end
  taxRequest = create_invoice_request_for_order(order)
  exactorApi = ExactorApi::ExactorApiService.new
  return map_calculation_response(exactorApi.send_request(taxRequest))
end

#commit_refund_transaction(order) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/spree_exactor_connector.rb', line 81

def commit_refund_transaction(order)
  taxRequest = create_tax_request_skeleton()
  taxRequest.CommitRequest=CommitRequest.new
  taxRequest.CommitRequest.CommitDate=Date.current.to_s
  taxRequest.CommitRequest.InvoiceNumber=order.number
  invoice = InvoiceRequest.new
  invoice.ShipFrom=@settings.get_ship_from()
  invoice.ShipTo=map_from_spree_address(order.ship_address)
  invoice.BillTo=map_from_spree_address(order.bill_address)
  invoice.SaleDate=Date.current.to_s
  invoice.PurchaseOrderNumber=order.number.to_s
  if order.user.anonymous?.nil?
    invoice.ExemptionId=order.user.exactor_exemption_id
  end
  #SPECIFICATION: refund_amount = amount + [amount/(total-S&H)] * total tax.
  amount=order.current_payment.amount - (order.current_payment.amount*order.tax_total)/(order.total)
  amount=(-1)*amount if amount>0
  invoice.LineItem=[build_refund_lineitem(amount)]
  taxRequest.CommitRequest.InvoiceRequest=invoice
  exactorApi = ExactorApi::ExactorApiService.new
  return map_commit_response(exactorApi.send_request(taxRequest))
end

#commit_transaction(order) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'app/helpers/spree_exactor_connector.rb', line 71

def commit_transaction(order)
  taxRequest = create_tax_request_skeleton()
  taxRequest.CommitRequest=CommitRequest.new
  taxRequest.CommitRequest.PriorTransactionId=order.exactor_invoice_transaction
  taxRequest.CommitRequest.InvoiceNumber=order.number
  taxRequest.CommitRequest.CommitDate=Date.current.to_s
  exactorApi = ExactorApi::ExactorApiService.new
  return map_commit_response(exactorApi.send_request(taxRequest))
end

#do_cancel!(order) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/spree_exactor_connector.rb', line 124

def do_cancel!(order)
  if can_be_handled?(Spree::ExactorSetting::COMMIT_OPTIONS::TRANSACTION_STATE, order) or can_be_handled?(Spree::ExactorSetting::COMMIT_OPTIONS::SHIPMENT_STATE, order)
    if !order.exactor_commit_transaction.nil?
      response = refund_transaction(order)
      if (response.is_valid)
        order.update_column(:exactor_commit_transaction, nil)
      else
        order.errors[:base]<<"Tax calculation failure: #{response.error_code}:#{response.error_description}"
      end
    end
  end
end

#do_checkout!(order) ⇒ Object



147
148
149
150
151
# File 'app/helpers/spree_exactor_connector.rb', line 147

def do_checkout!(order)
  if (can_be_handled?(Spree::ExactorSetting::COMMIT_OPTIONS::TRANSACTION_STATE, order))
    do_commit!(order)
  end
end

#do_commit!(order) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/spree_exactor_connector.rb', line 113

def do_commit!(order)
  response = commit_transaction(order)
  if (response.is_valid)
    order.update_column(:exactor_invoice_transaction, nil)
    order.update_column(:exactor_commit_transaction, response.commit_id)
  else
    order.errors[:base]<<"Tax calculation failure: #{response.error_code}:#{response.error_description}"
  end
  return response
end

#do_payment_completed!(order) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/spree_exactor_connector.rb', line 153

def do_payment_completed!(order)
  if (can_be_handled?(Spree::ExactorSetting::COMMIT_OPTIONS::PAYMENT_STATE, order))
    update_tax_if_needed(order)
    resp = nil
    if order.exactor_commit_transaction
      resp = refund_transaction(order)
    end
    if (!resp or resp.is_valid)
      resp = do_commit!(order)
    end
    if (!resp.is_valid)
      order.current_payment.errors[:base]<<"Tax synchronization failure: #{resp.error_code}:#{resp.error_description}" unless order.current_payment.nil?
    end
  end
end

#do_refund_payment!(order) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/helpers/spree_exactor_connector.rb', line 169

def do_refund_payment!(order)
  if (can_be_handled?(Spree::ExactorSetting::COMMIT_OPTIONS::PAYMENT_STATE, order))
    response=nil
    if (order.current_payment.amount==order.total)
      response=refund_transaction(order)
    else
      if (!have_non_taxable_items?(order))
        response = commit_refund_transaction(order)
      else
        return
      end
    end
    if (response and response.is_valid)
      order.update_column(:exactor_commit_transaction, nil)
    else
      order.errors[:base]<<"Tax synchronization failure: #{response.error_code}:#{response.error_description}"
      order.current_payment.errors[:base]<<"Tax synchronization failure: #{response.error_code}:#{response.error_description}"
    end
  end
end

#do_shipped!(order) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'app/helpers/spree_exactor_connector.rb', line 137

def do_shipped!(order)
  if (can_be_handled?(Spree::ExactorSetting::COMMIT_OPTIONS::SHIPMENT_STATE, order))
    update_tax_if_needed(order)
    resp = do_commit!(order)
    if (!resp.is_valid)
      order.shipment.errors[:base]<<"Tax synchronization failure: #{resp.error_code}:#{resp.error_description}"
    end
  end
end

#refund_transaction(order) ⇒ Object



104
105
106
107
108
109
110
111
# File 'app/helpers/spree_exactor_connector.rb', line 104

def refund_transaction(order)
  taxRequest = create_tax_request_skeleton()
  taxRequest.RefundRequest=RefundRequest.new
  taxRequest.RefundRequest.RefundDate=Date.current.to_s
  taxRequest.RefundRequest.PriorTransactionId=order.exactor_commit_transaction
  exactorApi = ExactorApi::ExactorApiService.new
  return map_refund_response(exactorApi.send_request(taxRequest))
end

#send_test_transaction(addressType, merchantId, userId) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/spree_exactor_connector.rb', line 22

def send_test_transaction (addressType, merchantId, userId)
  exactorApi = ExactorApi::ExactorApiService.new
  testRequest = TaxRequest.new
  testRequest.MerchantId=merchantId
  testRequest.UserId=userId
  testInvoice = InvoiceRequest.new
  addressType.FullName = TEST_TRANSACTION_NAME
  testInvoice.ShipFrom=addressType
  testInvoice.ShipTo=addressType
  testInvoice.BillTo=addressType
  testInvoice.SaleDate=Date.current.to_s
  testLineItem = InvoiceRequest::LineItem.new(nil, "#{TEST_ITEM_DESRIPTION} #{PLUGIN_VERSION}", 1, 0.0)
  testInvoice.LineItem=testLineItem
  testRequest.InvoiceRequest= testInvoice
  testRequest.xmlattr_version=PLUGIN_VERSION
  testRequest.xmlattr_plugin = PLUGIN_NAME
  return exactorApi.send_request(testRequest)
end