Class: TaxCloud::Transaction

Inherits:
Record
  • Object
show all
Defined in:
lib/tax_cloud/transaction.rb

Overview

Lookup tax rate, authorize, and capture the information to be logged into TaxCloud.

Note: The Transaction must not change between the lookup and authorization method calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Transaction

Create a new transaction.

Parameters

params

Transaction params.



24
25
26
27
# File 'lib/tax_cloud/transaction.rb', line 24

def initialize(params = {})
  params = { cart_items: [] }.merge(params)
  super params
end

Instance Attribute Details

#cart_idObject

User-defined cart ID for the order.



11
12
13
# File 'lib/tax_cloud/transaction.rb', line 11

def cart_id
  @cart_id
end

#cart_itemsObject

Array of CartItems.



13
14
15
# File 'lib/tax_cloud/transaction.rb', line 13

def cart_items
  @cart_items
end

#customer_idObject

User-defined customer ID for the Transaction.



9
10
11
# File 'lib/tax_cloud/transaction.rb', line 9

def customer_id
  @customer_id
end

#destinationObject

The Address of which the shipment arrives.



19
20
21
# File 'lib/tax_cloud/transaction.rb', line 19

def destination
  @destination
end

#order_idObject

The order ID for authorized, captured, and authorized_with_captured methods.



15
16
17
# File 'lib/tax_cloud/transaction.rb', line 15

def order_id
  @order_id
end

#originObject

The Address of which the shipment originates.



17
18
19
# File 'lib/tax_cloud/transaction.rb', line 17

def origin
  @origin
end

Instance Method Details

#authorized(options = {}) ⇒ Object

Once a purchase has been made and payment has been authorized, this method must be called. A matching Lookup call must have been made before this is called.

Options

  • date_authorized - The date the transaction was authorized. Default is today.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tax_cloud/transaction.rb', line 48

def authorized(options = {})
  options = { date_authorized: Date.today }.merge(options)

  request_params = {
    'customerID' => customer_id,
    'cartID' => cart_id,
    'orderID' => order_id,
    'dateAuthorized' => xml_date(options[:date_authorized])
  }

  response = TaxCloud.client.request :authorized, request_params
  TaxCloud::Responses::Authorized.parse response
end

#authorized_with_capture(options = {}) ⇒ Object

Combines the authorized and captured methods into a single call

Options

date_authorized

The date the transaction was authorized. Default is today.

date_captured
  • The date the transaction was captured. Default is today.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tax_cloud/transaction.rb', line 84

def authorized_with_capture(options = {})
  options = { date_authorized: Date.today, date_captured: Date.today }.merge(options)
  request_params = {
    'customerID' => customer_id,
    'cartID' => cart_id,
    'orderID' => order_id,
    'dateAuthorized' => xml_date(options[:date_authorized]),
    'dateCaptured' => xml_date(options[:date_captured])
  }

  response = TaxCloud.client.request :authorized_with_capture, request_params
  TaxCloud::Responses::AuthorizedWithCapture.parse response
end

#captured(options = {}) ⇒ Object

Complete the transaction. The order_id passed into captured must match the order_id that was passed into authorized.

Options

date_captured

The time the transaction was captured. Default is today.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tax_cloud/transaction.rb', line 66

def captured(options = {})
  options = { date_captured: Date.today }.merge(options)
  request_params = {
    'customerID' => customer_id,
    'cartID' => cart_id,
    'orderID' => order_id,
    'dateCaptured' => xml_date(options[:date_captured])
  }

  response = TaxCloud.client.request :captured, request_params
  TaxCloud::Responses::Captured.parse response
end

#lookupObject

Lookup the tax rate for the transaction. The returned information is based on the originating address, destination address, and cart items.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tax_cloud/transaction.rb', line 31

def lookup
  request_params = {
    'customerID' => customer_id,
    'cartID' => cart_id,
    'cartItems' => { 'CartItem' => cart_items.map(&:to_hash) },
    'origin' => origin.to_hash,
    'destination' => destination.to_hash
  }

  response = TaxCloud.client.request :lookup, request_params
  TaxCloud::Responses::Lookup.parse response
end

#returned(options = {}) ⇒ Object

Marks any included cart items as returned.

Options

returned_date

The date the return occured. Default is today.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tax_cloud/transaction.rb', line 102

def returned(options = {})
  options = { returned_date: Date.today }.merge(options)
  request_params = {
    'orderID' => order_id,
    'cartItems' => { 'CartItem' => cart_items.map(&:to_hash) },
    'returnedDate' => xml_date(options[:returned_date])
  }

  response = TaxCloud.client.request :returned, request_params
  TaxCloud::Responses::Returned.parse response
end