-
Notifications
You must be signed in to change notification settings - Fork 11
/
SalesTaxService.php
773 lines (626 loc) · 24.7 KB
/
SalesTaxService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
<?php
/**
* Avatax plugin for Craft CMS 3.x
*
* Calculate and add sales tax to an order's base tax using Avalara's AvaTax service.
*
* @link http://surprisehighway.com
* @copyright Copyright (c) 2019 Surprise Highway
*/
namespace surprisehighway\avatax\services;
use surprisehighway\avatax\Avatax;
use Avalara\AvaTaxClient;
use Avalara\AddressValidationInfo;
use Craft;
use craft\base\Component;
use craft\commerce\Plugin as Commerce;
use craft\elements\Address;
use craft\commerce\models\OrderAdjustment;
use craft\commerce\models\Transaction;
use craft\commerce\elements\Order;
use craft\commerce\helpers\Currency;
use yii\base\Exception;
use yii\log\Logger;
/**
* @author Surprise Highway
* @package Avatax
* @since 2.0.0
*/
class SalesTaxService extends Component
{
// Public Properties
// =========================================================================
/**
* @var settings
*/
public $settings;
/**
* @var boolean
*/
public $debug = false;
/**
* @var string The type of commit (order or invoice)
*/
public $type;
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
$settings = Avatax::$plugin->getSettings();
$this->settings = $settings;
$this->debug = $settings->debug;
}
/**
* @param array $settings
* @return object or boolean
*
* From any other plugin file, call it like this:
* Avatax::getInstance()->SalesTaxService->connectionTest()
*
* Creates a new client with the given settings and tests the connection.
* See https://developer.avalara.com/api-reference/avatax/rest/v2/methods/Utilities/Ping/
*
*/
public function connectionTest($settings)
{
$client = $this->createClient($settings);
return $client->ping();
}
/**
* @param object Order $order
* @return object
*
* From any other plugin file, call it like this:
* Avatax::getInstance()->SalesTaxService->createSalesOrder()
*
* Creates a new sales order - a temporary transaction to determine the tax rate.
* See "Sales Orders vs Sales Invoices" https://developer.avatax.com/blog/2016/11/04/estimating-tax-with-rest-v2/
* See also: https://developer.avatax.com/avatax/use-cases/
*
*/
public function createSalesOrder(Order $order)
{
// check for form overrides to plugin settings and bail if disabled
$disabled = $this->parseOverrideParam('avatax_disable_tax_calculation');
$enabled = $this->parseOverrideParam('avatax_force_tax_calculation');
if((!$this->settings->enableTaxCalculation || $disabled) && !$enabled)
{
Avatax::info(__FUNCTION__.'(): Tax Calculation is disabled.');
return false;
}
$this->type = 'order';
$client = $this->createClient();
$tb = new \Avalara\TransactionBuilder(
$client, $this->getCompanyCode(),
\Avalara\DocumentType::C_SALESORDER,
$this->getCustomerCode($order)
);
$totalTax = $this->getTotalTax($order, $tb);
return $totalTax;
}
/**
* @param object Order $order
* @return object
*
* From any other plugin file, call it like this:
* Avatax::getInstance()->SalesTaxService->createSalesInvoice()
*
* Creates and commits a new sales invoice
* See "Sales Orders vs Sales Invoices" https://developer.avatax.com/blog/2016/11/04/estimating-tax-with-rest-v2/
* See also: https://developer.avatax.com/avatax/use-cases/
*
*/
public function createSalesInvoice(Order $order)
{
if(!$this->settings->enableCommitting)
{
Avatax::info(__FUNCTION__.'(): Document Committing is disabled.');
return false;
}
$this->type = 'invoice';
$client = $this->createClient();
$tb = new \Avalara\TransactionBuilder(
$client, $this->getCompanyCode(),
\Avalara\DocumentType::C_SALESINVOICE,
$this->getCustomerCode($order)
);
$tb->withCommit();
$totalTax = $this->getTotalTax($order, $tb);
return $totalTax;
}
/**
* @param float $amount amount of the refund
* @param object Transaction $transaction
* @return boolean
*
* Handle a return event.
*
*/
public function handleRefund($amount, Transaction $transaction)
{
// catch rounding issues so we can just issue a full refund if possible
$paymentCurrency = Commerce::getInstance()->getPaymentCurrencies()->getPaymentCurrencyByIso($transaction->order->paymentCurrency);
$refundAmount = Currency::round($amount, $paymentCurrency);
$paymentAmount = Currency::round($transaction->paymentAmount, $paymentCurrency);
if($refundAmount < $paymentAmount) {
return $this->refundPartialTransaction($amount, $transaction);
}
return $this->refundFullTransaction($amount, $transaction);
}
/**
* @param float $amount amount of the refund
* @param object Transaction $transaction
* @return boolean
*
* Refund a committed sales invoice
* See "Refund Transaction" https://developer.avalara.com/api-reference/avatax/rest/v2/methods/Transactions/RefundTransaction
*
*/
public function refundFullTransaction($amount, Transaction $transaction)
{
$client = $this->createClient();
$order = $transaction->order;
$request = array(
'companyCode' => $this->getCompanyCode(),
'transactionCode' => $this->getTransactionCode($order)
);
$model = array(
'refundTransactionCode' => $request['transactionCode'].'-refund',
'refundType' => \Avalara\RefundType::C_FULL,
'refundDate' => date('Y-m-d'),
'referenceCode' => 'Refund from Craft Commerce'
);
extract($request);
$response = $client->refundTransaction(
$companyCode,
$transactionCode,
null,
null,
'true',
$model
);
if($this->debug)
{
$request = array_merge($request, $model);
Avatax::info('\Avalara\Client->refundTransaction(): ', ['request' =>json_encode($request), 'response' => json_encode($response)]);
}
if(isset($response->status) && $response->status === 'Committed')
{
Avatax::info('Transaction Code '.$transactionCode.' was successfully refunded (full).');
return true;
}
Avatax::error('Transaction Code '.$transactionCode.' could not be refunded.', ['request' => json_encode($request), 'response' => json_encode($response)]);
return false;
}
/**
* @param float $amount amount of the refund
* @param object Transaction $transaction
* @return boolean
*
* Refund a specific amount to a customer by creating and committing a new Return Invoice.
* Note that this is not tied to a specific order so tax refund is determined by the customer location and exemption status.
* See "Create Transaction" https://developer.avalara.com/api-reference/avatax/rest/v2/methods/Transactions/CreateTransaction/
* See https://community.avalara.com/avalara/topics/refund-transaction-api?topic-reply-list[settings][filter_by]=all&topic-reply-list[settings][reply_id]=19097602#reply_19097602
*/
public function refundPartialTransaction($amount, Transaction $transaction)
{
if(!$this->settings->enablePartialRefunds)
{
Avatax::info(__FUNCTION__.'(): Sending partial refunds to AvaTax is disabled.');
return false;
}
$order = $transaction->order;
// if no tax was recorded do not send to Avalara to calculate
if( !($order->getTotalTax() > 0)) {
return false;
}
// check for previous refunds and increment suffix to avoid duplicate ids
$count = 0;
foreach($transaction->order->getTransactions() as $childTransaction)
{
if($childTransaction->type === 'refund')
{
$count++;
}
}
$suffix = ($count > 0) ? '-'.$count : '';
// begin request
$client = $this->createClient();
$tb = new \Avalara\TransactionBuilder(
$client, $this->getCompanyCode(),
\Avalara\DocumentType::C_RETURNINVOICE,
$this->getCustomerCode($order)
);
$tb->withLineItem([
['amount' => -$amount]
])->withTransactionCode(
$this->getTransactionCode($order).'-refund'.$suffix
)->withReferenceCode(
'Partial refund from Craft Commerce'
)->withAddress(
'singleLocation',
$order->shippingAddress->addressLine1,
NULL,
NULL,
$order->shippingAddress->locality,
$order->shippingAddress->administrativeArea,
$order->shippingAddress->postalCode,
$order->shippingAddress->countryCode
)->withCommit();
// add entity/use code if set for the customer
if(!is_null($order->getCustomer()))
{
if($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer()))
{
$tb = $tb->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer()));
}
}
$response = $tb->create();
if($this->debug)
{
// workaround to save the model as array for debug logging
$m = $tb; $model = $m->createAdjustmentRequest(null, null)['newTransaction'];
Avatax::info('\Avalara\TransactionBuilder->create(): ', ['request' =>json_encode($model), 'response' => json_encode($response)]);
}
if(isset($response->status) && $response->status === 'Committed')
{
Avatax::info('Transaction Code '.$this->getTransactionCode($order).' was successfully refunded (partial).');
return true;
}
Avatax::error('Transaction Code '.$this->getTransactionCode($order).' could not be refunded.');
return false;
}
/**
* @param object $address Address model craft\elements\Address
* @return boolean
*
* From any other plugin file, call it like this:
* Avatax::getInstance()->SalesTaxService->validateAddress()
*
* Validates and address
* See: https://developer.avalara.com/api-reference/avatax/rest/v2/methods/Addresses/ResolveAddressPost/
*
*/
public function validateAddress(Address $address)
{
if(!$this->settings['enableAddressValidation'])
{
Avatax::info(__FUNCTION__.'(): Address validation is disabled.');
return false;
}
$response = $this->getValidateAddress($address);
if(!empty($response->validatedAddresses) && isset($response->coordinates))
{
return true;
}
// Request failed
Avatax::error('Address validation failed.', ['request' => json_encode($address), 'response' => json_encode($response)]);
return false;
}
/**
* @param object $address Avatax Address model Avalara\AvaTaxClient\AddressValidationInfo
* @return object
*/
function getValidateAddress(Address $address)
{
$signature = $this->getAddressSignature($address);
$cacheKey = 'avatax-address-'.$signature;
$cache = Craft::$app->getCache();
// Check if validated address has been cached, if not make api call.
$response = $cache->get($cacheKey);
//if($response) Avatax::info('Cached address found: '.$cacheKey);
if(!$response)
{
// Convert commerce address to avatax address model
$request = new AddressValidationInfo();
$request->textCase = 'Mixed';
$request->line1 = $address->addressLine1;
$request->line2 = $address->addressLine2;
$request->line3 = '';
$request->city = $address->locality;
$request->region = $address->administrativeArea;
$request->country = $address->countryCode;
$request->postalCode = $address->postalCode;
$request->latitude = '';
$request->longitude = '';
// Make avatax api request
$client = $this->createClient();
$response = $client->resolveAddress($request->line1, $request->line2, $request->line3, $request->city, $request->region, $request->postalCode, $request->country, $request->textCase, $request->latitude, $request->longitude);
$cache->set($cacheKey, $response);
Avatax::info('\Avalara\AvaTaxClient->resolveAddress():', ['request' => json_encode($request), 'response' => json_encode($response)]);
}
return $response;
}
// Private Methods
// =========================================================================
/**
* @return string $companyCode
*/
private function getCompanyCode()
{
if($this->settings['environment'] === 'production')
{
$companyCode = $this->settings->getCompanyCode();
}
if($this->settings['environment'] === 'sandbox')
{
$companyCode =$this->settings->getSandboxCompanyCode();
}
return $companyCode;
}
/**
* @return string $customerCode
*/
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->getCustomer()))
{
if($this->getFieldValue('avataxCustomerCode', $order->getCustomer()))
{
$customerCode = $this->getFieldValue('avataxCustomerCode', $order->getCustomer());
}
}
// Override value from an order field if available
if($this->getFieldValue('avataxCustomerCode', $order))
{
$customerCode = $this->getFieldValue('avataxCustomerCode', $order);
}
return $customerCode;
}
/**
* @return string|null
*
* Check for override value in a plaintext or dropdown field.
*/
private function getFieldValue($handle, $element)
{
if($element->fieldValues !== null && array_key_exists($handle, $element->fieldValues))
{
$field = $element->fieldValues[$handle];
$value = isset($field->value) ? $field->value : $field;
if(is_string($value) && !empty($value))
{
return $value;
}
}
return null;
}
/**
* @return string $transactionCode
*
* Use the prefixed order number as the document code so that
* we can reference it again for subsequent calls if needed.
*/
private function getTransactionCode($order)
{
$prefix = 'cr_';
return $prefix.$order->number;
}
/**
* @return object $client
*/
private function createClient($settings = null)
{
$settings = ($settings) ? $settings : $this->settings;
$pluginName = 'Craft Commerce '.Avatax::$plugin->name;
$pluginVersion = Avatax::$plugin->version;
$machineName = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'localhost';
if($settings['environment'] === 'production')
{
$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($accountId, $licenseKey);
return $client;
}
}
if($settings['environment'] === 'sandbox')
{
$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($sandboxAccountId, $sandboxLicenseKey);
return $client;
}
}
// Don't have credentials
Avatax::error('Avatax Account Credentials not found. Check the plugin settings.');
}
/**
* @param object Order $order
* @param object Avatax\TransactionBuilder $transaction
* @return object
*
*/
private function getTotalTax($order, $transaction)
{
if($this->settings['enableAddressValidation'] && $order->shippingAddress)
{
// Make sure we have a valid address before continuing.
if($this->validateAddress($order->shippingAddress) === false)
{
return false;
}
}
$defaultTaxCode = $this->settings['defaultTaxCode'];
$defaultShippingCode = $this->settings['defaultShippingCode'];
$defaultDiscountCode = $this->settings['defaultDiscountCode'];
$t = $transaction->withTransactionCode(
$this->getTransactionCode($order)
)
->withAddress(
'shipFrom',
$this->settings['shipFromStreet1'],
$this->settings['shipFromStreet2'],
$this->settings['shipFromStreet3'],
$this->settings['shipFromCity'],
$this->settings['shipFromState'],
$this->settings['shipFromZipCode'],
$this->settings['shipFromCountry']
)
->withAddress(
'shipTo',
$order->shippingAddress->addressLine1,
NULL,
NULL,
$order->shippingAddress->locality,
$order->shippingAddress->administrativeArea,
$order->shippingAddress->postalCode,
$order->shippingAddress->countryCode
);
// Add each line item to the transaction
foreach ($order->lineItems as $lineItem) {
// Our product has the avatax tax category specified
if($lineItem->taxCategory->handle === 'avatax'){
$taxCode = $defaultTaxCode;
if($this->getFieldValue('avataxTaxCode', $lineItem->purchasable->product)) {
$taxCode = $this->getFieldValue('avataxTaxCode', $lineItem->purchasable->product);
}
$itemCode = $lineItem->id;
if(!empty($lineItem->sku)) {
$itemCode = $lineItem->sku;
}
// amount, $quantity, $itemCode, $taxCode)
$t = $t->withLine(
$lineItem->subtotal, // Total amount for the line item
$lineItem->qty, // Quantity
$itemCode, // Item Code
$taxCode // Tax Code - Default or Custom Tax Code.
);
// add human-readable description to line item
$t = $t->withLineDescription($lineItem->purchasable->product->title);
}
}
// Add each discount line item
$discountCode = $defaultDiscountCode;
foreach ($order->adjustments as $adjustment) {
/** @var OrderAdjustment $adjustment */
if($adjustment->type === 'discount') {
// if the discount is for a specific lineItem make sure the discountCode
// for this adjustment matches the lineItem tax code
if ($adjustmentLineItem = $adjustment->getLineItem())
{
$discountCode = $defaultTaxCode;
// check to see if there is an Avatax Tax Code override specified
if($this->getFieldValue('avataxTaxCode', $lineItem->purchasable->product))
{
$discountCode = $this->getFieldValue('avataxTaxCode', $lineItem->purchasable->product);
}
}
$t = $t->withLine(
$adjustment->amount, // Total amount for the line item
1, // quantity
$adjustment->name, // Item Code
$discountCode // Tax Code
);
// add description to discount line item
$t = $t->withLineDescription($adjustment->description);
}
}
// Add shipping cost as line-item
$shippingTaxCode = $defaultShippingCode;
$t = $t->withLine(
$order->getTotalShippingCost(), // total amount for the line item
1, // quantity
"FREIGHT", // Item Code
$shippingTaxCode // Tax code for freight (Shipping)
);
// add description to shipping line item
$t = $t->withLineDescription('Total Shipping Cost');
// add entity/use code if set for a logged-in User
if(!is_null($order->getCustomer()))
{
if($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer()))
{
$t = $t->withEntityUseCode($this->getFieldValue('avataxCustomerUsageType', $order->getCustomer()));
}
}
// workaround to save the model as array for debug logging
$m = $t; $model = $m->createAdjustmentRequest(null, null)['newTransaction'];
$signature = $this->getOrderSignature($order);
$cacheKey = 'avatax-'.$this->type.'-'.$signature;
$cache = Craft::$app->getCache();
// Check if tax request has been cached when not committing, if not make api call.
$response = $cache->get($cacheKey);
//if($response) Avatax::info('Cached order found: '.$cacheKey);
if(!$response || $this->type === 'invoice')
{
$response = $t->create();
$cache->set($cacheKey, $response);
if($this->debug)
{
Avatax::info('\Avalara\TransactionBuilder->create() '.$this->type.':', ['request' => json_encode($model), 'response' => json_encode($response)]);
}
}
if(isset($response->totalTax))
{
return $response->totalTax;
}
// Log error
Avatax::error('Request to avatax.com failed', ['request' => json_encode($model), 'response' => json_encode($response)]);
return false;
}
/**
* Returns a hash derived from the order's properties.
*/
private function getOrderSignature(Order $order)
{
$orderNumber = $order->number;
$shipping = $order->getTotalShippingCost();
$discount = $order->getTotalDiscount();
$tax = $order->getTotalTax();
$total = $order->totalPrice;
$addressLine1 = $order->shippingAddress->addressLine1;
$addressLine2 = $order->shippingAddress->addressLine2;
$city = $order->shippingAddress->locality;
$state = $order->shippingAddress->administrativeArea;
$zipCode = $order->shippingAddress->postalCode;
$country = $order->shippingAddress->countryCode;
$address = $addressLine1.$addressLine2.$city.$state.$zipCode.$country;
$lineItems = '';
foreach ($order->lineItems as $lineItem)
{
$itemCode = $lineItem->id;
$subtotal = $lineItem->subtotal;
$qty = $lineItem->qty;
$lineItems .= $itemCode.$subtotal.$qty;
}
return md5($orderNumber.$shipping.$discount.$tax.$total.$lineItems.$address);
}
/**
* Returns a hash derived from the address.
*/
private function getAddressSignature(Address $address)
{
$addressLine1 = $address->addressLine1;
$addressLine2 = $address->addressLine2;
$city = $address->locality;
$state = $address->administrativeArea;
$zipCode = $address->postalCode;
$country = $address->countryCode;
return md5($addressLine1.$addressLine2.$city.$state.$zipCode.$country);
}
/**
* Parse override form parameters
*/
private function parseOverrideParam($param)
{
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
return false;
}
$value = Craft::$app->getRequest()->getParam($param);
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}