Delete wc order (WIP)

This commit is contained in:
Emili Castells Guasch 2025-04-25 18:07:07 +02:00
parent f42d87bb29
commit 52763fc67e
No known key found for this signature in database
3 changed files with 11 additions and 14 deletions

View file

@ -188,18 +188,6 @@ class CardFieldsModule implements ServiceModule, ExtendingModule, ExecutableModu
$logger->warning( "Could not capture order {$order->id()}" );
// Deletes WC order if it exists in PayPal order.
$wc_order_id = (int)$order->purchase_units()[0]->custom_id();
if( $wc_order_id ) {
$wc_order = wc_get_order( $wc_order_id );
if ( $wc_order instanceof WC_Order ) {
$result = $this->is_hpos_enabled() ? $wc_order->delete(true) : wp_delete_post($wc_order_id, true);
if($result) {
$logger->info( "Deleting failed WC order #$wc_order_id" );
}
}
}
throw new DomainException( esc_html__( 'Could not capture the PayPal order.', 'woocommerce-paypal-payments' ) );
}
}

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use DomainException;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
@ -543,7 +544,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$error
)
);
} catch ( Exception $error ) {
} catch ( DomainException $error ) {
return $this->handle_payment_failure( $wc_order, $error, true );
}
catch ( Exception $error ) {
return $this->handle_payment_failure( $wc_order, $error );
}
}

View file

@ -24,9 +24,10 @@ trait ProcessPaymentTrait {
*
* @param WC_Order|null $wc_order The order.
* @param Exception $error The error causing the failure.
* @param bool $delete_order Whether to delete the order.
* @return array The data that can be returned by the gateway process_payment method.
*/
protected function handle_payment_failure( ?WC_Order $wc_order, Exception $error ): array {
protected function handle_payment_failure( ?WC_Order $wc_order, Exception $error, bool $delete_order = false ): array {
$this->logger->error( 'Payment failed: ' . $this->format_exception( $error ) );
if ( $wc_order ) {
@ -34,6 +35,10 @@ trait ProcessPaymentTrait {
'failed',
$this->format_exception( $error )
);
if($delete_order) {
$wc_order->delete( true );
}
}
$this->session_handler->destroy_session_data();