| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 6 |
use Payplug\Authentication; |
| 7 |
use libphonenumber\PhoneNumberType; |
| 8 |
use libphonenumber\PhoneNumberUtil; |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* PayPlug WooCommerce Gateway. |
| 17 |
* |
| 18 |
* @package Payplug\PayplugWoocommerce\Gateway |
| 19 |
*/ |
| 20 |
class PayplugGatewayOney3x extends PayplugGateway |
| 21 |
{ |
| 22 |
const OPTION_NAME = "payplug_oney_config"; |
| 23 |
const ONEY_UNAVAILABLE_CODE_COUNTRY_NOT_ALLOWED = 2; |
| 24 |
const ONEY_UNAVAILABLE_CODE_CART_SIZE_TOO_HIGH = 3; |
| 25 |
const ONEY_PRODUCT_QUANTITY_MAXIMUM = 1000; |
| 26 |
|
| 27 |
protected $oney_response; |
| 28 |
protected $min_oney_price; |
| 29 |
protected $max_oney_price; |
| 30 |
protected $allowed_country_codes; |
| 31 |
|
| 32 |
public function __construct() |
| 33 |
{ |
| 34 |
parent::__construct(); |
| 35 |
$this->id = 'oney_x3_with_fees'; |
| 36 |
$this->method_title = _x('PayPlug Oney 3x', 'Gateway method title', 'payplug'); |
| 37 |
$this->method_description = __('Enable PayPlug Oney 3x for your customers.', 'payplug'); |
| 38 |
$this->title = __('Pay by card in 3x with Oney', 'payplug'); |
| 39 |
|
| 40 |
if (is_admin()) { |
| 41 |
wp_register_style('payplug-admin-oney', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-admin-oney.css', [], PAYPLUG_GATEWAY_VERSION); |
| 42 |
wp_enqueue_style('payplug-admin-oney'); |
| 43 |
} |
| 44 |
|
| 45 |
add_action('woocommerce_order_item_add_action_buttons', [$this, 'oney_refund_text']); |
| 46 |
|
| 47 |
if (is_checkout()) { |
| 48 |
$account = PayplugWoocommerceHelper::get_account_data_from_options(true); |
| 49 |
$oney_configuration = $account['configuration']['oney']; |
| 50 |
$this->min_oney_price = $oney_configuration['min_amounts']['EUR'] / 100; |
| 51 |
$this->max_oney_price = $oney_configuration['max_amounts']['EUR'] / 100; |
| 52 |
$this->allowed_country_codes = $oney_configuration['allowed_countries']; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get payment icons. |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function get_icon() |
| 62 |
{ |
| 63 |
|
| 64 |
if ($this->check_oney_is_available() === true) { |
| 65 |
$total_price = floatval(WC()->cart->total); |
| 66 |
$this->oney_response = $this->api->simulate_oney_payment($total_price); |
| 67 |
$currency = get_woocommerce_currency_symbol(get_option('woocommerce_currency')); |
| 68 |
$f = function ($fn) { |
| 69 |
return $fn; |
| 70 |
}; |
| 71 |
if(is_array($this->oney_response)) { |
| 72 |
$this->description = <<<HTML |
| 73 |
<p> |
| 74 |
<div class="payplug-oney-flex"> |
| 75 |
<div>{$f(__('Bring', 'payplug'))} :</div> |
| 76 |
<div>{$this->oney_response['x3_with_fees']['down_payment_amount']} {$currency}</div> |
| 77 |
</div> |
| 78 |
<div class="payplug-oney-flex"> |
| 79 |
<div>{$f(__('1st monthly payment', 'payplug'))} :</div> |
| 80 |
<div>{$this->oney_response['x3_with_fees']['installments'][0]['amount']} {$currency}</div> |
| 81 |
</div> |
| 82 |
<div class="payplug-oney-flex"> |
| 83 |
<div>{$f(__('2nd monthly payment', 'payplug'))} :</div> |
| 84 |
<div>{$this->oney_response['x3_with_fees']['installments'][1]['amount']} {$currency}</div> |
| 85 |
</div> |
| 86 |
</p> |
| 87 |
HTML; |
| 88 |
} else { |
| 89 |
$this->description = $this->oney_response; |
| 90 |
} |
| 91 |
|
| 92 |
$available_img = 'lg-3xoney-checkout.png'; |
| 93 |
} else { |
| 94 |
$available_img = 'lg-3xoney-checkout-disabled.png'; |
| 95 |
} |
| 96 |
|
| 97 |
$icons = apply_filters('payplug_payment_icons', [ |
| 98 |
'payplug' => sprintf('<img src="%s" alt="Oney 3x" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/' . $available_img)), |
| 99 |
]); |
| 100 |
$icons_str = ''; |
| 101 |
foreach ($icons as $icon) { |
| 102 |
$icons_str .= $icon; |
| 103 |
} |
| 104 |
return $icons_str; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Check if Oney is available |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public function check_oney_is_available() |
| 113 |
{ |
| 114 |
$cart = WC()->cart; |
| 115 |
$total_price = floatval($cart->total); |
| 116 |
$products_qty = (int) $cart->cart_contents_count; |
| 117 |
|
| 118 |
// Min and max |
| 119 |
if ($total_price < $this->min_oney_price || $total_price > $this->max_oney_price) { |
| 120 |
$this->description = '<div class="payment_method_oney_x3_with_fees_disabled">'.sprintf(__('The total amount of your order should be between %s€ and %s€ to pay with Oney.', 'payplug'), $this->min_oney_price, $this->max_oney_price).'</div>'; |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
// Cart check |
| 125 |
if ($products_qty >= self::ONEY_PRODUCT_QUANTITY_MAXIMUM) { |
| 126 |
$this->description = '<div class="payment_method_oney_x3_with_fees_disabled">'.sprintf(__('The payment with Oney is unavailable because you have more than %s items in your cart.', 'payplug'), self::ONEY_PRODUCT_QUANTITY_MAXIMUM).'</div>'; |
| 127 |
return self::ONEY_UNAVAILABLE_CODE_CART_SIZE_TOO_HIGH; |
| 128 |
} |
| 129 |
|
| 130 |
// Country check |
| 131 |
$country_code_shipping = WC()->customer->get_shipping_country(); |
| 132 |
$country_code_billing = WC()->customer->get_billing_country(); |
| 133 |
|
| 134 |
if (!in_array($country_code_billing, $this->allowed_country_codes) || !in_array($country_code_shipping, $this->allowed_country_codes)) { |
| 135 |
$this->description = '<div class="payment_method_oney_x3_with_fees_disabled">'.__('Unavailable for the specified country.', 'payplug').'</div>'; |
| 136 |
return self::ONEY_UNAVAILABLE_CODE_COUNTRY_NOT_ALLOWED; |
| 137 |
} |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Check the order amount to ensure it's on the allowed range. |
| 144 |
* |
| 145 |
* @param int $amount |
| 146 |
* |
| 147 |
* @return int|\WP_Error |
| 148 |
*/ |
| 149 |
public function validate_order_amount($amount) |
| 150 |
{ |
| 151 |
if ($amount / 100 < $this->min_oney_price || $amount / 100 > $this->max_oney_price) { |
| 152 |
return new \WP_Error( |
| 153 |
'invalid order amount', |
| 154 |
sprintf(__('The total amount of your order should be between %s€ and %s€ to pay with Oney.', 'payplug'), $this->min_oney_price, $this->max_oney_price) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
return $amount; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param \WC_Order $order |
| 163 |
* @param int $amount |
| 164 |
* @param int $customer_id |
| 165 |
* |
| 166 |
* @return array |
| 167 |
* @throws \Exception |
| 168 |
*/ |
| 169 |
public function process_standard_payment($order, $amount, $customer_id) |
| 170 |
{ |
| 171 |
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id(); |
| 172 |
try { |
| 173 |
if (!$this->check_oney_is_available()) { |
| 174 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 175 |
} elseif ($this->check_oney_is_available() === self::ONEY_UNAVAILABLE_CODE_COUNTRY_NOT_ALLOWED) { |
| 176 |
$country_code = WC()->customer->get_shipping_country(); |
| 177 |
throw new \Exception(__('Unavailable for the specified country.')); |
| 178 |
} elseif ($this->check_oney_is_available() === self::ONEY_UNAVAILABLE_CODE_CART_SIZE_TOO_HIGH) { |
| 179 |
throw new \Exception(sprintf(__('The payment with Oney is unavailable because you have more than %s items in your cart.', 'payplug'), self::ONEY_PRODUCT_QUANTITY_MAXIMUM)); |
| 180 |
} |
| 181 |
|
| 182 |
$country = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_country : $order->get_billing_country(); |
| 183 |
$phone = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_phone : $order->get_billing_phone(); |
| 184 |
$billing_email = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_email : $order->get_billing_email(); |
| 185 |
$phone_number_util = PhoneNumberUtil::getInstance(); |
| 186 |
$phone_number = $phone_number_util->parse( $phone, $country ); |
| 187 |
if ( PhoneNumberType::MOBILE !== $phone_number_util->getNumberType( $phone_number ) ) { |
| 188 |
throw new \Exception(__('Mobile phone number fullfilled is invalid. Please retry.', 'payplug')); |
| 189 |
} |
| 190 |
|
| 191 |
if (!filter_var($billing_email, FILTER_VALIDATE_EMAIL) || strpos($billing_email,'+') !== false) { |
| 192 |
throw new \Exception(__("Your email address is too long and the + character is not valid, please change it to another address (max 100 characters).", 'payplug')); |
| 193 |
} |
| 194 |
|
| 195 |
$address_data = PayplugAddressData::from_order($order); |
| 196 |
|
| 197 |
$cart_items = []; |
| 198 |
$items = $order->get_items(); |
| 199 |
foreach($items as $item) { |
| 200 |
$data = $item->get_data(); |
| 201 |
$total = floatval(round($data['total'], 2)) * 100; |
| 202 |
$cart_items[] = [ |
| 203 |
'delivery_label' => 'storepickup', |
| 204 |
'delivery_type' => 'storepickup', |
| 205 |
'brand' => 'Woocommerce', |
| 206 |
'merchant_item_id' => 'cart-'.$data['id'].'-'.$data['product_id'], |
| 207 |
'name' => $data['name'], |
| 208 |
'expected_delivery_date' => date('Y-m-d'), |
| 209 |
'total_amount' => (int) $total, |
| 210 |
'price' => round($total / $data['quantity']), |
| 211 |
'quantity' => $data['quantity'] |
| 212 |
]; |
| 213 |
} |
| 214 |
|
| 215 |
$payment_data = [ |
| 216 |
'authorized_amount' => $amount, |
| 217 |
'auto_capture' => true, |
| 218 |
'currency' => get_woocommerce_currency(), |
| 219 |
'payment_method' => $this->id, |
| 220 |
'billing' => $address_data->get_billing(), |
| 221 |
'shipping' => $address_data->get_shipping(), |
| 222 |
'payment_context' => [ |
| 223 |
"cart" => $cart_items |
| 224 |
], |
| 225 |
'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), |
| 226 |
'hosted_payment' => [ |
| 227 |
'return_url' => esc_url_raw($order->get_checkout_order_received_url()), |
| 228 |
'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), |
| 229 |
], |
| 230 |
'metadata' => [ |
| 231 |
'order_id' => $order_id, |
| 232 |
'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', |
| 233 |
'domain' => $this->limit_length(esc_url_raw(home_url()), 500), |
| 234 |
], |
| 235 |
]; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Filter the payment data before it's used |
| 240 |
* |
| 241 |
* @param array $payment_data |
| 242 |
* @param int $order_id |
| 243 |
* @param array $customer_details |
| 244 |
* @param PayplugAddressData $address_data |
| 245 |
*/ |
| 246 |
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); |
| 247 |
$payment = $this->api->payment_create($payment_data); |
| 248 |
|
| 249 |
// Save transaction id for the order |
| 250 |
PayplugWoocommerceHelper::is_pre_30() |
| 251 |
? update_post_meta($order_id, '_transaction_id', $payment->id) |
| 252 |
: $order->set_transaction_id($payment->id); |
| 253 |
|
| 254 |
if (is_callable([$order, 'save'])) { |
| 255 |
$order->save(); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Fires once a payment has been created. |
| 260 |
* |
| 261 |
* @param int $order_id Order ID |
| 262 |
* @param PaymentResource $payment Payment resource |
| 263 |
*/ |
| 264 |
\do_action('payplug_gateway_payment_created', $order_id, $payment); |
| 265 |
|
| 266 |
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); |
| 267 |
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); |
| 268 |
|
| 269 |
PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id)); |
| 270 |
|
| 271 |
return [ |
| 272 |
'result' => 'success', |
| 273 |
'redirect' => $payment->hosted_payment->payment_url, |
| 274 |
'cancel' => $payment->hosted_payment->cancel_url, |
| 275 |
]; |
| 276 |
} catch (\HttpException $e) { |
| 277 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error'); |
| 278 |
throw new \Exception(__($e->getMessage(), 'payplug')); |
| 279 |
} catch (\Exception $e) { |
| 280 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error'); |
| 281 |
throw new \Exception(__($e->getMessage(), 'payplug')); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* Check if the gatteway is allowed for the order amount |
| 288 |
* |
| 289 |
* @param array |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
public function check_gateway($gateways) |
| 293 |
{ |
| 294 |
if (isset($gateways[$this->id]) && $gateways[$this->id]->id == $this->id) { |
| 295 |
if(!PayplugWoocommerceHelper::is_oney_available()) { |
| 296 |
unset($gateways[$this->id]); |
| 297 |
} |
| 298 |
} |
| 299 |
return $gateways; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Show Oney refund text |
| 304 |
* |
| 305 |
* @return void |
| 306 |
*/ |
| 307 |
public function oney_refund_text($order) |
| 308 |
{ |
| 309 |
if ($this->id === $order->get_payment_method() && parent::can_refund_order($order) && $order->get_status() !== "refunded" && $this->api) { |
| 310 |
$order_metadata = $order->get_meta('_payplug_metadata'); |
| 311 |
$payment = $this->api->payment_retrieve($order_metadata['transaction_id']); |
| 312 |
$today = current_time('Y-m-d H:i:s'); |
| 313 |
$can_refund_date = date('Y-m-d H:i:s', $payment->__get('refundable_after')); |
| 314 |
if ($can_refund_date >= $today) { |
| 315 |
echo "<p style='color: red;'>" . __('Refund will be possible 48 hours after the last payment or refund transaction.', 'payplug') . "</p>"; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
public function payment_fields() |
| 321 |
{ |
| 322 |
$description = $this->get_description(); |
| 323 |
if (!empty($description)) { |
| 324 |
echo wpautop(wptexturize($description)); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
} |
| 329 |
|