1 | <?php |
||||
2 | /** |
||||
3 | * Gateway |
||||
4 | * |
||||
5 | * @author Pronamic <[email protected]> |
||||
6 | * @copyright 2005-2018 Pronamic |
||||
7 | * @license GPL-3.0-or-later |
||||
8 | * @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
||||
9 | */ |
||||
10 | |||||
11 | namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
||||
12 | |||||
13 | use Pronamic\WordPress\Pay\Core\Util as Core_Util; |
||||
14 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||||
15 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||||
16 | use Pronamic\WordPress\Pay\Payments\Payment; |
||||
17 | |||||
18 | /** |
||||
19 | * Gateway |
||||
20 | * |
||||
21 | * @author Remco Tolsma |
||||
22 | * @version 2.0.2 |
||||
23 | * @since 1.0.0 |
||||
24 | */ |
||||
25 | class Gateway extends Core_Gateway { |
||||
26 | /** |
||||
27 | * Client. |
||||
28 | * |
||||
29 | * @var Client |
||||
30 | */ |
||||
31 | private $client; |
||||
32 | |||||
33 | /** |
||||
34 | * Constructs and initializes an OmniKassa 2.0 gateway. |
||||
35 | * |
||||
36 | * @param Config $config Config. |
||||
37 | */ |
||||
38 | public function __construct( Config $config ) { |
||||
39 | parent::__construct( $config ); |
||||
40 | |||||
41 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||||
42 | |||||
43 | // Client. |
||||
44 | $this->client = new Client(); |
||||
45 | |||||
46 | $url = Client::URL_PRODUCTION; |
||||
47 | |||||
48 | if ( self::MODE_TEST === $config->mode ) { |
||||
49 | $url = Client::URL_SANDBOX; |
||||
50 | } |
||||
51 | |||||
52 | $this->client->set_url( $url ); |
||||
53 | $this->client->set_refresh_token( $config->refresh_token ); |
||||
54 | $this->client->set_signing_key( $config->signing_key ); |
||||
55 | } |
||||
56 | |||||
57 | /** |
||||
58 | * Get supported payment methods. |
||||
59 | * |
||||
60 | * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods() |
||||
61 | * @return array |
||||
62 | */ |
||||
63 | public function get_supported_payment_methods() { |
||||
64 | return array( |
||||
65 | PaymentMethods::AFTERPAY, |
||||
66 | PaymentMethods::BANCONTACT, |
||||
67 | PaymentMethods::CREDIT_CARD, |
||||
68 | PaymentMethods::IDEAL, |
||||
69 | PaymentMethods::MAESTRO, |
||||
70 | PaymentMethods::PAYPAL, |
||||
71 | ); |
||||
72 | } |
||||
73 | |||||
74 | /** |
||||
75 | * Start. |
||||
76 | * |
||||
77 | * @see Core_Gateway::start() |
||||
78 | * |
||||
79 | * @param Payment $payment Payment. |
||||
80 | */ |
||||
81 | public function start( Payment $payment ) { |
||||
82 | // Merchant order ID. |
||||
83 | $merchant_order_id = $payment->format_string( $this->config->order_id ); |
||||
84 | |||||
85 | $payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id ); |
||||
86 | |||||
87 | // Shipping address. |
||||
88 | $shipping_address = $payment->get_shipping_address(); |
||||
89 | |||||
90 | if ( null !== $shipping_address ) { |
||||
91 | $shipping_detail = new Address(); |
||||
92 | |||||
93 | $name = $shipping_address->get_name(); |
||||
94 | |||||
95 | if ( null !== $name ) { |
||||
96 | $shipping_detail->set_first_name( $name->get_first_name() ); |
||||
97 | $shipping_detail->set_middle_name( $name->get_middle_name() ); |
||||
98 | $shipping_detail->set_last_name( $name->get_last_name() ); |
||||
99 | } |
||||
100 | |||||
101 | $shipping_detail->set_street( $shipping_address->get_street_name() ); |
||||
102 | $shipping_detail->set_house_number( $shipping_address->get_house_number() ); |
||||
103 | $shipping_detail->set_house_number_addition( $shipping_address->get_house_number_addition() ); |
||||
104 | $shipping_detail->set_postal_code( $shipping_address->get_postal_code() ); |
||||
105 | $shipping_detail->set_city( $shipping_address->get_city() ); |
||||
106 | $shipping_detail->set_country_code( $shipping_address->get_country_code() ); |
||||
107 | } |
||||
108 | |||||
109 | // Billing address. |
||||
110 | $billing_address = $payment->get_billing_address(); |
||||
111 | |||||
112 | if ( null !== $billing_address ) { |
||||
113 | $billing_detail = new Address(); |
||||
114 | |||||
115 | $name = $billing_address->get_name(); |
||||
116 | |||||
117 | if ( null !== $name ) { |
||||
118 | $billing_detail->set_first_name( $name->get_first_name() ); |
||||
119 | $billing_detail->set_middle_name( $name->get_middle_name() ); |
||||
120 | $billing_detail->set_last_name( $name->get_last_name() ); |
||||
121 | } |
||||
122 | |||||
123 | $billing_detail->set_street( $billing_address->get_street_name() ); |
||||
124 | $billing_detail->set_house_number( $billing_address->get_house_number() ); |
||||
125 | $billing_detail->set_house_number_addition( $billing_address->get_house_number_addition() ); |
||||
126 | $billing_detail->set_postal_code( $billing_address->get_postal_code() ); |
||||
127 | $billing_detail->set_city( $billing_address->get_city() ); |
||||
128 | $billing_detail->set_country_code( $billing_address->get_country_code() ); |
||||
129 | } |
||||
130 | |||||
131 | // Customer information. |
||||
132 | $customer = $payment->get_customer(); |
||||
133 | |||||
134 | if ( null !== $customer ) { |
||||
135 | $customer_information = new CustomerInformation(); |
||||
136 | |||||
137 | $customer_information->set_email_address( $customer->get_email() ); |
||||
138 | $customer_information->set_telephone_number( $customer->get_phone() ); |
||||
139 | } |
||||
140 | |||||
141 | // Payment brand. |
||||
142 | $payment_brand = PaymentBrands::transform( $payment->get_method() ); |
||||
143 | |||||
144 | // New order. |
||||
145 | $order = new Order( |
||||
146 | $merchant_order_id, |
||||
147 | new Money( |
||||
148 | $payment->get_currency(), |
||||
149 | $payment->get_amount()->get_cents() |
||||
0 ignored issues
–
show
|
|||||
150 | ), |
||||
151 | $payment->get_return_url() |
||||
152 | ); |
||||
153 | |||||
154 | $order->set_description( $payment->get_description() ); |
||||
155 | $order->set_language( $payment->get_customer()->get_language() ); |
||||
156 | $order->set_order_items( $payment->get_order_items() ); |
||||
0 ignored issues
–
show
The method
get_order_items() does not exist on Pronamic\WordPress\Pay\Payments\Payment . Did you maybe mean get_order_id() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
157 | $order->set_shipping_detail( $shipping_detail ); |
||||
158 | $order->set_billing_detail( $billing_detail ); |
||||
159 | $order->set_customer_information( $customer_information ); |
||||
160 | $order->set_payment_brand( $payment_brand ); |
||||
161 | |||||
162 | if ( null !== $payment_brand ) { |
||||
163 | // Payment brand force should only be set if payment brand is not empty. |
||||
164 | $order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE ); |
||||
165 | } |
||||
166 | |||||
167 | // Maybe update access token. |
||||
168 | $this->maybe_update_access_token(); |
||||
169 | |||||
170 | // Handle errors. |
||||
171 | if ( $this->get_client_error() ) { |
||||
172 | return; |
||||
173 | } |
||||
174 | |||||
175 | // Announce order. |
||||
176 | $result = $this->client->order_announce( $this->config, $order ); |
||||
177 | |||||
178 | // Handle errors. |
||||
179 | if ( $this->get_client_error() ) { |
||||
180 | return; |
||||
181 | } |
||||
182 | |||||
183 | if ( $result ) { |
||||
184 | $payment->set_action_url( $result->redirectUrl ); |
||||
185 | } |
||||
186 | } |
||||
187 | |||||
188 | /** |
||||
189 | * Update status of the specified payment. |
||||
190 | * |
||||
191 | * @param Payment $payment Payment. |
||||
192 | */ |
||||
193 | public function update_status( Payment $payment ) { |
||||
194 | if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok. |
||||
195 | return; |
||||
196 | } |
||||
197 | |||||
198 | $parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok. |
||||
199 | |||||
200 | // Note. |
||||
201 | $note_values = array( |
||||
202 | 'order_id' => $parameters->get_order_id(), |
||||
203 | 'status' => $parameters->get_status(), |
||||
204 | 'signature' => $parameters->get_signature(), |
||||
205 | 'valid' => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false', |
||||
206 | ); |
||||
207 | |||||
208 | $note = ''; |
||||
209 | |||||
210 | $note .= '<p>'; |
||||
211 | $note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' ); |
||||
212 | $note .= '</p>'; |
||||
213 | |||||
214 | $note .= '<dl>'; |
||||
215 | |||||
216 | foreach ( $note_values as $key => $value ) { |
||||
217 | $note .= sprintf( '<dt>%s</dt>', esc_html( $key ) ); |
||||
218 | $note .= sprintf( '<dd>%s</dd>', esc_html( $value ) ); |
||||
219 | } |
||||
220 | |||||
221 | $note .= '</dl>'; |
||||
222 | |||||
223 | $payment->add_note( $note ); |
||||
224 | |||||
225 | // Validate. |
||||
226 | if ( ! $parameters->is_valid( $this->config->signing_key ) ) { |
||||
227 | return; |
||||
228 | } |
||||
229 | |||||
230 | // Status. |
||||
231 | $payment->set_status( Statuses::transform( $parameters->get_status() ) ); |
||||
232 | } |
||||
233 | |||||
234 | /** |
||||
235 | * Handle notification. |
||||
236 | * |
||||
237 | * @param Notification $notification Notification. |
||||
238 | * |
||||
239 | * @return void |
||||
240 | */ |
||||
241 | public function handle_notification( Notification $notification ) { |
||||
242 | if ( ! $notification->is_valid( $this->config->signing_key ) ) { |
||||
243 | return; |
||||
244 | } |
||||
245 | |||||
246 | switch ( $notification->get_event_name() ) { |
||||
247 | case 'merchant.order.status.changed': |
||||
248 | $this->handle_merchant_order_status_changed( $notification ); |
||||
249 | } |
||||
250 | } |
||||
251 | |||||
252 | /** |
||||
253 | * Handle `merchant.order.status.changed` event. |
||||
254 | * |
||||
255 | * @param Notification $notification Notification. |
||||
256 | * |
||||
257 | * @return void |
||||
258 | */ |
||||
259 | private function handle_merchant_order_status_changed( Notification $notification ) { |
||||
260 | do { |
||||
261 | $order_results = $this->client->get_order_results( $notification->get_authentication() ); |
||||
262 | |||||
263 | if ( ! $order_results || $order_results->is_valid( $this->config->signing_key ) ) { |
||||
264 | return; |
||||
265 | } |
||||
266 | |||||
267 | foreach ( $order_results as $order_result ) { |
||||
268 | $payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() ); |
||||
269 | |||||
270 | if ( empty( $payment ) ) { |
||||
271 | continue; |
||||
272 | } |
||||
273 | |||||
274 | $payment->set_transaction_id( $order_result->get_omnikassa_order_id() ); |
||||
275 | $payment->set_status( Statuses::transform( $order_result->get_order_status() ) ); |
||||
276 | |||||
277 | // Note. |
||||
278 | $note = ''; |
||||
279 | |||||
280 | $note .= '<p>'; |
||||
281 | $note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' ); |
||||
282 | $note .= '</p>'; |
||||
283 | $note .= '<pre>'; |
||||
284 | $note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT ); |
||||
285 | $note .= '</pre>'; |
||||
286 | |||||
287 | $payment->add_note( $note ); |
||||
288 | |||||
289 | $payment->save(); |
||||
290 | } |
||||
291 | } while ( $order_results->more_available() ); |
||||
292 | } |
||||
293 | |||||
294 | /** |
||||
295 | * Maybe update access token. |
||||
296 | * |
||||
297 | * @return void |
||||
298 | */ |
||||
299 | private function maybe_update_access_token() { |
||||
300 | if ( $this->config->is_access_token_valid() ) { |
||||
301 | return; |
||||
302 | } |
||||
303 | |||||
304 | $data = $this->client->get_access_token_data(); |
||||
305 | |||||
306 | if ( ! is_object( $data ) ) { |
||||
307 | return; |
||||
308 | } |
||||
309 | |||||
310 | $this->config->access_token = $data->token; |
||||
311 | $this->config->access_token_valid_until = $data->validUntil; |
||||
312 | |||||
313 | update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token ); |
||||
314 | update_post_meta( |
||||
315 | $this->config->post_id, |
||||
316 | '_pronamic_gateway_omnikassa_2_access_token_valid_until', |
||||
317 | $data->validUntil |
||||
318 | ); |
||||
319 | } |
||||
320 | |||||
321 | /** |
||||
322 | * Get client error. |
||||
323 | * |
||||
324 | * @return \WP_Error|bool |
||||
325 | */ |
||||
326 | private function get_client_error() { |
||||
327 | $error = $this->client->get_error(); |
||||
328 | |||||
329 | if ( is_wp_error( $error ) ) { |
||||
330 | $this->error = $error; |
||||
331 | |||||
332 | return $error; |
||||
333 | } |
||||
334 | |||||
335 | return false; |
||||
336 | } |
||||
337 | } |
||||
338 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.