PaymentCallbackProcessor.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Webkul\BagistoApi\State;
  3. use ApiPlatform\Metadata\Operation;
  4. use ApiPlatform\State\ProcessorInterface;
  5. use Webkul\BagistoApi\Dto\PaymentCallbackInput;
  6. use Webkul\BagistoApi\Exception\OperationFailedException;
  7. use Webkul\BagistoApi\Services\PaymentService;
  8. /**
  9. * Single point of entry for the gateway return (success/cancel/failure).
  10. * Address blocks on the input are required only for express success
  11. * calls where the gateway supplied the buyer's real shipping address.
  12. */
  13. class PaymentCallbackProcessor implements ProcessorInterface
  14. {
  15. public function __construct(
  16. protected PaymentService $paymentService,
  17. ) {}
  18. public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
  19. {
  20. if (! $data instanceof PaymentCallbackInput) {
  21. throw new OperationFailedException(__('bagistoapi::app.graphql.checkout.invalid-input'));
  22. }
  23. $result = $this->paymentService->callback($data);
  24. $order = $result['order'];
  25. return (object) [
  26. 'id' => (int) $order->id,
  27. 'success' => $result['status'] === 'success',
  28. 'message' => $result['message'],
  29. 'orderId' => (string) $order->id,
  30. 'status' => $result['status'],
  31. 'orderStatus' => (string) $order->status,
  32. 'gatewayStatus' => $result['gatewayStatus'],
  33. ];
  34. }
  35. }