Skip to content

Commit

Permalink
Add order id mismatch exception (#8855)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsingyuc authored May 27, 2024
1 parent 7e80354 commit aa9c095
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog/add-order-id-mismatch-exception
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Add Order_ID_Mismatch_Exception
14 changes: 9 additions & 5 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use WCPay\Constants\Intent_Status;
use WCPay\Constants\Payment_Type;
use WCPay\Constants\Payment_Method;
use WCPay\Exceptions\{ Add_Payment_Method_Exception, Amount_Too_Small_Exception, Process_Payment_Exception, Intent_Authentication_Exception, API_Exception, Invalid_Address_Exception, Fraud_Prevention_Enabled_Exception, Invalid_Phone_Number_Exception, Rate_Limiter_Enabled_Exception };
use WCPay\Exceptions\{ Add_Payment_Method_Exception, Amount_Too_Small_Exception, Process_Payment_Exception, Intent_Authentication_Exception, API_Exception, Invalid_Address_Exception, Fraud_Prevention_Enabled_Exception, Invalid_Phone_Number_Exception, Rate_Limiter_Enabled_Exception, Order_ID_Mismatch_Exception, Order_Not_Found_Exception };
use WCPay\Core\Server\Request\Cancel_Intention;
use WCPay\Core\Server\Request\Capture_Intention;
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
Expand Down Expand Up @@ -1409,9 +1409,12 @@ public function update_saved_payment_method( $payment_method, $order_id, $is_tes
* @param WCPay\Payment_Information $payment_information Payment info.
* @param bool $scheduled_subscription_payment Used to determinate is scheduled subscription payment to add more fields into API request.
*
* @return array|null An array with result of payment and redirect URL, or nothing.
* @return array|null An array with result of payment and redirect URL, or nothing.
* @throws API_Exception
* @throws Intent_Authentication_Exception When the payment intent could not be authenticated.
* @throws Exception When amount too small.
* @throws Invalid_Address_Exception
* @throws Order_Not_Found_Exception
* @throws Order_ID_Mismatch_Exception When the payment intent could not be authenticated.
* @throws \WCPay\Core\Exceptions\Server\Request\Extend_Request_Exception When request class filter filed to extend request class because of incompatibility.
* @throws \WCPay\Core\Exceptions\Server\Request\Immutable_Parameter_Exception When immutable parameter gets changed in request class.
* @throws \WCPay\Core\Exceptions\Server\Request\Invalid_Request_Parameter_Exception When you send incorrect request value via setters.
Expand Down Expand Up @@ -1514,7 +1517,7 @@ public function process_payment_for_order( $cart, $payment_information, $schedul
$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_id ) {
throw new Intent_Authentication_Exception(
throw new Order_ID_Mismatch_Exception(
sprintf(
/* translators: %s: metadata. We do not need to translate WooPayMeta */
esc_html( __( 'We\'re not able to process this payment. Please try again later. WooPayMeta: intent_meta_order_id: %1$s, order_id: %2$s', 'woocommerce-payments' ) ),
Expand Down Expand Up @@ -1625,7 +1628,7 @@ public function process_payment_for_order( $cart, $payment_information, $schedul
$intent_meta_order_id = is_numeric( $intent_meta_order_id_raw ) ? intval( $intent_meta_order_id_raw ) : 0;

if ( $intent_meta_order_id !== $order_id ) {
throw new Intent_Authentication_Exception(
throw new Order_ID_Mismatch_Exception(
__( "We're not able to process this payment. Please try again later.", 'woocommerce-payments' ),
'order_id_mismatch'
);
Expand Down Expand Up @@ -4487,6 +4490,7 @@ private function handle_afterpay_shipping_requirement( WC_Order $order, Create_A
* @param Create_And_Confirm_Intention $request The request object for creating and confirming intention.
* @param Payment_Information $payment_information The payment information object.
* @param WC_Order $order The order object.
* @throws Invalid_Address_Exception
*
* @return void
*/
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public static function init() {
include_once __DIR__ . '/exceptions/class-fraud-ruleset-exception.php';
include_once __DIR__ . '/exceptions/class-fraud-prevention-enabled-exception.php';
include_once __DIR__ . '/exceptions/class-order-not-found-exception.php';
include_once __DIR__ . '/exceptions/class-order-id-mismatch-exception.php';
include_once __DIR__ . '/exceptions/class-rate-limiter-enabled-exception.php';
include_once __DIR__ . '/constants/class-base-constant.php';
include_once __DIR__ . '/constants/class-country-code.php';
Expand Down
16 changes: 16 additions & 0 deletions includes/exceptions/class-order-id-mismatch-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Class Order_ID_Mismatch_Exception
*
* @package WooCommerce\Payments
*/

namespace WCPay\Exceptions;

defined( 'ABSPATH' ) || exit;

/**
* Exception for throwing an error when payment processing is not possible or fails.
*/
class Order_ID_Mismatch_Exception extends Base_Exception {
}
26 changes: 26 additions & 0 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use WCPay\Exceptions\API_Exception;
use WCPay\Exceptions\Fraud_Prevention_Enabled_Exception;
use WCPay\Exceptions\Process_Payment_Exception;
use WCPay\Exceptions\Order_ID_Mismatch_Exception;
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
use WCPay\Internal\Payment\Factor;
use WCPay\Internal\Payment\Router;
Expand Down Expand Up @@ -2559,6 +2560,31 @@ public function test_process_payment_for_order_rejects_with_cached_minimum_amoun
$this->card_gateway->process_payment_for_order( WC()->cart, $pi );
}

public function test_process_payment_for_order_rejects_with_order_id_mismatch() {
$order = WC_Helper_Order::create_order();
$intent_meta_order_id = 0;
$woopay_intent_id = 'woopay_invalid_intent_id_mock';
$payment_intent = WC_Helper_Intention::create_intention(
[
'status' => 'success',
'metadata' => [ 'order_id' => (string) $intent_meta_order_id ],
]
);

$_POST['platform-checkout-intent'] = $woopay_intent_id;

$payment_information = new Payment_Information( 'pm_test', $order, null, null, null, null, null, '', 'card' );

$this->mock_wcpay_request( Get_Intention::class, 1, $woopay_intent_id )
->expects( $this->once() )
->method( 'format_response' )
->willReturn( $payment_intent );

$this->expectException( 'WCPay\Exceptions\Order_ID_Mismatch_Exception' );
$this->expectExceptionMessage( 'We&#039;re not able to process this payment. Please try again later. WooPayMeta: intent_meta_order_id: ' . $intent_meta_order_id . ', order_id: ' . $order->get_id() );
$this->card_gateway->process_payment_for_order( WC()->cart, $payment_information );
}

public function test_set_mandate_data_to_payment_intent_if_not_required() {
$payment_method = 'woocommerce_payments_sepa_debit';
$order = WC_Helper_Order::create_order();
Expand Down

0 comments on commit aa9c095

Please sign in to comment.