| 1 |
<?php |
| 2 |
|
| 3 |
namespace AmeliaBooking\Application\Commands\Square; |
| 4 |
|
| 5 |
use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 |
use AmeliaBooking\Application\Commands\CommandResult; |
| 7 |
use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 |
use AmeliaBooking\Application\Services\Validation\ValidationService; |
| 9 |
use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 10 |
use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 11 |
use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 12 |
use AmeliaBooking\Infrastructure\Repository\Payment\PaymentRepository; |
| 13 |
use Interop\Container\Exception\ContainerException; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class SquareRefundWebhookCommandHandler |
| 17 |
* |
| 18 |
* @package AmeliaBooking\Application\Commands\Square |
| 19 |
*/ |
| 20 |
class SquareRefundWebhookCommandHandler extends CommandHandler |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @param SquareRefundWebhookCommand $command |
| 24 |
* |
| 25 |
* @return CommandResult |
| 26 |
* @throws AccessDeniedException |
| 27 |
* @throws NotFoundException |
| 28 |
* @throws QueryExecutionException |
| 29 |
* @throws ContainerException |
| 30 |
* @throws InvalidArgumentException |
| 31 |
* @throws \Exception |
| 32 |
*/ |
| 33 |
public function handle(SquareRefundWebhookCommand $command) |
| 34 |
{ |
| 35 |
/** @var PaymentRepository $paymentRepository */ |
| 36 |
$paymentRepository = $this->container->get('domain.payment.repository'); |
| 37 |
|
| 38 |
$data = $command->getField('data'); |
| 39 |
|
| 40 |
if (!ValidationService::verifySignature(json_encode($data), 'middleware', $command->getField('signature'))) { |
| 41 |
throw new AccessDeniedException('Signature mismatch.'); |
| 42 |
} |
| 43 |
|
| 44 |
$result = new CommandResult(); |
| 45 |
|
| 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 |
$result->setResult(CommandResult::RESULT_ERROR); |
| 52 |
$result->setMessage('Cannot find payment'); |
| 53 |
$result->setData(['success' => false]); |
| 54 |
|
| 55 |
return $result; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ($payments->toArray() as $payment) { |
| 59 |
// if (floatval($payment['amount']) <= floatval($data['object']['refund']['amount_money']['amount']/100)) { |
| 60 |
$paymentRepository->updateFieldById($payment['id'], 'refunded', 'status'); |
| 61 |
// } |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
$result->setResult(CommandResult::RESULT_SUCCESS); |
| 66 |
$result->setMessage('Successfully updated payment status'); |
| 67 |
$result->setData(['success' => true]); |
| 68 |
|
| 69 |
return $result; |
| 70 |
} |
| 71 |
} |
| 72 |
|