PayPal
Get your PayPal Credentials
To integrate with PayPal, you need to obtain API credentials by creating an application in the PayPal Developer Portal.
Follow these steps:
- Create an account on the PayPal Developer Portal
- Create a new application, official docs
- Configure Log in with PayPal under "Other features"
- Set up your Return URL (redirect URL)
- Configure user information permissions
- Note your Client ID and Client Secret
- PayPal has two environments: Sandbox (for testing) and Live (for production)
- For testing, create sandbox test accounts in the Developer Dashboard under "Sandbox" → "Accounts"
- You cannot use your real PayPal account to test in sandbox mode - you must use the generated test accounts
- The Return URL in your PayPal app settings must exactly match your redirect URI
- The PayPal API does not work with localhost. You need to use a public domain for the redirect URL and HTTPS for local testing. You can use NGROK or another similar tool for this.
Make sure to configure "Log in with PayPal" in your app settings:
- Go to your app in the Developer Dashboard
- Under "Other features", check "Log in with PayPal"
- Click "Advanced Settings"
- Enter your Return URL
- Select the user information you want to access (email, name, etc.)
- Enter Privacy Policy and User Agreement URLs
- PayPal doesn't use traditional OAuth2 scopes in the authorization URL. Instead, you configure permissions directly in the Developer Dashboard
- For live apps, PayPal must review and approve your application before it can go live, which typically takes a few weeks
Configure the provider
To configure the provider, you need to import the provider and pass it to the social_providers option of the auth instance.
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
paypal: BetterAuth::SocialProviders.paypal(
client_id: ENV.fetch("PAYPAL_CLIENT_ID"),
client_secret: ENV.fetch("PAYPAL_CLIENT_SECRET"),
environment: "sandbox"
)
}
)Options
The PayPal provider accepts the following options:
environment:'sandbox' | 'live'- PayPal environment to use (default:'sandbox')request_shipping_address:boolean- Whether to request shipping address information (default:false)
require "better_auth"
auth = BetterAuth.auth(
secret: ENV.fetch("BETTER_AUTH_SECRET"),
base_url: ENV.fetch("BETTER_AUTH_URL", "http://localhost:3000"),
social_providers: {
paypal: BetterAuth::SocialProviders.paypal(
client_id: ENV.fetch("PAYPAL_CLIENT_ID"),
client_secret: ENV.fetch("PAYPAL_CLIENT_SECRET"),
environment: "live",
request_shipping_address: true
)
}
)Sign In with PayPal
To sign in with PayPal, call auth.api.sign_in_social on your Ruby auth instance. The endpoint body takes the following properties:
provider: The provider to use. It should be set topaypal.
response = auth.api.sign_in_social(
body: {
provider: "paypal",
callback_url: "/dashboard",
error_callback_url: "/login",
disable_redirect: true
}
)
redirect_url = response.fetch(:url)Additional Options:
environment: PayPal environment to use.- Default:
"sandbox" - Options:
"sandbox"|"live"
- Default:
request_shipping_address: Whether to request shipping address information.- Default:
false
- Default:
scope: Additional scopes to request (combined with default permissions).- Default: Configured in PayPal Developer Dashboard
- Note: PayPal doesn't use traditional OAuth2 scopes - permissions are set in the Dashboard For more details refer to the Scopes Reference
map_profile_to_user: Custom function to map PayPal profile data to user object.get_user_info: Custom function to retrieve user information. For more details refer to the User Referenceverify_id_token: Custom ID token verification function.