| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Class PayplugWoocommerceRequest |
| 12 |
* @package Payplug\PayplugWoocommerce |
| 13 |
*/ |
| 14 |
class PayplugWoocommerceRequest { |
| 15 |
|
| 16 |
/** |
| 17 |
* Gateway settings. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $settings; |
| 22 |
|
| 23 |
/** |
| 24 |
* PayplugWoocommerceRequest constructor. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$this->settings = get_option( 'woocommerce_payplug_settings', [] ); |
| 28 |
if ( empty( $this->settings ) || ! isset( $this->settings['enabled'] ) || 'yes' !== $this->settings['enabled'] ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
// Don't load for change payment method page. |
| 33 |
if ( isset( $_GET['change_payment_method'] ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
add_action( 'template_redirect', [ $this, 'set_session' ] ); |
| 38 |
add_action( 'wc_ajax_payplug_create_order', [ $this, 'ajax_create_order' ] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Sets the WC customer session if one is not set. |
| 43 |
* This is needed so nonces can be verified by AJAX Request. |
| 44 |
*/ |
| 45 |
public function set_session() { |
| 46 |
if ( ! is_product() || ( isset( WC()->session ) && WC()->session->has_session() ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); |
| 51 |
$wc_session = new $session_class(); |
| 52 |
|
| 53 |
if ( version_compare( WC_VERSION, '3.3', '>=' ) ) { |
| 54 |
$wc_session->init(); |
| 55 |
} |
| 56 |
|
| 57 |
$wc_session->set_customer_session_cookie( true ); |
| 58 |
} |
| 59 |
|
| 60 |
public function ajax_create_order() { |
| 61 |
if ( WC()->cart->is_empty() ) { |
| 62 |
wp_send_json_error( __( 'Empty cart', 'payplug' ) ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
| 66 |
define( 'WOOCOMMERCE_CHECKOUT', true ); |
| 67 |
} |
| 68 |
|
| 69 |
WC()->checkout()->process_checkout(); |
| 70 |
|
| 71 |
die( 0 ); |
| 72 |
} |
| 73 |
} |