PayPalClient is not a class that ships with Spring Boot or with PayPal. It is a name developers give to their own thin wrapper around PayPal's REST API. Its role is to keep everything PayPal-specific in one place: base URL, OAuth2 access tokens, creating and capturing orders, refunds and error handling, so the rest of your application only calls methods such as createOrder() and captureOrder().
Many older tutorials build this class on the com.paypal.api.payments REST SDK, which is deprecated. The example below calls the current PayPal Orders v2 REST API directly with Spring's RestClient (Spring Framework 6.1 and later).
A dedicated client class gives you several benefits:
Keep the class free of business rules. Deciding what to charge or when to ship belongs in your service layer.
PayPal's older Java options have been superseded:
| Option | Status |
|---|---|
PayPal-Rest-SDK (com.paypal.api.payments, APIContext) | Deprecated and archived; avoid in new code |
Checkout Java SDK (com.paypal.http, OrdersCreateRequest) | Deprecated in favour of the newer server SDK |
| PayPal Server SDK for Java | Current official SDK for Orders v2 and related APIs |
Direct REST calls to /v2/checkout/orders | Always available; used in the example below |
Check PayPal's developer documentation for the current SDK coordinates and version before adding a dependency. Calling the REST API yourself keeps the example independent of any SDK version.
Never hard-code client credentials. Bind them from configuration and supply the secret from an environment variable or a secrets manager:
Use https://api-m.paypal.com as the base URL for the live environment. Register the properties with @EnableConfigurationProperties(PayPalProperties.class) or @ConfigurationPropertiesScan.
The class does four jobs. token() authenticates with the client id and secret through HTTP Basic auth and caches the bearer token until shortly before it expires. createOrder() creates an order with the CAPTURE intent. captureOrder() collects the money after the buyer approves. The PayPal-Request-Id header makes retries idempotent, so PayPal does not create a duplicate order. In production, generate that key once per business operation and reuse it on retries, not a new value per attempt.
For production code, replace Map with small records for requests and responses so the compiler checks field names.
createOrder("EUR", "25.00") and returns the order id to the browser.captureOrder(orderId), checks that the status is COMPLETED, and then marks your own order as paid.Never trust the amount sent by the browser. Look the price up on the server before creating the order.
retrieve() throws RestClientResponseException subclasses for 4xx and 5xx responses. Catch them in the client and translate them into your own exception type.No. It is a plain Spring bean you write yourself. Spring Boot only provides the HTTP client and configuration support it uses.
No. It is deprecated. Use PayPal's current server SDK or call the Orders v2 REST API directly, as shown above.
Use the sandbox base URL and sandbox credentials from the PayPal developer dashboard, then test with sandbox buyer accounts.
An in-memory field is fine for a single instance, as in the example. In a cluster, each instance can keep its own token, since tokens are cheap to request.
A PayPalClient class is your own adapter around PayPal's API. It owns authentication, token caching, order creation, capture and error handling, and it hides these details from the rest of the application. Build it on the current Orders v2 API with RestClient, keep secrets in configuration, use idempotency keys, and verify amounts and webhooks on the server.