HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/blijegasten/blijegasten.be/app/Komma/Shop/Listeners/TransactionChanged.php
<?php


namespace App\Komma\Shop\Listeners;


use App\Komma\Shop\Orders\Kms\OrderMailServiceInterface;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Shop\Payment\Events\TransactionChanged as TransactionChangedEvent;
use App\Komma\Shop\Payment\TransactionStatus;
use App\Komma\Users\Models\SiteUser;

/**
 * Class TransactionChanged
 *
 * Triggered when a transaction changed because a psp said so.
 *
 * The purpose of this class is to delegate with a capital D to
 * other services in response of a transaction change. To, for example,
 * update an order, send out mails, make invoices etc.
 *
 * @package App\Komma\Shop\Listeners
 */
class TransactionChanged
{
    /** @var OrderMailServiceInterface $orderMailService */
    private $orderMailService;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        $this->orderMailService = app(OrderMailServiceInterface::class);
    }

    /**
     * Handle the event.
     *
     * @param TransactionChangedEvent $event
     * @return void
     */
    public function handle(TransactionChangedEvent $event) {

        /** @var Order $order */
        $order = $event->transaction->order()->first();

//        /** @var SiteUser $customer */
//        $customer = $order->customer()->first();

        \Log::debug(self::class . ': Webhook called with status "' . $event->transaction->status . '" (Transaction Id: '.$event->transaction->id.', Order Id: '.$order->id.').');

        //Change the order status depending on the transaction status. And notify customer and staff if needed
        switch ($event->transaction->status)
        {

            case TransactionStatus::OPEN:
                $order->status = OrderStatus::PENDING;
////                $this->orderMailService->mailCustomerAboutCurrentOrderStatus($order);
                break;

            case TransactionStatus::AUTHORIZED:
            case TransactionStatus::PAYMENT_PENDING:
                break;


            case TransactionStatus::PAYMENT_UNKNOWN:
                \Log::warning(self::class .': Webhook called failed. We have not a follow up action defined. Contact Blije gasten...');
                break;

            case TransactionStatus::CANCELED_CUSTOMER:
            case TransactionStatus::EXPIRED:
                $order->status = OrderStatus::AWAITING_PAYMENT;
                if(!$order->isDirty()) break; //Don't mail about non changed order
                $this->orderMailService->mailCustomerPayLater($order);
//                $this->orderMailService->mailStaffAboutCurrentOrderStatus($order);
                break;

            case TransactionStatus::PAYMENT_PAID:
                if($order->status === OrderStatus::COMPLETED) break; // NOTE: Skip mailing if order has already been paid, Mollie triggers paid also on refund.
                $order->status = OrderStatus::AWAITING_FULFILLMENT;
                if(!$order->isDirty()) break; //Don't mail about non changed order
                $this->orderMailService->mailCustomerAboutCurrentOrderStatus($order);
                $this->orderMailService->mailStaffAboutCurrentOrderStatus($order);
                break;

            default:
                \Log::warning(self::class .': Webhook action undefined.');
        }

        // Remove the country models, because those aren't db attributes
        if(isset($order->invoice_country)) unset($order->invoice_country);
        if(isset($order->shipping_country)) unset($order->shipping_country);

        //And finally save the order
        $order->save();
    }
}