jetsgit / spree_tax_cloud

Spree Interface for TaxCloud SOAP API
19 stars 32 forks source link

Tax adjustment never created after tax cloud transaction lookup raises exception #57

Closed bbuchalter closed 10 years ago

bbuchalter commented 10 years ago

Any exception that occurs during a tax cloud transaction lookup prevents the next line of the caller from being executed. In this case:

  def lookup_tax_cloud
    unless tax_cloud_transaction.nil?
      tax_cloud_transaction.lookup
    else
      create_tax_cloud_transaction
      tax_cloud_transaction.lookup
      tax_cloud_adjustment  # never called if lookup raises exception
    end
  end

However, because the transaction was created successfully, we will never again follow the execution path which would cause a tax adjustment to be created. Thus subsequent update_adjustments calls have no effect.

My initial strategy for dealing with this issue was to simply wrap the vulnerable code in a transaction, however, I was quickly reminded that objects will not have their instance data returned to their pre-transactional state.

So I think the best strategy for ensuring an adjustment is created along with a transaction is as follows:

  def lookup_tax_cloud
    unless tax_cloud_transaction.nil?
      tax_cloud_transaction.lookup
    else
      create_tax_cloud_transaction
      begin
        tax_cloud_transaction.lookup
      ensure
        tax_cloud_adjustment
      end
    end
  end

Even if the adjustment is created with a zero dollar amount, subsequent lookups will allow update_adjustments to do it's job.

If you like what you see, let me know and I can submit a PR.

Thanks,

Brian

jetsgit commented 10 years ago

@bbuchalter, that looks good if you wish to submit a PR.

jetsgit commented 10 years ago

Closed with merge