Merchant API code sample to accept terms of service.

### Java

    // Copyright 2024 Google LLC
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    //     https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.

    package shopping.merchant.samples.accounts.termsofservices.v1;
    import com.google.api.gax.core.FixedCredentialsProvider;
    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.shopping.merchant.accounts.v1.AcceptTermsOfServiceRequest;
    import com.google.shopping.merchant.accounts.v1.TermsOfServiceServiceClient;
    import com.google.shopping.merchant.accounts.v1.TermsOfServiceServiceSettings;
    import shopping.merchant.samples.utils.Authenticator;
    import shopping.merchant.samples.utils.Config;

    /** This class demonstrates how to accept the TermsOfService agreement in a given account. */
    public class AcceptTermsOfServiceSample {

      public static void acceptTermsOfService(String accountId, String tosVersion, String regionCode)
          throws Exception {

        // Obtains OAuth token based on the user's configuration.
        GoogleCredentials credential = new Authenticator().authenticate();

        // Creates service settings using the credentials retrieved above.
        TermsOfServiceServiceSettings tosServiceSettings =
            TermsOfServiceServiceSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credential))
                .build();

        // Calls the API and catches and prints any network failures/errors.
        try (TermsOfServiceServiceClient tosServiceClient =
            TermsOfServiceServiceClient.create(tosServiceSettings)) {

          // The parent has the format: accounts/{account}
          AcceptTermsOfServiceRequest request =
              AcceptTermsOfServiceRequest.newBuilder()
                  .setName(String.format("termsOfService/%s", tosVersion))
                  .setAccount(String.format("accounts/%s", accountId))
                  .setRegionCode(regionCode)
                  .build();

          System.out.println("Sending request to accept terms of service...");
          tosServiceClient.acceptTermsOfService(request);

          System.out.println("Successfully accepted terms of service.");
        } catch (Exception e) {
          System.out.println(e);
        }
      }

      public static void main(String[] args) throws Exception {
        Config config = Config.load();

        // See GetTermsOfServiceAgreementStateSample to understand how to check which version of the
        // terms of service needs to be accepted, if any.
        // Likewise, if you know that the terms of service needs to be accepted, you can also simply
        // call RetrieveLatestTermsOfService to get the latest version of the terms of service.
        // Region code is either a country when the ToS applies specifically to that country or 001 when
        // it applies globally.
        acceptTermsOfService(config.getAccountId().toString(), "VERSION_HERE", "REGION_CODE_HERE");
      }
    }

### PHP

    <?php
    /**
     * Copyright 2025 Google LLC
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     https://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    require_once __DIR__ . '/../../../../vendor/autoload.php';
    require_once __DIR__ . '/../../../Authentication/Authentication.php';
    require_once __DIR__ . '/../../../Authentication/Config.php';
    use Google\ApiCore\ApiException;
    use Google\Shopping\Merchant\Accounts\V1\AcceptTermsOfServiceRequest;
    use Google\Shopping\Merchant\Accounts\V1\Client\TermsOfServiceServiceClient;

    /**
     * Demonstrates how to accept the TermsOfService agreement in a given account.
     */
    class AcceptTermsOfService
    {

        /**
         * Accepts the Terms of Service agreement.
         *
         * @param string $accountId The account ID.
         * @param string $tosVersion The Terms of Service version.
         * @param string $regionCode The region code.
         * @return void
         */
        public static function acceptTermsOfService($accountId, $tosVersion, $regionCode): void
        {
            // Get OAuth credentials.
            $credentials = Authentication::useServiceAccountOrTokenFile();

            // Create client options.
            $options = ['credentials' => $credentials];

            // Create a TermsOfServiceServiceClient.
            $tosServiceClient = new TermsOfServiceServiceClient($options);

            try {
                // Prepare the request.
                $request = new AcceptTermsOfServiceRequest([
                    'name' => sprintf("termsOfService/%s", $tosVersion),
                    'account' => sprintf("accounts/%s", $accountId),
                    'region_code' => $regionCode,
                ]);

                print "Sending request to accept terms of service...\n";
                $tosServiceClient->acceptTermsOfService($request);

                print "Successfully accepted terms of service.\n";
            } catch (ApiException $e) {
                print $e->getMessage();
            }
        }

        /**
         * Helper to execute the sample.
         *
         * @return void
         */
        public function callSample(): void
        {
            $config = Config::generateConfig();

            // Replace with actual values.
            $tosVersion = "132";
            $regionCode = "US";

            self::acceptTermsOfService($config['accountId'], $tosVersion, $regionCode);
        }
    }

    // Run the script
    $sample = new AcceptTermsOfService();
    $sample->callSample();

### Python

    # -*- coding: utf-8 -*-
    # Copyright 2024 Google LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    """Module for accepting the Terms of Service agreement for a given account."""

    from examples.authentication import configuration
    from examples.authentication import generate_user_credentials
    from google.shopping.merchant_accounts_v1 import AcceptTermsOfServiceRequest
    from google.shopping.merchant_accounts_v1 import TermsOfServiceServiceClient

    # Replace with your actual values.
    _ACCOUNT_ID = configuration.Configuration().read_merchant_info()
    _TOS_VERSION = (  # Replace with the Terms of Service version to accept
        "VERSION_HERE"
    )
    _REGION_CODE = "US"  # Replace with the region code


    def accept_terms_of_service():
      """Accepts the Terms of Service agreement for a given account."""

      credentials = generate_user_credentials.main()
      client = TermsOfServiceServiceClient(credentials=credentials)

      # Construct the request
      request = AcceptTermsOfServiceRequest(
          name=f"termsOfService/{_TOS_VERSION}",
          account=f"accounts/{_ACCOUNT_ID}",
          region_code=_REGION_CODE,
      )

      try:
        print("Sending request to accept terms of service...")
        client.accept_terms_of_service(request=request)
        print("Successfully accepted terms of service.")
      except RuntimeError as e:
        print(e)


    if __name__ == "__main__":
      accept_terms_of_service()