PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
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 / SquarePaymentCommandHandler.php
ameliabooking / src / Application / Commands / Square Last commit date
DisconnectFromSquareAccountCommand.php 2 years ago DisconnectFromSquareAccountCommandHandler.php 1 year ago FetchAccessTokenSquareCommand.php 1 year ago FetchAccessTokenSquareCommandHandler.php 1 year ago GetSquareAuthURLCommand.php 2 years ago GetSquareAuthURLCommandHandler.php 1 year ago SquarePaymentCommand.php 1 year ago SquarePaymentCommandHandler.php 1 year ago SquarePaymentNotifyCommand.php 1 year ago SquarePaymentNotifyCommandHandler.php 1 year ago SquareRefundWebhookCommand.php 1 year ago SquareRefundWebhookCommandHandler.php 2 years ago
SquarePaymentCommandHandler.php
213 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\Booking\BookingApplicationService;
8 use AmeliaBooking\Application\Services\Payment\PaymentApplicationService;
9 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
10 use AmeliaBooking\Domain\Entity\Booking\Reservation;
11 use AmeliaBooking\Domain\Entity\Cache\Cache;
12 use AmeliaBooking\Domain\Entity\Entities;
13 use AmeliaBooking\Domain\Factory\Cache\CacheFactory;
14 use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface;
15 use AmeliaBooking\Domain\ValueObjects\Number\Float\Price;
16 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
17 use AmeliaBooking\Domain\ValueObjects\String\PaymentType;
18 use AmeliaBooking\Domain\ValueObjects\String\Token;
19 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
20 use AmeliaBooking\Infrastructure\Repository\Cache\CacheRepository;
21 use AmeliaBooking\Infrastructure\Services\Payment\CurrencyService;
22 use AmeliaBooking\Infrastructure\Services\Payment\SquareService;
23 use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings;
24 use Exception;
25 use Interop\Container\Exception\ContainerException;
26 use Slim\Exception\ContainerValueNotFoundException;
27 use Square\Models\PaymentLink;
28
29 /**
30 * Class SquarePaymentCommandHandler
31 *
32 * @package AmeliaBooking\Application\Commands\Square
33 */
34 class SquarePaymentCommandHandler extends CommandHandler
35 {
36 public $mandatoryFields = [
37 'bookings',
38 'payment'
39 ];
40
41 /**
42 * @param SquarePaymentCommand $command
43 *
44 * @return CommandResult
45 * @throws QueryExecutionException
46 * @throws ContainerValueNotFoundException
47 * @throws InvalidArgumentException
48 * @throws Exception
49 * @throws ContainerException
50 */
51 public function handle(SquarePaymentCommand $command)
52 {
53 $result = new CommandResult();
54
55 $this->checkMandatoryFields($command);
56
57 $type = $command->getField('type') ?: Entities::APPOINTMENT;
58
59 /** @var ReservationServiceInterface $reservationService */
60 $reservationService = $this->container->get('application.reservation.service')->get($type);
61
62 /** @var BookingApplicationService $bookingAS */
63 $bookingAS = $this->container->get('application.booking.booking.service');
64
65 /** @var PaymentApplicationService $paymentAS */
66 $paymentAS = $this->container->get('application.payment.service');
67
68 /** @var SquareService $paymentService */
69 $paymentService = $this->container->get('infrastructure.payment.square.service');
70
71 /** @var CacheRepository $cacheRepository */
72 $cacheRepository = $this->container->get('domain.cache.repository');
73
74 /** @var CurrencyService $currencyService */
75 $currencyService = $this->container->get('infrastructure.payment.currency.service');
76
77 /** @var Reservation $reservation */
78 $reservation = $reservationService->getNew(true, true, true);
79
80 $reservationService->processBooking(
81 $result,
82 $bookingAS->getAppointmentData($command->getFields()),
83 $reservation,
84 false
85 );
86
87 if ($result->getResult() === CommandResult::RESULT_ERROR) {
88 return $result;
89 }
90
91
92 $paymentAmount = $reservationService->getReservationPaymentAmount($reservation);
93
94 if (!$paymentAmount) {
95 $result->setResult(CommandResult::RESULT_ERROR);
96 $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']);
97 $result->setData(
98 [
99 'paymentSuccessful' => false,
100 'onSitePayment' => true
101 ]
102 );
103
104 return $result;
105 }
106
107
108 $token = new Token();
109
110 /** @var Cache $cache */
111 $cache = CacheFactory::create(
112 [
113 'name' => $token->getValue(),
114 'data' => json_encode(
115 [
116 'status' => null,
117 'request' => $command->getField('componentProps'),
118 ]
119 ),
120 ]
121 );
122
123 $cacheId = $cacheRepository->add($cache);
124
125 $cache->setId(new Id($cacheId));
126
127
128 $additionalInformation = $paymentAS->getBookingInformationForPaymentSettings(
129 $reservation,
130 PaymentType::SQUARE
131 );
132
133 $identifier = $cacheId . '_' . $token->getValue() . '_' . $type;
134
135 $returnUrl = explode('#', $command->getField('returnUrl'));
136 $bookings = $command->getField('bookings');
137
138 $redirectUrl = AMELIA_ACTION_URL . '__payment__square__notify&name=' . $identifier . '&returnUrl=' . $returnUrl[0];
139
140 $response = null;
141
142 $transfers = [];
143
144 try {
145 $response = $paymentService->execute(
146 [
147 'redirectUrl' => $redirectUrl,
148 'amount' => $currencyService->getAmountInFractionalUnit(new Price($paymentAmount)),
149 'description' => $additionalInformation['description'] ?: $reservation->getBookable()->getName()->getValue(),
150 'metaData' => $additionalInformation['metaData'] ?: [],
151 'customer' => $bookings ? $bookings[0]['customer'] : null
152 ],
153 $transfers
154 );
155 } catch (Exception $e) {
156 $result->setResult(CommandResult::RESULT_ERROR);
157 $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']);
158 $result->setData(
159 [
160 'message' => $e->getMessage(),
161 'paymentSuccessful' => false,
162 ]
163 );
164 }
165
166 if ($response && $response->isSuccess() && $response->getResult() && $response->getResult()->getPaymentLink()) {
167 /**@var PaymentLink $paymentLink */
168 $paymentLink = $response->getResult()->getPaymentLink();
169 $orderId = $paymentLink->getOrderId();
170
171
172 $result = $reservationService->processRequest(
173 $bookingAS->getAppointmentData($command->getFields()),
174 $reservation,
175 true
176 );
177
178 $result = $paymentAS->updateCache($result, $command->getFields(), $cache, $reservation, ['orderId' => $orderId]);
179
180 $paymentService->updatePaymentLink(
181 $paymentLink,
182 $redirectUrl . '&squareOrderId=' . $orderId . (!empty($returnUrl[1]) ? '&fragment=' . $returnUrl[1] : ''),
183 $result->getData()['paymentId']
184 );
185
186 if ($result->getResult() === CommandResult::RESULT_ERROR) {
187 return $result;
188 }
189
190 $result->setResult(CommandResult::RESULT_SUCCESS);
191 $result->setMessage('Proceed to Square Checkout Page');
192 $result->setData(
193 [
194 'redirectUrl' => $paymentLink->getUrl()
195 ]
196 );
197
198 return $result;
199 }
200
201 $result->setResult(CommandResult::RESULT_ERROR);
202 $result->setMessage(FrontendStrings::getCommonStrings()['payment_error']);
203 $result->setData(
204 [
205 'message' => $response ? $paymentService->getErrorMessage($response) : null,
206 'paymentSuccessful' => false,
207 ]
208 );
209
210 return $result;
211 }
212 }
213