maksuturva / magento2_payment_module

Svea Payment module for Magento 2
Other
4 stars 13 forks source link

Add support for Magento tax and discount configurations #48

Closed jtaskila closed 2 years ago

jtaskila commented 2 years ago

This pull request introduces changes to the module which add the support for various tax and discount configurations in Magento.

Summary of changes

1. Removing totalAmount calculation

The custom totalAmount calculation from Svea\Maksuturva\Model\Gateway\Implementation::getForm() was removed and replaced with Total\TotalCalculation::getProductsTotal() where the total is coming from Magento and it automatically respects all tax and discount configurations. From orders grand total we need to subtract only the things we need separately EXCEPT discounts. Discounts should be included in the productsTotal amount.

2. Refactoring discount calculation

Discounts use a custom calculation implemented in Total\TotalCalculation::getDiscountAmount(). The discount amount is 'tax compensated' so it also respects Magentos discount configurations automatically. The logic is based on productsTotal and orders subtotal. That's how we can calculate the discount amount without worrying about configurations so pmt_amount and pmt_sellercosts with discount rows always match the real total of the order.

3. Removing total calculation logic from gift card row

Gift cards are added as a separate row in Svea\Maksuturva\Observer\AddGiftCardPaymentRow. The logic is the same as before but the total is not modified in the observer because the total already includes all discounts.

4. Making sure gift card amount is always negative

Gift card amount has to be sent as a negative number so there was an additional logic added to the observer which makes the discount always negative even if the discount comes as a positive number from Magento.

Svea-Maintainer commented 2 years ago

Testing with various tax configurations... we don't have gift card functionality in our test environment but let's see...

Svea-Maintainer commented 2 years ago

Tested with different configuration setups and tax rules. Gift card features not tested.