# Switch gateway account

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/idempotency)

Moves a card payment source from one gateway account to another.

Use this operation to migrate payment methods between gateway accounts, such as when:

-   consolidating gateway accounts
-   switching from Spreedly to direct gateway integration
-   aligning payment sources with regional/business unit gateway configurations

#### Supported gateways[](#supported-gateways)

See the **Use Cases** section for the list of supported source-destination gateway combinations.

#### Impact on `reference_id`[](#impact-on-referenceid)

This operation updates the [`reference_id`](/docs/api/payment_sources/payment_source-object#reference_id) attribute of the payment source. In case you are using this value for any downstream system integrations, you will need to update the `reference_id` for the payment source in the downstream system to the new value.

### Prerequisites & Constraints

-   The [`payment_source.type`](/docs/api/payment_sources/payment_source-object#type) must be `card`.
-   The destination payment gateway account must be active and not [archived](https://www.chargebee.com/docs/payments/2.0/kb/billing/archiving-gateways).

### Impacts

**Payment source**

-   The following attributes are updated:
-   [`gateway_account_id`](/docs/api/payment_sources/payment_source-object#gateway_account_id)
-   [`gateway`](/docs/api/payment_sources/payment_source-object#gateway)
-   [`reference_id`](/docs/api/payment_sources/payment_source-object#reference_id)
-   The role ([primary](/docs/api/customers/customer-object#primary_payment_source_id) or [backup](/docs/api/customers/customer-object#backup_payment_source_id)) of the payment source is not changed.

### Use Cases

#### Supported source-destination gateway combinations[](#supported-source-destination-gateway-combinations)

The API supports only the following source-destination gateway combinations:

Source

Destination

Any of the gateways via Spreedly. These include [Authorize.net](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/authorize), [Bambora](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/bambora), [BlueSnap](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/bluesnap-spreedly), [Moneris](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/moneris), [Orbital (Chase Paymentech)](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/orbital), [Paymill](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/paymill), [PayPal Payments Pro](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/paypal_payments_pro), [PayPal Payflow Pro](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/paypal_payflow_pro), [SagePay](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/sagepay), [Worldline Online Payments](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/ingenico), [Worldpay](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/worldpay).

-   Any of the gateways via Spreedly. OR
-   Any of the following gateways: [Stripe](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/stripe), [Authorize.net (direct integration)](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/authorize-direct), [Braintree](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/braintree), [BlueSnap (direct integration)](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/bluesnap), and [Worldpay (direct integration)](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/worldpay-direct).

[PayPal Express Checkout](https://www.chargebee.com/docs/payments/2.0/payment-gateways-and-configuration/paypal_express_checkout)

[PayPal Commerce](https://www.chargebee.com/docs/payments/1.0/payment-gateways-and-configuration/paypal-commerce)

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/payment_sources/pm___test__XpbTXGTSRp4XbjFJ/switch_gateway_account \
     -u {site_api_key}:\
     -d gateway_account_id="gw___test__5SK2lMpwSRp4Wrl37"
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = PaymentSource.SwitchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ")
		.GatewayAccountId("gw___test__5SK2lMpwSRp4Wrl37")
		.Request();

Customer customer = result.Customer;
PaymentSource paymentSource = result.PaymentSource;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    paymentsourceAction "github.com/chargebee/chargebee-go/v3/actions/paymentsource"
    "github.com/chargebee/chargebee-go/v3/models/paymentsource"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := paymentsourceAction.SwitchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ", &paymentsource.SwitchGatewayAccountRequestParams{
        GatewayAccountId : "gw___test__5SK2lMpwSRp4Wrl37",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        PaymentSource := res.PaymentSource
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.PaymentSourceSwitchGatewayAccountRequest{
    GatewayAccountId : "gw___test__5SK2lMpwSRp4Wrl37",
}
  res, err := client.PaymentSource.SwitchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Customer := res.Customer
        PaymentSource := res.PaymentSource
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = PaymentSource.switchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ")
            .gatewayAccountId("gw___test__5SK2lMpwSRp4Wrl37")
            .request();

        Customer customer = result.customer();
        PaymentSource paymentSource = result.paymentSource();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.customer.Customer;
import com.chargebee.v4.models.paymentSource.PaymentSource;
import com.chargebee.v4.models.paymentSource.params.PaymentSourceSwitchGatewayAccountParams;
import com.chargebee.v4.models.paymentSource.responses.PaymentSourceSwitchGatewayAccountResponse;

public class PaymentSourceSwitchGatewayAccount {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        PaymentSourceSwitchGatewayAccountParams params = PaymentSourceSwitchGatewayAccountParams.builder()
            .gatewayAccountId("gw___test__5SK2lMpwSRp4Wrl37")
            .build();

        PaymentSourceSwitchGatewayAccountResponse response = client
            .paymentSources()
            .switchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ", params);

        Customer customer = response.getCustomer();
        PaymentSource paymentSource = response.getPaymentSource();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.paymentSource.switchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ", {
        gateway_account_id: "gw___test__5SK2lMpwSRp4Wrl37"
    });

    console.log(result);
    const customer = result.customer;
    const paymentSource = result.payment_source;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->paymentSource()->switchGatewayAccount("pm___test__XpbTXGTSRp4XbjFJ", [
    "gateway_account_id" => "gw___test__5SK2lMpwSRp4Wrl37"
]);
$customer = $result->customer;
$paymentSource = $result->payment_source;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.PaymentSource.switch_gateway_account("pm___test__XpbTXGTSRp4XbjFJ",
    cb_client.PaymentSource.SwitchGatewayAccountParams(
        gateway_account_id="gw___test__5SK2lMpwSRp4Wrl37"
    )
)
customer = response.customer
payment_source = response.payment_source
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::PaymentSource.switch_gateway_account("pm___test__XpbTXGTSRp4XbjFJ",{
  :gateway_account_id => "gw___test__5SK2lMpwSRp4Wrl37"
})

customer = result.customer
payment_source = result.payment_source
```

## Sample Response

```json
{
  "customer": {
    "allow_direct_debit": false,
    "auto_collection": "on",
    "card_status": "valid",
    "created_at": 1517487256,
    "deleted": false,
    "excess_payments": 0,
    "first_name": "Mark",
    "id": "__test__XpbTXGTSRp4X57FI",
    "last_name": "Henry",
    "net_term_days": 0,
    "object": "customer",
    "payment_method": {
      "gateway": "pin",
      "gateway_account_id": "gw___test__5SK2lMpwSRp4Wrl37",
      "object": "payment_method",
      "reference_id": "UKZpy3LgND7f5jOIeAjwXbRsbEx",
      "status": "valid",
      "type": "card"
    },
    "pii_cleared": "active",
    "preferred_currency_code": "USD",
    "primary_payment_source_id": "pm___test__XpbTXGTSRp4XbjFJ",
    "promotional_credits": 0,
    "refundable_credits": 0,
    "resource_version": 1517487258458,
    "taxability": "taxable",
    "unbilled_charges": 0,
    "updated_at": 1517487258
  },
  "payment_source": {
    "card": {
      "brand": "visa",
      "expiry_month": 12,
      "expiry_year": 2022,
      "funding_type": "not_known",
      "iin": "411111",
      "last4": "1111",
      "masked_number": "************1111",
      "object": "card"
    },
    "created_at": 1517487258,
    "customer_id": "__test__XpbTXGTSRp4X57FI",
    "deleted": false,
    "gateway": "pin",
    "gateway_account_id": "gw___test__5SK2lMpwSRp4Wrl37",
    "id": "pm___test__XpbTXGTSRp4XbjFJ",
    "object": "payment_source",
    "reference_id": "UKZpy3LgND7f5jOIeAjwXbRsbEx",
    "resource_version": 1517487258565,
    "status": "valid",
    "type": "card",
    "updated_at": 1517487258
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/payment_sources/{cust-payment-source-id}/switch_gateway_account

## Input Parameters

- `gateway_account_id` (required, string, max chars=50)
  The gateway account you want to switch to.

## Returns

- `customer` (Customer object)
  Resource object representing customer

- `payment_source` (Payment source object)
  Resource object representing payment\_source
