DisconnectFromSquareAccountCommand.php
2 years ago
DisconnectFromSquareAccountCommandHandler.php
7 months ago
DisconnectFromSquareAccountDirectlyCommand.php
7 months ago
DisconnectFromSquareAccountDirectlyCommandHandler.php
7 months ago
FetchAccessTokenSquareCommand.php
1 year ago
FetchAccessTokenSquareCommandHandler.php
7 months ago
GetSquareAuthURLCommand.php
11 months ago
GetSquareAuthURLCommandHandler.php
11 months ago
SquareRefundWebhookCommand.php
1 year ago
SquareRefundWebhookCommandHandler.php
1 week ago
SquareRefundWebhookCommandHandler.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Square; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Logger\LoggerInterface; |
| 6 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 7 | use AmeliaBooking\Application\Commands\CommandResult; |
| 8 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 9 | use AmeliaBooking\Application\Services\Validation\ValidationService; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 13 | use AmeliaBooking\Infrastructure\Repository\Payment\PaymentRepository; |
| 14 | use Interop\Container\Exception\ContainerException; |
| 15 | |
| 16 | /** |
| 17 | * Class SquareRefundWebhookCommandHandler |
| 18 | * |
| 19 | * @package AmeliaBooking\Application\Commands\Square |
| 20 | */ |
| 21 | class SquareRefundWebhookCommandHandler extends CommandHandler |
| 22 | { |
| 23 | /** |
| 24 | * @param SquareRefundWebhookCommand $command |
| 25 | * |
| 26 | * @return CommandResult |
| 27 | * @throws AccessDeniedException |
| 28 | * @throws NotFoundException |
| 29 | * @throws QueryExecutionException |
| 30 | * @throws ContainerException |
| 31 | * @throws InvalidArgumentException |
| 32 | * @throws \Exception |
| 33 | */ |
| 34 | public function handle(SquareRefundWebhookCommand $command) |
| 35 | { |
| 36 | /** @var PaymentRepository $paymentRepository */ |
| 37 | $paymentRepository = $this->container->get('domain.payment.repository'); |
| 38 | |
| 39 | $data = $command->getField('data'); |
| 40 | |
| 41 | if (!ValidationService::verifySignature(json_encode($data), 'middleware', $command->getField('signature'))) { |
| 42 | throw new AccessDeniedException('Signature mismatch.'); |
| 43 | } |
| 44 | |
| 45 | $result = new CommandResult(); |
| 46 | |
| 47 | if ($data && !empty($data['object']['refund']['payment_id'])) { |
| 48 | $payments = $paymentRepository->getByEntityId($data['object']['refund']['payment_id'], 'transactionId'); |
| 49 | |
| 50 | if ($payments->length() === 0) { |
| 51 | $this->container->getLoggerService()->channel(LoggerInterface::CHANNEL_PAYMENT)->error( |
| 52 | 'Square refund webhook processing failed', |
| 53 | [ |
| 54 | 'paymentId' => $data['object']['refund']['payment_id'], |
| 55 | 'reason' => 'Cannot find payment', |
| 56 | ] |
| 57 | ); |
| 58 | |
| 59 | $result->setResult(CommandResult::RESULT_ERROR); |
| 60 | $result->setMessage('Cannot find payment'); |
| 61 | $result->setData(['success' => false]); |
| 62 | |
| 63 | return $result; |
| 64 | } |
| 65 | |
| 66 | foreach ($payments->toArray() as $payment) { |
| 67 | $paymentRepository->updateFieldById($payment['id'], 'refunded', 'status'); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 72 | $result->setMessage('Successfully updated payment status'); |
| 73 | $result->setData(['success' => true]); |
| 74 | |
| 75 | return $result; |
| 76 | } |
| 77 | } |
| 78 |