| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Front\PayplugOney\Requests; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\Front\PayplugOney\OneySimulation; |
| 6 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x; |
| 7 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 8 |
use function is_cart; |
| 9 |
use function is_checkout; |
| 10 |
use function is_product; |
| 11 |
|
| 12 |
Abstract class OneyBase |
| 13 |
{ |
| 14 |
|
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $country; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var OneySimulation |
| 22 |
*/ |
| 23 |
private $simulation = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Dependency injection |
| 27 |
* @var \Payplug\PayplugWoocommerce\Front\PayplugOney\Country\OneyBase |
| 28 |
*/ |
| 29 |
private $oney; |
| 30 |
|
| 31 |
|
| 32 |
public function __construct() |
| 33 |
{ |
| 34 |
add_action( 'wp_ajax_simulate_oney_payment', [ $this, 'simulateOneyPayment' ]); |
| 35 |
add_action( 'wp_ajax_nopriv_simulate_oney_payment', [ $this, 'simulateOneyPayment' ]); |
| 36 |
add_action('woocommerce_cart_totals_after_order_total', [$this, 'showOneyAnimationCart']); |
| 37 |
|
| 38 |
$options = get_option('woocommerce_payplug_settings', []); |
| 39 |
|
| 40 |
if (isset($options['oney_product_animation']) && ($options['oney_product_animation'] == 'yes')){ |
| 41 |
add_action( 'woocommerce_before_add_to_cart_form', [ $this, 'showOneyAnimationProduct' ] ); |
| 42 |
|
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* request simulation |
| 48 |
* print results |
| 49 |
*/ |
| 50 |
public function simulateOneyPayment(){ |
| 51 |
$simulation = new OneySimulation($this->oney); |
| 52 |
$this->simulation = $simulation->OneySimulation(); |
| 53 |
$html = $this->drawAnimation(); |
| 54 |
|
| 55 |
wp_send_json_success( |
| 56 |
array( |
| 57 |
'popup' => $html |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
wp_die(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* draw html popup |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
public function drawAnimation(){ |
| 69 |
$class = "Payplug\\PayplugWoocommerce\\Front\\Layout\\Oney" . $this->getCountry(); |
| 70 |
|
| 71 |
switch($this->oney->getOneyType()){ |
| 72 |
case "without_fees": |
| 73 |
$footer = $class::footerOneyWithoutFees($this->oney->get_min_amount(), $this->oney->get_max_amount()); |
| 74 |
$content = $class::simulationPopupContentWithoutFees($this); |
| 75 |
break; |
| 76 |
default: |
| 77 |
$footer = $class::footerOneyWithFees($this->oney->get_min_amount(), $this->oney->get_max_amount()); |
| 78 |
$content = $class::simulationPopupContent($this); |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
$html = <<<HTML |
| 83 |
$content |
| 84 |
$footer |
| 85 |
HTML; |
| 86 |
return $html; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Button to show oney popup cart page |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function showOneyAnimationCart() |
| 96 |
{ |
| 97 |
|
| 98 |
if ( ( is_cart() ) && PayplugWoocommerceHelper::is_oney_available()) { |
| 99 |
global $product; |
| 100 |
|
| 101 |
$total_price = (is_numeric( floatval(WC()->cart->total))) ? floatval(WC()->cart->total) : (float)($product->get_price()); |
| 102 |
$this->oney->setTotalPrice($total_price); |
| 103 |
$this->oney->handleTotalProducts(); |
| 104 |
|
| 105 |
//don't show animation |
| 106 |
if ( !PayplugWoocommerceHelper::check_order_max_amount($this->oney->getTotalPrice()) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
if ($this->oney->getTotalPrice() < $this->oney->get_min_amount() || $this->oney->getTotalPrice() > $this->oney->get_max_amount() || $this->oney->getTotalProducts() >= PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM) { |
| 111 |
$this->oney->setDisable(true); |
| 112 |
} |
| 113 |
|
| 114 |
$this->oneyGeneratePopup(); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Button to show oney popup product page |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function showOneyAnimationProduct() |
| 125 |
{ |
| 126 |
if ( (is_product()) && PayplugWoocommerceHelper::is_oney_available()) { |
| 127 |
global $product; |
| 128 |
$price = $product->get_price(); |
| 129 |
|
| 130 |
if(method_exists($product,"get_available_variations")){ |
| 131 |
$available_variations = $product->get_available_variations(); |
| 132 |
} |
| 133 |
|
| 134 |
if(!empty($available_variations)){ |
| 135 |
foreach ($available_variations as $k => $value){ |
| 136 |
$this->oney->setVariations($value); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$this->oney->setTotalPrice($price); |
| 141 |
$this->oney->handleTotalProducts(); |
| 142 |
|
| 143 |
//don't show animation |
| 144 |
if ( !PayplugWoocommerceHelper::check_order_max_amount($price) ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
if ($price < $this->oney->get_min_amount() || $price > $this->oney->get_max_amount() || $this->oney->getTotalProducts() >= PayplugGatewayOney3x::ONEY_PRODUCT_QUANTITY_MAXIMUM) { |
| 149 |
$this->oney->setDisable(true); |
| 150 |
} |
| 151 |
|
| 152 |
$this->oneyGeneratePopup(); |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* get Html for Oney |
| 159 |
*/ |
| 160 |
public function oneyGeneratePopup(){ |
| 161 |
$class = "Payplug\\PayplugWoocommerce\\Front\\Layout\\Oney" . $this->getCountry(); |
| 162 |
$class = new $class(); |
| 163 |
echo $class::payWithOney($this->oney); |
| 164 |
echo $class::disabledOneyPopup($this->oney); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param $options |
| 170 |
* @return String |
| 171 |
*/ |
| 172 |
public function setCountry($options){ |
| 173 |
$this->country = !empty($options["payplug_merchant_country"]) ? $options["payplug_merchant_country"] : "FR"; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public function getCountry(){ |
| 180 |
return $this->country; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @return array|OneySimulation |
| 185 |
*/ |
| 186 |
public function getSimulation(){ |
| 187 |
return $this->simulation; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @param $oney |
| 192 |
*/ |
| 193 |
public function setOney($oney) |
| 194 |
{ |
| 195 |
$this->oney = $oney; |
| 196 |
} |
| 197 |
} |
| 198 |
|