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.



22
23
24
25
# File 'lib/tax_cloud/transaction.rb', line 22

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

Instance Attribute Details

#cart_idObject

User-defined cart ID for the order.



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

def cart_id
  @cart_id
end

#cart_itemsObject

Array of CartItems.



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

def cart_items
  @cart_items
end

#customer_idObject

User-defined customer ID for the Transaction.



7
8
9
# File 'lib/tax_cloud/transaction.rb', line 7

def customer_id
  @customer_id
end

#destinationObject

The Address of which the shipment arrives.



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

def destination
  @destination
end

#order_idObject

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



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

def order_id
  @order_id
end

#originObject

The Address of which the shipment originates.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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