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

Avoid WooPay duplicate charges when order cannot be completely processed #7968

Merged
merged 2 commits into from
Jan 4, 2024
Merged
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
5 changes: 5 additions & 0 deletions changelog/fix-2199-woopay-duplicate-payments
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: Fix WooPay duplicate charges.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove the additional empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is automatically generated and will be removed in the release process, so that's fine 🙂

10 changes: 7 additions & 3 deletions includes/class-duplicate-payment-prevention-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ public function check_payment_intent_attached_to_order_succeeded( WC_Order $orde
return;
}

$intent_meta_order_id_raw = $intent->get_metadata()['order_id'] ?? '';
$intent_meta_order_id = is_numeric( $intent_meta_order_id_raw ) ? intval( $intent_meta_order_id_raw ) : 0;
if ( $intent_meta_order_id !== $order->get_id() ) {
$intent_meta_order_id_raw = $intent->get_metadata()['order_id'] ?? '';
$intent_meta_order_id = is_numeric( $intent_meta_order_id_raw ) ? intval( $intent_meta_order_id_raw ) : 0;
$intent_meta_order_number_raw = $intent->get_metadata()['order_number'] ?? '';
$intent_meta_order_number = is_numeric( $intent_meta_order_number_raw ) ? intval( $intent_meta_order_number_raw ) : 0;
$paid_on_woopay = filter_var( $intent->get_metadata()['paid_on_woopay'] ?? false, FILTER_VALIDATE_BOOLEAN );
$is_woopay_order = $order->get_id() === $intent_meta_order_number;
if ( ! ( $paid_on_woopay && $is_woopay_order ) && $intent_meta_order_id !== $order->get_id() ) {
Comment on lines +105 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: No changes are necessary but what do you think about renaming the variables as follows:

Suggested change
$intent_meta_order_id_raw = $intent->get_metadata()['order_id'] ?? '';
$intent_meta_order_id = is_numeric( $intent_meta_order_id_raw ) ? intval( $intent_meta_order_id_raw ) : 0;
$intent_meta_order_number_raw = $intent->get_metadata()['order_number'] ?? '';
$intent_meta_order_number = is_numeric( $intent_meta_order_number_raw ) ? intval( $intent_meta_order_number_raw ) : 0;
$paid_on_woopay = filter_var( $intent->get_metadata()['paid_on_woopay'] ?? false, FILTER_VALIDATE_BOOLEAN );
$is_woopay_order = $order->get_id() === $intent_meta_order_number;
if ( ! ( $paid_on_woopay && $is_woopay_order ) && $intent_meta_order_id !== $order->get_id() ) {
$intent_meta_order_id_raw = $intent->get_metadata()['order_id'] ?? '';
$intent_meta_merchant_order_id = is_numeric( $intent_meta_order_id_raw ) ? intval( $intent_meta_order_id_raw ) : 0;
$intent_meta_order_number_raw = $intent->get_metadata()['order_number'] ?? '';
$intent_meta_woopay_order_id = is_numeric( $intent_meta_order_number_raw ) ? intval( $intent_meta_order_number_raw ) : 0;
$paid_on_woopay = filter_var( $intent->get_metadata()['paid_on_woopay'] ?? false, FILTER_VALIDATE_BOOLEAN );
$is_woopay_order = $order->get_id() === $intent_meta_woopay_order_id;
$is_merchant_order = $intent_meta_merchant_order_id === $order->get_id();
if ( ! ( $paid_on_woopay && $is_woopay_order ) && ! $is_merchant_order ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this renaming is beneficial because depending on the context we're analyzing the intent order ID may belong to WooPay order instead of the merchant's, and that may cause confusion. I only named is_woopay_order like that because it seems the order_number was created especially to distinguish WooPay orders and merchant orders.

return;
}

Expand Down
Loading