PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.21
Booking for Appointments and Events Calendar – Amelia v1.2.21
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Square / SquarePaymentNotifyCommandHandler.php
ameliabooking / src / Application / Commands / Square Last commit date
DisconnectFromSquareAccountCommand.php 2 years ago DisconnectFromSquareAccountCommandHandler.php 2 years ago FetchAccessTokenSquareCommand.php 2 years ago FetchAccessTokenSquareCommandHandler.php 2 years ago GetSquareAuthURLCommand.php 2 years ago GetSquareAuthURLCommandHandler.php 2 years ago SquarePaymentCommand.php 2 years ago SquarePaymentCommandHandler.php 2 years ago SquarePaymentNotifyCommand.php 2 years ago SquarePaymentNotifyCommandHandler.php 2 years ago SquareRefundWebhookCommand.php 2 years ago SquareRefundWebhookCommandHandler.php 2 years ago
SquarePaymentNotifyCommandHandler.php
99 lines
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\Services\Payment\PaymentApplicationService;
8 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
9 use AmeliaBooking\Domain\Entity\Cache\Cache;
10 use AmeliaBooking\Infrastructure\Repository\Cache\CacheRepository;
11 use AmeliaBooking\Infrastructure\Services\Payment\SquareService;
12 use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings;
13 use Exception;
14 use Interop\Container\Exception\ContainerException;
15 use Square\Models\Order;
16
17 /**
18 * Class SquarePaymentNotifyCommandHandler
19 *
20 * @package AmeliaBooking\Application\Commands\Square
21 */
22 class SquarePaymentNotifyCommandHandler extends CommandHandler
23 {
24 public $mandatoryFields = [
25 'name'
26 ];
27
28 /**
29 * @param SquarePaymentNotifyCommand $command
30 *
31 * @return CommandResult
32 * @throws InvalidArgumentException
33 * @throws Exception
34 * @throws ContainerException
35 */
36 public function handle(SquarePaymentNotifyCommand $command)
37 {
38 /** @var PaymentApplicationService $paymentAS */
39 $paymentAS = $this->container->get('application.payment.service');
40 /** @var SquareService $paymentService */
41 $paymentService = $this->container->get('infrastructure.payment.square.service');
42 /** @var CacheRepository $cacheRepository */
43 $cacheRepository = $this->container->get('domain.cache.repository');
44
45 $this->checkMandatoryFields($command);
46
47 $name = $command->getField('name');
48 /** @var Cache $cache */
49 $cache = ($data = explode('_', $name)) && isset($data[0], $data[1]) ?
50 $cacheRepository->getByIdAndName($data[0], $data[1]) : null;
51
52 if (!$cache || !$cache->getPaymentId()) {
53 $result = new CommandResult();
54 $result->setResult(CommandResult::RESULT_ERROR);
55 $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']);
56 $result->setData(
57 [
58 'message' => 'Cache object not saved',
59 'paymentSuccessful' => false,
60 ]
61 );
62
63 return $result;
64 }
65
66 $response = $paymentService->getOrderResponse($command->getField('squareOrderId'));
67
68 if ($response->isError()) {
69 $result = new CommandResult();
70 $result->setResult(CommandResult::RESULT_ERROR);
71 $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']);
72 $result->setData(
73 [
74 'message' => $paymentService->getErrorMessage($response),
75 'paymentSuccessful' => false,
76 ]
77 );
78
79 return $result;
80 }
81
82 /**@var Order $order */
83 $order = $response->getResult()->getOrder();
84
85 $paymentId = null;
86 if ($order && $order->getTenders() && sizeof($order->getTenders()) > 0) {
87 $paymentId = $order->getTenders()[0]->getPaymentId();
88 $response = $paymentService->completePayment($paymentId);
89 }
90
91 $status = 'paid';
92
93 $result = $paymentAS->updateAppointmentAndCache($data[2], $status, $cache, $paymentId);
94 $returnUrl = urldecode($command->getField('returnUrl'));
95 $result->setUrl($returnUrl. (strpos($returnUrl, '?') ? '&' : '?') . 'ameliaCache=' . $name);
96 return $result;
97 }
98 }
99