Merchant API code sample to list online return policies.
Java
// 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.
package shopping.merchant.samples.accounts.onlinereturnpolicy.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.ListOnlineReturnPoliciesRequest;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicy;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicyServiceClient;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicyServiceClient.ListOnlineReturnPoliciesPagedResponse;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicyServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to list the OnlineReturnPolicy for a given Merchant Center account.
*/
public class ListOnlineReturnPoliciesSample {
public static void listOnlineReturnPolicies(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
OnlineReturnPolicyServiceSettings onlineReturnPolicyServiceSettings =
OnlineReturnPolicyServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify where to list the online return policies.
// The parent has the format: accounts/{account}
String parent = "accounts/" + config.getAccountId().toString();
// Calls the API and catches and prints any network failures/errors.
try (OnlineReturnPolicyServiceClient onlineReturnPolicyServiceClient =
OnlineReturnPolicyServiceClient.create(onlineReturnPolicyServiceSettings)) {
// The name has the format: accounts/{account}/onlineReturnPolicies
ListOnlineReturnPoliciesRequest request =
ListOnlineReturnPoliciesRequest.newBuilder().setParent(parent).setPageSize(20).build();
System.out.println("Sending List OnlineReturnPolicies request:");
ListOnlineReturnPoliciesPagedResponse onlineReturnPolicyPagedResponse =
onlineReturnPolicyServiceClient.listOnlineReturnPolicies(request);
int count = 0;
// Iterates through the OnlineReturnPolicy and prints each OnlineReturnPolicy.
for (OnlineReturnPolicy onlineReturnPolicy : onlineReturnPolicyPagedResponse.iterateAll()) {
System.out.println(onlineReturnPolicy);
count++;
}
System.out.println("The following count of OnlineReturnPolicies is returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listOnlineReturnPolicies(config);
}
}
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\Client\OnlineReturnPolicyServiceClient;
use Google\Shopping\Merchant\Accounts\V1\ListOnlineReturnPoliciesRequest;
/**
* This class demonstrates how to list the OnlineReturnPolicies for a given
* Merchant Center account.
*/
class ListOnlineReturnPoliciesSample
{
/**
* A helper function to create the parent string.
*
* @param string $accountId The merchant account ID.
*
* @return string The parent has the format: `accounts/{account}`
*/
private static function getParent(string $accountId): string
{
return sprintf('accounts/%s', $accountId);
}
/**
* Lists the online return policies for your Merchant Center account.
*
* @param array $config The configuration data for authentication and account ID.
*/
public static function listOnlineReturnPolicies(array $config): void
{
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$onlineReturnPolicyServiceClient =
new OnlineReturnPolicyServiceClient($options);
// Creates the parent account resource name.
$parent = self::getParent($config['accountId']);
// Calls the API and catches and prints any network failures/errors.
try {
// Creates the request object.
$request = new ListOnlineReturnPoliciesRequest([
'parent' => $parent,
'page_size' => 20,
]);
printf("Sending List OnlineReturnPolicies request:%s", PHP_EOL);
$pagedResponse = $onlineReturnPolicyServiceClient->listOnlineReturnPolicies(
$request
);
$count = 0;
// Iterates through all elements of the response and prints them.
foreach ($pagedResponse->iterateAllElements() as $onlineReturnPolicy) {
// The `serializeToJsonString()` method is chosen here for better
// readability of the output.
printf(
'%s%s',
$onlineReturnPolicy->serializeToJsonString(true),
PHP_EOL
);
$count++;
}
printf(
'The following count of OnlineReturnPolicies is returned: %s',
PHP_EOL
);
printf('%d%s', $count, PHP_EOL);
} catch (ApiException $e) {
printf('%s%s', $e->getMessage(), PHP_EOL);
}
}
/**
* Helper to execute the sample.
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::listOnlineReturnPolicies($config);
}
}
// Runs the script.
$sample = new ListOnlineReturnPoliciesSample();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# 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
#
# 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.
"""Lists the online return policies for a given account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import ListOnlineReturnPoliciesRequest
from google.shopping.merchant_accounts_v1 import OnlineReturnPolicyServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"
def list_online_return_policies() -> None:
"""Lists the online return policies for a given account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = OnlineReturnPolicyServiceClient(credentials=credentials)
# Creates the request.
# The parent has the format: accounts/{account}
request = ListOnlineReturnPoliciesRequest(parent=_PARENT, page_size=20)
# Makes the request and catches and prints any error messages.
try:
print("Sending List OnlineReturnPolicies request:")
# Issues the request and iterates over the response.
response = client.list_online_return_policies(request=request)
count = 0
# Iterates through the OnlineReturnPolicy and prints each one.
for online_return_policy in response:
print(online_return_policy)
count += 1
print("The following count of OnlineReturnPolicies is returned: ")
print(count)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
list_online_return_policies()