Skip to content

Commit

Permalink
Merge pull request #32 from surprisehighway/release/v2.1.4
Browse files Browse the repository at this point in the history
Release/v2.1.4
  • Loading branch information
imagehat authored May 21, 2021
2 parents 6589d67 + 2f32ed3 commit 7a1377d
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 63 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.1.4
### Added
- Allow for live and sandbox credentials to be set as ENV variables with autosuggest fields in the plugin settings to keep sensitive info out of Project Config. [#11](https://github.com/surprisehighway/craft-avatax/issues/11) [30](https://github.com/surprisehighway/craft-avatax/issues/30)
- Update config override example to default to multi-environment settings with ENV examples.
- For new installs the address validation setting will be disabled by default.

### Fixed
- Better error handling. No longer throw exceptions on the front end but log more complete error responses. [#29](https://github.com/surprisehighway/craft-avatax/issues/29) [#31](https://github.com/surprisehighway/craft-avatax/issues/31)

## 2.1.3
### Fixed
- Fixed an issue where updating the shipping state did not refresh the cached order. [#27](https://github.com/surprisehighway/craft-avatax/issues/27)
Expand Down
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Setup and configuration is detailed below, but here's a quick overview of what y
4. Click the *Test Connection* button to verify your connection.
5. Click the *Save* button to save your settings.

> Hint: it is recommended to use [Environment Variables](https://craftcms.com/docs/3.x/config/#environmental-configuration) for the following settings: Account ID, License Key, Company Code, Sandbox Account ID, Sandbox License Key, Sandbox Company Code to prevent sensitive info being saved in Project Config or the database. See [Config Overrides](#config-overrides) below for more examples.
![Account Settings](resources/img/plugin-settings.png)

## Configuring AvaTax Ship From Origin
Expand Down Expand Up @@ -160,6 +162,78 @@ You can use Craft's [plugin config file](https://docs.craftcms.com/v3/extend/plu
1. Copy `config.php` from the `avataxtax` directory to your craft/config folder and rename it to `avatax.php`
2. Update values in `avatax.php` and save.

It is recommended to use ENV variables to following account settings:

.ENV file:
```
AVATAX_ACCOUNT_ID=123456
AVATAX_LICENSE_KEY=987654321
AVATAX_COMPANY_CODE=MYCOMPANY
AVATAX_SANDBOX_ACCOUNT_ID=123456
AVATAX_SANDBOX_LICENSE_KEY=987654321
AVATAX_SANDBOX_COMPANY_CODE=MYCOMPANY
```

config/avatax.php:
```
<?php
return [
'*' => [
// The address you will be posting from.
'shipFromName' => 'John Doe',
'shipFromStreet1' => '201 E Randolph St',
'shipFromStreet2' => '',
'shipFromStreet3' => '',
'shipFromCity' => 'Chicago',
'shipFromState' => 'IL',
'shipFromZipCode' => '60601',
'shipFromCountry' => 'US',
// The default Avalara Tax Code to use for Products.
'defaultTaxCode' => 'P0000000',
// The default Avalara Tax Code to use for Shipping.
'defaultShippingCode' => 'FR',
// The default Avalara Tax Code to use for Discounts.
'defaultDiscountCode' => 'OD010000',
// Production account information from ENV.
'accountId' => '$AVATAX_ACCOUNT_ID',
'licenseKey' => '$AVATAX_LICENSE_KEY',
'companyCode' => '$AVATAX_COMPANY_CODE',
// Sandbox account information from ENV.
'sandboxAccountId' => '$AVATAX_SANDBOX_ACCOUNT_ID',
'sandboxLicenseKey' => '$AVATAX_SANDBOX_LICENSE_KEY',
'sandboxCompanyCode' => '$AVATAX_SANDBOX_COMPANY_CODE',
// Environment - 'production' or 'sandbox'.
'environment' => 'sandbox',
// AvaTax options - true or false
'enableTaxCalculation' => true,
'enableCommitting' => true,
'enableAddressValidation' => false,
'enablePartialRefunds' => true,
// Enable debugging - true or false
'debug' => true,
],
'production' => [
// Environment - 'production' or 'sandbox'.
'environment' => 'production',
// Enable debugging - true or false
'debug' => false,
],
];
```

## Ajax Examples

There is a JSON controller endpoint you can use for AJAX lookups/validation on the front-end. Currently the only endpoints are for address validation and [CertCapture](https://certcapture6xrest.docs.apiary.io) customer lookup by customer number.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "surprisehighway/craft-avatax",
"description": "Calculate and add sales tax to an order's base tax using Avalara's Avatax service.",
"type": "craft-plugin",
"version": "2.1.3",
"version": "2.1.4",
"keywords": [
"craft",
"cms",
Expand Down
Binary file modified resources/img/plugin-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 43 additions & 33 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,55 @@
*/

return [
// The address you will be posting from.
'shipFromName' => 'John Doe',
'shipFromStreet1' => '201 E Randolph St',
'shipFromStreet2' => '',
'shipFromStreet3' => '',
'shipFromCity' => 'Chicago',
'shipFromState' => 'IL',
'shipFromZipCode' => '60601',
'shipFromCountry' => 'US',
'*' => [
// The address you will be posting from.
'shipFromName' => 'John Doe',
'shipFromStreet1' => '201 E Randolph St',
'shipFromStreet2' => '',
'shipFromStreet3' => '',
'shipFromCity' => 'Chicago',
'shipFromState' => 'IL',
'shipFromZipCode' => '60601',
'shipFromCountry' => 'US',

// The default Avalara Tax Code to use for Products.
'defaultTaxCode' => 'P0000000',
// The default Avalara Tax Code to use for Products.
'defaultTaxCode' => 'P0000000',

// The default Avalara Tax Code to use for Shipping.
'defaultShippingCode' => 'FR',
// The default Avalara Tax Code to use for Shipping.
'defaultShippingCode' => 'FR',

// The default Avalara Tax Code to use for Discounts.
'defaultDiscountCode' => 'OD010000',
// The default Avalara Tax Code to use for Discounts.
'defaultDiscountCode' => 'OD010000',

// Production account information.
'accountId' => '',
'licenseKey' => '',
'companyCode' => '',
// Production account information from ENV.
'accountId' => '$AVATAX_ACCOUNT_ID',
'licenseKey' => '$AVATAX_LICENSE_KEY',
'companyCode' => '$AVATAX_COMPANY_CODE',

// Sandbox account information.
'sandboxAccountId' => '',
'sandboxLicenseKey' => '',
'sandboxCompanyCode' => '',
// Sandbox account information from ENV.
'sandboxAccountId' => '$AVATAX_SANDBOX_ACCOUNT_ID',
'sandboxLicenseKey' => '$AVATAX_SANDBOX_LICENSE_KEY',
'sandboxCompanyCode' => '$AVATAX_SANDBOX_COMPANY_CODE',

// Environment - 'production' or 'sandbox'.
'environment' => 'sandbox',
// Environment - 'production' or 'sandbox'.
'environment' => 'sandbox',

// AvaTax options - true or false
'enableTaxCalculation' => true,
'enableCommitting' => true,
'enableAddressValidation' => true,
'enablePartialRefunds' => true,

// Enable debugging - true or false
'debug' => true,
// AvaTax options - true or false
'enableTaxCalculation' => true,
'enableCommitting' => true,
'enableAddressValidation' => false,
'enablePartialRefunds' => true,

// Enable debugging - true or false
'debug' => true,
],

'production' => [
// Environment - 'production' or 'sandbox'.
'environment' => 'production',

// Enable debugging - true or false
'debug' => false,
],

];
67 changes: 67 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Craft;
use craft\base\Model;
use craft\behaviors\EnvAttributeParserBehavior;

/**
* @author Surprise Highway
Expand Down Expand Up @@ -161,6 +162,71 @@ class Settings extends Model
// Public Methods
// =========================================================================

public function behaviors()
{
return [
'parser' => [
'class' => EnvAttributeParserBehavior::class,
'attributes' => [
'accountId',
'licenseKey',
'companyCode',
'sandboxAccountId',
'sandboxLicenseKey',
'sandboxCompanyCode',
],
],
];
}

/**
* @return string
*/
public function getAccountId()
{
return (Craft::parseEnv($this->accountId) ?? '');
}

/**
* @return string
*/
public function getLicenseKey()
{
return (Craft::parseEnv($this->licenseKey) ?? '');
}

/**
* @return string
*/
public function getCompanyCode()
{
return (Craft::parseEnv($this->companyCode) ?? '');
}

/**
* @return string
*/
public function getSandboxAccountId()
{
return (Craft::parseEnv($this->sandboxAccountId) ?? '');
}

/**
* @return string
*/
public function getSandboxLicenseKey()
{
return (Craft::parseEnv($this->sandboxLicenseKey) ?? '');
}

/**
* @return string
*/
public function getSandboxCompanyCode()
{
return (Craft::parseEnv($this->sandboxCompanyCode) ?? '');
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -253,6 +319,7 @@ public function rules()
],
'boolean'
],
['enableAddressValidation', 'default', 'value' => '0'],
];
}
}
34 changes: 17 additions & 17 deletions src/services/SalesTaxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function refundFullTransaction($amount, Transaction $transaction)
return true;
}

Avatax::error('Transaction Code '.$transactionCode.' could not be refunded.');
Avatax::error('Transaction Code '.$transactionCode.' could not be refunded.', ['request' => json_encode($request), 'response' => json_encode($response)]);

return false;
}
Expand Down Expand Up @@ -370,8 +370,7 @@ public function validateAddress(Address $address)
}

