Skip to content

Commit

Permalink
Merge pull request #123 from spark-solutions/refund-date
Browse files Browse the repository at this point in the history
Send completed_at date as taxDate
  • Loading branch information
Maria Syczewska authored Sep 23, 2019
2 parents 56deddf + bf04dd3 commit 8dc98be
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
module SpreeAvataxOfficial
module Transactions
class PartialRefundPresenter < CreatePresenter
OVERRIDE_TYPE = 'TaxDate'.freeze
OVERRIDE_REASON = 'Refund'.freeze

def initialize(order:, refund_items:, transaction_code:)
@order = order
@refund_items = refund_items
@transaction_code = transaction_code
@transaction_type = SpreeAvataxOfficial::Transaction::RETURN_INVOICE
end

# based on https://developer.avalara.com/api-reference/avatax/rest/v2/models/CreateTransactionModel/
# date should be refund date, taxDate should be order's completed_at date
def to_json
super.merge(
date: formatted_date(Time.current),
taxOverride: tax_override
)
end

private

attr_reader :refund_items

def tax_override
{
type: self.class::OVERRIDE_TYPE,
reason: self.class::OVERRIDE_REASON,
taxDate: formatted_date(order.completed_at)
}
end

def items_payload
refund_items.map do |item, (quantity, amount)|
SpreeAvataxOfficial::ItemPresenter.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
SpreeAvataxOfficial::AddressPresenter.new(address: order.ship_address, address_type: 'ShipTo').to_json
),
lines: [SpreeAvataxOfficial::ItemPresenter.new(item: line_item, custom_quantity: quantity, custom_amount: amount).to_json],
commit: false,
commit: true,
discount: 0.0,
currencyCode: order.currency,
purchaseOrderNo: order.number
purchaseOrderNo: order.number,
taxOverride: {
reason: 'Refund',
taxDate: order.completed_at.strftime('%Y-%m-%d'),
type: 'TaxDate'
}
}
end

Expand All @@ -35,6 +40,8 @@
let(:amount) { line_item.amount * 2 }

it 'serializes the object' do
order.update(completed_at: Time.current)

expect(subject.to_json).to eq result
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject do
described_class.call(
order: order,
transaction_code: 'test-code-123',
transaction_code: 'test-code-1234',
refund_items: refund_item
)
end
Expand Down

0 comments on commit 8dc98be

Please sign in to comment.