1 | <?php |
||||
2 | /** |
||||
3 | * Payments controller |
||||
4 | * |
||||
5 | * @author Pronamic <[email protected]> |
||||
6 | * @copyright 2005-2020 Pronamic |
||||
7 | * @license GPL-3.0-or-later |
||||
8 | * @package Pronamic\WordPress\Pay\Gateways\Adyen |
||||
9 | */ |
||||
10 | |||||
11 | namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
||||
12 | |||||
13 | use JsonSchema\Exception\ValidationException; |
||||
14 | use Pronamic\WordPress\Pay\Plugin; |
||||
15 | use WP_REST_Request; |
||||
16 | |||||
17 | /** |
||||
18 | * Payments result controller |
||||
19 | * |
||||
20 | * @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete |
||||
21 | * |
||||
22 | * @author Reüel van der Steege |
||||
23 | * @version 1.1.0 |
||||
24 | * @since 1.1.0 |
||||
25 | */ |
||||
26 | class PaymentsController { |
||||
27 | /** |
||||
28 | * Setup. |
||||
29 | * |
||||
30 | * @return void |
||||
31 | */ |
||||
32 | 5 | public function setup() { |
|||
33 | 5 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|||
34 | 5 | } |
|||
35 | |||||
36 | /** |
||||
37 | * REST API init. |
||||
38 | * |
||||
39 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
||||
40 | * @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
||||
41 | * |
||||
42 | * @return void |
||||
43 | */ |
||||
44 | 10 | public function rest_api_init() { |
|||
45 | // Register REST route `/payments//{payment_id}`. |
||||
46 | 10 | register_rest_route( |
|||
47 | 10 | Integration::REST_ROUTE_NAMESPACE, |
|||
48 | 10 | '/payments/(?P<payment_id>\d+)', |
|||
49 | array( |
||||
50 | 10 | 'methods' => 'POST', |
|||
51 | 10 | 'callback' => array( $this, 'rest_api_adyen_payments' ), |
|||
52 | 'args' => array( |
||||
53 | 'payment_id' => array( |
||||
54 | 10 | 'description' => __( 'Payment ID.', 'pronamic_ideal' ), |
|||
55 | 10 | 'type' => 'integer', |
|||
56 | ), |
||||
57 | ), |
||||
58 | ) |
||||
59 | ); |
||||
60 | |||||
61 | // Register REST route `/payments/details/{payment_id}`. |
||||
62 | 10 | register_rest_route( |
|||
63 | 10 | Integration::REST_ROUTE_NAMESPACE, |
|||
64 | 10 | '/payments/details/(?P<payment_id>\d+)', |
|||
65 | array( |
||||
66 | 10 | 'methods' => 'POST', |
|||
67 | 10 | 'callback' => array( $this, 'rest_api_adyen_payment_details' ), |
|||
68 | 'args' => array( |
||||
69 | 'payment_id' => array( |
||||
70 | 10 | 'description' => __( 'Payment ID.', 'pronamic_ideal' ), |
|||
71 | 10 | 'type' => 'integer', |
|||
72 | ), |
||||
73 | ), |
||||
74 | ) |
||||
75 | ); |
||||
76 | 10 | } |
|||
77 | |||||
78 | /** |
||||
79 | * REST API Adyen payments handler. |
||||
80 | * |
||||
81 | * @param WP_REST_Request $request Request. |
||||
82 | * @return object |
||||
83 | */ |
||||
84 | public function rest_api_adyen_payments( WP_REST_Request $request ) { |
||||
85 | $payment_id = $request->get_param( 'payment_id' ); |
||||
86 | |||||
87 | // Payment ID. |
||||
88 | if ( null === $payment_id ) { |
||||
89 | return new \WP_Error( |
||||
90 | 'pronamic-pay-adyen-no-payment-id', |
||||
91 | __( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' ) |
||||
92 | ); |
||||
93 | } |
||||
94 | |||||
95 | $payment = \get_pronamic_payment( $payment_id ); |
||||
96 | |||||
97 | if ( null === $payment ) { |
||||
98 | return new \WP_Error( |
||||
99 | 'pronamic-pay-adyen-payment-not-found', |
||||
100 | sprintf( |
||||
101 | /* translators: %s: payment ID */ |
||||
102 | __( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ), |
||||
103 | $payment_id |
||||
104 | ), |
||||
105 | $payment_id |
||||
106 | ); |
||||
107 | } |
||||
108 | |||||
109 | // State data. |
||||
110 | $data = \json_decode( $request->get_body() ); |
||||
111 | |||||
112 | if ( null === $data ) { |
||||
113 | return new \WP_Error( |
||||
114 | 'pronamic-pay-adyen-no-data', |
||||
115 | __( 'No data given in request body.', 'pronamic_ideal' ) |
||||
116 | ); |
||||
117 | } |
||||
118 | |||||
119 | // Gateway. |
||||
120 | $config_id = $payment->get_config_id(); |
||||
121 | |||||
122 | $gateway = Plugin::get_gateway( $config_id ); |
||||
123 | |||||
124 | if ( empty( $gateway ) ) { |
||||
125 | return new \WP_Error( |
||||
126 | 'pronamic-pay-adyen-gateway-not-found', |
||||
127 | sprintf( |
||||
128 | /* translators: %s: Gateway configuration ID */ |
||||
129 | __( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ), |
||||
130 | $config_id |
||||
131 | ), |
||||
132 | $config_id |
||||
133 | ); |
||||
134 | } |
||||
135 | |||||
136 | if ( ! isset( $gateway->client ) ) { |
||||
137 | return new \WP_Error( |
||||
138 | 'pronamic-pay-adyen-client-not-found', |
||||
139 | sprintf( |
||||
140 | /* translators: %s: Gateway configuration ID */ |
||||
141 | __( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ), |
||||
142 | $config_id |
||||
143 | ), |
||||
144 | $config_id |
||||
145 | ); |
||||
146 | } |
||||
147 | |||||
148 | // Create payment. |
||||
149 | if ( ! isset( $data->paymentMethod->type ) ) { |
||||
150 | return new \WP_Error( |
||||
151 | 'pronamic-pay-adyen-no-payment-method', |
||||
152 | __( 'No payment method given.', 'pronamic_ideal' ) |
||||
153 | ); |
||||
154 | } |
||||
155 | |||||
156 | $payment_method = PaymentMethod::from_object( $data->paymentMethod ); |
||||
157 | |||||
158 | try { |
||||
159 | $response = $gateway->create_payment( $payment, $payment_method ); |
||||
160 | } catch ( \Exception $e ) { |
||||
161 | return (object) array( |
||||
162 | 'error' => $e->getMessage(), |
||||
163 | ); |
||||
164 | } |
||||
165 | |||||
166 | // Update payment status based on response. |
||||
167 | PaymentResponseHelper::update_payment( $payment, $response ); |
||||
168 | |||||
169 | $result = array( |
||||
170 | 'resultCode' => $response->get_result_code(), |
||||
171 | ); |
||||
172 | |||||
173 | // Return action if available. |
||||
174 | $action = $response->get_action(); |
||||
175 | |||||
176 | if ( null !== $action ) { |
||||
177 | $result['action'] = $action->get_json(); |
||||
178 | } |
||||
179 | |||||
180 | // Return refusal reason if available. |
||||
181 | $refusal_reason = $response->get_refusal_reason(); |
||||
182 | |||||
183 | if ( null !== $refusal_reason ) { |
||||
184 | $result['refusalReason'] = $refusal_reason; |
||||
185 | } |
||||
186 | |||||
187 | return (object) $result; |
||||
188 | } |
||||
189 | |||||
190 | /** |
||||
191 | * REST API Adyen payment details handler. |
||||
192 | * |
||||
193 | * @param WP_REST_Request $request Request. |
||||
194 | * @return object |
||||
195 | */ |
||||
196 | public function rest_api_adyen_payment_details( WP_REST_Request $request ) { |
||||
197 | $payment_id = $request->get_param( 'payment_id' ); |
||||
198 | |||||
199 | // Payment ID. |
||||
200 | if ( null === $payment_id ) { |
||||
201 | return new \WP_Error( |
||||
202 | 'pronamic-pay-adyen-no-payment-id', |
||||
203 | __( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' ) |
||||
204 | ); |
||||
205 | } |
||||
206 | |||||
207 | $payment = \get_pronamic_payment( $payment_id ); |
||||
208 | |||||
209 | if ( null === $payment ) { |
||||
210 | return new \WP_Error( |
||||
211 | 'pronamic-pay-adyen-payment-not-found', |
||||
212 | sprintf( |
||||
213 | /* translators: %s: payment ID */ |
||||
214 | __( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ), |
||||
215 | $payment_id |
||||
216 | ), |
||||
217 | $payment_id |
||||
218 | ); |
||||
219 | } |
||||
220 | |||||
221 | // State data. |
||||
222 | $data = \json_decode( $request->get_body() ); |
||||
223 | |||||
224 | if ( null === $data ) { |
||||
225 | return new \WP_Error( |
||||
226 | 'pronamic-pay-adyen-no-data', |
||||
227 | __( 'No data given in request body.', 'pronamic_ideal' ) |
||||
228 | ); |
||||
229 | } |
||||
230 | |||||
231 | // Gateway. |
||||
232 | $config_id = $payment->get_config_id(); |
||||
233 | |||||
234 | $gateway = Plugin::get_gateway( $config_id ); |
||||
0 ignored issues
–
show
|
|||||
235 | |||||
236 | if ( empty( $gateway ) ) { |
||||
237 | return new \WP_Error( |
||||
238 | 'pronamic-pay-adyen-gateway-not-found', |
||||
239 | sprintf( |
||||
240 | /* translators: %s: Gateway configuration ID */ |
||||
241 | __( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ), |
||||
242 | $config_id |
||||
243 | ), |
||||
244 | $config_id |
||||
245 | ); |
||||
246 | } |
||||
247 | |||||
248 | if ( ! isset( $gateway->client ) ) { |
||||
249 | return new \WP_Error( |
||||
250 | 'pronamic-pay-adyen-client-not-found', |
||||
251 | sprintf( |
||||
252 | /* translators: %s: Gateway configuration ID */ |
||||
253 | __( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ), |
||||
254 | $config_id |
||||
255 | ), |
||||
256 | $config_id |
||||
257 | ); |
||||
258 | } |
||||
259 | |||||
260 | // Send additional details. |
||||
261 | if ( ! isset( $data->paymentMethod->type ) ) { |
||||
262 | return new \WP_Error( |
||||
263 | 'pronamic-pay-adyen-no-payment-method', |
||||
264 | __( 'No payment method given.', 'pronamic_ideal' ) |
||||
265 | ); |
||||
266 | } |
||||
267 | |||||
268 | // Payment details request. |
||||
269 | $payment_details_request = new PaymentDetailsRequest(); |
||||
0 ignored issues
–
show
The call to
Pronamic\WordPress\Pay\G...sRequest::__construct() has too few arguments starting with details .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||
270 | |||||
271 | $payment_details_request->set_payment_data( $data ); |
||||
272 | |||||
273 | try { |
||||
274 | $response = $gateway->send_payment_details( $payment, $payment_details_request ); |
||||
275 | |||||
276 | // Update payment status based on response. |
||||
277 | PaymentResponseHelper::update_payment( $payment, $response ); |
||||
278 | } catch ( \Exception $e ) { |
||||
279 | return (object) array( |
||||
280 | 'error' => $e->getMessage(), |
||||
281 | ); |
||||
282 | } |
||||
283 | |||||
284 | $result = array( |
||||
285 | 'resultCode' => $response->get_result_code(), |
||||
286 | ); |
||||
287 | |||||
288 | // Return action if available. |
||||
289 | $action = $response->get_action(); |
||||
290 | |||||
291 | if ( null !== $action ) { |
||||
292 | $result['action'] = $action->get_json(); |
||||
293 | } |
||||
294 | |||||
295 | // Return refusal reason if available. |
||||
296 | $refusal_reason = $response->get_refusal_reason(); |
||||
297 | |||||
298 | if ( null !== $refusal_reason ) { |
||||
299 | $result['refusalReason'] = $refusal_reason; |
||||
300 | } |
||||
301 | |||||
302 | return (object) $result; |
||||
303 | } |
||||
304 | } |
||||
305 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.