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 a possible error when linking accounts due to the delay between Google APIs #2412

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
13 changes: 11 additions & 2 deletions src/API/Google/Ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function get_billing_status(): string {
* @throws Exception When a link is unavailable.
*/
public function accept_merchant_link( int $merchant_id ) {
$link = $this->get_merchant_link( $merchant_id );
$link = $this->get_merchant_link( $merchant_id, 3 );
$link_status = $link->getStatus();
if ( $link_status === ProductLinkInvitationStatus::ACCEPTED ) {
return;
Expand Down Expand Up @@ -325,12 +325,17 @@ private function get_account_details( string $account ): ?array {
/**
* Get the link from a merchant account.
*
* The invitation link may not be available in Google Ads immediately after
* the invitation is sent from Google Merchant Center, so this method offers
* a parameter to specify the number of retries.
*
* @param int $merchant_id Merchant Center account id.
* @param int $attempts_left The number of attempts left to get the link.
*
* @return ProductLinkInvitation
* @throws Exception When the merchant link hasn't been created.
*/
private function get_merchant_link( int $merchant_id ): ProductLinkInvitation {
private function get_merchant_link( int $merchant_id, int $attempts_left = 0 ): ProductLinkInvitation {
$res = ( new AdsProductLinkInvitationQuery() )
->set_client( $this->client, $this->options->get_ads_id() )
->where( 'product_link_invitation.status', [ ProductLinkInvitationStatus::name( ProductLinkInvitationStatus::ACCEPTED ), ProductLinkInvitationStatus::name( ProductLinkInvitationStatus::PENDING_APPROVAL ) ], 'IN' )
Expand All @@ -345,6 +350,10 @@ private function get_merchant_link( int $merchant_id ): ProductLinkInvitation {
}
}

if ( $attempts_left > 0 ) {
return $this->get_merchant_link( $merchant_id, $attempts_left - 1 );
}

throw new Exception( __( 'Merchant link is not available to accept', 'google-listings-and-ads' ) );
}
}
24 changes: 24 additions & 0 deletions tests/Unit/API/Google/AdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,30 @@ public function test_accept_merchant_link_already_accepted() {
$this->ads->accept_merchant_link( self::TEST_MERCHANT_ID );
}

public function test_accept_merchant_link_with_retry_get_merchant_link() {
$this->options->method( 'get_ads_id' )->willReturn( self::TEST_ADS_ID );
$link = new ProductLinkInvitation();
$mc = $this->createMock( MerchantCenterLinkInvitationIdentifier::class );

$mc
->expects( $this->exactly( 4 ) )
->method( 'getMerchantCenterId' )
->willReturnOnConsecutiveCalls(
self::TEST_MERCHANT_ID + 3,
self::TEST_MERCHANT_ID + 2,
self::TEST_MERCHANT_ID + 1,
self::TEST_MERCHANT_ID
);

$link->setStatus( ProductLinkInvitationStatus::PENDING_APPROVAL );
$link->setMerchantCenter( $mc );

$service = $this->generate_mc_link_mock( [ $link ] );
$service->expects( $this->once() )->method( 'updateProductLinkInvitation' );

$this->ads->accept_merchant_link( self::TEST_MERCHANT_ID );
}

public function test_accept_merchant_link() {
$this->options->method( 'get_ads_id' )->willReturn( self::TEST_ADS_ID );
$link = new ProductLinkInvitation();
Expand Down