| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
use Payplug\PayplugWoocommerce\Controller\IntegratedPayment; |
| 6 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 7 |
|
| 8 |
class PayplugCreditCard extends PayplugGateway |
| 9 |
{ |
| 10 |
public $save_card = false; |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
parent::__construct(); |
| 15 |
|
| 16 |
$this->id = 'payplug'; |
| 17 |
$this->icon = ''; |
| 18 |
$this->has_fields = false; |
| 19 |
$this->method_title = _x('PayPlug', 'Gateway method title', 'payplug'); |
| 20 |
$this->method_description = __('Enable PayPlug for your customers.', 'payplug'); |
| 21 |
$this->new_method_label = __('Pay with another credit card', 'payplug'); |
| 22 |
$this->title = $this->get_configuration()->get_option('payment_methods.configuration.payplug.title'); |
| 23 |
$this->description = $this->get_configuration()->get_option('payment_methods.configuration.payplug.description'); |
| 24 |
$this->save_card = $this->get_configuration()->get_option('payment_methods.configuration.payplug.save_card') && is_user_logged_in(); |
| 25 |
$this->embedded_mode = $this->get_configuration()->get_option('payment_methods.configuration.payplug.embedded_mode'); |
| 26 |
|
| 27 |
$this->supports = [ |
| 28 |
'products', |
| 29 |
'refunds', |
| 30 |
'tokenization', |
| 31 |
'subscriptions', |
| 32 |
'subscription_cancellation', |
| 33 |
'subscription_suspension', |
| 34 |
'subscription_reactivation', |
| 35 |
'subscription_amount_changes', |
| 36 |
'subscription_date_changes', |
| 37 |
'subscription_payment_method_change', |
| 38 |
'subscription_payment_method_change_customer', |
| 39 |
'subscription_payment_method_change_admin', |
| 40 |
'multiple_subscriptions', |
| 41 |
]; |
| 42 |
|
| 43 |
// Ensure the description is not empty to correctly display users's save cards |
| 44 |
if (empty($this->description) && $this->save_card_available()) { |
| 45 |
$this->description = ' '; |
| 46 |
} |
| 47 |
|
| 48 |
if ('test' === $this->mode) { |
| 49 |
$this->description .= " \n"; |
| 50 |
$this->description .= __('You are in TEST MODE. In test mode you can use the card 4242424242424242 with any valid expiration date and CVC.', 'payplug'); |
| 51 |
$this->description = trim($this->description); |
| 52 |
} |
| 53 |
|
| 54 |
//add fields of IP to the description |
| 55 |
if ('integrated' == $this->embedded_mode) { |
| 56 |
$this->has_fields = true; |
| 57 |
} |
| 58 |
|
| 59 |
$this->handle_cc_enabled(); |
| 60 |
|
| 61 |
add_action('wp_enqueue_scripts', [$this, 'scripts']); |
| 62 |
if (PayplugWoocommerceHelper::is_subscriptions_enabled()) { |
| 63 |
add_action( |
| 64 |
'woocommerce_scheduled_subscription_payment_' . $this->id, |
| 65 |
[$this, 'scheduled_subscription_payment'], |
| 66 |
10, |
| 67 |
2 |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* if the plugin is disabled the gateways should be disabled |
| 74 |
* |
| 75 |
* @return mixed|string |
| 76 |
*/ |
| 77 |
private function handle_cc_enabled() |
| 78 |
{ |
| 79 |
if (!empty($this->settings['enabled']) && (bool) $this->settings['enabled']) { |
| 80 |
$active = $this->get_configuration()->get_option('payment_methods.configuration.payplug.active'); |
| 81 |
$this->enabled = ($active === null || (bool) $active) ? 'yes' : 'no'; |
| 82 |
} else { |
| 83 |
$this->enabled = 'no'; |
| 84 |
} |
| 85 |
|
| 86 |
return $this->enabled; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get payment icons. |
| 91 |
* |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function get_icon() |
| 95 |
{ |
| 96 |
$src = ('it_IT' === get_locale()) |
| 97 |
? PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/logos_scheme_PostePay.svg' |
| 98 |
: PAYPLUG_GATEWAY_PLUGIN_URL . '/assets/images/checkout/logos_scheme_CB.svg'; |
| 99 |
|
| 100 |
$icons = apply_filters('payplug_payment_icons', [ |
| 101 |
'payplug' => sprintf('<img src="%s" alt="Visa & Mastercard" class="payplug-payment-icon" />', esc_url($src)), |
| 102 |
]); |
| 103 |
|
| 104 |
$icons_str = ''; |
| 105 |
foreach ($icons as $icon) { |
| 106 |
$icons_str .= $icon; |
| 107 |
} |
| 108 |
|
| 109 |
return $icons_str; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Embedded payment form scripts. |
| 114 |
* |
| 115 |
* Register scripts and additionnal data needed for the |
| 116 |
* embedded payment form. |
| 117 |
*/ |
| 118 |
public function scripts(): void |
| 119 |
{ |
| 120 |
if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order']) && !is_add_payment_method_page() && !isset($_GET['change_payment_method'])) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// If PayPlug is not enabled bail. |
| 125 |
if ('no' == $this->enabled) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// If keys are not set bail. |
| 130 |
if (empty($this->get_api_key($this->mode))) { |
| 131 |
PayplugGateway::log('Keys are not set correctly.'); |
| 132 |
|
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// Register checkout styles. |
| 137 |
wp_register_style('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-checkout.css', [], PAYPLUG_GATEWAY_VERSION); |
| 138 |
wp_enqueue_style('payplug-checkout'); |
| 139 |
|
| 140 |
if ( |
| 141 |
('integrated' == $this->embedded_mode && !PayplugWoocommerceHelper::is_checkout_block()) || |
| 142 |
('integrated' == $this->embedded_mode && is_wc_endpoint_url('order-pay')) |
| 143 |
) { |
| 144 |
$this->integrated_payments_scripts(); |
| 145 |
} |
| 146 |
|
| 147 |
if (('popup' == $this->embedded_mode) && ('payplug' == $this->id || 'american_express' == $this->id) && !PayplugWoocommerceHelper::is_checkout_block()) { |
| 148 |
$this->popup_payments_scripts(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Integrated payment form scripts. |
| 154 |
* |
| 155 |
* Register scripts and additionnal data needed for the |
| 156 |
* embedded payment form. |
| 157 |
*/ |
| 158 |
public function integrated_payments_scripts(): void |
| 159 |
{ |
| 160 |
$translations = [ |
| 161 |
'cardholder' => __('payplug_integrated_payment_cardholder', 'payplug'), |
| 162 |
'your_card' => __('payplug_integrated_payment_your_card', 'payplug'), |
| 163 |
'card_number' => __('payplug_integrated_payment_card_number', 'payplug'), |
| 164 |
'expiration_date' => __('payplug_integrated_payment_expiration_date', 'payplug'), |
| 165 |
'cvv' => __('payplug_integrated_payment_cvv', 'payplug'), |
| 166 |
'one_click' => __('payplug_integrated_payment_oneClick', 'payplug'), |
| 167 |
'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'), |
| 168 |
'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'), |
| 169 |
'nonce' => wp_create_nonce('woocommerce-process_checkout'), |
| 170 |
'mode' => PayplugWoocommerceHelper::check_mode(), // true for TEST, false for LIVE |
| 171 |
'check_payment_url' => \WC_AJAX::get_endpoint('payplug_check_payment'), |
| 172 |
'secureDomain' => PayplugWoocommerceHelper::get_secure_domain(), |
| 173 |
]; |
| 174 |
|
| 175 |
/**x |
| 176 |
* Integrated payments scripts |
| 177 |
*/ |
| 178 |
wp_enqueue_style('payplugIP', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-integrated-payments.css', [], PAYPLUG_GATEWAY_VERSION); |
| 179 |
|
| 180 |
wp_register_script('payplug-domain', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-domain.js', [], 'v1.0'); |
| 181 |
wp_enqueue_script('payplug-domain'); |
| 182 |
wp_register_script('payplug-integrated-payments-api', IP_API, [], 'v1.1', true); |
| 183 |
wp_enqueue_script('payplug-integrated-payments-api'); |
| 184 |
|
| 185 |
wp_register_script('jquery-bind-first', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/jquery.bind-first-0.2.3.min.js', ['jquery'], '1.0.0', true); |
| 186 |
wp_enqueue_script('jquery-bind-first'); |
| 187 |
|
| 188 |
wp_register_script('payplug-integrated-payments', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-integrated-payments.js', ['jquery', 'jquery-bind-first', 'payplug-integrated-payments-api'], 'v1.1', true); |
| 189 |
wp_enqueue_script('payplug-integrated-payments'); |
| 190 |
|
| 191 |
wp_localize_script('payplug-integrated-payments', 'payplug_integrated_payment_params', $translations); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* popup payment form scripts. |
| 196 |
* |
| 197 |
* Register scripts and additionnal data needed for the |
| 198 |
* embedded payment form. |
| 199 |
*/ |
| 200 |
public function popup_payments_scripts(): void |
| 201 |
{ |
| 202 |
//load popup features |
| 203 |
wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true); |
| 204 |
wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', ['jquery', 'payplug'], PAYPLUG_GATEWAY_VERSION, true); |
| 205 |
wp_localize_script('payplug-checkout', 'payplug_checkout_params', [ |
| 206 |
'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'), |
| 207 |
'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'), |
| 208 |
'nonce' => [ |
| 209 |
'checkout' => wp_create_nonce('woocommerce-process_checkout'), |
| 210 |
], |
| 211 |
'is_embedded' => 'redirect' !== $this->embedded_mode, |
| 212 |
]); |
| 213 |
|
| 214 |
wp_enqueue_script('payplug-checkout'); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* extra payment fields |
| 219 |
*/ |
| 220 |
public function payment_fields(): void |
| 221 |
{ |
| 222 |
$description = $this->get_description(); |
| 223 |
|
| 224 |
if (!empty($description)) { |
| 225 |
echo wpautop(wptexturize($description)); |
| 226 |
} |
| 227 |
|
| 228 |
if ('integrated' == $this->embedded_mode) { |
| 229 |
echo IntegratedPayment::template_form($this->save_card); |
| 230 |
} |
| 231 |
|
| 232 |
if ($this->save_card_available()) { |
| 233 |
$this->tokenization_script(); |
| 234 |
$this->saved_payment_methods(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Process the subscription scheduled payment |
| 240 |
*/ |
| 241 |
public function scheduled_subscription_payment($amount, $order) |
| 242 |
{ |
| 243 |
$order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id(); |
| 244 |
$subscription = wcs_get_subscription($order->get_meta('_subscription_renewal')); |
| 245 |
$payplug_parent_meta = $subscription->get_parent()->get_meta('_payplug_metadata'); |
| 246 |
|
| 247 |
if (!$payplug_parent_meta) { |
| 248 |
PayplugGateway::log('Could not find the intial payment data belong to the current user and the current subscription.', 'error'); |
| 249 |
throw new \Exception(__('Invalid payment method.', 'payplug')); |
| 250 |
} |
| 251 |
|
| 252 |
$parent_order = $subscription->get_parent(); |
| 253 |
$parent_tokens = $parent_order->get_payment_tokens(); |
| 254 |
|
| 255 |
if (!empty($parent_tokens)) { |
| 256 |
$token = $parent_tokens[0]; |
| 257 |
} else { |
| 258 |
$token = $this->payplug_api->payment_retrieve($payplug_parent_meta['transaction_id'])->card->id; |
| 259 |
} |
| 260 |
|
| 261 |
if (!$token) { |
| 262 |
PayplugGateway::log('Could not find the payment token or the payment doesn\'t belong to the current user.', 'error'); |
| 263 |
throw new \Exception(__('Invalid payment method.', 'payplug')); |
| 264 |
} |
| 265 |
|
| 266 |
$amount = (int) PayplugWoocommerceHelper::get_payplug_amount($amount); |
| 267 |
|
| 268 |
try { |
| 269 |
$address_data = PayplugAddressData::from_order($order); |
| 270 |
$return_url = esc_url_raw($order->get_checkout_order_received_url()); |
| 271 |
|
| 272 |
if (!(substr($return_url, 0, 4) === 'http')) { |
| 273 |
$return_url = get_site_url() . $return_url; |
| 274 |
} |
| 275 |
|
| 276 |
$payment_data = [ |
| 277 |
'amount' => $amount, |
| 278 |
'currency' => get_woocommerce_currency(), |
| 279 |
'payment_method' => $token, |
| 280 |
'allow_save_card' => false, |
| 281 |
'billing' => $address_data->get_billing(), |
| 282 |
'shipping' => $address_data->get_shipping(), |
| 283 |
'initiator' => 'MERCHANT', |
| 284 |
'hosted_payment' => [ |
| 285 |
'return_url' => $return_url, |
| 286 |
'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()), |
| 287 |
], |
| 288 |
'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')), |
| 289 |
'metadata' => [ |
| 290 |
'order_id' => $order->get_id(), |
| 291 |
'customer_id' => ((int) get_current_user_id() > 0) ? get_current_user_id() : 'guest', |
| 292 |
'domain' => $this->limit_length(esc_url_raw(home_url()), 500), |
| 293 |
'woocommerce_block' => \WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout'), |
| 294 |
'subscription' => 'renewal', |
| 295 |
], |
| 296 |
]; |
| 297 |
|
| 298 |
PayplugGateway::log(sprintf('Processing payment for order #%s', $order_id)); |
| 299 |
PayplugGateway::log(sprintf('Processing payment for subscription #%s', $order->get_meta('_subscription_renewal'))); |
| 300 |
|
| 301 |
/** This filter is documented in src/Gateway/PayplugGateway */ |
| 302 |
$payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data); |
| 303 |
|
| 304 |
$payment = $this->payplug_api->payment_create($payment_data); |
| 305 |
|
| 306 |
// Save transaction id for the order |
| 307 |
PayplugWoocommerceHelper::is_pre_30() |
| 308 |
? update_post_meta($order_id, '_transaction_id', $payment->id) |
| 309 |
: $order->set_transaction_id($payment->id); |
| 310 |
|
| 311 |
if (is_callable([$order, 'save'])) { |
| 312 |
$order->save(); |
| 313 |
} |
| 314 |
|
| 315 |
/** This action is documented in src/Gateway/PayplugGateway */ |
| 316 |
\do_action('payplug_gateway_payment_created', $order_id, $payment); |
| 317 |
|
| 318 |
$metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment); |
| 319 |
PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata); |
| 320 |
|
| 321 |
$this->response->process_payment($payment, true); |
| 322 |
|
| 323 |
if (($payment->__get('is_paid'))) { |
| 324 |
$redirect = $order->get_checkout_order_received_url(); |
| 325 |
} elseif (isset($payment->__get('hosted_payment')->payment_url)) { |
| 326 |
$redirect = $payment->__get('hosted_payment')->payment_url; |
| 327 |
} else { |
| 328 |
$redirect = $return_url; |
| 329 |
} |
| 330 |
|
| 331 |
return [ |
| 332 |
'payment_id' => $payment->id, |
| 333 |
'result' => 'success', |
| 334 |
'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2 |
| 335 |
'redirect' => $redirect, |
| 336 |
]; |
| 337 |
} catch (HttpException $e) { |
| 338 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error'); |
| 339 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 340 |
} catch (\Exception $e) { |
| 341 |
PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error'); |
| 342 |
throw new \Exception(__('Payment processing failed. Please retry.', 'payplug')); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|