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, |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
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 | // Billing and shipping details. |
||||||||
88 | $shipping_address = $payment->get_shipping_address(); |
||||||||
0 ignored issues
–
show
The method
get_shipping_address() does not exist on Pronamic\WordPress\Pay\Payments\Payment .
(
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. ![]() |
|||||||||
89 | $billing_address = $payment->get_billing_address(); |
||||||||
0 ignored issues
–
show
The method
get_billing_address() does not exist on Pronamic\WordPress\Pay\Payments\Payment .
(
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. ![]() |
|||||||||
90 | |||||||||
91 | $shipping_detail = new Address(); |
||||||||
92 | $shipping_detail->set_first_name( $shipping_address->get_name()->get_first_name() ); |
||||||||
93 | $shipping_detail->set_middle_name( $shipping_address->get_name()->get_middle_name() ); |
||||||||
94 | $shipping_detail->set_last_name( $shipping_address->get_name()->get_last_name() ); |
||||||||
95 | $shipping_detail->set_street( $shipping_address->get_street_name() ); |
||||||||
96 | $shipping_detail->set_house_number( $shipping_address->get_house_number() ); |
||||||||
97 | $shipping_detail->set_house_number_addition( $shipping_address->get_house_number_addition() ); |
||||||||
98 | $shipping_detail->set_postal_code( $shipping_address->get_postal_code() ); |
||||||||
99 | $shipping_detail->set_city( $shipping_address->get_city() ); |
||||||||
100 | $shipping_detail->set_country_code( $shipping_address->get_country_code() ); |
||||||||
101 | |||||||||
102 | $billing_detail = new Address(); |
||||||||
103 | $billing_detail->set_first_name( $billing_address->get_name()->get_first_name() ); |
||||||||
104 | $billing_detail->set_middle_name( $billing_address->get_name()->get_middle_name() ); |
||||||||
105 | $billing_detail->set_last_name( $billing_address->get_name()->get_last_name() ); |
||||||||
106 | $billing_detail->set_street( $billing_address->get_street_name() ); |
||||||||
107 | $billing_detail->set_house_number( $billing_address->get_house_number() ); |
||||||||
108 | $billing_detail->set_house_number_addition( $billing_address->get_house_number_addition() ); |
||||||||
109 | $billing_detail->set_postal_code( $billing_address->get_postal_code() ); |
||||||||
110 | $billing_detail->set_city( $billing_address->get_city() ); |
||||||||
111 | $billing_detail->set_country_code( $billing_address->get_country_code() ); |
||||||||
112 | |||||||||
113 | // Customer information. |
||||||||
114 | $customer_information = new CustomerInformation(); |
||||||||
115 | $customer_information->set_email_address( $payment->get_contact()->get_email() ); |
||||||||
0 ignored issues
–
show
The method
get_contact() does not exist on Pronamic\WordPress\Pay\Payments\Payment . Did you maybe mean get_country() ?
(
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. ![]() |
|||||||||
116 | $customer_information->set_telephone_number( $payment->get_contact()->get_phone() ); |
||||||||
117 | |||||||||
118 | // Payment brand. |
||||||||
119 | $payment_brand = PaymentBrands::transform( $payment->get_method() ); |
||||||||
120 | |||||||||
121 | // New order. |
||||||||
122 | $order = new Order( |
||||||||
123 | $merchant_order_id, |
||||||||
124 | new Money( |
||||||||
125 | $payment->get_currency(), |
||||||||
0 ignored issues
–
show
$payment->get_currency() of type Pronamic\WordPress\Money\Currency is incompatible with the type string expected by parameter $currency of Pronamic\WordPress\Pay\G...a2\Money::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
126 | Core_Util::amount_to_cents( $payment->get_amount()->get_amount() ) |
||||||||
127 | ), |
||||||||
128 | $payment->get_return_url() |
||||||||
129 | ); |
||||||||
130 | |||||||||
131 | $order->set_description( $payment->get_description() ); |
||||||||
132 | $order->set_language( $payment->get_contact()->get_language() ); |
||||||||
133 | $order->set_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. ![]() The method
set_items() does not exist on Pronamic\WordPress\Pay\Gateways\OmniKassa2\Order .
(
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. ![]() |
|||||||||
134 | $order->set_shipping_detail( $shipping_detail ); |
||||||||
135 | $order->set_billing_detail( $billing_detail ); |
||||||||
136 | $order->set_customer_information( $customer_information ); |
||||||||
137 | $order->set_payment_brand( $payment_brand ); |
||||||||
138 | |||||||||
139 | if ( null !== $payment_brand ) { |
||||||||
140 | // Payment brand force should only be set if payment brand is not empty. |
||||||||
141 | $order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE ); |
||||||||
142 | } |
||||||||
143 | |||||||||
144 | // Maybe update access token. |
||||||||
145 | $this->maybe_update_access_token(); |
||||||||
146 | |||||||||
147 | // Handle errors. |
||||||||
148 | if ( $this->get_client_error() ) { |
||||||||
149 | return; |
||||||||
150 | } |
||||||||
151 | |||||||||
152 | // Announce order. |
||||||||
153 | $result = $this->client->order_announce( $this->config, $order ); |
||||||||
154 | |||||||||
155 | // Handle errors. |
||||||||
156 | if ( $this->get_client_error() ) { |
||||||||
157 | return; |
||||||||
158 | } |
||||||||
159 | |||||||||
160 | if ( $result ) { |
||||||||
161 | $payment->set_action_url( $result->redirectUrl ); |
||||||||
162 | } |
||||||||
163 | } |
||||||||
164 | |||||||||
165 | /** |
||||||||
166 | * Update status of the specified payment. |
||||||||
167 | * |
||||||||
168 | * @param Payment $payment Payment. |
||||||||
169 | */ |
||||||||
170 | public function update_status( Payment $payment ) { |
||||||||
171 | if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok. |
||||||||
172 | return; |
||||||||
173 | } |
||||||||
174 | |||||||||
175 | $parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok. |
||||||||
176 | |||||||||
177 | // Note. |
||||||||
178 | $note_values = array( |
||||||||
179 | 'order_id' => $parameters->get_order_id(), |
||||||||
180 | 'status' => $parameters->get_status(), |
||||||||
181 | 'signature' => $parameters->get_signature(), |
||||||||
182 | 'valid' => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false', |
||||||||
183 | ); |
||||||||
184 | |||||||||
185 | $note = ''; |
||||||||
186 | |||||||||
187 | $note .= '<p>'; |
||||||||
188 | $note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' ); |
||||||||
189 | $note .= '</p>'; |
||||||||
190 | |||||||||
191 | $note .= '<dl>'; |
||||||||
192 | |||||||||
193 | foreach ( $note_values as $key => $value ) { |
||||||||
194 | $note .= sprintf( '<dt>%s</dt>', esc_html( $key ) ); |
||||||||
195 | $note .= sprintf( '<dd>%s</dd>', esc_html( $value ) ); |
||||||||
196 | } |
||||||||
197 | |||||||||
198 | $note .= '</dl>'; |
||||||||
199 | |||||||||
200 | $payment->add_note( $note ); |
||||||||
201 | |||||||||
202 | // Validate. |
||||||||
203 | if ( ! $parameters->is_valid( $this->config->signing_key ) ) { |
||||||||
204 | return; |
||||||||
205 | } |
||||||||
206 | |||||||||
207 | // Status. |
||||||||
208 | $payment->set_status( Statuses::transform( $parameters->get_status() ) ); |
||||||||
209 | } |
||||||||
210 | |||||||||
211 | /** |
||||||||
212 | * Handle notification. |
||||||||
213 | * |
||||||||
214 | * @param Notification $notification Notification. |
||||||||
215 | * |
||||||||
216 | * @return void |
||||||||
217 | */ |
||||||||
218 | public function handle_notification( Notification $notification ) { |
||||||||
219 | if ( ! $notification->is_valid( $this->config->signing_key ) ) { |
||||||||
220 | return; |
||||||||
221 | } |
||||||||
222 | |||||||||
223 | switch ( $notification->get_event_name() ) { |
||||||||
224 | case 'merchant.order.status.changed': |
||||||||
225 | $this->handle_merchant_order_status_changed( $notification ); |
||||||||
226 | } |
||||||||
227 | } |
||||||||
228 | |||||||||
229 | /** |
||||||||
230 | * Handle `merchant.order.status.changed` event. |
||||||||
231 | * |
||||||||
232 | * @param Notification $notification Notification. |
||||||||
233 | * |
||||||||
234 | * @return void |
||||||||
235 | */ |
||||||||
236 | private function handle_merchant_order_status_changed( Notification $notification ) { |
||||||||
237 | do { |
||||||||
238 | $order_results = $this->client->get_order_results( $notification->get_authentication() ); |
||||||||
239 | |||||||||
240 | if ( ! $order_results || $order_results->is_valid( $this->config->signing_key ) ) { |
||||||||
241 | return; |
||||||||
242 | } |
||||||||
243 | |||||||||
244 | foreach ( $order_results as $order_result ) { |
||||||||
245 | $payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() ); |
||||||||
246 | |||||||||
247 | if ( empty( $payment ) ) { |
||||||||
248 | continue; |
||||||||
249 | } |
||||||||
250 | |||||||||
251 | $payment->set_transaction_id( $order_result->get_omnikassa_order_id() ); |
||||||||
252 | $payment->set_status( Statuses::transform( $order_result->get_order_status() ) ); |
||||||||
253 | |||||||||
254 | // Note. |
||||||||
255 | $note = ''; |
||||||||
256 | |||||||||
257 | $note .= '<p>'; |
||||||||
258 | $note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' ); |
||||||||
259 | $note .= '</p>'; |
||||||||
260 | $note .= '<pre>'; |
||||||||
261 | $note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT ); |
||||||||
262 | $note .= '</pre>'; |
||||||||
263 | |||||||||
264 | $payment->add_note( $note ); |
||||||||
265 | |||||||||
266 | $payment->save(); |
||||||||
267 | } |
||||||||
268 | } while ( $order_results->more_available() ); |
||||||||
269 | } |
||||||||
270 | |||||||||
271 | /** |
||||||||
272 | * Maybe update access token. |
||||||||
273 | * |
||||||||
274 | * @return void |
||||||||
275 | */ |
||||||||
276 | private function maybe_update_access_token() { |
||||||||
277 | if ( $this->config->is_access_token_valid() ) { |
||||||||
278 | return; |
||||||||
279 | } |
||||||||
280 | |||||||||
281 | $data = $this->client->get_access_token_data(); |
||||||||
282 | |||||||||
283 | if ( ! is_object( $data ) ) { |
||||||||
284 | return; |
||||||||
285 | } |
||||||||
286 | |||||||||
287 | $this->config->access_token = $data->token; |
||||||||
288 | $this->config->access_token_valid_until = $data->validUntil; |
||||||||
289 | |||||||||
290 | update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token ); |
||||||||
0 ignored issues
–
show
|
|||||||||
291 | update_post_meta( |
||||||||
292 | $this->config->post_id, |
||||||||
293 | '_pronamic_gateway_omnikassa_2_access_token_valid_until', |
||||||||
294 | $data->validUntil |
||||||||
295 | ); |
||||||||
296 | } |
||||||||
297 | |||||||||
298 | /** |
||||||||
299 | * Get client error. |
||||||||
300 | * |
||||||||
301 | * @return \WP_Error|bool |
||||||||
302 | */ |
||||||||
303 | private function get_client_error() { |
||||||||
304 | $error = $this->client->get_error(); |
||||||||
305 | |||||||||
306 | if ( is_wp_error( $error ) ) { |
||||||||
307 | $this->error = $error; |
||||||||
308 | |||||||||
309 | return $error; |
||||||||
310 | } |
||||||||
311 | |||||||||
312 | return false; |
||||||||
313 | } |
||||||||
314 | } |
||||||||
315 |