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/Model/PaymentIntent/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/StripePayment/Model/PaymentIntent/DescriptionProcessor.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\Model\PaymentIntent;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Encryption\EncryptorInterface;
use Psr\Log\LoggerInterface;

class DescriptionProcessor
{
    const PAGE_LIMIT = 10;

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @var EncryptorInterface
     */
    private $encryptor;

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

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

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

    /**
     * @param EncryptorInterface $encryptor
     * @param DescriptionFormatter $formatter
     * @param ScopeConfigInterface $scopeConfig
     * @param ResourceConnection $resourceConnection
     * @param LoggerInterface $logger
     */
    public function __construct(
        EncryptorInterface $encryptor,
        DescriptionFormatter $formatter,
        ScopeConfigInterface $scopeConfig,
        ResourceConnection $resourceConnection,
        LoggerInterface $logger
    ) {
        $this->encryptor = $encryptor;
        $this->formatter = $formatter;
        $this->scopeConfig = $scopeConfig;
        $this->resourceConnection = $resourceConnection;
        $this->logger = $logger;
    }

    /**
     * @param bool $isDryRun
     * @return array
     */
    public function updateAllPaymentIntents(bool $isDryRun = false): array
    {
        $result = [];

        try {
            $stripe = $this->getStripeClient();
            $params = [
                'limit' => self::PAGE_LIMIT
            ];

            $hasMore = true;
            while ($hasMore) {
                $paymentIntents = $stripe->paymentIntents->all($params);
                $id = null;

                foreach ($paymentIntents as $paymentIntent) {
                    $id = (string)$paymentIntent->id;
                    if ($description = $this->updatePaymentIntent($id, null, $isDryRun)) {
                        $result[$id] = $description;
                    }
                }

                $hasMore = $paymentIntents->has_more;
                $params['starting_after'] = $id;
            }
        } catch (\Stripe\Exception\ApiErrorException $error) {
            $this->logger->error($error->getMessage());
        }

        return $result;
    }

    /**
     * @param string $id
     * @param string|null $description
     * @param bool $isDryRun
     * @return string|null
     */
    public function updatePaymentIntent(string $id, ?string $description = null, bool $isDryRun = false): ?string
    {
        $description = !$description ? $this->getPaymentIntentDescription($id) : $description;
        if (!$description) {
            return null;
        }

        if (!$isDryRun) {
            try {
                $stripe = $this->getStripeClient();

                $stripe->paymentIntents->update(
                    $id,
                    ['description' => $description]
                );
            } catch (\Stripe\Exception\ApiErrorException $error) {
                $this->logger->error($error->getMessage());
                return null;
            }
        }

        return $description;
    }

    /**
     * @param string $id
     * @return string|null
     */
    private function getPaymentIntentDescription(string $id): ?string
    {
        $connection = $this->resourceConnection->getConnection();

        $select = $connection->select()
            ->from(
                ['spt' => $connection->getTableName('sales_payment_transaction')],
                ['id' => 'txn_id']
            )
            ->joinLeft(
                ['so' => $connection->getTableName('sales_order')],
                'so.entity_id = spt.order_id',
                ['order_increment_id' => 'so.increment_id']
            )
            ->joinLeft(
                ['soa' => $connection->getTableName('sales_order_address')],
                'soa.entity_id = so.billing_address_id',
                [
                    'company' => 'soa.company',
                    'prefix' => 'soa.prefix',
                    'firstname' => 'soa.firstname',
                    'lastname' => 'soa.lastname'
                ]
            )
            ->joinLeft(
                ['si' => $connection->getTableName('sales_invoice')],
                'si.order_id = so.entity_id',
                ['invoice_increment_id' => 'si.increment_id']
            )
            ->joinLeft(
                ['q' => $connection->getTableName('quote')],
                'q.entity_id = so.quote_id',
                [
                    'is_multi_shipping' => 'q.is_multi_shipping'
                ]
            )
            ->where('spt.txn_id = ?', $id);

        $result = $connection->fetchRow($select);

        if (!$result) {
            return null;
        }

        return $this->formatter->getFormattedDescription(
            isset($result['is_multi_shipping']) && (bool)$result['is_multi_shipping'],
            $result['order_increment_id'] ?: '',
            $result['invoice_increment_id'] ?: '',
            $result['company'] ?: '',
            $result['prefix'] ?: '',
            $result['firstname'] ?: '',
            $result['lastname'] ?: ''
        );
    }

    /**
     * @return \Stripe\StripeClient
     */
    private function getStripeClient(): \Stripe\StripeClient
    {
        return (new \Stripe\StripeClient($this->getSecretKey()));
    }

    private function getSecretKey(): string
    {
        $mode = $this->getMode();
        $secretKey = $this->getConfigData("stripe_{$mode}_sk") ?: '';
        return $this->encryptor->decrypt($secretKey);
    }

    /**
     * @return string
     */
    private function getMode(): string
    {
        return $this->getConfigData('stripe_mode') ?: 'test';
    }

    /**
     * @param string $path
     * @return string
     */
    private function getConfigData(string $path): string
    {
        return $this->scopeConfig->getValue("payment/stripe_payments_basic/{$path}");
    }
}

Spamworldpro Mini