I am adding custom tax based on a particular shipping and payment method using the following plugin:
use Magento\Customer\Api\Data\AddressInterfaceFactory as CustomerAddressFactory;
use Magento\Customer\Api\Data\RegionInterfaceFactory as CustomerAddressRegionFactory;
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote\Address;
class Tax extends \Magento\Tax\Model\Sales\Total\Quote\Tax
{
/**
* Class constructor
*
* @param \Magento\Tax\Model\Config $taxConfig
* @param \Magento\Tax\Api\TaxCalculationInterface $taxCalculationService
* @param \Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsDataObjectFactory
* @param \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactory
* @param \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory
* @param CustomerAddressFactory $customerAddressFactory
* @param CustomerAddressRegionFactory $customerAddressRegionFactory
* @param \Magento\Tax\Helper\Data $taxData
*/
public function __construct(
\Magento\Tax\Model\Config $taxConfig,
\Magento\Tax\Api\TaxCalculationInterface $taxCalculationService,
\Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsDataObjectFactory,
\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemDataObjectFactory,
\Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory,
CustomerAddressFactory $customerAddressFactory,
CustomerAddressRegionFactory $customerAddressRegionFactory,
\Magento\Tax\Model\Calculation $taxCalculation,
\Magento\Tax\Helper\Data $taxData,
\Magento\Customer\Model\Session $session,
\Magento\Customer\Model\Group $group
) {
$this->setCode('tax');
$this->taxCalculation = $taxCalculation;
$this->session = $session;
$this->group = $group;
parent::__construct(
$taxConfig,
$taxCalculationService,
$quoteDetailsDataObjectFactory,
$quoteDetailsItemDataObjectFactory,
$taxClassKeyDataObjectFactory,
$customerAddressFactory,
$customerAddressRegionFactory,
$taxData
);
}
/**
* Custom Collect tax totals for quote address
*
* @param Quote $quote
* @param ShippingAssignmentInterface $shippingAssignment
* @param Address\Total $total
* @return $this
*/
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
parent::collect($quote, $shippingAssignment, $total);
$taxamount = 0;
$customerSession = $this->session;
$customerGroup = $this->group;
$customerGroupId = $customerSession->getCustomer()->getGroupId();
$collection = $customerGroup->load($customerGroupId);
$customerGroup = $collection->getCustomerGroupCode();
$shippingAddress = $quote->getShippingAddress();
$shippingMethod = $shippingAddress->getShippingMethod();
if($shippingMethod == 'shqpickup1_collect'){
foreach($quote->getAllVisibleItems() as $item) {
// Tax Calculation
$productTaxClassId = $item->getTaxClassId();
$request = new \Magento\Framework\DataObject(
[
'country_id' => 'US',
'region_id' =>'43',
'postcode' => '10001',
'customer_class_id' => '3',
'product_class_id' => $productTaxClassId
]
);
// Calculate tax
$taxInfo = $this->taxCalculation->getResource()->getRateInfo($request);
// Classify different taxes
if (count($taxInfo['process']) > 0) {
foreach ($taxInfo['process'][0]['rates'] as $key => $rate) {
$itemPrice = $item->getQty() * $item->getPrice();
if($item->getDiscountAmount() > 0){
$itemPrice = ($item->getQty() * $item->getPrice())-$item->getDiscountAmount();
}
$taxamount += ($itemPrice * $rate['percent']) / 100;
}
}
}
// echo 'Tax amount: '.$taxamount;
if($customerSession->isLoggedIn()){
// Tax Exempt Customer
if($customerGroup != 'Tax Exempt Customer'){
$total->setTaxAmount($taxamount);
$total->setBaseTaxAmount($taxamount);
$total->setTotalAmount($this->getCode(),$taxamount);
$total->setBaseTotalAmount($this->getCode(),$taxamount);
}
}else{
$total->setTaxAmount($taxamount);
$total->setBaseTaxAmount($taxamount);
$total->setTotalAmount($this->getCode(),$taxamount);
$total->setBaseTotalAmount($this->getCode(),$taxamount);
}
}
return $this;
}
Paypal is not accepting this amount. When I click place order it redirects to the paypal site and after entering the payment it redirects back to the order review page with an error: 'PayPal NVP gateway errors: This transaction couldn't be completed. Please redirect your customer to PayPal'.