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

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

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