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

Fix incorrect payment intent status check in cancel authorization API endpoint. #7150

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Corrected an issue causing incorrect responses at the cancel authorization API endpoint.
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function cancel_authorization( WP_REST_Request $request ) {

$result = $this->gateway->cancel_authorization( $order );

if ( Intent_Status::SUCCEEDED !== $result['status'] ) {
if ( Intent_Status::CANCELED !== $result['status'] ) {
zmaglica marked this conversation as resolved.
Show resolved Hide resolved
return new WP_Error(
'wcpay_cancel_error',
sprintf(
Expand Down
201 changes: 201 additions & 0 deletions tests/unit/admin/test-class-wc-rest-payments-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,207 @@ public function test_create_terminal_intent_invalid_capture_method() {
$this->assertSame( 500, $data['status'] );
}

public function test_cancel_authorization_success() {
$order = $this->create_mock_order();
$request = new WP_REST_Request( 'POST' );
$request->set_url_params(
[
'order_id' => $order->get_id(),
]
);
$request->set_body_params(
[
'payment_intent_id' => $this->mock_intent_id,
]
);

$this->mock_gateway
->expects( $this->once() )
->method( 'cancel_authorization' )
->with( $this->isInstanceOf( WC_Order::class ) )
->willReturn(
[
'status' => Intent_Status::CANCELED,
'id' => $this->mock_intent_id,
]
);

$mock_intent = WC_Helper_Intention::create_intention(
[
'id' => $this->mock_intent_id,
'status' => Intent_Status::REQUIRES_CAPTURE,
'metadata' => [
'order_id' => $order->get_id(),
],
]
);
$wcpay_request = $this->mock_wcpay_request( Get_Intention::class, 1, $this->mock_intent_id );

$wcpay_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $mock_intent );
$response = $this->controller->cancel_authorization( $request );

$response_data = $response->get_data();

$this->assertEquals( 200, $response->status );
$this->assertEquals(
[
'status' => Intent_Status::CANCELED,
'id' => $this->mock_intent_id,
],
$response_data
);
}
public function test_cancel_authorization_will_fail_if_order_is_incorrect() {
$order = $this->create_mock_order();
$request = new WP_REST_Request( 'POST' );
$request->set_url_params(
[
'order_id' => $order->get_id() + 1,
]
);
$request->set_body_params(
[
'payment_intent_id' => $this->mock_intent_id,
]
);

$this->mock_gateway
->expects( $this->never() )
->method( 'cancel_authorization' );

$this->mock_wcpay_request( Get_Intention::class, 0 );

$response = $this->controller->cancel_authorization( $request );

$this->assertInstanceOf( 'WP_Error', $response );
$data = $response->get_error_data();
$this->assertArrayHasKey( 'status', $data );
$this->assertSame( 404, $data['status'] );
}
public function test_cancel_authorization_will_fail_if_order_is_refunded() {
$order = $this->create_mock_order();
wc_create_refund(
[
'order_id' => $order->get_id(),
'amount' => 10.0,
'line_items' => [],
]
);
$request = new WP_REST_Request( 'POST' );
$request->set_url_params(
[
'order_id' => $order->get_id(),
]
);
$request->set_body_params(
[
'payment_intent_id' => $this->mock_intent_id,
]
);

$this->mock_gateway
->expects( $this->never() )
->method( 'cancel_authorization' );

$this->mock_wcpay_request( Get_Intention::class, 0 );

$response = $this->controller->cancel_authorization( $request );

$this->assertInstanceOf( 'WP_Error', $response );
$data = $response->get_error_data();
$this->assertArrayHasKey( 'status', $data );
$this->assertSame( 400, $data['status'] );
}
public function test_cancel_authorization_will_fail_if_order_does_not_match_with_payment_intent() {
$order = $this->create_mock_order();
$request = new WP_REST_Request( 'POST' );
$request->set_url_params(
[
'order_id' => $order->get_id(),
]
);
$request->set_body_params(
[
'payment_intent_id' => $this->mock_intent_id,
]
);

$mock_intent = WC_Helper_Intention::create_intention(
[
'id' => $this->mock_intent_id,
'status' => Intent_Status::REQUIRES_CAPTURE,
'metadata' => [
'order_id' => $order->get_id() + 1,
],
]
);
$wcpay_request = $this->mock_wcpay_request( Get_Intention::class, 1, $this->mock_intent_id );

$wcpay_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $mock_intent );

$this->mock_gateway
->expects( $this->never() )
->method( 'cancel_authorization' );

$response = $this->controller->cancel_authorization( $request );

$this->assertInstanceOf( 'WP_Error', $response );
$data = $response->get_error_data();
$this->assertArrayHasKey( 'status', $data );
$this->assertSame( 409, $data['status'] );
}

public function test_cancel_authorization_will_fail_if_gateway_fails_to_cancel_authorization() {
$order = $this->create_mock_order();
$request = new WP_REST_Request( 'POST' );
$request->set_url_params(
[
'order_id' => $order->get_id(),
]
);
$request->set_body_params(
[
'payment_intent_id' => $this->mock_intent_id,
]
);

$mock_intent = WC_Helper_Intention::create_intention(
[
'id' => $this->mock_intent_id,
'status' => Intent_Status::REQUIRES_CAPTURE,
'metadata' => [
'order_id' => $order->get_id(),
],
]
);
$wcpay_request = $this->mock_wcpay_request( Get_Intention::class, 1, $this->mock_intent_id );

$wcpay_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $mock_intent );

$this->mock_gateway
->method( 'cancel_authorization' )
->with( $this->isInstanceOf( WC_Order::class ) )
->willReturn(
[
'status' => Intent_Status::REQUIRES_CAPTURE,
'id' => $this->mock_intent_id,
]
);

$response = $this->controller->cancel_authorization( $request );

$this->assertInstanceOf( 'WP_Error', $response );
$data = $response->get_error_data();
$this->assertArrayHasKey( 'status', $data );
$this->assertSame( 502, $data['status'] );
}

private function create_mock_order() {
$charge = $this->create_charge_object();

Expand Down
Loading