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