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

Added better handling when terminal payment fails #10168

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions includes/class-wc-payments-order-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,41 @@ public function mark_terminal_payment_completed( $order, $intent_id, $intent_sta
$this->complete_order_processing( $order, $intent_status );
}


/**
* Mark terminal payment failed function.
*
* @param WC_Order $order Order object.
* @param string $intent_id The ID of the intent associated with this order.
* @param string $intent_status The status of the intent related to this order.
* @param string $charge_id The charge ID related to the intent/order.
* @param string $message Optional message to add to the failed note.
* @param int|null $created Optional timestamp when the payment was created.
*
* @return void
*/
public function mark_terminal_payment_failed( $order, string $intent_id, string $intent_status, string $charge_id, string $message, ?int $created = null ) {
if ( ! $this->order_prepared_for_processing( $order, $intent_id ) ) {
return;
}

$order_status_before_update = $order->get_status();
$this->update_order_status( $order, Order_Status::FAILED );

$note = $this->generate_terminal_payment_failure_note( $intent_id, $charge_id, $message, $this->get_order_amount( $order ), $created );
if ( $this->order_note_exists( $order, $note ) ) {
$this->complete_order_processing( $order );
return;
}

$order->add_order_note( $note );
$this->complete_order_processing( $order, $intent_status );
// Trigger the failed order status hook to send notifications etc only if the order status was not already failed to avoid duplicate notifications.
if ( Order_Status::FAILED === $order_status_before_update ) {
do_action( 'woocommerce_order_status_pending_to_failed_notification', $order->get_id(), $order );
}
}

/**
* Check if a note content has already existed in the order.
*
Expand Down Expand Up @@ -1431,6 +1466,43 @@ private function generate_payment_failure_note( $intent_id, $charge_id, $message

return $note;
}
/**
* Get content for the failure order note and additional message, if included.
*
* @param string $intent_id The ID of the intent associated with this order.
* @param string $charge_id The charge ID related to the intent/order.
* @param string $message Optional message to add to the note.
* @param string $formatted_amount The formatted order total.
* @param int|null $created Optional timestamp when the payment was created.
*
* @return string Note content.
*/
private function generate_terminal_payment_failure_note( $intent_id, $charge_id, $message, $formatted_amount, ?int $created = null ) {
$transaction_url = WC_Payments_Utils::compose_transaction_url( $intent_id, $charge_id );
$timestamp = $created ?? time();
$formatted_time = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );

$note = sprintf(
WC_Payments_Utils::esc_interpolated_html(
/* translators: %1: the authorized amount, %2: WooPayments, %3: transaction ID of the payment, %4: timestamp */
__( 'A terminal payment of %1$s <strong>failed</strong> using %2$s (<a>%3$s</a>) at %4$s.', 'woocommerce-payments' ),
[
'strong' => '<strong>',
'a' => ! empty( $transaction_url ) ? '<a href="' . $transaction_url . '" target="_blank" rel="noopener noreferrer">' : '<code>',
]
),
$formatted_amount,
'WooPayments',
WC_Payments_Utils::get_transaction_url_id( $intent_id, $charge_id ),
$formatted_time
);

if ( ! empty( $message ) ) {
$note .= ' ' . $message;
}

return $note;
}

/**
* Generates the payment authorized order note.
Expand Down
8 changes: 6 additions & 2 deletions includes/class-wc-payments-webhook-processing-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,12 @@ private function process_webhook_payment_intent_failed( $event_body ) {
$event_object = $this->read_webhook_property( $event_data, 'object' );
$intent_id = $this->read_webhook_property( $event_object, 'id' );
$intent_status = $this->read_webhook_property( $event_object, 'status' );

$this->order_service->mark_payment_failed( $order, $intent_id, $intent_status, $charge_id, $this->get_failure_message_from_error( $last_payment_error ) );
if ( Payment_Method::CARD_PRESENT === $payment_method_type ) {
$created = $event_object['created'] ?? null;
$this->order_service->mark_terminal_payment_failed( $order, $intent_id, $intent_status, $charge_id, $this->get_failure_message_from_error( $last_payment_error ), $created );
} else {
$this->order_service->mark_payment_failed( $order, $intent_id, $intent_status, $charge_id, $this->get_failure_message_from_error( $last_payment_error ) );
}
}

/**
Expand Down
Loading