Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass additional data to the intent authentication exception #7873

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Pass additional data to the intent authentication exception
15 changes: 11 additions & 4 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,6 @@ public function new_process_payment( WC_Order $order ) {
*
* @return array|null An array with result of payment and redirect URL, or nothing.
* @throws Process_Payment_Exception Error processing the payment.
* @throws Exception Error processing the payment.
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
Expand Down Expand Up @@ -944,7 +943,7 @@ public function process_payment( $order_id ) {

$payment_information = $this->prepare_payment_information( $order );
return $this->process_payment_for_order( WC()->cart, $payment_information );
} catch ( Exception $e ) {
} catch ( Throwable $e ) {
// We set this variable to be used in following checks.
$blocked_due_to_fraud_rules = $e instanceof API_Exception && 'wcpay_blocked_by_fraud_rule' === $e->get_error_code();

Expand Down Expand Up @@ -1033,7 +1032,8 @@ public function process_payment( $order_id ) {

// Re-throw the exception after setting everything up.
// This makes the error notice show up both in the regular and block checkout.
throw new Exception( WC_Payments_Utils::get_filtered_error_message( $e ) );
$additional_data = method_exists( $e, 'getAdditionalData' ) ? $e->getAdditionalData() : [];
throw new Process_Payment_Exception( WC_Payments_Utils::get_filtered_error_message( $e ), 'process_payment_error', 0, null, $additional_data );
}
}

Expand Down Expand Up @@ -1272,7 +1272,14 @@ public function process_payment_for_order( $cart, $payment_information, $schedul
if ( $intent_meta_order_id !== $order_id ) {
throw new Intent_Authentication_Exception(
__( "We're not able to process this payment. Please try again later.", 'woocommerce-payments' ),
'order_id_mismatch'
'order_id_mismatch',
0,
null,
[
'error_code' => 'order_id_mismatch',
'intent_meta_order_id' => $intent_meta_order_id,
'order_id' => $order_id,
]
);
}
}
Expand Down
39 changes: 33 additions & 6 deletions includes/exceptions/class-base-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ abstract class Base_Exception extends Exception {
*/
private $error_code;

/**
* Additional error data.
*
* @var array
*/
private $additional_data = [];

/**
* Constructor, including the usual $message, $code, and $previous,
* and a new parameter $error_code.
*
* @param string $message The Exception message to throw.
* @param string $error_code String error code.
* @param int $code The Exception code.
* @param \Throwable $previous The previous exception used for the exception chaining.
* @param string $message The Exception message to throw.
* @param string $error_code String error code.
* @param int $code The Exception code.
* @param \Throwable $previous The previous exception used for the exception chaining.
* @param array $additional_data The additional data.
*/
public function __construct( $message, $error_code, $code = 0, $previous = null ) {
$this->error_code = $error_code;
public function __construct( $message, $error_code, $code = 0, $previous = null, $additional_data = [] ) {
$this->error_code = $error_code;
$this->additional_data = $additional_data;

parent::__construct( $message, $code, $previous );
}
Expand All @@ -46,4 +55,22 @@ public function __construct( $message, $error_code, $code = 0, $previous = null
public function get_error_code() {
return $this->error_code;
}

/**
* Returns the error code.
*
* @return string Error code, for example 'order_not_found'.
*/
public function getErrorCode() {
return $this->error_code;
}

/**
* Returns additional error data.
*
* @return array
*/
public function getAdditionalData() {
return $this->additional_data;
}
}
Loading