Update PaymentsStatusHandlingTrait.php

Improve error handling in `PaymentsStatusHandlingTrait` to avoid null reference errors

- Added safety checks for purchase_units, payments, captures, and authorizations
- Ensured that methods are only called when necessary data exists
- Skipped processing if data is missing, preventing potential fatal errors without logging
- Enhanced code robustness for production environment
This commit is contained in:
Muhammad Ahmed 2024-08-20 16:46:48 +05:00 committed by GitHub
parent 31afc7263e
commit eb2250b0e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -34,10 +34,26 @@ trait PaymentsStatusHandlingTrait {
Order $order,
WC_Order $wc_order
): void {
if ( $order->intent() === 'CAPTURE' ) {
$this->handle_capture_status( $order->purchase_units()[0]->payments()->captures()[0], $wc_order );
} elseif ( $order->intent() === 'AUTHORIZE' ) {
$this->handle_authorization_status( $order->purchase_units()[0]->payments()->authorizations()[0], $wc_order );
if ($order->intent() === 'CAPTURE') {
$purchase_units = $order->purchase_units();
if (!empty($purchase_units) && isset($purchase_units[0])) {
$payments = $purchase_units[0]->payments();
if ($payments && !empty($payments->captures())) {
$this->handle_capture_status($payments->captures()[0], $wc_order);
}
}
} elseif ($order->intent() === 'AUTHORIZE') {
$purchase_units = $order->purchase_units();
if (!empty($purchase_units) && isset($purchase_units[0])) {
$payments = $purchase_units[0]->payments();
if ($payments && !empty($payments->authorizations())) {
$this->handle_authorization_status($payments->authorizations()[0], $wc_order);
}
}
}
}