> ## Documentation Index
> Fetch the complete documentation index at: https://better-auth-razorpay.amankushwaha.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Customers

> Manage Razorpay customers automatically on sign-up or manually through the Better Auth plugin.

The plugin can create a Razorpay customer automatically when a user signs up, keep the email in sync, and expose full customer management endpoints. This page covers automatic creation, manual operations, organization customers, bank accounts, saved card tokens, and React hooks.

## Automatic customer creation on sign-up

Set `createCustomerOnSignUp: true` in the plugin options. When a user completes sign-up, the plugin creates a Razorpay customer with `fail_existing: "0"` (so an existing customer with the same details is returned instead of duplicated), stores the Razorpay customer ID in `user.razorpayCustomerId`, and then calls `onCustomerCreate`.

```ts auth.ts theme={null}
import { betterAuth } from "better-auth";
import { razorpay } from "@amank-root/better-auth-razorpay";

export const auth = betterAuth({
  plugins: [
    razorpay({
      keyId: process.env.RAZORPAY_KEY_ID!,
      keySecret: process.env.RAZORPAY_KEY_SECRET!,
      createCustomerOnSignUp: true,
      getCustomerCreateParams: async (user, ctx) => {
        return {
          contact: user.phone || "+919000090000",
          notes: {
            internalId: user.id,
          },
        };
      },
      onCustomerCreate: async ({ razorpayCustomer, user }, ctx) => {
        console.log("Razorpay customer created:", razorpayCustomer.id);
      },
    }),
  ],
});
```

**createCustomerOnSignUp** `boolean` (default: false)

Whether to create a Razorpay customer automatically when a user signs up.

**getCustomerCreateParams** `function`

Async function that receives the user object and context, and returns partial customer create parameters such as `contact` or `notes`.

**onCustomerCreate** `function`

Callback invoked after the Razorpay customer is created and the ID is stored on the user record.

## Email sync

When a user's email is updated through Better Auth, the plugin automatically syncs the new email to the linked Razorpay customer.

## Manual customer operations

You can create, edit, list, and fetch customers directly through the client or server API.

### Create a customer

```ts client.ts theme={null}
import { authClient } from "./auth-client";

async function createCustomer() {
  const { data, error } = await authClient.razorpay.customer.create({
    name: "Ravi Kumar",
    email: "ravi@example.com",
    contact: "+919000090000",
    notes: {
      source: "manual",
    },
  });

  if (error) throw error;
  return data;
}
```

### Edit a customer

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.edit({
  customerId: "cust_XXXX",
  name: "Ravi K",
  contact: "+919000090001",
});
```

### List customers

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.list({
  query: { count: 20, skip: 0 },
});
```

### Fetch a customer

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.get({
  query: { id: "cust_XXXX" },
});
```

## Organization customers

When you use the Better Auth `organization()` plugin and enable `organization: { enabled: true }` in the Razorpay plugin, each organization can have its own Razorpay customer ID stored in `organization.razorpayCustomerId`.

```ts auth.ts theme={null}
import { betterAuth } from "better-auth";
import { organization } from "better-auth/plugins";
import { razorpay } from "@amank-root/better-auth-razorpay";

export const auth = betterAuth({
  plugins: [
    organization(),
    razorpay({
      keyId: process.env.RAZORPAY_KEY_ID!,
      keySecret: process.env.RAZORPAY_KEY_SECRET!,
      createCustomerOnSignUp: true,
      organization: { enabled: true },
      subscription: {
        enabled: true,
        plans: [{ planId: "plan_XXXX", name: "pro" }],
        authorizeReference: async ({ user, session, referenceId, action }, ctx) => {
          return true;
        },
      },
    }),
  ],
});
```

Organization name updates are automatically synced to the linked Razorpay customer. Deleting an organization that has an active subscription is blocked and returns `ORGANIZATION_HAS_ACTIVE_SUBSCRIPTION`.

## Bank accounts

Add and delete bank accounts for a customer. This is useful for payouts or subscription payments that require bank details.

### Add a bank account

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.bankAccount.create({
  customerId: "cust_XXXX",
  ifsc_code: "HDFC0000123",
  account_number: "123456789012",
  beneficiary_name: "Ravi Kumar",
});
```

