|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Moyasar\Magento2\Helper; |
| 4 | + |
| 5 | +use Magento\Framework\App\Helper\AbstractHelper; |
| 6 | +use Magento\Framework\App\Helper\Context; |
| 7 | +use Magento\Sales\Model\Order; |
| 8 | +use Magento\SalesRule\Model\RuleFactory; |
| 9 | +use Magento\SalesRule\Model\CouponFactory; |
| 10 | +use Magento\SalesRule\Model\Rule; |
| 11 | +use Magento\Store\Model\StoreManagerInterface; |
| 12 | +use Moyasar\Magento2\Helper\CurrencyHelper; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Moyasar\Magento2\Helper\MoyasarLogs; |
| 16 | + |
| 17 | + |
| 18 | +class MoyasarCoupon extends AbstractHelper |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var CurrencyHelper |
| 22 | + */ |
| 23 | + protected $currencyHelper; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var LoggerInterface |
| 27 | + */ |
| 28 | + protected $logger; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor |
| 32 | + * |
| 33 | + * @param Context $context |
| 34 | + * @param CurrencyHelper $currencyHelper |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + Context $context, |
| 38 | + CurrencyHelper $currencyHelper |
| 39 | + ) |
| 40 | + { |
| 41 | + parent::__construct($context); |
| 42 | + $this->currencyHelper = $currencyHelper; |
| 43 | + $this->logger = new MoyasarLogs();; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Try applying a coupon to an existing order, based on Payment metadata. |
| 48 | + * |
| 49 | + * **Note**: This will NOT re-collect or re-price an already placed order. |
| 50 | + * It simply stores the coupon code & notes on the order. |
| 51 | + * |
| 52 | + * @param $order |
| 53 | + * @param $payment |
| 54 | + */ |
| 55 | + public function tryApplyCouponToOrder($order, $payment) |
| 56 | + { |
| 57 | + // We expect these custom keys from your payment metadata |
| 58 | + if (!isset($payment['metadata']['#coupon_id'])) { |
| 59 | + return; // No coupon metadata to apply |
| 60 | + } |
| 61 | + // Extract discount info from Payment metadata: |
| 62 | + $capturedAmount = $payment['amount'] ?? 0; |
| 63 | + $totalPrice = $payment['metadata']['#coupon_original_amount'] ?? 0; |
| 64 | + $currency = $payment['currency'] ?? $order->getOrderCurrencyCode(); |
| 65 | + $discountedAmount = $this->currencyHelper->amountToMajor($totalPrice - $capturedAmount, $currency); |
| 66 | + if ($order->getSubtotal() !== $order->getGrandTotal()) { |
| 67 | + $taxPercent = $order->getTaxAmount() / $order->getSubtotal(); |
| 68 | + $discountedAmount = $discountedAmount / (1 + $taxPercent); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + $order->setDiscountAmount(-$discountedAmount); |
| 73 | + $order->setDiscountDescription('Moyasar Discount: ' . $payment['metadata']['#coupon_id']); |
| 74 | + $order->setBaseDiscountAmount(-$discountedAmount); |
| 75 | + |
| 76 | + // Recalculate grand total |
| 77 | + $newGrandTotal = $this->currencyHelper->amountToMajor($capturedAmount, $currency); |
| 78 | + $order->setGrandTotal($newGrandTotal); |
| 79 | + $order->setBaseGrandTotal($newGrandTotal); |
| 80 | + $order->setTotalPaid($newGrandTotal); |
| 81 | + $order->save(); |
| 82 | + $this->logger->info("[Moyasar][Coupon] Successfully applied coupon code '{$payment['metadata']['#coupon_id']}' to order #{$order->getIncrementId()} (no recalculation)."); |
| 83 | + } |
| 84 | +} |
0 commit comments