// Request failed
Avatax::error('Address validation failed.');
throw new Exception('Invalid address.');
Avatax::error('Address validation failed.', ['request' => json_encode($address), 'response' => json_encode($response)]);

return false;
}
Expand Down Expand Up @@ -430,12 +429,12 @@ private function getCompanyCode()
{
if($this->settings['environment'] === 'production')
{
$companyCode = $this->settings['companyCode'];
$companyCode = $this->settings->getCompanyCode();
}

if($this->settings['environment'] === 'sandbox')
{
$companyCode = $this->settings['sandboxCompanyCode'];
$companyCode =$this->settings->getSandboxCompanyCode();
}

return $companyCode;
Expand Down Expand Up @@ -514,35 +513,38 @@ private function createClient($settings = null)

if($settings['environment'] === 'production')
{
if(!empty($settings['accountId']) && !empty($settings['licenseKey']))
$accountId = (Craft::parseEnv($settings['accountId'])) ?? '';
$licenseKey = (Craft::parseEnv($settings['licenseKey'])) ?? '';

if(!empty($accountId) && !empty($licenseKey))
{
// Create a new client
$client = new AvaTaxClient($pluginName, $pluginVersion, $machineName, 'production');

$client->withLicenseKey( $settings['accountId'], $settings['licenseKey'] );
$client->withLicenseKey($accountId, $licenseKey);

return $client;
}
}

if($settings['environment'] === 'sandbox')
{
if(!empty($settings['sandboxAccountId']) && !empty($settings['sandboxLicenseKey']))
$sandboxAccountId = (Craft::parseEnv($settings['sandboxAccountId'])) ?? '';
$sandboxLicenseKey = (Craft::parseEnv($settings['sandboxLicenseKey'])) ?? '';

if(!empty($sandboxAccountId) && !empty($sandboxLicenseKey))
{
// Create a new client
$client = new AvaTaxClient($pluginName, $pluginVersion, $machineName, 'sandbox');

$client->withLicenseKey( $settings['sandboxAccountId'], $settings['sandboxLicenseKey'] );
$client->withLicenseKey($sandboxAccountId, $sandboxLicenseKey);

return $client;
}
}

// Don't have credentials
Avatax::error('Avatax Account Credentials not found');

// throw a craft exception which returns the error
throw new Exception('Avatax Account Credentials not found');
Avatax::error('Avatax Account Credentials not found. Check the plugin settings.');
}

/**
Expand Down Expand Up @@ -708,10 +710,8 @@ private function getTotalTax($order, $transaction)
return $response->totalTax;
}

Avatax::error('Request to avatax.com failed');

// Request failed
throw new Exception('Request could not be completed');
// Log error
Avatax::error('Request to avatax.com failed', ['request' => json_encode($model), 'response' => json_encode($response)]);

return false;
}
Expand Down
Loading

0 comments on commit 7a1377d

Please sign in to comment.