| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Front; |
| 4 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x; |
| 5 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 6 |
|
| 7 |
// Exit if accessed directly |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class PayplugOney |
| 13 |
{ |
| 14 |
private $min_amount = 0; |
| 15 |
private $max_amount = 0; |
| 16 |
private $icon = ''; |
| 17 |
private $disable = false; |
| 18 |
private $total_products = 0; |
| 19 |
private $total_price = 0; |
| 20 |
private $simulatedClass = ''; |
| 21 |
private $oney_type="with_fees"; |
| 22 |
|
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
$this->handleMinMaxAmount(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* set min and max allowed amount |
| 30 |
*/ |
| 31 |
public function handleMinMaxAmount() |
| 32 |
{ |
| 33 |
$oney_range = PayplugWoocommerceHelper::get_min_max_oney(); |
| 34 |
$this->set_max_amount($oney_range['max']); |
| 35 |
$this->set_min_amount($oney_range['min']); |
| 36 |
} |
| 37 |
|
| 38 |
public function handleOneySimulation() |
| 39 |
{ |
| 40 |
$this->setTotalPrice($_POST['price']); |
| 41 |
|
| 42 |
if ($this->getTotalPrice() < $this->get_min_amount() || $this->getTotalPrice() > $this->get_max_amount()) |
| 43 |
return false; |
| 44 |
|
| 45 |
try { |
| 46 |
$simulation = $this->requestOneySimulation(); |
| 47 |
|
| 48 |
} catch (\Exception $e) { |
| 49 |
PayplugGatewayOney3x::log("Simulate Oney Without Fees Payment, " . $e->getMessage() ); |
| 50 |
return false; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
return $simulation; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* return oney simulation data for the order |
| 59 |
* @param $class |
| 60 |
* @return array|object |
| 61 |
*/ |
| 62 |
public function requestOneySimulation() |
| 63 |
{ |
| 64 |
$class = $this->getSimulatedClass(); |
| 65 |
$api = new \Payplug\PayplugWoocommerce\Gateway\PayplugApi( new $class() ); |
| 66 |
$api->init(); |
| 67 |
return $api->simulate_oney_payment($this->getTotalPrice(), $this->getOneyType()); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* SUM totalproducts that are in the cart |
| 72 |
*/ |
| 73 |
public function handleTotalProducts() |
| 74 |
{ |
| 75 |
$this->addTotalProducts(1); |
| 76 |
if(is_cart()) { |
| 77 |
$this->resetTotalProducts(); |
| 78 |
foreach(WC()->cart->cart_contents as $product) { |
| 79 |
$this->addTotalProducts($product['quantity']); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param $oney |
| 86 |
* @param $oney_response |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
static function simulationPopupContent($oney, $x3oney_response, $x4oney_response ){ |
| 90 |
|
| 91 |
$financing_cost_3x = intval($x3oney_response['total_cost']) / 100; |
| 92 |
$financing_cost_4x = intval($x4oney_response['total_cost']) / 100; |
| 93 |
|
| 94 |
$f = function($fn) { return $fn; }; |
| 95 |
|
| 96 |
$without_fees_text = ""; |
| 97 |
if($oney->getOneyType() === "without_fees" ) |
| 98 |
$without_fees_text = "<span class='underline'>{$f(__('WITHOUT FEES', 'payplug'))}</span>"; |
| 99 |
|
| 100 |
$pop_content = <<<HTML |
| 101 |
<div id='oney-popup-close'> |
| 102 |
<div class='oney-popup-close-mdiv'> |
| 103 |
<div class='oney-popup-close-md'></div> |
| 104 |
</div> |
| 105 |
</div> |
| 106 |
<div class='oney-img oney-logo no-margin'></div> |
| 107 |
<div class='oney-title'> |
| 108 |
<p class='no-margin oney-color'>{$f(__('PAYMENT', 'payplug'))} $without_fees_text </p> |
| 109 |
<p class='no-margin bold oney-color'>{$f(__('BY CREDIT CARD', 'payplug'))}</p> |
| 110 |
</div> |
| 111 |
<div class='oney-content oney-3x-content'> |
| 112 |
<div class='oney-img oney-3x no-margin'></div> |
| 113 |
<div class='oney-details'> |
| 114 |
<p class='bold no-margin'> {$f(__('Bring', 'payplug'))} : {$x3oney_response['down_payment_amount']} €</p> |
| 115 |
<p class='bold no-margin'>+2 {$f(__('monthly payment of', 'payplug'))} : {$x3oney_response['installments'][0]['amount']} € </p> |
| 116 |
<p class='no-margin'> {$f(__('Of which financing cost', 'payplug'))} : {$financing_cost_3x} € </p> |
| 117 |
<p class='no-margin'> {$f(__('TAEG', 'payplug'))} : {$x3oney_response['effective_annual_percentage_rate']} % </p> |
| 118 |
</div> |
| 119 |
</div> |
| 120 |
<div class='oney-separator'></div> |
| 121 |
<div class='oney-content oney-4x-content'> |
| 122 |
<div class='oney-img oney-4x no-margin'></div> |
| 123 |
<div class='oney-details'> |
| 124 |
<p class='bold no-margin'> {$f(__('Bring', 'payplug'))} : {$x4oney_response['down_payment_amount']} €</p> |
| 125 |
<p class='bold no-margin'>+3 {$f(__('monthly payment of', 'payplug'))} : {$x4oney_response['installments'][0]['amount']} € </p> |
| 126 |
<p class='no-margin'> {$f(__('Of which financing cost', 'payplug'))} : {$financing_cost_4x} € </p> |
| 127 |
<p class='no-margin'> {$f(__('TAEG', 'payplug'))} : {$x4oney_response['effective_annual_percentage_rate']} % </p> |
| 128 |
</div> |
| 129 |
</div> |
| 130 |
HTML; |
| 131 |
|
| 132 |
return $pop_content; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Setter |
| 137 |
* @param $amount |
| 138 |
*/ |
| 139 |
public function set_min_amount($amount) |
| 140 |
{ |
| 141 |
$this->min_amount = (int) $amount; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Setter |
| 146 |
* @param $amount |
| 147 |
*/ |
| 148 |
public function set_max_amount($amount) |
| 149 |
{ |
| 150 |
$this->max_amount = (int) $amount; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @return int |
| 155 |
*/ |
| 156 |
public function get_min_amount() |
| 157 |
{ |
| 158 |
return $this->min_amount; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @return int |
| 163 |
*/ |
| 164 |
public function get_max_amount() |
| 165 |
{ |
| 166 |
return $this->max_amount; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function getIcon() |
| 173 |
{ |
| 174 |
return $this->icon; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @param string $icon |
| 179 |
*/ |
| 180 |
public function setIcon($icon) |
| 181 |
{ |
| 182 |
$this->icon = $icon; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @return bool |
| 187 |
*/ |
| 188 |
public function isDisable() |
| 189 |
{ |
| 190 |
if($this->disable){ |
| 191 |
return "disabled"; |
| 192 |
} |
| 193 |
|
| 194 |
return ''; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* @param bool $disable |
| 199 |
*/ |
| 200 |
public function setDisable($disable) |
| 201 |
{ |
| 202 |
$this->disable = $disable; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @param $qty |
| 207 |
* @return void |
| 208 |
*/ |
| 209 |
public function addTotalProducts($qty) |
| 210 |
{ |
| 211 |
$this->total_products += $qty; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @set total_products to 0 |
| 216 |
*/ |
| 217 |
public function resetTotalProducts() |
| 218 |
{ |
| 219 |
$this->total_products = 0; |
| 220 |
} |
| 221 |
|
| 222 |
public function getTotalProducts() |
| 223 |
{ |
| 224 |
return $this->total_products; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @return int |
| 229 |
*/ |
| 230 |
public function getTotalPrice() |
| 231 |
{ |
| 232 |
return $this->total_price; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* @param int $total_price |
| 237 |
*/ |
| 238 |
public function setTotalPrice($total_price) |
| 239 |
{ |
| 240 |
$this->total_price = $total_price; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* @return string |
| 245 |
*/ |
| 246 |
public function getSimulatedClass() |
| 247 |
{ |
| 248 |
return $this->simulatedClass; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* @param string $simulatedClass |
| 253 |
*/ |
| 254 |
public function setSimulatedClass($simulatedClass) |
| 255 |
{ |
| 256 |
$this->simulatedClass = $simulatedClass; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* @return string |
| 261 |
*/ |
| 262 |
public function getOneyType() |
| 263 |
{ |
| 264 |
return $this->oney_type; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* @param string $oney_type |
| 269 |
*/ |
| 270 |
public function setOneyType($oney_type) |
| 271 |
{ |
| 272 |
$this->oney_type = $oney_type; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
} |
| 277 |
|