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