Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/StripePayment/Observer/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/StripePayment/Observer/OrderInvoiceSaveAfter.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Radosław Łańcucki <[email protected]>
 * @copyright Copyright (c) 2022 KDC (https://digitalcommerce.kaliop.com)
 */
declare(strict_types=1);

namespace Cnc\StripePayment\Observer;

use Cnc\StripePayment\Model\PaymentIntent\DescriptionFormatter;
use Cnc\StripePayment\Model\PaymentIntent\DescriptionProcessor;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Invoice;
use Psr\Log\LoggerInterface;

class OrderInvoiceSaveAfter implements ObserverInterface
{
    private const STRIPE_PAYMENT_CODE_PREFIX = 'stripe_';

    /**
     * @var DescriptionProcessor
     */
    private $processor;

    /**
     * @var DescriptionFormatter
     */
    private $formatter;

    /**
     * @var CartRepositoryInterface
     */
    private $quoteRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param DescriptionProcessor $processor
     * @param DescriptionFormatter $formatter
     * @param CartRepositoryInterface $quoteRepository
     * @param LoggerInterface $logger
     */
    public function __construct(
        DescriptionProcessor $processor,
        DescriptionFormatter $formatter,
        CartRepositoryInterface $quoteRepository,
        LoggerInterface $logger
    ) {
        $this->processor = $processor;
        $this->formatter = $formatter;
        $this->quoteRepository = $quoteRepository;
        $this->logger = $logger;
    }

    /**
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        /** @var Invoice $invoice */
        $invoice = $observer->getEvent()->getInvoice();
        if (!$this->isValid($invoice)) {
            return;
        }

        $order = $invoice->getOrder();

        $payment = $order ? $order->getPayment() : null;
        $method = $payment ? $payment->getMethod() : '';
        if (!$this->isStripePayment($method) || !$invoice->getTransactionId() || !$order) {
            return;
        }

        $billingAddress = $order->getBillingAddress() ?? false;
        $quote = null;

        try {
            $quote = $this->quoteRepository->get($order->getQuoteId());
        } catch (NoSuchEntityException $e) {
            $this->logger->error($e->getMessage());
        }

        $description = $this->formatter->getFormattedDescription(
            $quote && (bool)$quote->getIsMultiShipping(),
            $order->getIncrementId(),
            $invoice->getIncrementId(),
            $billingAddress ? (string)$billingAddress->getCompany() : '',
            $billingAddress ? (string)$billingAddress->getPrefix() : '',
            $billingAddress ? (string)$billingAddress->getFirstname() : '',
            $billingAddress ? (string)$billingAddress->getLastname() : ''
        );

        $this->processor->updatePaymentIntent(
            $invoice->getTransactionId(),
            $description
        );
    }

    /**
     * @param InvoiceInterface $invoice
     * @return bool
     */
    private function isValid(InvoiceInterface $invoice): bool
    {
        if ($invoice->getOrigData('entity_id')) {
            return false;
        }

        return true;
    }

    /**
     * @param string $method
     * @return bool
     */
    private function isStripePayment(string $method): bool
    {
        return strpos($method,self::STRIPE_PAYMENT_CODE_PREFIX) !== false;
    }
}

Spamworldpro Mini