PluginProbe
Tamara Checkout / trunk
Tamara Checkout vtrunk
1.9.9.23 1.9.9.22 1.9.9.20 trunk 1.0.13 1.7.4 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 1.9.9.1 1.9.9.11 1.9.9.12 1.9.9.13 1.9.9.14 1.9.9.15 1.9.9.16 1.9.9.17 1.9.9.2 1.9.9.3 1.9.9.4 1.9.9.5 All 29 releases
tamara-checkout / src / Services / TamaraNotificationService.php

TamaraNotificationService.php in Tamara Checkout trunk, at src/Services/TamaraNotificationService.php

226 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace Tamara\Wp\Plugin\Services;
5
6 use Exception;
7 use Tamara\Wp\Plugin\Dependencies\Tamara\Client;
8 use Tamara\Wp\Plugin\Dependencies\Tamara\Exception\RequestDispatcherException;
9 use Tamara\Wp\Plugin\Dependencies\Tamara\Notification\NotificationService;
10 use Tamara\Wp\Plugin\Dependencies\Tamara\Request\Order\AuthoriseOrderRequest;
11 use Tamara\Wp\Plugin\Dependencies\Tamara\Response\Order\AuthoriseOrderResponse;
12 use Tamara\Wp\Plugin\TamaraCheckout;
13 use Tamara\Wp\Plugin\Traits\ConfigTrait;
14 use Tamara\Wp\Plugin\Traits\ServiceTrait;
15 use Tamara\Wp\Plugin\Traits\WPAttributeTrait;
16
17 /**
18 * Class TamaraNotificationService
19 * @package Tamara\Wp\Plugin\Services
20 * @method \Tamara\Wp\Plugin\TamaraCheckout getContainer()
21 */
22 class TamaraNotificationService
23 {
24 use ConfigTrait;
25 use WPAttributeTrait;
26 use ServiceTrait;
27
28 private const
29 REGISTER_WEBHOOKS = [
30 'order_expired',
31 'order_declined',
32 ];
33
34 /**
35 * @var WCTamaraGateway
36 */
37 public $wcTamaraGateway;
38
39 /**
40 * @var string Token Key for Tamara Notification service
41 */
42 public $tokenKey;
43
44 /**
45 * @var Client
46 */
47 public $tamaraClient;
48
49 /**
50 * @var array Tamara custom order statuses
51 */
52 public $tamaraStatus = [];
53
54 /** @noinspection PhpFullyQualifiedNameUsageInspection */
55 /**
56 * Prepare Tamara Client instance
57 *
58 * @throws \Illuminate\Contracts\Container\BindingResolutionException
59 */
60 public function init()
61 {
62 $this->wcTamaraGateway = $this->getContainer()->getService(WCTamaraGateway::class);
63 $this->tamaraClient = $this->wcTamaraGateway->tamaraClient;
64
65 $this->tokenKey = $this->wcTamaraGateway->notificationToken;
66
67 $this->tamaraStatus = $this->wcTamaraGateway->tamaraStatus;
68 }
69
70 /**
71 * Handle Instant Payment Notification from Tamara to update order
72 */
73 public function handleIpnRequest()
74 {
75 try {
76 TamaraCheckout::getInstance()->logMessage('Tamara start processing IPN');
77
78 $notification = NotificationService::create($this->tokenKey);
79 $message = $notification->processAuthoriseNotification();
80
81 TamaraCheckout::getInstance()->logMessage(sprintf("Tamara Notification Process Response: %s", print_r($message, true)));
82
83 if (!empty($message)) {
84 $wcOrderId = $message->getOrderReferenceId();
85 $tamaraOrderId = $message->getOrderId();
86 $wcOrder = wc_get_order($wcOrderId);
87
88 update_post_meta($wcOrderId, 'payment_method', $wcOrder->get_payment_method());
89 $this->wcTamaraGateway->updateTamaraOrderId($wcOrderId, $tamaraOrderId);
90
91 if (!TamaraCheckout::getInstance()->isOrderAuthorised($wcOrderId) && $wcOrder && ('approved' === $message->getOrderStatus())) {
92 $this->authoriseOrder($wcOrderId, $tamaraOrderId);
93 }
94
95 http_response_code(200);
96 } else {
97 TamaraCheckout::getInstance()->logMessage("Tamara IPN Failed: empty message");
98 http_response_code(406);
99 }
100 } catch (Exception $exception) {
101 TamaraCheckout::getInstance()->logMessage(sprintf("Tamara IPN Failed: %s", print_r($exception, true)));
102 if (403 === $exception->getCode()) {
103 http_response_code($exception->getCode());
104 } else {
105 http_response_code(406);
106 }
107 }
108 }
109
110 /**
111 * Authorise Tamara Order method
112 *
113 * @param int $wcOrderId
114 * @param string $tamaraOrderId
115 */
116 public function authoriseOrder($wcOrderId, $tamaraOrderId)
117 {
118 try {
119 $response = $this->tamaraClient->authoriseOrder(new AuthoriseOrderRequest($tamaraOrderId));
120 TamaraCheckout::getInstance()->logMessage(sprintf("Tamara Payment Authorise Process Response: %s", print_r($response, true)));
121 } catch (RequestDispatcherException $e) {
122 TamaraCheckout::getInstance()->logMessage(
123 sprintf(
124 "Tamara Payment Authorise Failed.\nError message: ' %s'.\nTrace: %s",
125 $e->getMessage(),
126 $e->getTraceAsString()
127 )
128 );
129 }
130
131 if (!empty($response)) {
132 $wcOrder = wc_get_order($wcOrderId);
133 if ($response->isSuccess()) {
134 $orderNote = 'Tamara - Order authorised successfully with Tamara Notification';
135 $newOrderStatus = $this->tamaraStatus['authorise_done'];
136 $updateOrderStatusNote = 'Payment received. ';
137 TamaraCheckout::getInstance()->updateOrderStatusAndAddOrderNote($wcOrder, $orderNote, $newOrderStatus, $updateOrderStatusNote);
138
139 // Empty cart if payment done
140 WC()->cart->empty_cart();
141 update_post_meta($wcOrderId, 'tamara_authorized', true);
142 update_post_meta($wcOrderId, 'payment_method', $wcOrder->get_payment_method());
143 $this->wcTamaraGateway->updateTamaraOrderId($wcOrderId, $tamaraOrderId);
144 if (TamaraCheckout::TAMARA_GATEWAY_CHECKOUT_ID === $wcOrder->get_payment_method()) {
145 TamaraCheckout::getInstance()->updateWcOrderPaymentMethodAccordingToTamaraOrder($wcOrderId, $wcOrder);
146 }
147
148 } elseif ($this->isAuthorizedResponse($response)) {
149 $wcOrder->add_order_note(
150 __(
151 'Tamara - Order authorised re-occurred, ignore it.',
152 'tamara-checkout'
153 )
154 );
155 } else {
156 $orderNote = 'Tamara - Order authorised failed with Tamara Notification';
157 $newOrderStatus = $this->tamaraStatus['authorise_failed'];
158 $updateOrderStatusNote = 'Tamara - Order authorised failed.';
159 TamaraCheckout::getInstance()->updateOrderStatusAndAddOrderNote($wcOrder, $orderNote, $newOrderStatus, $updateOrderStatusNote);
160 }
161 }
162 }
163
164 /**
165 * Handle webhook execution
166 */
167 public function handleWebhook()
168 {
169 TamaraCheckout::getInstance()->logMessage('Start Tamara Webhook');
170 try {
171 $notification = NotificationService::create($this->tokenKey);
172 $webhookMessage = $notification->processWebhook();
173 $eventType = $webhookMessage->getEventType();
174
175 TamaraCheckout::getInstance()->logMessage(sprintf("Webhook Event Type: %s", print_r($eventType, true)));
176 TamaraCheckout::getInstance()->logMessage(sprintf("Webhook Process Message: %s", print_r($webhookMessage, true)));
177
178 if (!in_array($eventType, self::REGISTER_WEBHOOKS)) {
179 $response = [
180 'Event type: ' => $eventType,
181 'Webhook tamara order id: ' => $webhookMessage->getOrderId(),
182 'Webhook reference order id: ' => $webhookMessage->getOrderReferenceId(),
183 ];
184 TamaraCheckout::getInstance()->logMessage(sprintf("Wrong Webhook event. Response Data: %s", print_r($response, true)));
185
186 return false;
187 }
188
189 if (!empty($webhookMessage) && !TamaraCheckout::getInstance()->isOrderAuthorised($webhookMessage->getOrderReferenceId())) {
190 if ($eventType === 'order_expired' || $eventType === 'order_declined') {
191 $wcOrderId = $webhookMessage->getOrderReferenceId();
192 $wcOrder = wc_get_order($wcOrderId);
193 // translators: %s: Event type
194 $orderNote = esc_html( sprintf( __('Tamara - Event type `%s` received via webhook', 'tamara-checkout'), $eventType) );
195 $newOrderStatus = $this->tamaraStatus['order_cancelled'];
196 TamaraCheckout::getInstance()->updateOrderStatusAndAddOrderNote($wcOrder, $orderNote, $newOrderStatus, '');
197 }
198
199 http_response_code(200);
200 }
201 } catch (Exception $tamaraWebhookException) {
202 TamaraCheckout::getInstance()->logMessage(sprintf("Error message: '%s'.\nTrace: %s",
203 $tamaraWebhookException->getMessage(), $tamaraWebhookException->getTraceAsString()));
204 if (403 === $tamaraWebhookException->getCode()) {
205 http_response_code($tamaraWebhookException->getCode());
206 } else {
207 http_response_code(406);
208 }
209 }
210 TamaraCheckout::getInstance()->logMessage('End Tamara Webhook');
211
212 return true;
213 }
214
215 /**
216 * Check if a response telling an order is authorised
217 *
218 * @param AuthoriseOrderResponse $response
219 *
220 * @return bool
221 */
222 protected function isAuthorizedResponse(AuthoriseOrderResponse $response)
223 {
224 return $response->getStatusCode() === 409;
225 }
226 }