PluginProbe
PayPlug for WooCommerce (Official) / 2.6.3
PayPlug for WooCommerce (Official) v2.6.3
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 / Controller / PayplugGenericGateway.php

PayplugGenericGateway.php in PayPlug for WooCommerce (Official) 2.6.3, at src/Controller/PayplugGenericGateway.php

413 lines 12.8 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\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
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 return true;
142 }
143
144 public function process_standard_payment($order, $amount, $customer_id)
145 {
146 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
147 try {
148
149 //if there's no auth to process payment
150 if (!$this->checkGateway()) {
151 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
152 }
153
154 $address_data = PayplugAddressData::from_order($order);
155
156 $return_url = esc_url_raw($order->get_checkout_order_received_url());
157
158 if (!(substr( $return_url, 0, 4 ) === "http")) {
159 $return_url = get_site_url().$return_url;
160 }
161
162 $payment_data = [
163 'amount' => $amount,
164 'currency' => get_woocommerce_currency(),
165 'payment_method' => $this->id,
166 'billing' => $address_data->get_billing(),
167 'shipping' => $address_data->get_shipping(),
168 'hosted_payment' => [
169 'return_url' => $return_url,
170 'cancel_url' => esc_url_raw($order->get_cancel_order_url_raw()),
171 ],
172 'notification_url' => esc_url_raw(WC()->api_request_url('PayplugGateway')),
173 'metadata' => [
174 'order_id' => $order_id,
175 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
176 'domain' => $this->limit_length(esc_url_raw(home_url()), 500),
177 ],
178 "save_card"=> false,
179 "force_3ds"=> false
180 ];
181
182 /**
183 * Filter the payment data before it's used
184 *
185 * @param array $payment_data
186 * @param int $order_id
187 * @param array $customer_details
188 * @param PayplugAddressData $address_data
189 */
190 $payment_data = apply_filters('payplug_gateway_payment_data', $payment_data, $order_id, [], $address_data);
191 $payment = $this->api->payment_create($payment_data);
192
193 // Save transaction id for the order
194 PayplugWoocommerceHelper::is_pre_30()
195 ? update_post_meta($order_id, '_transaction_id', $payment->id)
196 : $order->set_transaction_id($payment->id);
197
198 if (is_callable([$order, 'save'])) {
199 $order->save();
200 }
201
202 /**
203 * Fires once a payment has been created.
204 *
205 * @param int $order_id Order ID
206 * @param PaymentResource $payment Payment resource
207 */
208 \do_action('payplug_gateway_payment_created', $order_id, $payment);
209
210 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
211 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
212
213 PayplugGateway::log(sprintf('Payment creation complete for order #%s', $order_id));
214
215 return [
216 'result' => 'success',
217 'redirect' => $payment->hosted_payment->payment_url,
218 'cancel' => $payment->hosted_payment->cancel_url,
219 ];
220
221 } catch (HttpException $e) {
222 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
223 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
224 } catch (\Exception $e) {
225 PayplugGateway::log(sprintf('Error while processing order #%s : %s', $order_id, $e->getMessage()), 'error');
226 throw new \Exception(__('Payment processing failed. Please retry.', 'payplug'));
227 }
228
229 }
230
231 /**
232 * Process refund for an order paid with PayPlug gateway.
233 *
234 * @param int $order_id
235 * @param null $amount
236 * @param string $reason
237 *
238 * @return bool|\WP_Error
239 */
240 public function process_refund($order_id, $amount = null, $reason = ''){
241
242 PayplugGateway::log(sprintf('Processing refund for order #%s', $order_id));
243
244 if( !$this->user_logged_in()){
245 PayplugGateway::log(__('You must be logged in with your PayPlug account.', 'payplug'), 'error');
246 return new \WP_Error('process_refund_error', __('You must be logged in with your PayPlug account.', 'payplug'));
247 }
248
249 $order = wc_get_order($order_id);
250 if (!$order instanceof \WC_Order) {
251 PayplugGateway::log(sprintf('The order #%s does not exist.', $order_id), 'error');
252 return new \WP_Error('process_refund_error', sprintf(__('The order %s does not exist.', 'payplug'), $order_id));
253 }
254
255 if ($order->get_status() === "cancelled") {
256 PayplugGateway::log(sprintf('The order #%s cannot be refund.', $order_id), 'error');
257 return new \WP_Error('process_refund_error', sprintf(__('The order %s cannot be refund.', 'payplug'), $order_id));
258 }
259
260 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id();
261 if (empty($transaction_id)) {
262 PayplugGateway::log(sprintf('The order #%s does not have PayPlug transaction ID associated with it.', $order_id), 'error');
263 return new \WP_Error('process_refund_error', __('No PayPlug transaction was found for this order. The refund could not be processed.', 'payplug'));
264 }
265
266 /**
267 * PPRO gateways feature!
268 */
269 if( isset($this->enable_refund) && $this->enable_refund === false){
270 add_action('admin_head', [$this, 'hide_wc_refund_button'] );
271 PayplugGateway::log(__('payplug_refund_disabled_error', 'payplug'), 'error');
272 return new \WP_Error('process_refund_error', __('payplug_refund_disabled_error', 'payplug'));
273 }
274
275 $customer_id = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
276 $data = [
277 'metadata' => [
278 'order_id' => $order_id,
279 'customer_id' => ((int) $customer_id > 0) ? $customer_id : 'guest',
280 'refund_from' => 'woocommerce',
281 ]
282 ];
283
284 if (!is_null($amount)) {
285 $data['amount'] = PayplugWoocommerceHelper::get_payplug_amount($amount);
286 }
287
288 if (!empty($reason)) {
289 $data['metadata']['reason'] = $reason;
290 }
291
292 /**
293 * Filter the refund data before it's used.
294 *
295 * @param array $data
296 * @param int $order_id
297 * @param string $transaction_id
298 */
299 $data = apply_filters('payplug_gateway_refund_data', $data, $order_id, $transaction_id);
300
301 try {
302 $refund = $this->api->refund_create($transaction_id, $data);
303
304 /**
305 * Fires once a refund has been created.
306 *
307 * @param int $order_id Order ID
308 * @param RefundResource $refund Refund resource
309 * @param string $transaction_id Transaction id
310 */
311 \do_action('payplug_gateway_refund_created', $order_id, $refund, $transaction_id);
312
313 $refund_meta_key = sprintf('_pr_%s', wc_clean($refund->id));
314 if (PayplugWoocommerceHelper::is_pre_30()) {
315 update_post_meta($order_id, $refund_meta_key, $refund->id);
316 } else {
317 $order->add_meta_data($refund_meta_key, $refund->id, true);
318 $order->save();
319 }
320
321 $note = sprintf(__('Refund %s : Refunded %s', 'payplug'), wc_clean($refund->id), wc_price(((int) $refund->amount) / 100));
322 if (!empty($refund->metadata['reason'])) {
323 $note .= sprintf(' (%s)', esc_html($refund->metadata['reason']));
324 }
325 $order->add_order_note($note);
326
327 try {
328 $payment = $this->api->payment_retrieve($transaction_id);
329 $metadata = PayplugWoocommerceHelper::extract_transaction_metadata($payment);
330 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
331 } catch (\Exception $e) {
332 }
333
334 PayplugGateway::log('Refund process complete for the order.');
335
336 return true;
337 } catch (HttpException $e) {
338 PayplugGateway::log(sprintf('Refund request error for the order %s from PayPlug API : %s', $order_id, wc_print_r($e->getErrorObject(), true)), 'error');
339
340 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
341 } catch (\Exception $e) {
342 PayplugGateway::log(sprintf('Refund request error for the order %s : %s', $order_id, wc_clean($e->getMessage())), 'error');
343
344 return new \WP_Error('process_refund_error', __('The transaction could not be refunded. Please try again.', 'payplug'));
345 }
346
347
348
349 }
350
351
352 public function hide_wc_refund_button(){
353 global $post;
354 $payment_methods = ['giropay', 'satispay', 'sofort', 'ideal', 'mybank'];
355 $order = new \WC_Order($post->ID);
356 if (in_array($order->get_payment_method(), $payment_methods)) {
357 ?>
358 <script>
359 jQuery(function () {
360 jQuery('.refund-items').attr("disabled", true);
361 });
362 </script>
363 <?php
364 }
365 }
366
367 /**
368 * refund not possible for PPRO payments
369 *
370 * @return void
371 */
372 public function refund_not_available($order)
373 {
374 if ($this->id === $order->get_payment_method() ) {
375 echo "<p style='color: red;'>" . __('payplug_refund_disabled_error', 'payplug') . "</p>";
376 }
377 }
378
379 /**
380 * Empty the cart and add all order_items
381 *
382 * @param $cart
383 * @param $items
384 * @return void
385 */
386 private function order_items_to_cart($cart, $items){
387 $cart->empty_cart();
388 foreach ($items as $item){
389 $cart->add_to_cart($item->get_product_id(), $item->get_quantity());
390 }
391 }
392
393 /**
394 * get threshold values for the current payment method
395 * @param $account
396 * @return void
397 */
398 private function get_thresholds_values($account){
399
400 if(!empty($account["payment_methods"][$this->id]['min_amounts']['EUR'])){
401 $this->min_thresholds = floatval($account["payment_methods"][$this->id]['min_amounts']['EUR']/100);
402
403 }
404
405 if(!empty($account["payment_methods"][$this->id]['max_amounts']['EUR'])){
406 $this->max_thresholds = floatval($account["payment_methods"][$this->id]['max_amounts']['EUR']/100);
407
408 }
409
410 }
411
412 }
413