- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2
 
Fix webhook race condition where paid_out event arrives after failed … #91
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
          
     Open
      
      
            sidshas03
  wants to merge
  1
  commit into
  gocardless:develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
sidshas03:fix/webhook-race-condition-issue-7
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| <?php | ||
| 
     | 
||
| use PHPUnit\Framework\TestCase; | ||
| 
     | 
||
| /** | ||
| * Test class for webhook race condition fix. | ||
| * Tests the scenario where a paid_out event arrives after a failed event. | ||
| */ | ||
| class WebhookRaceConditionTests extends TestCase { | ||
| /** | ||
| * Set up our mocked WP functions. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function setUp() : void { | ||
| \WP_Mock::setUp(); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Tear down WP Mock. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function tearDown() : void { | ||
| \WP_Mock::tearDown(); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Test that paid_out event after failed event properly transitions order status. | ||
| */ | ||
| public function test_paid_out_after_failed_event() { | ||
| // Mock the order object | ||
| $order = \Mockery::mock( 'WC_Order' ); | ||
| $order->shouldReceive( 'get_status' )->andReturn( 'failed' ); | ||
| $order->shouldReceive( 'get_order_number' )->andReturn( '12345' ); | ||
| $order->shouldReceive( 'update_status' )->with( 'processing', 'Payment received after initial failure - updating status' )->once(); | ||
| $order->shouldReceive( 'payment_complete' )->with( 'payment_123' )->once(); | ||
| 
     | 
||
| // Mock the gateway | ||
| $gateway = \Mockery::mock( 'WC_GoCardless_Gateway' ); | ||
| $gateway->shouldReceive( 'get_order_from_resource' )->andReturn( $order ); | ||
| 
     | 
||
| // Mock wc_gocardless_get_order_prop | ||
| \WP_Mock::userFunction( 'wc_gocardless_get_order_prop' ) | ||
| ->with( $order, 'id' ) | ||
| ->andReturn( 123 ); | ||
| \WP_Mock::userFunction( 'wc_gocardless_get_order_prop' ) | ||
| ->with( $order, 'payment_method' ) | ||
| ->andReturn( 'gocardless' ); | ||
| 
     | 
||
| // Mock wc_gocardless function | ||
| $wc_gocardless_mock = \Mockery::mock(); | ||
| $wc_gocardless_mock->shouldReceive( 'log' )->andReturn( true ); | ||
| \WP_Mock::userFunction( 'wc_gocardless' )->andReturn( $wc_gocardless_mock ); | ||
| 
     | 
||
| // Create the event payload | ||
| $event = array( | ||
| 'action' => 'paid_out', | ||
| 'links' => array( | ||
| 'payment' => 'payment_123', | ||
| ), | ||
| ); | ||
| 
     | 
||
| // Use reflection to access the protected method | ||
| $reflection = new ReflectionClass( $gateway ); | ||
| $method = $reflection->getMethod( '_process_payment_event' ); | ||
| $method->setAccessible( true ); | ||
| 
     | 
||
| // Execute the method | ||
| $result = $method->invoke( $gateway, $event ); | ||
| 
     | 
||
| // Assert the method returns true | ||
| $this->assertTrue( $result ); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Test that paid_out event after cancelled event properly transitions order status. | ||
| */ | ||
| public function test_paid_out_after_cancelled_event() { | ||
| // Mock the order object | ||
| $order = \Mockery::mock( 'WC_Order' ); | ||
| $order->shouldReceive( 'get_status' )->andReturn( 'cancelled' ); | ||
| $order->shouldReceive( 'get_order_number' )->andReturn( '12345' ); | ||
| $order->shouldReceive( 'update_status' )->with( 'processing', 'Payment received after cancellation - updating status' )->once(); | ||
| $order->shouldReceive( 'payment_complete' )->with( 'payment_123' )->once(); | ||
| 
     | 
||
| // Mock the gateway | ||
| $gateway = \Mockery::mock( 'WC_GoCardless_Gateway' ); | ||
| $gateway->shouldReceive( 'get_order_from_resource' )->andReturn( $order ); | ||
| 
     | 
||
| // Mock wc_gocardless_get_order_prop | ||
| \WP_Mock::userFunction( 'wc_gocardless_get_order_prop' ) | ||
| ->with( $order, 'id' ) | ||
| ->andReturn( 123 ); | ||
| \WP_Mock::userFunction( 'wc_gocardless_get_order_prop' ) | ||
| ->with( $order, 'payment_method' ) | ||
| ->andReturn( 'gocardless' ); | ||
| 
     | 
||
| // Mock wc_gocardless function | ||
| $wc_gocardless_mock = \Mockery::mock(); | ||
| $wc_gocardless_mock->shouldReceive( 'log' )->andReturn( true ); | ||
| \WP_Mock::userFunction( 'wc_gocardless' )->andReturn( $wc_gocardless_mock ); | ||
| 
     | 
||
| // Create the event payload | ||
| $event = array( | ||
| 'action' => 'paid_out', | ||
| 'links' => array( | ||
| 'payment' => 'payment_123', | ||
| ), | ||
| ); | ||
| 
     | 
||
| // Use reflection to access the protected method | ||
| $reflection = new ReflectionClass( $gateway ); | ||
| $method = $reflection->getMethod( '_process_payment_event' ); | ||
| $method->setAccessible( true ); | ||
| 
     | 
||
| // Execute the method | ||
| $result = $method->invoke( $gateway, $event ); | ||
| 
     | 
||
| // Assert the method returns true | ||
| $this->assertTrue( $result ); | ||
| } | ||
| 
     | 
||
| /** | ||
| * Test that paid_out event with normal status doesn't trigger status change. | ||
| */ | ||
| public function test_paid_out_with_normal_status() { | ||
| // Mock the order object | ||
| $order = \Mockery::mock( 'WC_Order' ); | ||
| $order->shouldReceive( 'get_status' )->andReturn( 'processing' ); | ||
| $order->shouldReceive( 'get_order_number' )->andReturn( '12345' ); | ||
| $order->shouldReceive( 'update_status' )->never(); | ||
| $order->shouldReceive( 'payment_complete' )->with( 'payment_123' )->once(); | ||
| 
     | 
||
| // Mock the gateway | ||
| $gateway = \Mockery::mock( 'WC_GoCardless_Gateway' ); | ||
| $gateway->shouldReceive( 'get_order_from_resource' )->andReturn( $order ); | ||
| 
     | 
||
| // Mock wc_gocardless_get_order_prop | ||
| \WP_Mock::userFunction( 'wc_gocardless_get_order_prop' ) | ||
| ->with( $order, 'id' ) | ||
| ->andReturn( 123 ); | ||
| \WP_Mock::userFunction( 'wc_gocardless_get_order_prop' ) | ||
| ->with( $order, 'payment_method' ) | ||
| ->andReturn( 'gocardless' ); | ||
| 
     | 
||
| // Mock wc_gocardless function | ||
| $wc_gocardless_mock = \Mockery::mock(); | ||
| $wc_gocardless_mock->shouldReceive( 'log' )->andReturn( true ); | ||
| \WP_Mock::userFunction( 'wc_gocardless' )->andReturn( $wc_gocardless_mock ); | ||
| 
     | 
||
| // Create the event payload | ||
| $event = array( | ||
| 'action' => 'paid_out', | ||
| 'links' => array( | ||
| 'payment' => 'payment_123', | ||
| ), | ||
| ); | ||
| 
     | 
||
| // Use reflection to access the protected method | ||
| $reflection = new ReflectionClass( $gateway ); | ||
| $method = $reflection->getMethod( '_process_payment_event' ); | ||
| $method->setAccessible( true ); | ||
| 
     | 
||
| // Execute the method | ||
| $result = $method->invoke( $gateway, $event ); | ||
| 
     | 
||
| // Assert the method returns true | ||
| $this->assertTrue( $result ); | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iamdharmesh Code here looks fine to me but curious for your thoughts on this, if there's any scenario where we wouldn't want to change a failed or cancelled order to processing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, @sidshas03.
@dkotter I don’t see an issue with changing a failed or cancelled order to processing. However, I’m not sure what we are fixing here. We already have a
$order->payment_complete()call after these changes, so the order status will be updated to processing, and we don’t need to update it explicitly here. Also, updating it to processing directly will skip the$order->payment_complete()logic due to the internal check, and it will also skip updating the transaction ID in the order.@sidshas03 Could you please provide detailed steps to reproduce the issue? I’ve tried running different scenarios here, but I’m not able to reproduce it on
trunk. Any help in reproducing the issue would be greatly appreciated.Thanks!