| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Controller; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\Gateway\PayplugAddressData; |
| 6 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 7 |
use Payplug\PayplugWoocommerce\Interfaces\PayplugGatewayBuilder; |
| 8 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 9 |
|
| 10 |
|
| 11 |
use Payplug\Authentication; |
| 12 |
use Payplug\Exception\ConfigurationException; |
| 13 |
use Payplug\Exception\HttpException; |
| 14 |
use Payplug\Exception\ForbiddenException; |
| 15 |
use Payplug\Payplug; |
| 16 |
use Payplug\Resource\Refund as RefundResource; |
| 17 |
|
| 18 |
class PayplugGenericGateway extends PayplugGateway implements PayplugGatewayBuilder |
| 19 |
{ |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
parent::__construct(); |
| 24 |
|
| 25 |
//this is only for PPRo payments |
| 26 |
add_action('woocommerce_after_order_itemmeta', [$this, 'hide_wc_refund_button']); |
| 27 |
//TODO:: add requirements here |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Generic code to fetch payment gateway specific image |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function get_icon() |
| 36 |
{ |
| 37 |
|
| 38 |
//get object $image |
| 39 |
$icons = apply_filters('payplug_payment_icons', [ |
| 40 |
'payplug' => sprintf('<img src="%s" alt="%s" class="payplug-payment-icon" />', esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/' . $this->image), $this->id . " Icon"), |
| 41 |
]); |
| 42 |
|
| 43 |
$icons_str = ''; |
| 44 |
foreach ($icons as $icon) { |
| 45 |
$icons_str .= $icon; |
| 46 |
} |
| 47 |
|
| 48 |
return $icons_str; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function display_notice() |
| 55 |
{ |
| 56 |
$error_message = 'payplug_' . $this->id . '_unauthorized_message'; |
| 57 |
?> |
| 58 |
<div class="notice notice-error is-dismissible"> |
| 59 |
<p><?php echo __( $error_message, 'payplug' ); ?></p> |
| 60 |
</div> |
| 61 |
<?php |
| 62 |
} |
| 63 |
|
| 64 |
public function checkGateway() |
| 65 |
{ |
| 66 |
|
| 67 |
//check if module is enabled |
| 68 |
if(!empty($this->settings['enabled']) && 'no' === $this->settings['enabled']){ |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
//TODO:: MISSING saved configurations |
| 73 |
$account = PayplugWoocommerceHelper::generic_get_account_data_from_options( $this->id ); |
| 74 |
|
| 75 |
//account doesnt have permissions |
| 76 |
if ( |
| 77 |
( isset( $account["payment_methods"] ) ) && |
| 78 |
( empty( $account["payment_methods"][ $this->id ] ) ) && |
| 79 |
( ! $account["payment_methods"][ $this->id ]['enabled'] ) |
| 80 |
) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
//check if it's activated on the BO |
| 85 |
if ( |
| 86 |
! isset( $account['permissions'][ $this->id ] ) || |
| 87 |
( isset( $account['permissions'][ $this->id ] ) && |
| 88 |
!$account['permissions'][ $this->id ] ) |
| 89 |
) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if (is_checkout()) { |
| 94 |
if ( empty( WC()->cart ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
//for backend orders |
| 99 |
if ( ! empty( get_query_var( 'order-pay' ) ) ) { |
| 100 |
$order = wc_get_order( get_query_var( 'order-pay' ) ); |
| 101 |
$items = $order->get_items(); |
| 102 |
|
| 103 |
$country_code_shipping = $order->get_shipping_country(); |
| 104 |
$country_code_billing = $order->get_billing_country(); |
| 105 |
|
| 106 |
$this->order_items_to_cart( WC()->cart, $items ); |
| 107 |
} |
| 108 |
|
| 109 |
$order_amount = $this->get_order_total(); |
| 110 |
|
| 111 |
$this->allowed_country_codes = $account["payment_methods"][ $this->id ]['allowed_countries']; |
| 112 |
$this->get_thresholds_values( $account ); |
| 113 |
|
| 114 |
|
| 115 |
//threshold validations |
| 116 |
if ( ( ! empty( $this->min_thresholds ) && $order_amount < $this->min_thresholds ) || ( ! empty( $this->max_thresholds ) && $order_amount > $this->max_thresholds ) ) { |
| 117 |
$this->description = '<div class="payment_method_oney_x3_with_fees_disabled">' . __( $this->id . '_threshold.', 'payplug' ) . '</div>'; |
| 118 |
|
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if ( empty( $country_code_billing ) || empty( $country_code_shipping ) ) { |
| 123 |
$country_code_shipping = WC()->customer->get_shipping_country(); |
| 124 |
$country_code_billing = WC()->customer->get_billing_country(); |
| 125 |
} |
| 126 |
if (is_array($this->allowed_country_codes)) { |
| 127 |
if ( in_array( "ALL", $this->allowed_country_codes) || empty( $this->allowed_country_codes ) ) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
//check if country is allowed |
| 132 |
if ( in_array( $country_code_billing, $this->allowed_country_codes ) ) { |
| 133 |
return true; |
| 134 |
|
| 135 |
} else { |
| 136 |
$this->description = '<div class="payment_method_oney_x3_with_fees_disabled">' . __( 'Unavailable for the specified country.', 'payplug' ) . '</div>'; |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
public function process_standard_payment($order, $amount, $customer_id) |
| 147 |
{ |
| 148 |
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id(); |
| 149 |
try { |
| 150 |
|
| 151 |
//if there's no auth to process payment |
| 152 |
if (!$this->checkGateway()) { |
| 153 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 154 |
} |
| 155 |
|
| 156 |
$address_data = PayplugAddressData::from_order($order); |
| 157 |
|
| 158 |
$return_url = esc_url_raw($order->get_checkout_order_received_url()); |
| 159 |
|
| 160 |
if (!(substr( $return_url, 0, 4 ) === "http")) { |
| 161 |
$return_url = get_site_url().$return_url; |
| 162 |
} |
| 163 |
|
| 164 |
$payment_data = [ |
| 165 |
'amount' => $amount, |
| 166 |
'currency' => get_woocommerce_currency(), |
| 167 |
'payment_method' => $this->id, |
| 168 |
'billing' => $address_data->get_billing(), |
| 169 |
'shipping' => $address_data->get_shipping(), |
| 170 |
'hosted_payment' => [ |
| 171 |
'return_url' => $return_url, |
| 172 |
'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), |
| 173 |
], |
| 174 |
'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), |
| 175 |
'metadata' => [ |
| 176 |
'order_id' => $order_id, |
| 177 |
'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', |
| 178 |
'domain' => $this->limit_length(esc_url_raw(home_url()), 500), |
| 179 |
], |
| 180 |
"save_card"=> false, |
| 181 |
"force_3ds"=> false |
| 182 |
]; |
| 183 |
|
| 184 |
/** |
| 185 |
* Filter the payment data before it's used |
| 186 |
* |
| 187 |
* @param array $payment_data |
| 188 |
* @param int $order_id |
| 189 |
* @param array $customer_details |
| 190 |
* @param PayplugAddressData $address_data |
| 191 |
*/ |
| 192 |
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); |
| 193 |
$payment = $this->api->payment_create($payment_data); |
| 194 |
|
| 195 |
// Save transaction id for the order |
| 196 |
PayplugWoocommerceHelper::is_pre_30() |
| 197 |
? update_post_meta($order_id, '_transaction_id', $payment->id) |
| 198 |
: $order->set_transaction_id($payment->id); |
| 199 |
|
| 200 |
if (is_callable([$order, 'save'])) { |
| 201 |
$order->save(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Fires once a payment has been created. |
| 206 |
* |
| 207 |
* @param int $order_id Order ID |
| 208 |
* @param PaymentResource $payment Payment resource |
| 209 |
*/ |
| 210 |
\do_action('payplug_gateway_payment_created', $order_id, $payment); |
| 211 |
|
| 212 |
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); |
| 213 |
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); |
| 214 |
|
| 215 |
PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id)); |
| 216 |
|
| 217 |
return [ |
| 218 |
'result' => 'success', |
| 219 |
'redirect' => $payment->hosted_payment->payment_url, |
| 220 |
'cancel' => $payment->hosted_payment->cancel_url, |
| 221 |
]; |
| 222 |
|
| 223 |
} catch (HttpException $e) { |
| 224 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error'); |
| 225 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 226 |
} catch (\Exception $e) { |
| 227 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error'); |
| 228 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Process refund for an order paid with PayPlug gateway. |
| 235 |
* |
| 236 |
* @param int $order_id |
| 237 |
* @param null $amount |
| 238 |
* @param string $reason |
| 239 |
* |
| 240 |
* @return bool|\WP_Error |
| 241 |
*/ |
| 242 |
public function process_refund($order_id, $amount = null, $reason = ''){ |
| 243 |
|
| 244 |
PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id)); |
| 245 |
|
| 246 |
if( !$this->user_logged_in()){ |
| 247 |
PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error'); |
| 248 |
return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug')); |
| 249 |
} |
| 250 |
|
| 251 |
$order = wc_get_order($order_id); |
| 252 |
if (!$order instanceof \WC_Order) { |
| 253 |
PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error'); |
| 254 |
return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id)); |
| 255 |
} |
| 256 |
|
| 257 |
if ($order->get_status() === "cancelled") { |
| 258 |
PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error'); |
| 259 |
return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id)); |
| 260 |
} |
| 261 |
|
| 262 |
$transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id(); |
| 263 |
if (empty($transaction_id)) { |
| 264 |
PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error'); |
| 265 |
return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug')); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* PPRO gateways feature! |
| 270 |
*/ |
| 271 |
if( isset($this->enable_refund) && $this->enable_refund === false){ |
| 272 |
add_action('admin_head', [$this, 'hide_wc_refund_button'] ); |
| 273 |
PayplugGateway::log(__('payplug_refund_disabled_error', 'payplug'), 'error'); |
| 274 |
return new \WP_Error('process_refund_error', __('payplug_refund_disabled_error', 'payplug')); |
| 275 |
} |
| 276 |
|
| 277 |
$customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); |
| 278 |
$data = [ |
| 279 |
'metadata' => [ |
| 280 |
'order_id' => $order_id, |
| 281 |
'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest', |
| 282 |
'refund_from' => 'woocommerce', |
| 283 |
] |
| 284 |
]; |
| 285 |
|
| 286 |
if (!is_null($amount)) { |
| 287 |
$data['amount'] = PayplugWoocommerceHelper::get_payplug_amount($amount); |
| 288 |
} |
| 289 |
|
| 290 |
if (!empty($reason)) { |
| 291 |
$data['metadata']['reason'] = $reason; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Filter the refund data before it's used. |
| 296 |
* |
| 297 |
* @param array $data |
| 298 |
* @param int $order_id |
| 299 |
* @param string $transaction_id |
| 300 |
*/ |
| 301 |
$data = apply_filters('payplug_gateway_refund_data', $data, $order_id, $transaction_id); |
| 302 |
|
| 303 |
try { |
| 304 |
$refund = $this->api->refund_create($transaction_id, $data); |
| 305 |
|
| 306 |
/** |
| 307 |
* Fires once a refund has been created. |
| 308 |
* |
| 309 |
* @param int $order_id Order ID |
| 310 |
* @param RefundResource $refund Refund resource |
| 311 |
* @param string $transaction_id Transaction id |
| 312 |
*/ |
| 313 |
\do_action('payplug_gateway_refund_created', $order_id, $refund, $transaction_id); |
| 314 |
|
| 315 |
$refund_meta_key = sprintf('_pr_%s', wc_clean($refund->id)); |
| 316 |
if (PayplugWoocommerceHelper::is_pre_30()) { |
| 317 |
update_post_meta($order_id, $refund_meta_key, $refund->id); |
| 318 |
} else { |
| 319 |
$order->add_meta_data($refund_meta_key, $refund->id, true); |
| 320 |
$order->save(); |
| 321 |
} |
| 322 |
|
| 323 |
$note = sprintf(__('Refund %s : Refunded %s', 'payplug'), wc_clean($refund->id), wc_price(((int) $refund->amount) / 100)); |
| 324 |
if (!empty($refund->metadata['reason'])) { |
| 325 |
$note .= sprintf(' (%s)', esc_html($refund->metadata['reason'])); |
| 326 |
} |
| 327 |
$order->add_order_note($note); |
| 328 |
|
| 329 |
try { |
| 330 |
$payment = $this->api->payment_retrieve($transaction_id); |
| 331 |
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); |
| 332 |
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); |
| 333 |
} catch (\Exception $e) { |
| 334 |
} |
| 335 |
|
| 336 |
PayplugGateway::log('Refund process complete for the order.'); |
| 337 |
|
| 338 |
return true; |
| 339 |
} catch (HttpException $e) { |
| 340 |
PayplugGateway::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error'); |
| 341 |
|
| 342 |
return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug')); |
| 343 |
} catch (\Exception $e) { |
| 344 |
PayplugGateway::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error'); |
| 345 |
|
| 346 |
return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug')); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
public function hide_wc_refund_button(){ |
| 355 |
global $post; |
| 356 |
$payment_methods = ['giropay', 'satispay', 'sofort', 'ideal', 'mybank']; |
| 357 |
$order = new \WC_Order($post->ID); |
| 358 |
if (in_array($order->get_payment_method(), $payment_methods)) { |
| 359 |
?> |
| 360 |
<script> |
| 361 |
jQuery(function () { |
| 362 |
jQuery('.refund-items').attr("disabled", true); |
| 363 |
}); |
| 364 |
</script> |
| 365 |
<?php |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* refund not possible for PPRO payments |
| 371 |
* |
| 372 |
* @return void |
| 373 |
*/ |
| 374 |
public function refund_not_available($order) |
| 375 |
{ |
| 376 |
if ($this->id === $order->get_payment_method() ) { |
| 377 |
echo "<p style='color: red;'>" . __('payplug_refund_disabled_error', 'payplug') . "</p>"; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Empty the cart and add all order_items |
| 383 |
* |
| 384 |
* @param $cart |
| 385 |
* @param $items |
| 386 |
* @return void |
| 387 |
*/ |
| 388 |
private function order_items_to_cart($cart, $items){ |
| 389 |
$cart->empty_cart(); |
| 390 |
foreach ($items as $item){ |
| 391 |
$cart->add_to_cart($item->get_product_id(), $item->get_quantity()); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* get threshold values for the current payment method |
| 397 |
* @param $account |
| 398 |
* @return void |
| 399 |
*/ |
| 400 |
private function get_thresholds_values($account){ |
| 401 |
|
| 402 |
if(!empty($account["payment_methods"][$this->id]['min_amounts']['EUR'])){ |
| 403 |
$this->min_thresholds = floatval($account["payment_methods"][$this->id]['min_amounts']['EUR']/100); |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
if(!empty($account["payment_methods"][$this->id]['max_amounts']['EUR'])){ |
| 408 |
$this->max_thresholds = floatval($account["payment_methods"][$this->id]['max_amounts']['EUR']/100); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
} |
| 415 |
|