Merchant API code sample to create an online return policy.
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.CreateOnlineReturnPolicyRequest;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicy;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicy.ItemCondition;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicy.Policy;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicy.Policy.Type;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicy.ReturnMethod;
import com.google.shopping.merchant.accounts.v1.OnlineReturnPolicyServiceClient;
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 create an OnlineReturnPolicy for a given Merchant Center account.
*/
public class CreateOnlineReturnPolicySample {
public static void createOnlineReturnPolicy(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 create the online return policy.
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/{return_policy}
CreateOnlineReturnPolicyRequest request =
CreateOnlineReturnPolicyRequest.newBuilder()
.setParent(parent)
.setOnlineReturnPolicy(
OnlineReturnPolicy.newBuilder()
.setLabel("US Return Policy")
.setReturnPolicyUri("https://www.google.com/returnpolicy-sample")
.addCountries("US")
.setPolicy(Policy.newBuilder().setType(Type.LIFETIME_RETURNS).build())
.addItemConditions(ItemCondition.NEW)
.addReturnMethods(ReturnMethod.IN_STORE)
.setProcessRefundDays(10)
.build())
.build();
System.out.println("Sending create OnlineReturnPolicy request:");
OnlineReturnPolicy response =
onlineReturnPolicyServiceClient.createOnlineReturnPolicy(request);
System.out.println("Retrieved OnlineReturnPolicy below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
createOnlineReturnPolicy(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\CreateOnlineReturnPolicyRequest;
use Google\Shopping\Merchant\Accounts\V1\OnlineReturnPolicy;
use Google\Shopping\Merchant\Accounts\V1\OnlineReturnPolicy\ItemCondition;
use Google\Shopping\Merchant\Accounts\V1\OnlineReturnPolicy\Policy;
use Google\Shopping\Merchant\Accounts\V1\OnlineReturnPolicy\Policy\Type;
use Google\Shopping\Merchant\Accounts\V1\OnlineReturnPolicy\ReturnMethod;
/**
* This class demonstrates how to create an OnlineReturnPolicy for a given
* Merchant Center account.
*/
class CreateOnlineReturnPolicySample
{
/**
* 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);
}
/**
* Creates an online return policy for your Merchant Center account.
*
* @param array $config The configuration data for authentication and account ID.
*/
public static function createOnlineReturnPolicy(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 {
// Defines the policy details, such as the return window.
$policy = new Policy(['type' => Type::LIFETIME_RETURNS]);
// Constructs the full OnlineReturnPolicy object.
$onlineReturnPolicy = new OnlineReturnPolicy([
'label' => 'US Return Policy',
'return_policy_uri' =>
'https://www.google.com/returnpolicy-sample',
'countries' => ['US'],
'policy' => $policy,
'item_conditions' => [ItemCondition::PBNEW],
'return_methods' => [ReturnMethod::IN_STORE],
'process_refund_days' => 10,
]);
// Creates the request object.
$request = new CreateOnlineReturnPolicyRequest([
'parent' => $parent,
'online_return_policy' => $onlineReturnPolicy,
]);
printf("Sending create OnlineReturnPolicy request:%s", PHP_EOL);
$response = $onlineReturnPolicyServiceClient->createOnlineReturnPolicy(
$request
);
printf("Retrieved OnlineReturnPolicy below%s", PHP_EOL);
// The `serializeToJsonString()` method is chosen here for better
// readability of the output.
printf('%s%s', $response->serializeToJsonString(true), 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::createOnlineReturnPolicy($config);
}
}
// Runs the script.
$sample = new CreateOnlineReturnPolicySample();
$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 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.
"""This is a sample for creating an online return policy."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import CreateOnlineReturnPolicyRequest
from google.shopping.merchant_accounts_v1 import OnlineReturnPolicy
from google.shopping.merchant_accounts_v1 import OnlineReturnPolicyServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"
def create_online_return_policy() -> None:
"""Creates an online return policy."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = OnlineReturnPolicyServiceClient(credentials=credentials)
# Creates an OnlineReturnPolicy and populates its attributes.
online_return_policy = OnlineReturnPolicy()
online_return_policy.label = "US Return Policy"
online_return_policy.return_policy_uri = (
"https://www.google.com/returnpolicy-sample"
)
online_return_policy.countries.append("US")
online_return_policy.policy.type_ = (
OnlineReturnPolicy.Policy.Type.LIFETIME_RETURNS
)
online_return_policy.item_conditions.append(
OnlineReturnPolicy.ItemCondition.NEW
)
online_return_policy.return_methods.append(
OnlineReturnPolicy.ReturnMethod.IN_STORE
)
online_return_policy.process_refund_days = 10
# Creates the request.
request = CreateOnlineReturnPolicyRequest(
parent=_PARENT, online_return_policy=online_return_policy
)
# Makes the request and catches and prints any error messages.
try:
print("Sending create OnlineReturnPolicy request:")
response = client.create_online_return_policy(request=request)
print("Retrieved OnlineReturnPolicy below")
print(response)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
create_online_return_policy()