PluginProbe
PayPlug for WooCommerce (Official) / 2.10.0
PayPlug for WooCommerce (Official) v2.10.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Controller / PayplugGenericGateway.php

PayplugGenericGateway.php in PayPlug for WooCommerce (Official) 2.10.0, at src/Controller/PayplugGenericGateway.php

531 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Controller;
4
5 use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData;
6 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
7 use Payplug\PayplugWoocommerce\Interfaces\PayplugGatewayBuilder;
8 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
9 use Automattic\WooCommerce\Utilities\OrderUtil;
10
11
12 use Payplug\Authentication;
13 use Payplug\Exception\ConfigurationException;
14 use Payplug\Exception\HttpException;
15 use Payplug\Exception\ForbiddenException;
16 use Payplug\Payplug;
17 use Payplug\Resource\Payment as PaymentResource;
18 use Payplug\Resource\Refund as RefundResource;
19
20 class PayplugGenericGateway extends PayplugGateway implements PayplugGatewayBuilder
21 {
22
23 public function __construct()
24 {
25 parent::__construct();
26
27 //this is only for PPRo payments
28 add_action('woocommerce_after_order_itemmeta', [$this, 'hide_wc_refund_button']);
29 //TODO:: add requirements here
30
31 }
32
33 /**
34 * Generic code to fetch payment gateway specific image
35 * @return string
36 */
37 public function get_icon()
38 {
39
40 //get object $image
41 $icons = apply_filters('payplug_payment_icons', [
42 'payplug' => sprintf('<img src="%s" alt="%s" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/' . $this->image), $this->id . " Icon"),
43 ]);
44
45 $icons_str = '';
46 foreach ($icons as $icon) {
47 $icons_str .= $icon;
48 }
49
50 return $icons_str;
51 }
52
53 /**
54 * @return void
55 */
56 public function display_notice()
57 {
58 $error_message = 'payplug_' . $this->id . '_unauthorized_message';
59 ?>
60 <div class="notice notice-error is-dismissible">
61 <p><?php echo __( $error_message, 'payplug' ); ?></p>
62 </div>
63 <?php
64 }
65
66 public function checkGateway()
67 {
68
69 //check if module is enabled
70 if(!empty($this->settings['enabled']) && 'no' === $this->settings['enabled']){
71 return false;
72 }
73
74 $account = PayplugWoocommerceHelper::generic_get_account_data_from_options( $this->id );
75
76 if( !$this->check_api_gateway_enable($account)){
77 return false;
78 }
79
80
81 if(!is_admin()){
82 if (empty(WC()->cart) && !is_admin()) {
83 return false;
84 }
85
86 //for backend orders
87 if (!empty(get_query_var('order-pay'))) {
88 $order = wc_get_order( (int) get_query_var('order-pay'));
89 $items = $order->get_items();
90 $country_code_shipping = $order->get_shipping_country();
91 $country_code_billing = $order->get_billing_country();
92 $this->order_items_to_cart(WC()->cart, $items);
93 }
94
95 if ( empty( $country_code_billing ) || empty( $country_code_shipping ) ) {
96 $country_code_shipping = method_exists(WC()->customer, "get_shipping_country") ? WC()->customer->get_shipping_country() : null;
97 $country_code_billing = method_exists(WC()->customer, "get_billing_country") ? WC()->customer->get_billing_country() : null;;
98 }
99
100 if( !$this->check_billing_country_permissions($account, $country_code_billing) ){
101 return false;
102 }
103 }
104
105 return true;
106 }
107
108 /**
109 * check if payment is enable and customer has permissions
110 * @param $account
111 * @return bool
112 */
113 protected function check_api_gateway_enable($account){
114
115 if (
116 isset( $account["payment_methods"] ) &&
117 empty( $account["payment_methods"][ $this->id ] ) &&
118 !$account["payment_methods"][ $this->id ]['enabled']
119 ) {
120 return false;
121 }
122
123
124 if (! isset( $account['permissions'][ $this->id ] ) ||
125 ( isset( $account['permissions'][ $this->id ] ) && !$account['permissions'][ $this->id ] )
126 ) {
127 return false;
128 }
129
130 return true;
131 }
132
133
134 /**
135 * Check if billing country can use this payment method
136 * @param $account
137 * @param $billing_code
138 * @return bool
139 */
140 public function check_billing_country_permissions($account, $billing_code){
141 $this->allowed_country_codes = !empty($account["payment_methods"][ $this->id ]['allowed_countries']) ? $account["payment_methods"][ $this->id ]['allowed_countries'] : null;
142
143 if (is_array($this->allowed_country_codes)) {
144 if ( in_array( "ALL", $this->allowed_country_codes) || empty( $this->allowed_country_codes ) ) {
145 return true;
146 }
147
148 //check if country is allowed
149 if ( in_array( $billing_code, $this->allowed_country_codes ) ) {
150 return true;
151
152 } else {
153 return false;
154 }
155 }
156
157 return true;
158 }
159
160
161 /**
162 * if payment was generated by an intend, we shouldn't generate another one and try to pay it, this would generate duplications
163 * @param $order
164 * @return array|null
165 * @throws \Exception
166 */
167 private function process_standard_intent_payment($order){
168
169 if ( !is_wc_endpoint_url('order-pay') &&
170 $this->is_checkout_block() &&
171 (
172 ( $this->id === "payplug" && ($this->payment_method === 'integrated'|| $this->payment_method === 'popup') ) ||
173 ( $this->id === "american_express" && $this->payment_method === 'popup')
174 ) &&
175 !empty($order->get_transaction_id()) ) {
176
177 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
178
179 try {
180 $payment = $this->api->payment_retrieve($order->get_transaction_id());
181 if (ob_get_length() > 0) {
182 ob_clean();
183 }
184
185
186 // Save transaction id for the order
187 PayplugWoocommerceHelper::is_pre_30()
188 ? update_post_meta($order_id, '_transaction_id', $payment->id)
189 : $order->set_transaction_id($payment->id);
190
191 if (is_callable([$order, 'save'])) {
192 $order->save();
193 }
194
195 /**
196 * Fires once a payment has been created.
197 *
198 * @param int $order_id Order ID
199 * @param PaymentResource $payment Payment resource
200 */
201 \do_action('payplug_gateway_payment_created', $order_id, $payment);
202
203 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
204 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
205
206 PayplugGateway::log(sprintf('Payment intent created for order #%s', $order_id));
207
208 $return_url = esc_url_raw($order->get_checkout_order_received_url());
209
210 wp_send_json_success(
211 array(
212 'payment_id' => $payment->id,
213 'result' => 'success',
214 'redirect' => !empty($payment->hosted_payment->payment_url) ? $payment->hosted_payment->payment_url : $return_url,
215 'cancel' => !empty($payment->hosted_payment->cancel_url) ? $payment->hosted_payment->cancel_url : null
216 )
217 );
218
219 return array("stt" => "OK");
220
221 } catch (HttpException $e) {
222 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
223 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
224 } catch (\Exception $e) {
225 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
226 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
227 }
228 }
229
230 return null;
231 }
232
233 public function process_standard_payment($order, $amount, $customer_id)
234 {
235
236 $intent = $this->process_standard_intent_payment($order);
237 if( !empty($intent) ){
238 return $intent;
239 }
240
241 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
242 try {
243
244 //if there's no auth to process payment
245 if ( !$this->checkGateway() ){
246 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
247 }
248
249 $address_data = PayplugAddressData::from_order($order);
250
251 $return_url = esc_url_raw($order->get_checkout_order_received_url());
252
253 if (!(substr( $return_url, 0, 4 ) === "http")) {
254 $return_url = get_site_url().$return_url;
255 }
256
257 $payment_data = [
258 'amount' => $amount,
259 'currency' => get_woocommerce_currency(),
260 'payment_method' => $this->id,
261 'billing' => $address_data->get_billing(),
262 'shipping' => $address_data->get_shipping(),
263 'hosted_payment' => [
264 'return_url' => $return_url,
265 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
266 ],
267 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
268 'metadata' => [
269 'order_id' => $order_id,
270 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
271 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
272 ],
273 "save_card"=> false,
274 "force_3ds"=> false
275 ];
276
277 /**
278 * Filter the payment data before it's used
279 *
280 * @param array $payment_data
281 * @param int $order_id
282 * @param array $customer_details
283 * @param PayplugAddressData $address_data
284 */
285 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
286 $payment = $this->api->payment_create($payment_data);
287
288 // Save transaction id for the order
289 PayplugWoocommerceHelper::is_pre_30()
290 ? update_post_meta($order_id, '_transaction_id', $payment->id)
291 : $order->set_transaction_id($payment->id);
292
293 if (is_callable([$order, 'save'])) {
294 $order->save();
295 }
296
297 /**
298 * Fires once a payment has been created.
299 *
300 * @param int $order_id Order ID
301 * @param PaymentResource $payment Payment resource
302 */
303 \do_action('payplug_gateway_payment_created', $order_id, $payment);
304
305 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
306 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
307
308 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
309
310 return [
311 'result' => 'success',
312 'redirect' => $payment->hosted_payment->payment_url,
313 'cancel' => $payment->hosted_payment->cancel_url,
314 ];
315
316 } catch (HttpException $e) {
317 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
318 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
319 } catch (\Exception $e) {
320 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
321 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
322 }
323
324 }
325
326 /**
327 * Process refund for an order paid with PayPlug gateway.
328 *
329 * @param int $order_id
330 * @param null $amount
331 * @param string $reason
332 *
333 * @return bool|\WP_Error
334 */
335 public function process_refund($order_id, $amount = null, $reason = ''){
336
337 PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
338
339 if( !$this->user_logged_in()){
340 PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
341 return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
342 }
343
344 $order = wc_get_order($order_id);
345 if (!$order instanceof \WC_Order) {
346 PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
347 return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id));
348 }
349
350 if ($order->get_status() === "cancelled") {
351 PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
352 return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id));
353 }
354
355 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
356 if (empty($transaction_id)) {
357 PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
358 return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug'));
359 }
360
361 /**
362 * PPRO gateways feature!
363 */
364 if( isset($this->enable_refund) && $this->enable_refund === false){
365 add_action('admin_head', [$this, 'hide_wc_refund_button'] );
366 PayplugGateway::log(__('payplug_refund_disabled_error', 'payplug'), 'error');
367 return new \WP_Error('process_refund_error', __('payplug_refund_disabled_error', 'payplug'));
368 }
369
370 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
371 $data = [
372 'metadata' => [
373 'order_id' => $order_id,
374 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
375 'refund_from' => 'woocommerce',
376 ]
377 ];
378
379 if (!is_null($amount)) {
380 $data['amount'] = PayplugWoocommerceHelper::get_payplug_amount($amount);
381 }
382
383 if (!empty($reason)) {
384 $data['metadata']['reason'] = $reason;
385 }
386
387 /**
388 * Filter the refund data before it's used.
389 *
390 * @param array $data
391 * @param int $order_id
392 * @param string $transaction_id
393 */
394 $data = apply_filters('payplug_gateway_refund_data', $data, $order_id, $transaction_id);
395
396 try {
397 $refund = $this->api->refund_create($transaction_id, $data);
398
399 /**
400 * Fires once a refund has been created.
401 *
402 * @param int $order_id Order ID
403 * @param RefundResource $refund Refund resource
404 * @param string $transaction_id Transaction id
405 */
406 \do_action('payplug_gateway_refund_created', $order_id, $refund, $transaction_id);
407
408 $refund_meta_key = sprintf('_pr_%s', wc_clean($refund->id));
409 if (PayplugWoocommerceHelper::is_pre_30()) {
410 update_post_meta($order_id, $refund_meta_key, $refund->id);
411 } else {
412 $order->add_meta_data($refund_meta_key, $refund->id, true);
413 $order->save();
414 }
415
416 $note = sprintf(__('Refund %s : Refunded %s', 'payplug'), wc_clean($refund->id), wc_price(((int) $refund->amount) / 100));
417 if (!empty($refund->metadata['reason'])) {
418 $note .= sprintf(' (%s)', esc_html($refund->metadata['reason']));
419 }
420 $order->add_order_note($note);
421
422 try {
423 $payment = $this->api->payment_retrieve($transaction_id);
424 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
425 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
426 } catch (\Exception $e) {
427 }
428
429 PayplugGateway::log('Refund process complete for the order.');
430
431 return true;
432 } catch (HttpException $e) {
433 PayplugGateway::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
434
435 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
436 } catch (\Exception $e) {
437 PayplugGateway::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error');
438
439 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
440 }
441
442
443
444 }
445
446
447 public function hide_wc_refund_button(){
448 global $post;
449
450 $payment_methods = ['satispay', 'sofort', 'ideal', 'mybank'];
451
452 if ( class_exists("OrderUtil") && OrderUtil::custom_orders_table_usage_is_enabled() ) {
453 $order_id = !empty($_GET["id"]) ? $_GET["id"] : null;
454
455 }else{
456
457 if(!empty($post->ID)){
458 $order_id = $post->ID;
459
460 }else if( !empty($_GET["id"]) ){
461 $order_id = $_GET["id"];
462
463 }else{
464 $order_id = null;
465
466 }
467 }
468
469 if(empty($order_id)){
470 return false;
471 }
472
473 $order = new \WC_Order($order_id);
474 if (in_array($order->get_payment_method(), $payment_methods)) {
475 ?>
476 <script>
477 jQuery(function () {
478 jQuery('.refund-items').attr("disabled", true);
479 });
480 </script>
481 <?php
482 }
483 }
484
485 /**
486 * refund not possible for PPRO payments
487 *
488 * @return void
489 */
490 public function refund_not_available($order)
491 {
492 if ($this->id === $order->get_payment_method() ) {
493 echo "<p style='color: red;'>" . __('payplug_refund_disabled_error', 'payplug') . "</p>";
494 }
495 }
496
497 /**
498 * Empty the cart and add all order_items
499 *
500 * @param $cart
501 * @param $items
502 * @return void
503 */
504 private function order_items_to_cart($cart, $items){
505 $cart->empty_cart();
506 foreach ($items as $item){
507 $cart->add_to_cart($item->get_product_id(), $item->get_quantity(), $item->get_variation_id());
508 }
509 }
510
511 /**
512 * get threshold values for the current payment method
513 * @param $account
514 * @return void
515 */
516 private function get_thresholds_values($account){
517
518 if(!empty($account["payment_methods"][$this->id]['min_amounts']['EUR'])){
519 $this->min_thresholds = floatval($account["payment_methods"][$this->id]['min_amounts']['EUR']/100);
520
521 }
522
523 if(!empty($account["payment_methods"][$this->id]['max_amounts']['EUR'])){
524 $this->max_thresholds = floatval($account["payment_methods"][$this->id]['max_amounts']['EUR']/100);
525
526 }
527
528 }
529
530 }
531