PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.9
Yatra – Travel Booking & Tour Operator Software v3.0.2.9
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 / Controllers / PaymentController.php

PaymentController.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.9, at app/Controllers/PaymentController.php

204 lines 5.9 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\Controllers;
6
7 use WP_REST_Request;
8 use WP_REST_Response;
9 use WP_Error;
10 use Yatra\Services\PaymentService;
11
12 /**
13 * Payment REST API Controller
14 *
15 * Handles payment record operations (listing, viewing, managing payments)
16 */
17 class PaymentController extends BaseController
18 {
19 /**
20 * REST API namespace
21 */
22 protected string $namespace = 'yatra/v1';
23
24 /**
25 * Payment service instance
26 */
27 private PaymentService $paymentService;
28
29 /**
30 * Constructor - Initialize services
31 */
32 public function __construct()
33 {
34 $this->paymentService = new PaymentService();
35 }
36
37 /**
38 * Register REST API routes
39 */
40 public function register_routes(): void
41 {
42 register_rest_route($this->namespace, '/payments/stats', [
43 [
44 'methods' => 'GET',
45 'callback' => [$this, 'getPaymentStats'],
46 'permission_callback' => [$this, 'checkAdminPermission'],
47 ],
48 ]);
49
50 // List all payments
51 register_rest_route($this->namespace, '/payments', [
52 [
53 'methods' => 'GET',
54 'callback' => [$this, 'getPayments'],
55 'permission_callback' => [$this, 'checkAdminPermission'],
56 ],
57 [
58 'methods' => 'POST',
59 'callback' => [$this, 'createPayment'],
60 'permission_callback' => [$this, 'checkAdminPermission'],
61 ]
62 ]);
63
64 // Get single payment
65 register_rest_route($this->namespace, '/payments/(?P<id>\d+)', [
66 [
67 'methods' => 'GET',
68 'callback' => [$this, 'getPayment'],
69 'permission_callback' => [$this, 'checkAdminPermission'],
70 ],
71 [
72 'methods' => 'PUT',
73 'callback' => [$this, 'updatePayment'],
74 'permission_callback' => [$this, 'checkAdminPermission'],
75 ],
76 [
77 'methods' => 'DELETE',
78 'callback' => [$this, 'deletePayment'],
79 'permission_callback' => [$this, 'checkAdminPermission'],
80 ]
81 ]);
82 }
83
84 /**
85 * Check admin permission (align with bookings list — shop managers may only have yatra caps).
86 */
87 public function checkAdminPermission(): bool
88 {
89 if (current_user_can('yatra_view_bookings')) {
90 return true;
91 }
92
93 return current_user_can('manage_options');
94 }
95
96 /**
97 * GET /payments/stats - Counts per status for admin toolbar
98 */
99 public function getPaymentStats(WP_REST_Request $request): WP_REST_Response
100 {
101 $counts = $this->paymentService->getAdminStatusCounts();
102
103 return new WP_REST_Response($counts, 200);
104 }
105
106 /**
107 * GET /payments - List all payments
108 */
109 public function getPayments(WP_REST_Request $request): WP_REST_Response
110 {
111 $filters = [
112 'page' => (int) ($request->get_param('page') ?: 1),
113 'per_page' => (int) ($request->get_param('per_page') ?: 20),
114 'booking_id' => (int) $request->get_param('booking_id'),
115 'status' => $request->get_param('status') ?: '',
116 'gateway' => $request->get_param('gateway') ?: '',
117 'search' => $request->get_param('search') ?: '',
118 'date_from' => $request->get_param('date_from') ?: '',
119 'date_to' => $request->get_param('date_to') ?: '',
120 ];
121
122 $result = $this->paymentService->getPayments($filters);
123
124 return new WP_REST_Response([
125 'data' => $result['data'],
126 'total' => $result['total'],
127 'page' => $filters['page'],
128 'per_page' => $filters['per_page'],
129 'total_pages' => ceil($result['total'] / $filters['per_page']),
130 ], 200);
131 }
132
133 /**
134 * GET /payments/{id} - Get single payment
135 */
136 public function getPayment(WP_REST_Request $request)
137 {
138 $id = (int) $request->get_param('id');
139 $payment = $this->paymentService->getPayment($id);
140
141 if (!$payment) {
142 return new WP_Error('payment_not_found', 'Payment not found', ['status' => 404]);
143 }
144
145 return new WP_REST_Response($payment, 200);
146 }
147
148 /**
149 * POST /payments - Create payment
150 */
151 public function createPayment(WP_REST_Request $request)
152 {
153 $data = $request->get_json_params();
154
155 try {
156 $payment = $this->paymentService->createPayment($data);
157 return new WP_REST_Response($payment, 201);
158 } catch (\Exception $e) {
159 return new WP_Error('payment_creation_failed', $e->getMessage(), ['status' => 400]);
160 }
161 }
162
163 /**
164 * PUT /payments/{id} - Update payment
165 */
166 public function updatePayment(WP_REST_Request $request)
167 {
168 $id = (int) $request->get_param('id');
169 $data = $request->get_json_params();
170
171 try {
172 $payment = $this->paymentService->updatePayment($id, $data);
173
174 if (!$payment) {
175 return new WP_Error('payment_not_found', 'Payment not found', ['status' => 404]);
176 }
177
178 return new WP_REST_Response($payment, 200);
179 } catch (\Exception $e) {
180 return new WP_Error('payment_update_failed', $e->getMessage(), ['status' => 400]);
181 }
182 }
183
184 /**
185 * DELETE /payments/{id} - Delete payment
186 */
187 public function deletePayment(WP_REST_Request $request)
188 {
189 $id = (int) $request->get_param('id');
190
191 try {
192 $result = $this->paymentService->deletePayment($id);
193
194 if (!$result) {
195 return new WP_Error('payment_not_found', 'Payment not found', ['status' => 404]);
196 }
197
198 return new WP_REST_Response(['success' => true], 200);
199 } catch (\Exception $e) {
200 return new WP_Error('payment_deletion_failed', $e->getMessage(), ['status' => 400]);
201 }
202 }
203 }
204