### Delete a bank account

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.bankAccount.delete({
  customerId: "cust_XXXX",
  bankAccountId: "ba_XXXX",
});
```

## Saved card tokens

Fetch or delete saved card tokens for a customer. Tokens are created when a customer saves a card during checkout.

### Fetch tokens

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.tokens({
  query: { customerId: "cust_XXXX" },
});
```

### Fetch a single token

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.token({
  query: { customerId: "cust_XXXX", tokenId: "token_XXXX" },
});
```

### Delete a token

```ts client.ts theme={null}
const { data, error } = await authClient.razorpay.customer.token.delete({
  customerId: "cust_XXXX",
  tokenId: "token_XXXX",
});
```

## React hooks

The React entry point provides hooks for customer tokens and bank account operations.

### Fetch tokens

```tsx customer-tokens.tsx theme={null}
import { useRazorpayCustomerTokens } from "@amank-root/better-auth-razorpay/react";
import { authClient } from "./auth-client";

export function CustomerTokens({ customerId }: { customerId: string }) {
  const { data, isLoading, error } = useRazorpayCustomerTokens(authClient, customerId);

  if (isLoading) return <p>Loading tokens...</p>;
  if (error) return <p>Error: {error.message}</p>;

  return (
    <ul>
      {data?.items?.map((token) => (
        <li key={token.id}>
          {token.card?.network} ending in {token.card?.last4} — expires {token.card?.expiry_month}/{token.card?.expiry_year}
        </li>
      ))}
    </ul>
  );
}
```

### Delete a token

```tsx delete-token-button.tsx theme={null}
import { useDeleteToken } from "@amank-root/better-auth-razorpay/react";
import { authClient } from "./auth-client";

export function DeleteTokenButton({ customerId, tokenId }: { customerId: string; tokenId: string }) {
  const deleteToken = useDeleteToken(authClient);

  return (
    <button
      onClick={() => deleteToken.mutate({ customerId, tokenId })}
      disabled={deleteToken.isPending}
    >
      {deleteToken.isPending ? "Deleting..." : "Delete card"}
    </button>
  );
}
```

### Add a bank account

```tsx add-bank-account.tsx theme={null}
import { useAddBankAccount } from "@amank-root/better-auth-razorpay/react";
import { authClient } from "./auth-client";

export function AddBankAccountForm({ customerId }: { customerId: string }) {
  const addBank = useAddBankAccount(authClient);

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    const form = new FormData(e.currentTarget);
    await addBank.mutateAsync({
      customerId,
      ifsc_code: form.get("ifsc") as string,
      account_number: form.get("account") as string,
      beneficiary_name: form.get("name") as string,
    });
  }

  return (
    <form onSubmit={handleSubmit}>
      <input name="ifsc" placeholder="IFSC code" required />
      <input name="account" placeholder="Account number" required />
      <input name="name" placeholder="Beneficiary name" required />
      <button type="submit" disabled={addBank.isPending}>
        {addBank.isPending ? "Adding..." : "Add bank account"}
      </button>
    </form>
  );
}
```

## Error codes

The following error codes may be returned during customer operations:

* `UNABLE_TO_CREATE_CUSTOMER`: the customer could not be created in Razorpay.
* `CUSTOMER_EDIT_FAILED`: the customer could not be updated.
* `CUSTOMER_NOT_FOUND`: no Razorpay customer exists for the authenticated user.
* `BANK_ACCOUNT_ADD_FAILED`: the bank account could not be added.
* `BANK_ACCOUNT_DELETE_FAILED`: the bank account could not be deleted.
* `TOKEN_FETCH_FAILED`: failed to fetch saved card tokens.
* `TOKEN_DELETE_FAILED`: the token could not be deleted.
* `ORGANIZATION_NOT_FOUND`: the organization does not exist.
* `ORGANIZATION_SUBSCRIPTION_NOT_ENABLED`: organization subscriptions are not enabled.
* `ORGANIZATION_HAS_ACTIVE_SUBSCRIPTION`: the organization cannot be deleted because it has an active subscription.
* `ORGANIZATION_REFERENCE_ID_REQUIRED`: a reference ID is required for organization subscriptions.

## Next steps

* [Subscriptions](https://better-auth-razorpay.amankushwaha.dev/guides/subscriptions.md) — link customers to recurring billing plans.
* [Invoices](https://better-auth-razorpay.amankushwaha.dev/guides/invoices.md) — create invoices against customer records.
* [Items and add-ons](https://better-auth-razorpay.amankushwaha.dev/guides/items-and-addons.md) — build a catalog of billable items.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.