| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class PayplugWoocommerceRequest |
| 14 |
* @package Payplug\PayplugWoocommerce |
| 15 |
*/ |
| 16 |
class PayplugWoocommerceRequest { |
| 17 |
|
| 18 |
/** |
| 19 |
* Gateway settings. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $settings; |
| 24 |
|
| 25 |
/** |
| 26 |
* PayplugWoocommerceRequest constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
$this->settings = get_option( 'woocommerce_payplug_settings', [] ); |
| 30 |
if ( empty( $this->settings ) || ! isset( $this->settings['enabled'] ) || 'yes' !== $this->settings['enabled'] ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Don't load for change payment method page. |
| 35 |
if ( isset( $_GET['change_payment_method'] ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
add_action( 'template_redirect', [ $this, 'set_session' ] ); |
| 40 |
add_action( 'wc_ajax_payplug_create_order', [ $this, 'ajax_create_order' ] ); |
| 41 |
add_action( 'wc_ajax_applepay_update_payment', [ $this, 'applepay_update_payment' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Sets the WC customer session if one is not set. |
| 46 |
* This is needed so nonces can be verified by AJAX Request. |
| 47 |
*/ |
| 48 |
public function set_session() { |
| 49 |
if ( ! is_product() || ( isset( WC()->session ) && WC()->session->has_session() ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); |
| 54 |
$wc_session = new $session_class(); |
| 55 |
|
| 56 |
if ( version_compare( WC_VERSION, '3.3', '>=' ) ) { |
| 57 |
$wc_session->init(); |
| 58 |
} |
| 59 |
|
| 60 |
$wc_session->set_customer_session_cookie( true ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Create the woocommerce order in the BO |
| 65 |
* |
| 66 |
*/ |
| 67 |
public function ajax_create_order() { |
| 68 |
if ( WC()->cart->is_empty() ) { |
| 69 |
wp_send_json_error( __( 'Empty cart', 'payplug' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
| 73 |
define( 'WOOCOMMERCE_CHECKOUT', true ); |
| 74 |
} |
| 75 |
|
| 76 |
WC()->checkout()->process_checkout(); |
| 77 |
|
| 78 |
die( 0 ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Update Payplug API Payment for Apple Pay |
| 83 |
*/ |
| 84 |
public function applepay_update_payment() { |
| 85 |
|
| 86 |
$options = get_option('woocommerce_payplug_settings', []); |
| 87 |
$order_id = $_POST['order_id']; |
| 88 |
$payment_id = $_POST['payment_id']; |
| 89 |
|
| 90 |
try{ |
| 91 |
\Payplug\Payplug::init(array( |
| 92 |
'secretKey' => @$options['payplug_live_key'], |
| 93 |
'apiVersion' => "2019-08-06", |
| 94 |
)); |
| 95 |
|
| 96 |
$apple_pay = array(); |
| 97 |
$apple_pay['payment_token'] = $_POST['payment_token']; |
| 98 |
$payment = \Payplug\Payment::retrieve($payment_id); |
| 99 |
|
| 100 |
$data = array( 'apple_pay' => $apple_pay ); |
| 101 |
$update = $payment->update($data); |
| 102 |
|
| 103 |
wp_send_json_success([ "result" => $update->is_paid ]); |
| 104 |
|
| 105 |
}catch (\Exception $e){ |
| 106 |
wp_send_json_error($e->getMessage()); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|