PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.6
Yatra – Travel Booking & Tour Operator Software v3.0.2.6
3.0.16 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 All 84 releases
yatra / app / PaymentGateways / AbstractPaymentGateway.php

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

279 lines 7.4 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 public function getIcon(): string
43 {
44 // If icon is a relative path (starts with /), convert to full URL
45 if (!empty($this->icon) && strpos($this->icon, 'http') !== 0) {
46 // Priority 1: Check if icon is directly specified (e.g., 'paypal.svg', 'stripe.svg')
47 if (strpos($this->icon, '.') !== false) {
48 // Icon has extension, check in payment-gateways folder
49 $iconPath = YATRA_PLUGIN_PATH . "assets/images/payment-gateways/{$this->icon}";
50 if (file_exists($iconPath)) {
51 return YATRA_PLUGIN_URL . "assets/images/payment-gateways/{$this->icon}";
52 }
53 }
54
55 // Priority 2: Try to derive icon name from gateway ID
56 $iconName = $this->id;
57
58
59 return YATRA_PLUGIN_URL . "assets/images/payment-gateways/{$iconName}.svg";
60
61 }
62 return $this->icon;
63 }
64
65 public function isOffline(): bool
66 {
67 return $this->isOffline;
68 }
69
70 public function getSandboxUrl(): string
71 {
72 return $this->sandboxUrl;
73 }
74
75 public function getSupports(): array
76 {
77 return $this->supports;
78 }
79
80 public function getConfig(): array
81 {
82 return $this->config;
83 }
84
85 /**
86 * Load configuration from database
87 */
88 protected function loadConfig(): void
89 {
90 $allConfigs = get_option('yatra_gateway_configs', []);
91 if (is_string($allConfigs)) {
92 $allConfigs = maybe_unserialize($allConfigs);
93 }
94 $defaults = $this->getDefaultConfig();
95 $stored = $allConfigs[$this->id] ?? null;
96 if (!is_array($stored) || $stored === []) {
97 $this->config = $defaults;
98
99 return;
100 }
101 $this->config = array_merge($defaults, $stored);
102 }
103
104 /**
105 * Get default configuration
106 */
107 protected function getDefaultConfig(): array
108 {
109 // PayLater gateway is enabled by default, others are disabled
110 $defaults = ['enabled' => $this->id === 'pay_later'];
111 foreach ($this->getConfigFields() as $field) {
112 $defaults[$field['id']] = $field['default'] ?? '';
113 }
114 // Ensure 'enabled' is always present
115 if (!isset($defaults['enabled'])) {
116 $defaults['enabled'] = $this->id === 'pay_later';
117 }
118 return $defaults;
119 }
120
121 public function saveConfig(array $config): bool
122 {
123 $allConfigs = get_option('yatra_gateway_configs', []);
124 if (is_string($allConfigs)) {
125 $allConfigs = maybe_unserialize($allConfigs);
126 }
127 $allConfigs[$this->id] = array_merge($this->config, $config);
128 return update_option('yatra_gateway_configs', $allConfigs);
129 }
130
131 public function isAvailable(): bool
132 {
133 return $this->isEnabled() && $this->isProperlyConfigured();
134 }
135
136 public function isProperlyConfigured(): bool
137 {
138 return true;
139 }
140
141 /**
142 * Check if gateway is enabled in settings
143 */
144 public function isEnabled(): bool
145 {
146 return !empty($this->config['enabled'] ?? false);
147 }
148
149 /**
150 * Get option key for this gateway
151 */
152 protected function getOptionKey(): string
153 {
154 return 'yatra_gateway_' . $this->id;
155 }
156
157 /**
158 * Make HTTP request
159 */
160 protected function makeRequest(string $url, array $options = []): array
161 {
162 $response = wp_remote_request($url, $options);
163
164 if (is_wp_error($response)) {
165 return [
166 'success' => false,
167 'error' => $response->get_error_message(),
168 ];
169 }
170
171 $body = wp_remote_retrieve_body($response);
172 $code = wp_remote_retrieve_response_code($response);
173
174 // Remove BOM (Byte Order Mark) that some APIs like Authorize.net return
175 $body = preg_replace('/^\xEF\xBB\xBF/', '', $body);
176
177 // Try to decode JSON
178 $decoded = json_decode($body, true);
179
180 return [
181 'success' => $code >= 200 && $code < 300,
182 'code' => $code,
183 'body' => $decoded !== null ? $decoded : $body,
184 ];
185 }
186
187 /**
188 * Log gateway activity
189 */
190 protected function log(string $message, array $context = []): void
191 {
192 if (defined('WP_DEBUG') && WP_DEBUG) {
193 }
194 }
195
196 // Default implementations that can be overridden
197 public function handleWebhook(array $data): array
198 {
199 return ['success' => true];
200 }
201
202 public function processRefund(string $transactionId, float $amount): array
203 {
204 return [
205 'success' => false,
206 'error' => __('Refunds not supported by this gateway', 'yatra'),
207 ];
208 }
209
210 /**
211 * Check if gateway supports tokenization
212 */
213 public function supportsTokenization(): bool
214 {
215 return in_array('tokenization', $this->supports) || in_array('recurring', $this->supports);
216 }
217
218 /**
219 * Check if gateway supports recurring payments
220 */
221 public function supportsRecurring(): bool
222 {
223 return in_array('recurring', $this->supports) || in_array('scheduled_payments', $this->supports);
224 }
225
226 /**
227 * Create a customer (default: not supported)
228 */
229 public function createCustomer(array $customerData): array
230 {
231 return [
232 'success' => false,
233 'error' => __('Customer creation not supported by this gateway', 'yatra'),
234 ];
235 }
236
237 /**
238 * Save payment method (default: not supported)
239 */
240 public function savePaymentMethod(string $customerId, array $paymentMethodData): array
241 {
242 return [
243 'success' => false,
244 'error' => __('Saving payment methods not supported by this gateway', 'yatra'),
245 ];
246 }
247
248 /**
249 * Charge saved payment method (default: not supported)
250 */
251 public function chargePaymentMethod(string $customerId, string $paymentMethodId, array $paymentData): array
252 {
253 return [
254 'success' => false,
255 'error' => __('Charging saved payment methods not supported by this gateway', 'yatra'),
256 ];
257 }
258
259 /**
260 * Delete payment method (default: not supported)
261 */
262 public function deletePaymentMethod(string $paymentMethodId): array
263 {
264 return [
265 'success' => false,
266 'error' => __('Deleting payment methods not supported by this gateway', 'yatra'),
267 ];
268 }
269
270 /**
271 * Get customer's payment methods (default: not supported)
272 */
273 public function getPaymentMethods(string $customerId): array
274 {
275 return [];
276 }
277 }
278
279