Skip to content

Commit

Permalink
Try adding seller_message to failed order notes
Browse files Browse the repository at this point in the history
  • Loading branch information
htdat committed Dec 12, 2024
1 parent 3020129 commit b324b2c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ public function process_payment( $order_id ) {
);

$error_details = esc_html( rtrim( $e->getMessage(), '.' ) );
$error_details = $error_details . '. ' . esc_html( rtrim( $e->get_merchant_message(), '.' ) );

if ( $e instanceof API_Exception && 'card_error' === $e->get_error_type() ) {
// If the payment failed with a 'card_error' API exception, initialize the fraud meta box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,15 @@ public function scheduled_subscription_payment( $amount, $renewal_order ) {
$renewal_order->update_status( 'failed' );

if ( ! empty( $payment_information ) ) {
$error_details = esc_html( rtrim( $e->getMessage(), '.' ) );
$error_details = $error_details . '. ' . esc_html( rtrim( $e->get_merchant_message(), '.' ) );

$note = sprintf(
WC_Payments_Utils::esc_interpolated_html(
/* translators: %1: the failed payment amount, %2: error message */
__(
'A payment of %1$s <strong>failed</strong> to complete with the following message: <code>%2$s</code>.',
'woocommerce-payments'
'woocommerce-payments' // @todo
),
[
'strong' => '<strong>',
Expand All @@ -358,7 +361,7 @@ public function scheduled_subscription_payment( $amount, $renewal_order ) {
wc_price( $amount, [ 'currency' => WC_Payments_Utils::get_order_intent_currency( $renewal_order ) ] ),
$renewal_order
),
esc_html( rtrim( $e->getMessage(), '.' ) )
$error_details
);
$renewal_order->add_order_note( $note );
}
Expand Down
30 changes: 24 additions & 6 deletions includes/exceptions/class-api-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ class API_Exception extends Base_Exception {
/**
* Error type attribute from the server.
*
* @var string
* @var string|null
*/
private $error_type = null;

/**
* Decline code if it is a card error.
*
* @var string
* @var string|null
*/
private $decline_code = null;

/**
* Merchant message. This message should not be shown to shoppers.
*
* @var string|null
*/
private $merchant_message = null;

/**
* Constructor
*
Expand All @@ -42,13 +49,15 @@ class API_Exception extends Base_Exception {
* @param int $http_code HTTP response code.
* @param string $error_type Error type attribute.
* @param string $decline_code The decline code if it is a card error.
* @param string $merchant_message The merchant message. This message should not be shown to shoppers.
* @param int $code The Exception code.
* @param \Throwable $previous The previous exception used for the exception chaining.
*/
public function __construct( $message, $error_code, $http_code, $error_type = null, $decline_code = null, $code = 0, $previous = null ) {
$this->http_code = $http_code;
$this->error_type = $error_type;
$this->decline_code = $decline_code;
public function __construct( $message, $error_code, $http_code, $error_type = null, $decline_code = null, $merchant_message = null, $code = 0, $previous = null ) {
$this->http_code = $http_code;
$this->error_type = $error_type;
$this->decline_code = $decline_code;
$this->merchant_message = $merchant_message;

parent::__construct( $message, $error_code, $code, $previous );
}
Expand Down Expand Up @@ -79,4 +88,13 @@ public function get_error_type() {
public function get_decline_code() {
return $this->decline_code;
}

/**
* Returns the merchant message.
*
* @return string|null Merchant message.
*/
public function get_merchant_message(): ?string {
return $this->merchant_message;
}
}
7 changes: 6 additions & 1 deletion includes/wc-payment-api/class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2358,8 +2358,13 @@ protected function check_response_for_errors( $response ) {
$error_message
);

$merchant_message = null;
if ( 'card_declined' === $error_code && isset( $response_body['error']['payment_intent']['charges']['data'][0]['outcome']['seller_message'] ) ) {
$merchant_message = $response_body['error']['payment_intent']['charges']['data'][0]['outcome']['seller_message'];
}

Logger::error( "$error_message ($error_code)" );
throw new API_Exception( $message, $error_code, $response_code, $error_type, $decline_code );
throw new API_Exception( $message, $error_code, $response_code, $error_type, $decline_code, $merchant_message );
}
}

Expand Down

0 comments on commit b324b2c

Please sign in to comment.