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
|
|
|
// New order. |
88
|
|
|
$order = new Order( |
89
|
|
|
$merchant_order_id, |
90
|
|
|
MoneyTransformer::transform( $payment->get_amount() ), |
91
|
|
|
$payment->get_return_url() |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
// Shipping address. |
95
|
|
|
$order->set_shipping_detail( AddressTransformer::transform( $payment->get_shipping_address() ) ); |
96
|
|
|
|
97
|
|
|
// Billing address. |
98
|
|
|
$order->set_billing_detail( AddressTransformer::transform( $payment->get_billing_address() ) ); |
99
|
|
|
|
100
|
|
|
// Customer information. |
101
|
|
|
$customer = $payment->get_customer(); |
102
|
|
|
|
103
|
|
|
if ( null !== $customer ) { |
104
|
|
|
$order->set_language( $customer->get_language() ); |
105
|
|
|
|
106
|
|
|
$customer_information = new CustomerInformation(); |
107
|
|
|
|
108
|
|
|
$customer_information->set_email_address( $customer->get_email() ); |
109
|
|
|
$customer_information->set_telephone_number( $customer->get_phone() ); |
110
|
|
|
|
111
|
|
|
$order->set_customer_information( $customer_information ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Payment brand. |
115
|
|
|
$payment_brand = PaymentBrands::transform( $payment->get_method() ); |
116
|
|
|
|
117
|
|
|
$order->set_payment_brand( $payment_brand ); |
118
|
|
|
|
119
|
|
|
if ( null !== $payment_brand ) { |
120
|
|
|
// Payment brand force should only be set if payment brand is not empty. |
121
|
|
|
$order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Description. |
125
|
|
|
$order->set_description( $payment->get_description() ); |
126
|
|
|
|
127
|
|
|
// Lines. |
128
|
|
|
if ( null !== $payment->get_lines() ) { |
129
|
|
|
$order_items = new OrderItems(); |
|
|
|
|
130
|
|
|
|
131
|
|
|
foreach ( $payment->get_lines() as $line ) { |
132
|
|
|
$item = new OrderItem( |
133
|
|
|
$line->get_name(), |
134
|
|
|
$line->get_quantity(), |
135
|
|
|
MoneyTransformer::transform( $line->get_total_amount() ), |
136
|
|
|
Category::transform( $line->get_type() ) |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$order_items->add_item( $item ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$order->set_order_items( $order_items ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Maybe update access token. |
146
|
|
|
$this->maybe_update_access_token(); |
147
|
|
|
|
148
|
|
|
// Handle errors. |
149
|
|
|
if ( $this->get_client_error() ) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Announce order. |
154
|
|
|
$result = $this->client->order_announce( $this->config, $order ); |
155
|
|
|
|
156
|
|
|
// Handle errors. |
157
|
|
|
if ( $this->get_client_error() ) { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ( $result ) { |
162
|
|
|
$payment->set_action_url( $result->redirectUrl ); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Update status of the specified payment. |
168
|
|
|
* |
169
|
|
|
* @param Payment $payment Payment. |
170
|
|
|
*/ |
171
|
|
|
public function update_status( Payment $payment ) { |
172
|
|
|
if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok. |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok. |
177
|
|
|
|
178
|
|
|
// Note. |
179
|
|
|
$note_values = array( |
180
|
|
|
'order_id' => $parameters->get_order_id(), |
181
|
|
|
'status' => $parameters->get_status(), |
182
|
|
|
'signature' => $parameters->get_signature(), |
183
|
|
|
'valid' => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false', |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$note = ''; |
187
|
|
|
|
188
|
|
|
$note .= '<p>'; |
189
|
|
|
$note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' ); |
190
|
|
|
$note .= '</p>'; |
191
|
|
|
|
192
|
|
|
$note .= '<dl>'; |
193
|
|
|
|
194
|
|
|
foreach ( $note_values as $key => $value ) { |
195
|
|
|
$note .= sprintf( '<dt>%s</dt>', esc_html( $key ) ); |
196
|
|
|
$note .= sprintf( '<dd>%s</dd>', esc_html( $value ) ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$note .= '</dl>'; |
200
|
|
|
|
201
|
|
|
$payment->add_note( $note ); |
202
|
|
|
|
203
|
|
|
// Validate. |
204
|
|
|
if ( ! $parameters->is_valid( $this->config->signing_key ) ) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Status. |
209
|
|
|
$payment->set_status( Statuses::transform( $parameters->get_status() ) ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Handle notification. |
214
|
|
|
* |
215
|
|
|
* @param Notification $notification Notification. |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function handle_notification( Notification $notification ) { |
220
|
|
|
if ( ! $notification->is_valid( $this->config->signing_key ) ) { |
221
|
|
|
return; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
switch ( $notification->get_event_name() ) { |
225
|
|
|
case 'merchant.order.status.changed': |
226
|
|
|
$this->handle_merchant_order_status_changed( $notification ); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Handle `merchant.order.status.changed` event. |
232
|
|
|
* |
233
|
|
|
* @param Notification $notification Notification. |
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
private function handle_merchant_order_status_changed( Notification $notification ) { |
238
|
|
|
do { |
239
|
|
|
$order_results = $this->client->get_order_results( $notification->get_authentication() ); |
240
|
|
|
|
241
|
|
|
if ( ! $order_results || $order_results->is_valid( $this->config->signing_key ) ) { |
242
|
|
|
return; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
foreach ( $order_results as $order_result ) { |
246
|
|
|
$payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() ); |
247
|
|
|
|
248
|
|
|
if ( empty( $payment ) ) { |
249
|
|
|
continue; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$payment->set_transaction_id( $order_result->get_omnikassa_order_id() ); |
253
|
|
|
$payment->set_status( Statuses::transform( $order_result->get_order_status() ) ); |
254
|
|
|
|
255
|
|
|
// Note. |
256
|
|
|
$note = ''; |
257
|
|
|
|
258
|
|
|
$note .= '<p>'; |
259
|
|
|
$note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' ); |
260
|
|
|
$note .= '</p>'; |
261
|
|
|
$note .= '<pre>'; |
262
|
|
|
$note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT ); |
263
|
|
|
$note .= '</pre>'; |
264
|
|
|
|
265
|
|
|
$payment->add_note( $note ); |
266
|
|
|
|
267
|
|
|
$payment->save(); |
268
|
|
|
} |
269
|
|
|
} while ( $order_results->more_available() ); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Maybe update access token. |
274
|
|
|
* |
275
|
|
|
* @return void |
276
|
|
|
*/ |
277
|
|
|
private function maybe_update_access_token() { |
278
|
|
|
if ( $this->config->is_access_token_valid() ) { |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$data = $this->client->get_access_token_data(); |
283
|
|
|
|
284
|
|
|
if ( ! is_object( $data ) ) { |
285
|
|
|
return; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->config->access_token = $data->token; |
289
|
|
|
$this->config->access_token_valid_until = $data->validUntil; |
290
|
|
|
|
291
|
|
|
update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token ); |
292
|
|
|
update_post_meta( |
293
|
|
|
$this->config->post_id, |
294
|
|
|
'_pronamic_gateway_omnikassa_2_access_token_valid_until', |
295
|
|
|
$data->validUntil |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get client error. |
301
|
|
|
* |
302
|
|
|
* @return \WP_Error|bool |
303
|
|
|
*/ |
304
|
|
|
private function get_client_error() { |
305
|
|
|
$error = $this->client->get_error(); |
306
|
|
|
|
307
|
|
|
if ( is_wp_error( $error ) ) { |
308
|
|
|
$this->error = $error; |
309
|
|
|
|
310
|
|
|
return $error; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return false; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths