Skip to content

Commit

Permalink
[feat/3135] Refacto CapturePayPalOrderCommandHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinFlament committed Dec 6, 2024
1 parent be7373e commit 30ffdc8
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 26 deletions.
31 changes: 31 additions & 0 deletions src/Builder/Payload/CapturePayloadBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PrestashopCheckout\Builder\Payload;

use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Write\CaptureOrderPayloadDTO;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Entity\PayPalOrder;
use PrestaShop\Module\PrestashopCheckout\Repository\PayPalOrderRepository;

class CapturePayloadBuilder
{
private $payPalOrderRepository;

public function __construct(PayPalOrderRepository $payPalOrderRepository)
{
$this->payPalOrderRepository = $payPalOrderRepository;
}

public function buildPayload($mode, $orderId, $merchantId)
{
$order = $this->payPalOrderRepository->getPayPalOrderById($orderId);

return CaptureOrderPayloadDTO::create(
$mode,
$orderId,
['merchantId' => $merchantId],
$order->checkCustomerIntent(PayPalOrder::CUSTOMER_INTENT_USES_VAULTING)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\CommandHandler;

use Configuration;
use PrestaShop\Module\PrestashopCheckout\Builder\Payload\CapturePayloadBuilder;
use PrestaShop\Module\PrestashopCheckout\Context\PrestaShopContext;
use PrestaShop\Module\PrestashopCheckout\Customer\ValueObject\CustomerId;
use PrestaShop\Module\PrestashopCheckout\Event\EventDispatcherInterface;
use PrestaShop\Module\PrestashopCheckout\Exception\PsCheckoutException;
use PrestaShop\Module\PrestashopCheckout\Http\MaaslandHttpClient;
use PrestaShop\Module\PrestashopCheckout\PayPal\Customer\ValueObject\PayPalCustomerId;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CapturePayPalOrderCommand;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Write\CaptureOrderPayloadDTO;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Entity\PayPalOrder;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Event\PayPalOrderCompletedEvent;
use PrestaShop\Module\PrestashopCheckout\PayPal\Order\PayPalOrderStatus;
Expand All @@ -37,9 +39,11 @@
use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\Event\PayPalCapturePendingEvent;
use PrestaShop\Module\PrestashopCheckout\PayPal\Payment\Capture\PayPalCaptureStatus;
use PrestaShop\Module\PrestashopCheckout\PayPal\PaymentToken\Event\PaymentTokenCreatedEvent;
use PrestaShop\Module\PrestashopCheckout\PayPal\PayPalConfiguration;
use PrestaShop\Module\PrestashopCheckout\PayPalProcessorResponse;
use PrestaShop\Module\PrestashopCheckout\Repository\PayPalCustomerRepository;
use PrestaShop\Module\PrestashopCheckout\Repository\PayPalOrderRepository;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;

class CapturePayPalOrderCommandHandler
Expand All @@ -66,51 +70,43 @@ class CapturePayPalOrderCommandHandler
* @var PayPalCustomerRepository
*/
private $payPalCustomerRepository;
/**
* @var PayPalOrderRepository
*/
private $payPalOrderRepository;

/** @var CapturePayloadBuilder */
private $capturePayloadBuilder;

/** @var PayPalConfiguration */
private $payPalConfiguration;

public function __construct(
MaaslandHttpClient $maaslandHttpClient,
EventDispatcherInterface $eventDispatcher,
CacheInterface $orderPayPalCache,
PrestaShopContext $prestaShopContext,
PayPalCustomerRepository $payPalCustomerRepository,
PayPalOrderRepository $payPalOrderRepository
LoggerInterface $logger,
CapturePayloadBuilder $capturePayloadBuilder,
PayPalConfiguration $payPalConfiguration
) {
$this->maaslandHttpClient = $maaslandHttpClient;
$this->eventDispatcher = $eventDispatcher;
$this->orderPayPalCache = $orderPayPalCache;
$this->prestaShopContext = $prestaShopContext;
$this->payPalCustomerRepository = $payPalCustomerRepository;
$this->payPalOrderRepository = $payPalOrderRepository;
$this->capturePayloadBuilder = $capturePayloadBuilder;
$this->payPalConfiguration = $payPalConfiguration;
}

public function handle(CapturePayPalOrderCommand $capturePayPalOrderCommand)
{
$merchantId = Configuration::get('PS_CHECKOUT_PAYPAL_ID_MERCHANT', null, null, $this->prestaShopContext->getShopId());

$payload = [
'mode' => $capturePayPalOrderCommand->getFundingSource(),
'orderId' => $capturePayPalOrderCommand->getOrderId()->getValue(),
'payee' => ['merchant_id' => $merchantId],
];

$order = $this->payPalOrderRepository->getPayPalOrderById($capturePayPalOrderCommand->getOrderId());
$merchantId = $this->payPalConfiguration->getMerchantId();

if ($order->checkCustomerIntent(PayPalOrder::CUSTOMER_INTENT_USES_VAULTING)) {
$payload['vault'] = true;
}

$response = $this->maaslandHttpClient->captureOrder($payload);

$orderPayPal = json_decode($response->getBody(), true);

$payPalOrderFromCache = $this->orderPayPalCache->get($orderPayPal['id']);

$orderPayPal = array_replace_recursive($payPalOrderFromCache, $orderPayPal);
$capturePayloadDTO = $this->capturePayloadBuilder->buildPayload(
$capturePayPalOrderCommand->getFundingSource(),
$capturePayPalOrderCommand->getOrderId()->getValue(),
$merchantId
);

$orderPayPal = $this->captureOrder($capturePayloadDTO);
$capturePayPal = $orderPayPal['purchase_units'][0]['payments']['captures'][0];

if (isset($orderPayPal['payment_source'][$capturePayPalOrderCommand->getFundingSource()]['attributes']['vault'])) {
Expand Down Expand Up @@ -175,4 +171,51 @@ public function handle(CapturePayPalOrderCommand $capturePayPalOrderCommand)
throw new PsCheckoutException('PayPal declined the capture', PsCheckoutException::PAYPAL_PAYMENT_CAPTURE_DECLINED);
}
}

private function captureOrder(CaptureOrderPayloadDTO $captureOrderPayloadDTO)
{
try {
$response = $this->maaslandHttpClient->captureOrder($captureOrderPayloadDTO->toArray());

$orderPayPal = json_decode($response->getBody(), true);
$payPalOrderFromCache = $this->orderPayPalCache->get($orderPayPal['id']);

$orderPayPal = array_replace_recursive($payPalOrderFromCache, $orderPayPal);

return $orderPayPal;
} catch (\Exception $exception) {
return [];
}
}

private function processTheCapture()
{
if (isset($orderPayPal['payment_source'][$capturePayPalOrderCommand->getFundingSource()]['attributes']['vault'])) {
$vault = $orderPayPal['payment_source'][$capturePayPalOrderCommand->getFundingSource()]['attributes']['vault'];
if (isset($vault['customer']['id'])) {
try {
$payPalCustomerId = new PayPalCustomerId($vault['customer']['id']);
$customerId = new CustomerId($this->prestaShopContext->getCustomerId());
$this->payPalCustomerRepository->save($customerId, $payPalCustomerId);
} catch (\Exception $exception) {
}
}

if (isset($vault['id'])) {
$resource = $vault;
$resource['metadata'] = [
'order_id' => $orderPayPal['id'],
];
$paymentSource = $orderPayPal['payment_source'];
unset($paymentSource[$capturePayPalOrderCommand->getFundingSource()]['attributes']['vault']);
$resource['payment_source'] = $paymentSource;
$resource['payment_source'][$capturePayPalOrderCommand->getFundingSource()]['verification_status'] = $resource['status'];

$this->eventDispatcher->dispatch(new PaymentTokenCreatedEvent(
$resource,
$merchantId
));
}
}
}
}
10 changes: 10 additions & 0 deletions src/PayPal/Order/DTO/CaptureOrderDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO;

class CaptureOrderDTO
{
private $toto;
}
59 changes: 59 additions & 0 deletions src/PayPal/Order/DTO/PayPalOrderDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO;

class PayPalOrderDTO
{

/**
* @var int
*/
private $id;
/**
* @var int
*/
private $idCart;
/**
* @var string
*/
private $intent;
/**
* @var string
*/
private $fundingSource;
/**
* @var string
*/
private $status;
/**
* @var array
*/
private $paymentSource;
/**
* @var string
*/
private $environment;
/**
* @var bool
*/
private $isCardFields;
/**
* @var bool
*/
private $isExpressCheckout;
/**
* @var array
*/
private $customerIntent;
/**
* @var int|null
*/
private $paymentTokenId;

public static function createFromMasslandResponse(array $data): PayPalOrderDTO
{
return new PayPalOrderDTO();
}
}
36 changes: 36 additions & 0 deletions src/PayPal/Order/DTO/Write/CaptureOrderPayloadDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\DTO\Write;

class CaptureOrderPayloadDTO
{
private $mode;
private $orderId;
private $payee;
private $vault = false;

private function __construct($mode, $orderId, $payee, $vault)
{
$this->mode = $mode;
$this->orderId = $orderId;
$this->payee = $payee;
$this->vault = $vault;
}

public static function create($mode, $orderId, $merchantId, $vault)
{
return new self($mode, $orderId, $merchantId, $vault);
}

public function toArray(): array
{
return [
'mode' => $this->mode,
'orderId' => $this->orderId,
'payee' => $this->payee,
'vault' => $this->vault,
];
}
}
16 changes: 16 additions & 0 deletions src/PayPal/Processor/CaptureOrderProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PrestashopCheckout\PayPal\Processor;

class CaptureOrderProcessor
{
private $eventDispatcher;
private $logger;

public function process(array $order)
{

}
}

0 comments on commit 30ffdc8

Please sign in to comment.