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

Prevent duplicate payment processing #2354

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
12 changes: 7 additions & 5 deletions includes/abstracts/abstract-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,16 @@ public function process_response( $response, $order ) {
$order->payment_complete( $response->id );

/* translators: transaction id */
$message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id );
$order->add_order_note( $message );
$note = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id );
}

if ( 'failed' === $response->status ) {
$localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' );
$order->add_order_note( $localized_message );
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
$note = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' );
throw new WC_Stripe_Exception( print_r( $response, true ), $note );
}

if ( isset( $note ) && ! WC_Stripe_Helper::order_note_exists( $order, $note ) ) {
$order->add_order_note( $note );
}
} else {
$order->set_transaction_id( $response->id );
Expand Down
23 changes: 23 additions & 0 deletions includes/class-wc-stripe-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,27 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order )
$order->update_meta_data( '_stripe_intent_id', $payment_intent_id );
$order->save();
}

/**
* Check if a note content does already exist in the order.
*
* @param WC_Order $order The order object to add the note.
* @param string $note_content Note content.
*
* @return bool true if the note content exists, false otherwise.
*/
public static function order_note_exists( WC_Order $order, string $note_content ): bool {
// Get current notes of the order.
$current_notes = wc_get_order_notes(
[ 'order_id' => $order->get_id() ]
);

foreach ( $current_notes as $current_note ) {
if ( $current_note->content === $note_content ) {
return true;
}
}

return false;
}
}