| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Admin; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 10 |
use Payplug\PayplugWoocommerce\Gateway\PayplugResponse; |
| 11 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Custom WooCommerce actions. |
| 15 |
*/ |
| 16 |
class WoocommerceActions |
| 17 |
{ |
| 18 |
const WC_PAYPLUG_RETRIEVE_ACTION = 'retrieve_payplug_transaction'; |
| 19 |
|
| 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 |
} |
| 28 |
|
| 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'); |
| 47 |
|
| 48 |
return $actions; |
| 49 |
} |
| 50 |
|
| 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) |
| 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')); |
| 74 |
|
| 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')); |
| 86 |
|
| 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'); |
| 93 |
|
| 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')); |
| 104 |
|
| 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')); |
| 126 |
|
| 127 |
return; |
| 128 |
} |
| 129 |
PayplugGateway::log(sprintf('Order #%s : Refunds data updated.', $order_id), 'info'); |
| 130 |
} |
| 131 |
} |
| 132 |
|