Skip to main content
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.
auth.ts
boolean
default:"false"
Whether to create a Razorpay customer automatically when a user signs up.
function
Async function that receives the user object and context, and returns partial customer create parameters such as contact or notes.
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

client.ts

Edit a customer

client.ts

List customers

client.ts

Fetch a customer

client.ts

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.
auth.ts
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

client.ts

Delete a bank account

client.ts

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

client.ts

Fetch a single token

client.ts

Delete a token

client.ts

React hooks

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

Fetch tokens

customer-tokens.tsx

Delete a token

delete-token-button.tsx

Add a bank account

add-bank-account.tsx

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