PluginProbe
PayPlug for WooCommerce (Official) / 1.0.0
PayPlug for WooCommerce (Official) v1.0.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 / Admin / WoocommerceActions.php

WoocommerceActions.php in PayPlug for WooCommerce (Official) 1.0.0, at src/Admin/WoocommerceActions.php

147 lines 4.7 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\Admin;
4
5 // Exit if accessed directly
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
11 use Payplug\PayplugWoocommerce\Gateway\PayplugResponse;
12 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
13
14 /**
15 * Custom WooCommerce actions.
16 *
17 * @package Payplug\PayplugWoocommerce\Admin
18 */
19 class WoocommerceActions {
20
21 const WC_PAYPLUG_RETRIEVE_ACTION = 'retrieve_payplug_transaction';
22
23 public function __construct() {
24 add_filter( 'woocommerce_order_actions', [ $this, 'add_payment_retrieve_action' ] );
25 add_action( 'woocommerce_order_action_' . self::WC_PAYPLUG_RETRIEVE_ACTION, [
26 $this,
27 'handle_payment_retrieve_action'
28 ] );
29 }
30
31 /**
32 * Add custom PayPlug action.
33 *
34 * @param array $actions
35 *
36 * @return array
37 */
38 public function add_payment_retrieve_action( $actions = [] ) {
39 /* @var \WC_Order $theorder */
40 global $theorder;
41
42 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $theorder->id : $theorder->get_id();
43 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $theorder->get_transaction_id();
44 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $theorder->payment_method : $theorder->get_payment_method();
45 if ( 'payplug' !== $payment_method || empty( $transaction_id ) ) {
46 return $actions;
47 }
48
49 $actions[ self::WC_PAYPLUG_RETRIEVE_ACTION ] = __( 'Update PayPlug transaction data', 'payplug' );
50
51 return $actions;
52 }
53
54 /**
55 * Handle PayPlug custom action.
56 *
57 * Retrieve transaction data from PayPlug for the order.
58 *
59 * @param \WC_Order $order
60 *
61 * @return void
62 * @throws \WC_Data_Exception
63 */
64 public function handle_payment_retrieve_action( $order ) {
65 $order_id = PayplugWoocommerceHelper::is_pre_30() ? $order->id : $order->get_id();
66 $payment_method = PayplugWoocommerceHelper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
67 if ( 'payplug' !== $payment_method ) {
68 return;
69 }
70
71 PayplugGateway::log( sprintf( 'Order #%s : Starting retrieve action process.', $order_id ), 'info' );
72
73 $transaction_id = PayplugWoocommerceHelper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id();
74 if ( empty( $transaction_id ) ) {
75 PayplugGateway::log( sprintf( 'Order #%s : Missing transaction id.', $order_id ), 'error' );
76
77 \WC_Admin_Meta_Boxes::add_error( __( 'No transaction ID was found for this order.', 'payplug' ) );
78
79 return;
80 }
81
82 /* @var PayplugGateway $gateway */
83 $gateway = wc_get_payment_gateway_by_order( $order );
84 if (
85 ! $gateway
86 || ! $gateway->is_available()
87 || ! ( $gateway->response instanceof PayplugResponse )
88 ) {
89 PayplugGateway::log( sprintf( 'Order #%s : Error with PayPlug gateway. PayPlug gateway not found or PayPlugResponse class not available.', $order_id ), 'error' );
90
91 \WC_Admin_Meta_Boxes::add_error( __( 'An error occured with PayPlug gateway. Please make sure PayPlug settings are correct.', 'payplug' ) );
92
93 return;
94 }
95
96 try {
97 PayplugGateway::log( sprintf( 'Order #%s : Retrieve payment data for transaction %s.', $order_id, $transaction_id ), 'info' );
98
99 $payment = $gateway->api->payment_retrieve( $transaction_id );
100
101 PayplugGateway::log( sprintf( 'Order #%s : Process payment data.', $order_id ), 'info' );
102
103 $gateway->response->process_payment( $payment );
104 } catch ( \Exception $e ) {
105 PayplugGateway::log(
106 sprintf( 'Order #%s : An error occurred while retrieving the payment data with the message : %s',
107 $order_id,
108 $e->getMessage()
109 )
110 );
111
112 \WC_Admin_Meta_Boxes::add_error( __( 'An error occurred while collecting payment information from PayPlug.', 'payplug' ) );
113
114 return;
115 }
116
117 PayplugGateway::log( sprintf( 'Order #%s : Payment data updated.', $order_id ), 'info' );
118
119 try {
120 PayplugGateway::log( sprintf( 'Order #%s : Retrieve refunds for transaction %s', $order_id, $transaction_id ), 'info' );
121
122 $refunds = $gateway->api->refund_list( $transaction_id );
123
124 if ( ! empty( $refunds ) ) {
125
126 PayplugGateway::log( sprintf( 'Order #%s : Found %d refund(s) for the transaction.', $order_id, count( $refunds ) ), 'info' );
127
128 foreach ( $refunds as $refund ) {
129 $gateway->response->process_refund( $refund );
130 }
131 }
132 } catch ( \Exception $e ) {
133 PayplugGateway::log(
134 sprintf( 'Order #%s : An error occurred while retrieving the refund data with the message : %s',
135 $order_id,
136 $e->getMessage()
137 )
138 );
139
140 \WC_Admin_Meta_Boxes::add_error( __( 'An error occurred while collecting refund information from PayPlug.', 'payplug' ) );
141
142 return;
143 }
144
145 PayplugGateway::log( sprintf( 'Order #%s : Refunds data updated.', $order_id ), 'info' );
146 }
147 }