From af5792b3f9e8fed231de56b5637f5d2b11aa0662 Mon Sep 17 00:00:00 2001 From: imagehat Date: Thu, 14 Jul 2022 14:49:46 -0500 Subject: [PATCH 1/7] Prep for Craft 4 --- src/Avatax.php | 8 ++++---- src/controllers/BaseController.php | 2 +- src/controllers/JsonController.php | 2 +- src/controllers/UtilityController.php | 2 +- src/models/Settings.php | 4 ++-- src/services/CertCaptureService.php | 2 +- src/services/LogService.php | 2 +- src/services/SalesTaxService.php | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Avatax.php b/src/Avatax.php index b5ee3b2..d78e2f9 100644 --- a/src/Avatax.php +++ b/src/Avatax.php @@ -65,7 +65,7 @@ class Avatax extends Plugin /** * @var string */ - public $schemaVersion = '2.0.0'; + public string $schemaVersion = '2.0.0'; // Public Methods @@ -344,7 +344,7 @@ static function error($message, $data = []) /** * @inheritdoc */ - protected function createSettingsModel() + protected function createSettingsModel(): ?\craft\base\Model { return new Settings(); } @@ -352,7 +352,7 @@ protected function createSettingsModel() /** * @inheritdoc */ - protected function settingsHtml(): string + protected function settingsHtml(): ?string { return Craft::$app->view->renderTemplate( 'avatax/settings', @@ -365,7 +365,7 @@ protected function settingsHtml(): string /** * @inheritdoc */ - public function getSettingsResponse() + public function getSettingsResponse(): mixed { return Craft::$app->controller->redirect(UrlHelper::cpUrl('avatax/settings')); } diff --git a/src/controllers/BaseController.php b/src/controllers/BaseController.php index abb9f93..0ae11ac 100644 --- a/src/controllers/BaseController.php +++ b/src/controllers/BaseController.php @@ -31,7 +31,7 @@ class BaseController extends Controller * The actions must be in 'kebab-case' * @access protected */ - protected $allowAnonymous = false; + protected array|int|bool $allowAnonymous = false; // Public Methods // ========================================================================= diff --git a/src/controllers/JsonController.php b/src/controllers/JsonController.php index 891dd78..a856c65 100644 --- a/src/controllers/JsonController.php +++ b/src/controllers/JsonController.php @@ -35,7 +35,7 @@ class JsonController extends Controller * The actions must be in 'kebab-case' * @access protected */ - protected $allowAnonymous = true; + protected array|int|bool $allowAnonymous = true; // Public Methods // ========================================================================= diff --git a/src/controllers/UtilityController.php b/src/controllers/UtilityController.php index 2cfa754..662f23a 100644 --- a/src/controllers/UtilityController.php +++ b/src/controllers/UtilityController.php @@ -32,7 +32,7 @@ class UtilityController extends Controller * The actions must be in 'kebab-case' * @access protected */ - protected $allowAnonymous = false; + protected array|int|bool $allowAnonymous = false; // Public Methods // ========================================================================= diff --git a/src/models/Settings.php b/src/models/Settings.php index b9dfcbf..d9aedeb 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -162,7 +162,7 @@ class Settings extends Model // Public Methods // ========================================================================= - public function behaviors() + public function behaviors(): array { return [ 'parser' => [ @@ -230,7 +230,7 @@ public function getSandboxCompanyCode() /** * @inheritdoc */ - public function rules() + public function rules(): array { return [ ['environment', 'string'], diff --git a/src/services/CertCaptureService.php b/src/services/CertCaptureService.php index dcb6c49..e2966d3 100644 --- a/src/services/CertCaptureService.php +++ b/src/services/CertCaptureService.php @@ -58,7 +58,7 @@ class CertCaptureService extends Component /** * @inheritdoc */ - public function init() + public function init(): void { parent::init(); diff --git a/src/services/LogService.php b/src/services/LogService.php index 4bbfb42..333afa2 100644 --- a/src/services/LogService.php +++ b/src/services/LogService.php @@ -43,7 +43,7 @@ class LogService extends Component // Public Methods // ========================================================================= - public function init() + public function init(): void { $this->logFile = Craft::$app->path->getLogPath() . '/avatax.log'; diff --git a/src/services/SalesTaxService.php b/src/services/SalesTaxService.php index f4a8460..5194bf4 100644 --- a/src/services/SalesTaxService.php +++ b/src/services/SalesTaxService.php @@ -60,7 +60,7 @@ class SalesTaxService extends Component /** * @inheritdoc */ - public function init() + public function init(): void { parent::init(); From 8e1c8d7b1e2971377bae8d5b361163f3762ccaba Mon Sep 17 00:00:00 2001 From: imagehat Date: Thu, 14 Jul 2022 15:52:14 -0500 Subject: [PATCH 2/7] Address field changes --- src/controllers/JsonController.php | 25 +++++++---------- src/services/SalesTaxService.php | 44 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/controllers/JsonController.php b/src/controllers/JsonController.php index a856c65..39d3016 100644 --- a/src/controllers/JsonController.php +++ b/src/controllers/JsonController.php @@ -17,7 +17,7 @@ use Craft; use craft\web\Controller; use craft\commerce\Plugin as Commerce; -use craft\commerce\models\Address; +use craft\elements\Address; /** * @author Surprise Highway @@ -54,21 +54,16 @@ public function actionValidateAddress() // Shared attributes $attributes = [ - 'attention', - 'title', - 'firstName', - 'lastName', - 'address1', - 'address2', - 'city', - 'zipCode', - 'phone', + 'fullName', + 'addressLine1', + 'addressLine2', + 'locality', + 'postalCode', 'alternativePhone', - 'businessName', - 'businessTaxId', - 'businessId', - 'countryId', - 'stateValue' + 'organization', + 'organizationTaxId', + 'countryCode', + 'administrativeArea' ]; foreach ($attributes as $attr) diff --git a/src/services/SalesTaxService.php b/src/services/SalesTaxService.php index 5194bf4..0a7f666 100644 --- a/src/services/SalesTaxService.php +++ b/src/services/SalesTaxService.php @@ -18,7 +18,7 @@ use craft\base\Component; use craft\commerce\Plugin as Commerce; -use craft\commerce\models\Address; +use craft\elements\Address; use craft\commerce\models\OrderAdjustment; use craft\commerce\models\Transaction; use craft\commerce\elements\Order; @@ -301,12 +301,12 @@ public function refundPartialTransaction($amount, Transaction $transaction) 'Partial refund from Craft Commerce' )->withAddress( 'singleLocation', - $order->shippingAddress->address1, + $order->shippingAddress->addressLine1, NULL, NULL, - $order->shippingAddress->city, + $order->shippingAddress->locality, $this->getState($order->shippingAddress), - $order->shippingAddress->zipCode, + $order->shippingAddress->postalCode, $this->getCountry($order->shippingAddress) )->withCommit(); @@ -341,7 +341,7 @@ public function refundPartialTransaction($amount, Transaction $transaction) } /** - * @param object $address Address model craft\commerce\models\Address + * @param object $address Address model craft\elements\Address * @return boolean * * From any other plugin file, call it like this: @@ -399,13 +399,13 @@ function getValidateAddress(Address $address) $request = new AddressValidationInfo(); $request->textCase = 'Mixed'; - $request->line1 = $address->address1; - $request->line2 = $address->address2; + $request->line1 = $address->addressLine1; + $request->line2 = $address->addressLine2; $request->line3 = ''; - $request->city = $address->city; + $request->city = $address->locality; $request->region = $this->getState($address); $request->country = $this->getCountry($address); - $request->postalCode = $address->zipCode; + $request->postalCode = $address->postalCode; $request->latitude = ''; $request->longitude = ''; @@ -587,12 +587,12 @@ private function getTotalTax($order, $transaction) ) ->withAddress( 'shipTo', - $order->shippingAddress->address1, + $order->shippingAddress->addressLine1, NULL, NULL, - $order->shippingAddress->city, + $order->shippingAddress->locality, $this->getState($order->shippingAddress), - $order->shippingAddress->zipCode, + $order->shippingAddress->postalCode, $this->getCountry($order->shippingAddress) ); @@ -744,13 +744,13 @@ private function getOrderSignature(Order $order) $tax = $order->getTotalTax(); $total = $order->totalPrice; - $address1 = $order->shippingAddress->address1; - $address2 = $order->shippingAddress->address2; - $city = $order->shippingAddress->city; + $addressLine1 = $order->shippingAddress->addressLine1; + $addressLine2 = $order->shippingAddress->addressLine2; + $city = $order->shippingAddress->locality; $state = $this->getState($order->shippingAddress); - $zipCode = $order->shippingAddress->zipCode; + $zipCode = $order->shippingAddress->postalCode; $country = $this->getCountry($order->shippingAddress); - $address = $address1.$address2.$city.$state.$zipCode.$country; + $address = $addressLine1.$addressLine2.$city.$state.$zipCode.$country; $lineItems = ''; foreach ($order->lineItems as $lineItem) @@ -769,14 +769,14 @@ private function getOrderSignature(Order $order) */ private function getAddressSignature(Address $address) { - $address1 = $address->address1; - $address2 = $address->address2; - $city = $address->city; + $addressLine1 = $address->addressLine1; + $addressLine2 = $address->addressLine2; + $city = $address->locality; $state = $this->getState($address); - $zipCode = $address->zipCode; + $zipCode = $address->postalCode; $country = $this->getCountry($address); - return md5($address1.$address2.$city.$state.$zipCode.$country); + return md5($addressLine1.$addressLine2.$city.$state.$zipCode.$country); } /** From 8c047c9d12690ec9f8a4d42f1948b86c76f2bafa Mon Sep 17 00:00:00 2001 From: imagehat Date: Thu, 14 Jul 2022 15:53:04 -0500 Subject: [PATCH 3/7] Order customer user changes --- src/services/SalesTaxService.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/services/SalesTaxService.php b/src/services/SalesTaxService.php index 0a7f666..f73e418 100644 --- a/src/services/SalesTaxService.php +++ b/src/services/SalesTaxService.php @@ -311,11 +311,11 @@ public function refundPartialTransaction($amount, Transaction $transaction) )->withCommit(); // add entity/use code if set for the customer - if(!is_null($order->customer->user)) + if(!is_null($order->getCustomer())) { - if($this->getFieldValue('avataxCustomerUsageType', $order->customer->user)) + if($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer())) { - $tb = $tb->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->customer->user)); + $tb = $tb->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer())); } } @@ -453,11 +453,11 @@ private function getCustomerCode($order) $customerCode = (!empty($order->email)) ? $order->email : 'GUEST'; // Override value from a logged-in User field if available - if(!is_null($order->customer->user)) + if(!is_null($order->getCustomer())) { - if($this->getFieldValue('avataxCustomerCode', $order->customer->user)) + if($this->getFieldValue('avataxCustomerCode', $order->getCustomer())) { - $customerCode = $this->getFieldValue('avataxCustomerCode', $order->customer->user); + $customerCode = $this->getFieldValue('avataxCustomerCode', $order->getCustomer()); } } @@ -675,11 +675,11 @@ private function getTotalTax($order, $transaction) $t = $t->withLineDescription('Total Shipping Cost'); // add entity/use code if set for a logged-in User - if(!is_null($order->customer->user)) + if(!is_null($order->getCustomer())) { - if($this->getFieldValue('avataxCustomerUsageType', $order->customer->user)) + if($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer())) { - $t = $t->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->customer->user)); + $t = $t->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer())); } } From a39f430b6d1246a657049693b4cd00cd5c243581 Mon Sep 17 00:00:00 2001 From: imagehat Date: Thu, 14 Jul 2022 16:10:57 -0500 Subject: [PATCH 4/7] Update address event --- src/Avatax.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Avatax.php b/src/Avatax.php index d78e2f9..8a99710 100644 --- a/src/Avatax.php +++ b/src/Avatax.php @@ -20,6 +20,7 @@ use craft\events\PluginEvent; use craft\events\RegisterComponentTypesEvent; use craft\events\RegisterUrlRulesEvent; +use craft\events\ModelEvent; use craft\fields\Dropdown; use craft\fields\PlainText; use craft\helpers\UrlHelper; @@ -33,7 +34,7 @@ use craft\commerce\events\RefundTransactionEvent; use craft\commerce\models\TaxCategory; use craft\commerce\models\Transaction; -use craft\commerce\services\Addresses; +use craft\elements\Address; use craft\commerce\services\OrderAdjustments; use craft\commerce\services\Payments; use craft\commerce\services\TaxCategories; @@ -125,9 +126,9 @@ function(Event $event) { // Register address save event listener Event::on( - Addresses::class, - Addresses::EVENT_BEFORE_SAVE_ADDRESS, - function(AddressEvent $event) { + Address::class, + Address::EVENT_BEFORE_SAVE, + function(ModelEvent $event) { $this->onBeforeSaveAddress($event); } ); From c497b63782d9cd2c9ea7075436ee09755c9a70bc Mon Sep 17 00:00:00 2001 From: imagehat Date: Thu, 14 Jul 2022 16:43:19 -0500 Subject: [PATCH 5/7] Updated state and country attributes --- src/services/SalesTaxService.php | 34 +++++++++----------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/src/services/SalesTaxService.php b/src/services/SalesTaxService.php index f73e418..0ad95bd 100644 --- a/src/services/SalesTaxService.php +++ b/src/services/SalesTaxService.php @@ -305,9 +305,9 @@ public function refundPartialTransaction($amount, Transaction $transaction) NULL, NULL, $order->shippingAddress->locality, - $this->getState($order->shippingAddress), + $order->shippingAddress->administrativeArea, $order->shippingAddress->postalCode, - $this->getCountry($order->shippingAddress) + $order->shippingAddress->countryCode )->withCommit(); // add entity/use code if set for the customer @@ -403,8 +403,8 @@ function getValidateAddress(Address $address) $request->line2 = $address->addressLine2; $request->line3 = ''; $request->city = $address->locality; - $request->region = $this->getState($address); - $request->country = $this->getCountry($address); + $request->region = $address->administrativeArea; + $request->country = $address->countryCode; $request->postalCode = $address->postalCode; $request->latitude = ''; $request->longitude = ''; @@ -591,9 +591,9 @@ private function getTotalTax($order, $transaction) NULL, NULL, $order->shippingAddress->locality, - $this->getState($order->shippingAddress), + $order->shippingAddress->administrativeArea, $order->shippingAddress->postalCode, - $this->getCountry($order->shippingAddress) + $order->shippingAddress->countryCode ); // Add each line item to the transaction @@ -717,22 +717,6 @@ private function getTotalTax($order, $transaction) return false; } - /** - * Resolve the state based on available attributes. - */ - private function getState(Address $address) - { - return $address->stateId ? $address->getState()->abbreviation : $address->getStateText(); - } - - /** - * Resolve the country based on available attributes. - */ - private function getCountry(Address $address) - { - return $address->countryId ? $address->getCountry()->iso : $address->getCountryText(); - } - /** * Returns a hash derived from the order's properties. */ @@ -747,9 +731,9 @@ private function getOrderSignature(Order $order) $addressLine1 = $order->shippingAddress->addressLine1; $addressLine2 = $order->shippingAddress->addressLine2; $city = $order->shippingAddress->locality; - $state = $this->getState($order->shippingAddress); + $state = $order->shippingAddress->administrativeArea; $zipCode = $order->shippingAddress->postalCode; - $country = $this->getCountry($order->shippingAddress); + $country = $order->shippingAddress->countryCode; $address = $addressLine1.$addressLine2.$city.$state.$zipCode.$country; $lineItems = ''; @@ -774,7 +758,7 @@ private function getAddressSignature(Address $address) $city = $address->locality; $state = $this->getState($address); $zipCode = $address->postalCode; - $country = $this->getCountry($address); + $country = $address->countryCode; return md5($addressLine1.$addressLine2.$city.$state.$zipCode.$country); } From e8d19c4e57e43a9d937e1189831c0a4aaf78c61c Mon Sep 17 00:00:00 2001 From: imagehat Date: Thu, 14 Jul 2022 16:43:44 -0500 Subject: [PATCH 6/7] Updated address save event --- src/Avatax.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Avatax.php b/src/Avatax.php index 8a99710..1851516 100644 --- a/src/Avatax.php +++ b/src/Avatax.php @@ -169,10 +169,10 @@ public function onBeforeOrderComplete(Event $event) * Raised before address has been saved. * Validate an address in avatax. */ - public function onBeforeSaveAddress(AddressEvent $event) + public function onBeforeSaveAddress(ModelEvent $event) { - // @var AddressEvent $address - $address = $event->address; + // @var Address $address + $address = $event->sender; if(Craft::$app->getRequest()->getIsSiteRequest()) { $this->SalesTaxService->validateAddress($address); From e6d22d4cdcbfe4ca85aab0960bc1c8b7f270d759 Mon Sep 17 00:00:00 2001 From: imagehat Date: Mon, 18 Jul 2022 15:25:52 -0500 Subject: [PATCH 7/7] Bump version --- CHANGELOG.md | 4 ++++ README.md | 4 ++-- composer.json | 17 +++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e78767..9ee9485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ 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/). +## 3.0.0 +### Added +- Craft 4 compatibility. + ## 2.2.0 ### Added - Updated Avatax SDK to version 22.3.0. diff --git a/README.md b/README.md index 9e13af7..08315a5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Avatax plugin for Craft CMS 3.x +# Avatax plugin for Craft CMS 4.x Calculate and add sales tax to an order's base tax using Avalara's AvaTax service. @@ -8,7 +8,7 @@ Calculate and add sales tax to an order's base tax using Avalara's AvaTax servic ## Requirements -This plugin requires Craft 3.3 and Craft Commerce 2.2 or later, or Craft 3.4 and Craft Commerce 3.0 and later. +This plugin requires Craft 4 and Craft Commerce 4.0 or later and Craft Commerce 4.0 or later. > Related: The Craft 2.x Commerce 1.x [version](https://github.com/surprisehighway/craft-avataxtaxadjuster) of this plugin is no longer actively maintained but is still available. diff --git a/composer.json b/composer.json index 0d263fd..590dd46 100644 --- a/composer.json +++ b/composer.json @@ -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.2.0", + "version": "3.0.0", "keywords": [ "craft", "cms", @@ -22,8 +22,8 @@ } ], "require": { - "craftcms/commerce": "^2.2.0|^3.0", - "craftcms/cms": "^3.3", + "craftcms/commerce": "^4.0", + "craftcms/cms": "^4.0", "avalara/avataxclient": "^22.3.0" }, "repositories": [ @@ -48,5 +48,14 @@ "salesTaxService": "surprisehighway\\avatax\\services\\SalesTaxService", "logService": "surprisehighway\\avatax\\services\\LogService" } - } + }, + "config": { + "allow-plugins": { + "yiisoft/yii2-composer": true, + "craftcms/plugin-installer": true + }, + "platform": { + "php": "8.0.2" + } + } }