PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.14.2
Yatra – Travel Booking & Tour Operator Software v3.0.14.2
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / app / PaymentGateways / AbstractPaymentGateway.php

AbstractPaymentGateway.php in Yatra – Travel Booking & Tour Operator Software 3.0.14.2, at app/PaymentGateways/AbstractPaymentGateway.php

299 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\PaymentGateways;
6
7 /**
8 * Abstract Payment Gateway
9 * Base class for all payment gateways
10 */
11 abstract class AbstractPaymentGateway implements PaymentGatewayInterface
12 {
13 protected string $id;
14 protected string $title;
15 protected string $description;
16 protected string $icon = '';
17 protected string $sandboxUrl = '';
18 protected bool $isOffline = false;
19 protected array $supports = [];
20 protected array $config = [];
21
22 public function __construct()
23 {
24 $this->loadConfig();
25 }
26
27 public function getId(): string
28 {
29 return $this->id;
30 }
31
32 public function getTitle(): string
33 {
34 return $this->title;
35 }
36
37 public function getDescription(): string
38 {
39 return $this->description;
40 }
41
42 /**
43 * The gateway's built-in (raw, untranslated) default title/description —
44 * the value of the `$title`/`$description` property before any `__()`
45 * override. Used to tell whether a stored gateway-config value is an
46 * operator customization or just the persisted default: if it equals the
47 * default, the checkout should prefer the translated getTitle()/
48 * getDescription() instead of the stored English string, so the label
49 * translates. (Subclasses override getTitle()/getDescription() to return
50 * a translated string, but the property stays the raw default.)
51 */
52 public function getDefaultTitle(): string
53 {
54 return $this->title;
55 }
56
57 public function getDefaultDescription(): string
58 {
59 return $this->description;
60 }
61
62 public function getIcon(): string
63 {
64 // If icon is a relative path (starts with /), convert to full URL
65 if (!empty($this->icon) && strpos($this->icon, 'http') !== 0) {
66 // Priority 1: Check if icon is directly specified (e.g., 'paypal.svg', 'stripe.svg')
67 if (strpos($this->icon, '.') !== false) {
68 // Icon has extension, check in payment-gateways folder
69 $iconPath = YATRA_PLUGIN_PATH . "assets/images/payment-gateways/{$this->icon}";
70 if (file_exists($iconPath)) {
71 return YATRA_PLUGIN_URL . "assets/images/payment-gateways/{$this->icon}";
72 }
73 }
74
75 // Priority 2: Try to derive icon name from gateway ID
76 $iconName = $this->id;
77
78
79 return YATRA_PLUGIN_URL . "assets/images/payment-gateways/{$iconName}.svg";
80
81 }
82 return $this->icon;
83 }
84
85 public function isOffline(): bool
86 {
87 return $this->isOffline;
88 }
89
90 public function getSandboxUrl(): string
91 {
92 return $this->sandboxUrl;
93 }
94
95 public function getSupports(): array
96 {
97 return $this->supports;
98 }
99
100 public function getConfig(): array
101 {
102 return $this->config;
103 }
104
105 /**
106 * Load configuration from database
107 */
108 protected function loadConfig(): void
109 {
110 $allConfigs = get_option('yatra_gateway_configs', []);
111 if (is_string($allConfigs)) {
112 $allConfigs = maybe_unserialize($allConfigs);
113 }
114 $defaults = $this->getDefaultConfig();
115 $stored = $allConfigs[$this->id] ?? null;
116 if (!is_array($stored) || $stored === []) {
117 $this->config = $defaults;
118
119 return;
120 }
121 $this->config = array_merge($defaults, $stored);
122 }
123
124 /**
125 * Get default configuration
126 */
127 protected function getDefaultConfig(): array
128 {
129 // PayLater gateway is enabled by default, others are disabled
130 $defaults = ['enabled' => $this->id === 'pay_later'];
131 foreach ($this->getConfigFields() as $field) {
132 $defaults[$field['id']] = $field['default'] ?? '';
133 }
134 // Ensure 'enabled' is always present
135 if (!isset($defaults['enabled'])) {
136 $defaults['enabled'] = $this->id === 'pay_later';
137 }
138 return $defaults;
139 }
140
141 public function saveConfig(array $config): bool
142 {
143 $allConfigs = get_option('yatra_gateway_configs', []);
144 if (is_string($allConfigs)) {
145 $allConfigs = maybe_unserialize($allConfigs);
146 }
147 $allConfigs[$this->id] = array_merge($this->config, $config);
148 return update_option('yatra_gateway_configs', $allConfigs);
149 }
150
151 public function isAvailable(): bool
152 {
153 return $this->isEnabled() && $this->isProperlyConfigured();
154 }
155
156 public function isProperlyConfigured(): bool
157 {
158 return true;
159 }
160
161 /**
162 * Check if gateway is enabled in settings
163 */
164 public function isEnabled(): bool
165 {
166 return !empty($this->config['enabled'] ?? false);
167 }
168
169 /**
170 * Get option key for this gateway
171 */
172 protected function getOptionKey(): string
173 {
174 return 'yatra_gateway_' . $this->id;
175 }
176
177 /**
178 * Make HTTP request
179 */
180 protected function makeRequest(string $url, array $options = []): array
181 {
182 $response = wp_remote_request($url, $options);
183
184 if (is_wp_error($response)) {
185 return [
186 'success' => false,
187 'error' => $response->get_error_message(),
188 ];
189 }
190
191 $body = wp_remote_retrieve_body($response);
192 $code = wp_remote_retrieve_response_code($response);
193
194 // Remove BOM (Byte Order Mark) that some APIs like Authorize.net return
195 $body = preg_replace('/^\xEF\xBB\xBF/', '', $body);
196
197 // Try to decode JSON
198 $decoded = json_decode($body, true);
199
200 return [
201 'success' => $code >= 200 && $code < 300,
202 'code' => $code,
203 'body' => $decoded !== null ? $decoded : $body,
204 ];
205 }
206
207 /**
208 * Log gateway activity
209 */
210 protected function log(string $message, array $context = []): void
211 {
212 if (defined('WP_DEBUG') && WP_DEBUG) {
213 }
214 }
215
216 // Default implementations that can be overridden
217 public function handleWebhook(array $data): array
218 {
219 return ['success' => true];
220 }
221
222 public function processRefund(string $transactionId, float $amount): array
223 {
224 return [
225 'success' => false,
226 'error' => __('Refunds not supported by this gateway', 'yatra'),
227 ];
228 }
229
230 /**
231 * Check if gateway supports tokenization
232 */
233 public function supportsTokenization(): bool
234 {
235 return in_array('tokenization', $this->supports) || in_array('recurring', $this->supports);
236 }
237
238 /**
239 * Check if gateway supports recurring payments
240 */
241 public function supportsRecurring(): bool
242 {
243 return in_array('recurring', $this->supports, true);
244 }
245
246 /**
247 * Create a customer (default: not supported)
248 */
249 public function createCustomer(array $customerData): array
250 {
251 return [
252 'success' => false,
253 'error' => __('Customer creation not supported by this gateway', 'yatra'),
254 ];
255 }
256
257 /**
258 * Save payment method (default: not supported)
259 */
260 public function savePaymentMethod(string $customerId, array $paymentMethodData): array
261 {
262 return [
263 'success' => false,
264 'error' => __('Saving payment methods not supported by this gateway', 'yatra'),
265 ];
266 }
267
268 /**
269 * Charge saved payment method (default: not supported)
270 */
271 public function chargePaymentMethod(string $customerId, string $paymentMethodId, array $paymentData): array
272 {
273 return [
274 'success' => false,
275 'error' => __('Charging saved payment methods not supported by this gateway', 'yatra'),
276 ];
277 }
278
279 /**
280 * Delete payment method (default: not supported)
281 */
282 public function deletePaymentMethod(string $paymentMethodId): array
283 {
284 return [
285 'success' => false,
286 'error' => __('Deleting payment methods not supported by this gateway', 'yatra'),
287 ];
288 }
289
290 /**
291 * Get customer's payment methods (default: not supported)
292 */
293 public function getPaymentMethods(string $customerId): array
294 {
295 return [];
296 }
297 }
298
299