PluginProbe
PayPlug for WooCommerce (Official) / 2.18.0
PayPlug for WooCommerce (Official) v2.18.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / PayplugCreditCard.php

PayplugCreditCard.php in PayPlug for WooCommerce (Official) 2.18.0, at src/Gateway/PayplugCreditCard.php

345 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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']) && $this->settings['enabled']) {
80 $enabled = !empty($this->settings[$this->id]) ? $this->settings[$this->id] : $this->settings['enabled'];
81 $this->enabled = $enabled ? 'yes' : 'no';
82 } else {
83 $this->enabled = 'yes';
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()
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()
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 ];
173
174 /**x
175 * Integrated payments scripts
176 */
177 wp_enqueue_style('payplugIP', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/payplug-integrated-payments.css', [], PAYPLUG_GATEWAY_VERSION);
178
179 wp_register_script('payplug-domain', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-domain.js', [], 'v1.0');
180 wp_enqueue_script('payplug-domain');
181 wp_register_script('payplug-integrated-payments-api', IP_API, [], 'v1.1', true);
182 wp_enqueue_script('payplug-integrated-payments-api');
183
184 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);
185 wp_enqueue_script('jquery-bind-first');
186
187 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);
188 wp_enqueue_script('payplug-integrated-payments');
189
190 wp_localize_script('payplug-integrated-payments', 'payplug_integrated_payment_params', $translations);
191 }
192
193 /**
194 * popup payment form scripts.
195 *
196 * Register scripts and additionnal data needed for the
197 * embedded payment form.
198 */
199 public function popup_payments_scripts()
200 {
201 //load popup features
202 wp_register_script('payplug', 'https://api.payplug.com/js/1/form.latest.js', [], null, true);
203 wp_register_script('payplug-checkout', PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/js/payplug-checkout.js', ['jquery', 'payplug'], PAYPLUG_GATEWAY_VERSION, true);
204 wp_localize_script('payplug-checkout', 'payplug_checkout_params', [
205 'ajax_url' => \WC_AJAX::get_endpoint('payplug_create_order'),
206 'order_review_url' => \WC_AJAX::get_endpoint('payplug_order_review_url'),
207 'nonce' => [
208 'checkout' => wp_create_nonce('woocommerce-process_checkout'),
209 ],
210 'is_embedded' => 'redirect' !== $this->embedded_mode,
211 ]);
212
213 wp_enqueue_script('payplug-checkout');
214 }
215
216 /**
217 * extra payment fields
218 */
219 public function payment_fields()
220 {
221 $description = $this->get_description();
222
223 if (!empty($description)) {
224 echo wpautop(wptexturize($description));
225 }
226
227 if ('integrated' == $this->embedded_mode) {
228 echo IntegratedPayment::template_form($this->save_card);
229 }
230
231 if ($this->save_card_available()) {
232 $this->tokenization_script();
233 $this->saved_payment_methods();
234 }
235 }
236
237 /**
238 * Process the subscription scheduled payment
239 */
240 public function scheduled_subscription_payment($amount, $order)
241 {
242 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
243 $subscription = wcs_get_subscription($order->get_meta('_subscription_renewal'));
244 $payplug_parent_meta = $subscription->get_parent()->get_meta('_payplug_metadata');
245
246 if (!$payplug_parent_meta) {
247 PayplugGateway::log('Could not find the intial payment data belong to the current user and the current subscription.', 'error');
248 throw new \Exception(__('Invalid payment method.', 'payplug'));
249 }
250
251 $parent_order = $subscription->get_parent();
252 $parent_tokens = $parent_order->get_payment_tokens();
253
254 if (!empty($parent_tokens)) {
255 $token = $parent_tokens[0];
256 } else {
257 $token = $this->payplug_api->payment_retrieve($payplug_parent_meta['transaction_id'])->card->id;
258 }
259
260 if (!$token) {
261 PayplugGateway::log('Could not find the payment token or the payment doesn\'t belong to the current user.', 'error');
262 throw new \Exception(__('Invalid payment method.', 'payplug'));
263 }
264
265 $amount = (int) PayplugWoocommerceHelper::get_payplug_amount($amount);
266
267 try {
268 $address_data = PayplugAddressData::from_order($order);
269 $return_url = esc_url_raw($order->get_checkout_order_received_url());
270
271 if (!(substr($return_url, 0, 4) === 'http')) {
272 $return_url = get_site_url() . $return_url;
273 }
274
275 $payment_data = [
276 'amount' => $amount,
277 'currency' => get_woocommerce_currency(),
278 'payment_method' => $token,
279 'allow_save_card' => false,
280 'billing' => $address_data->get_billing(),
281 'shipping' => $address_data->get_shipping(),
282 'initiator' => 'MERCHANT',
283 'hosted_payment' => [
284 'return_url' => $return_url,
285 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
286 ],
287 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
288 'metadata' => [
289 'order_id' => $order->get_id(),
290 'customer_id' => ((int) get_current_user_id() > 0) ? get_current_user_id() : 'guest',
291 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
292 'woocommerce_block' => \WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout'),
293 'subscription' => 'renewal',
294 ],
295 ];
296
297 PayplugGateway::log(sprintf('Processing payment for order #%s', $order_id));
298 PayplugGateway::log(sprintf('Processing payment for subscription #%s', $order->get_meta('_subscription_renewal')));
299
300 /** This filter is documented in src/Gateway/PayplugGateway */
301 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
302
303 $payment = $this->payplug_api->payment_create($payment_data);
304
305 // Save transaction id for the order
306 PayplugWoocommerceHelper::is_pre_30()
307 ? update_post_meta($order_id, '_transaction_id', $payment->id)
308 : $order->set_transaction_id($payment->id);
309
310 if (is_callable([$order, 'save'])) {
311 $order->save();
312 }
313
314 /** This action is documented in src/Gateway/PayplugGateway */
315 \do_action('payplug_gateway_payment_created', $order_id, $payment);
316
317 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
318 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
319
320 $this->response->process_payment($payment, true);
321
322 if (($payment->__get('is_paid'))) {
323 $redirect = $order->get_checkout_order_received_url();
324 } elseif (isset($payment->__get('hosted_payment')->payment_url)) {
325 $redirect = $payment->__get('hosted_payment')->payment_url;
326 } else {
327 $redirect = $return_url;
328 }
329
330 return [
331 'payment_id' => $payment->id,
332 'result' => 'success',
333 'is_paid' => $payment->__get('is_paid'), // Use for path redirect before DSP2
334 'redirect' => $redirect,
335 ];
336 } catch (HttpException $e) {
337 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
338 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
339 } catch (\Exception $e) {
340 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
341 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
342 }
343 }
344 }
345