custom/plugins/SwagPayPal/src/Webhook/Handler/AbstractWebhookHandler.php line 100

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3. * (c) shopware AG <info@shopware.com>
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. */
  7. namespace Swag\PayPal\Webhook\Handler;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Swag\PayPal\RestApi\PayPalApiStruct;
  15. use Swag\PayPal\RestApi\V1\Api\Webhook as WebhookV1;
  16. use Swag\PayPal\RestApi\V2\Api\Order\PurchaseUnit\Payments\Payment;
  17. use Swag\PayPal\RestApi\V2\Api\Webhook as WebhookV2;
  18. use Swag\PayPal\SwagPayPal;
  19. use Swag\PayPal\Webhook\Exception\ParentPaymentNotFoundException;
  20. use Swag\PayPal\Webhook\Exception\WebhookException;
  21. use Swag\PayPal\Webhook\Exception\WebhookOrderTransactionNotFoundException;
  22. use Swag\PayPal\Webhook\WebhookHandler;
  23. abstract class AbstractWebhookHandler implements WebhookHandler
  24. {
  25. protected EntityRepository $orderTransactionRepository;
  26. protected OrderTransactionStateHandler $orderTransactionStateHandler;
  27. /**
  28. * @internal
  29. */
  30. public function __construct(
  31. EntityRepository $orderTransactionRepository,
  32. OrderTransactionStateHandler $orderTransactionStateHandler
  33. ) {
  34. $this->orderTransactionRepository = $orderTransactionRepository;
  35. $this->orderTransactionStateHandler = $orderTransactionStateHandler;
  36. }
  37. abstract public function getEventType(): string;
  38. /**
  39. * @param WebhookV1|WebhookV2 $webhook
  40. */
  41. abstract public function invoke(PayPalApiStruct $webhook, Context $context): void;
  42. /**
  43. * @throws ParentPaymentNotFoundException
  44. * @throws WebhookOrderTransactionNotFoundException
  45. */
  46. protected function getOrderTransaction(WebhookV1 $webhook, Context $context): OrderTransactionEntity
  47. {
  48. $payPalTransactionId = $webhook->getResource()->getParentPayment();
  49. if ($payPalTransactionId === null) {
  50. throw new ParentPaymentNotFoundException($this->getEventType());
  51. }
  52. $criteria = new Criteria();
  53. $criteria->addAssociation('order');
  54. $criteria->addFilter(
  55. new EqualsFilter(
  56. \sprintf('customFields.%s', SwagPayPal::ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_TRANSACTION_ID),
  57. $payPalTransactionId
  58. )
  59. );
  60. /** @var OrderTransactionEntity|null $orderTransaction */
  61. $orderTransaction = $this->orderTransactionRepository->search($criteria, $context)->first();
  62. if ($orderTransaction === null) {
  63. throw new WebhookOrderTransactionNotFoundException(
  64. \sprintf('with the PayPal ID "%s"', $payPalTransactionId),
  65. $this->getEventType()
  66. );
  67. }
  68. return $orderTransaction;
  69. }
  70. protected function getOrderTransactionV2(Payment $resource, Context $context): OrderTransactionEntity
  71. {
  72. $customId = $resource->getCustomId() ?? '[]';
  73. $customIdArray = \json_decode($customId, true);
  74. if (!\is_array($customIdArray)) {
  75. $orderTransactionId = $customId;
  76. } else {
  77. $orderTransactionId = $customIdArray['orderTransactionId'];
  78. }
  79. if ($orderTransactionId === null) {
  80. throw new WebhookException($this->getEventType(), 'Given webhook resource data does not contain needed custom ID');
  81. }
  82. $criteria = new Criteria([$orderTransactionId]);
  83. $criteria->addAssociation('order');
  84. /** @var OrderTransactionEntity|null $orderTransaction */
  85. $orderTransaction = $this->orderTransactionRepository->search($criteria, $context)->first();
  86. if ($orderTransaction === null) {
  87. throw new WebhookOrderTransactionNotFoundException(
  88. \sprintf('with custom ID "%s" (order transaction ID)', $orderTransactionId),
  89. $this->getEventType()
  90. );
  91. }
  92. return $orderTransaction;
  93. }
  94. protected function isChangeAllowed(OrderTransactionEntity $orderTransaction, string $invalidStatus): bool
  95. {
  96. $state = $orderTransaction->getStateMachineState();
  97. if ($state === null) {
  98. return true;
  99. }
  100. return $state->getTechnicalName() !== $invalidStatus;
  101. }
  102. }