Skip to content

Commit

Permalink
Handle payment the correct way
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 23, 2024
1 parent 34eb230 commit e604e94
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/WebhookHandler/OrderPackedWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\OrderPaymentTransitions;
use Sylius\Component\Core\OrderShippingTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Webmozart\Assert\Assert;

final class OrderPackedWebhookHandler implements WebhookHandlerInterface, LoggerAwareInterface
Expand Down Expand Up @@ -73,15 +73,7 @@ public function handle(object $data): void
}

if ($data->paymentCaptured) {
$this->logger->debug(sprintf('The payment is captured, so we will check if we can take the "%s" transition', OrderPaymentTransitions::TRANSITION_PAY));

$orderPaymentStateMachine = $this->stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH);

if ($orderPaymentStateMachine->can(OrderPaymentTransitions::TRANSITION_PAY)) {
$this->logger->debug(sprintf('Taking the "%s" transition', OrderPaymentTransitions::TRANSITION_PAY));

$orderPaymentStateMachine->apply(OrderPaymentTransitions::TRANSITION_PAY);
}
$this->completePayment($order);
}

$this->logger->debug(sprintf('Order state after: %s', $order->getState()));
Expand All @@ -100,6 +92,11 @@ public function supports(object $data): bool
return $data instanceof WebhookDataPickOrderPacked;
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

/**
* @param list<array{id: string, quantity: int}> $syliusOrderLines
* @param list<array{id: string, quantity: int}> $peakOrderLines
Expand All @@ -121,8 +118,23 @@ private static function assertSame(array $syliusOrderLines, array $peakOrderLine
Assert::count($peakOrderLines, 0);
}

public function setLogger(LoggerInterface $logger): void
private function completePayment(OrderInterface $order): void
{
$this->logger = $logger;
$this->logger->debug(sprintf('The payment is captured, so we will check if we can take the "%s" transition', PaymentTransitions::TRANSITION_COMPLETE));

$payment = $order->getLastPayment();
if (null === $payment) {
$this->logger->debug(sprintf('There is no payment on order %s', (string) $order->getId()));

return;
}

$paymentStateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);

if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_COMPLETE)) {
$this->logger->debug(sprintf('Taking the "%s" transition', PaymentTransitions::TRANSITION_COMPLETE));

$paymentStateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE);
}
}
}

0 comments on commit e604e94

Please sign in to comment.