You are currently viewing Integrating PayPal with Spring Boot

Integrating PayPal with Spring Boot

Integrating PayPal into a Spring Boot application allows you to accept payments from customers using PayPal’s secure payment platform. In this tutorial, we will guide you through the process of setting up a Spring Boot application to handle PayPal payments.

Table of Contents

  1. Create a PayPal Developer Account
  2. Set Up a Spring Boot Application
  3. Integrate PayPal SDK
  4. Implement PayPal Payment
  5. Handling Webhooks
  6. Testing
  7. Conclusion

1. Create a PayPal Developer Account

Before integrating PayPal with your Spring Boot application, you need to create a PayPal developer account and generate API credentials.

  1. Go to PayPal Developer Dashboard and sign up for an account or log in if you already have one.
  2. Navigate to My Apps & Credentials and create a new app to get your Client ID and Secret.

2. Set Up a Spring Boot Application

If you don’t have a Spring Boot application set up yet, you can create a new one using Spring Initializr.

  1. Go to Spring Initializr.
  2. Enter Group (com.example) and Artifact (paypal-integration) details.
  3. Add dependencies: Spring Web, Spring Boot DevTools, Thymeleaf (for web views).
  4. Generate and download the project.

3. Integrate PayPal SDK

Add PayPal Java SDK to your pom.xml:


Replace LATEST_VERSION with the latest version of the PayPal SDK. You can find the latest version on the Maven Central Repository.

4. Implement PayPal Payment

4.1. Configuration

Create a configuration class to set up PayPal environment and client:

import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class PayPalConfig {

    private String clientId;

    private String clientSecret;

    private String mode;

    public PayPalEnvironment payPalEnvironment() {
        return new PayPalEnvironment.Sandbox(clientId, clientSecret);

    public PayPalHttpClient payPalHttpClient(PayPalEnvironment environment) {
        return new PayPalHttpClient(environment);

4.2. Controller

Create a controller to handle payment requests:

import com.paypal.core.PayPalHttpClient;
import com.paypal.orders.Order;
import com.paypal.orders.OrdersCreateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

public class PayPalController {

    private PayPalHttpClient payPalClient;

    private String returnUrl;

    private String cancelUrl;

    public String createOrder() {
        OrdersCreateRequest request = new OrdersCreateRequest();

        try {
            Order order = payPalClient.execute(request).result();
        } catch (Exception e) {
            return e.getMessage();

    private OrderRequest buildRequestBody() {
        OrderRequest orderRequest = new OrderRequest();

        ApplicationContext applicationContext = new ApplicationContext().brandName("YourBrandName")

        AmountWithBreakdown amount = new AmountWithBreakdown().currencyCode("USD")

        orderRequest.purchaseUnits(Collections.singletonList(new PurchaseUnitRequest().amount(amount)));

        return orderRequest;

4.3. Properties

Define PayPal related properties in


4.4. Payment Success and Cancel Endpoints

Create success and cancel endpoints in the controller:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class PaymentController {

    public String paymentSuccess() {
        return "Payment successful!";

    public String paymentCancel() {
        return "Payment canceled!";

5. Handling Webhooks

If you want to handle PayPal webhooks for payment updates (e.g., capture, refund), you can create a webhook endpoint:

import com.paypal.http.HttpResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class WebhookController {

    public ResponseEntity<String> handleWebhook(@RequestBody String request) {
        // Parse and handle PayPal webhook event
        // For example:
        // 1. Deserialize the event from JSON
        // 2. Check the event type (payment capture, refund, etc.)
        // 3. Update your system accordingly

        return ResponseEntity.ok("Webhook received and processed.");

6. Testing

  1. Start your Spring Boot application.
  2. Access the endpoint to create an order: POST http://localhost:8080/createOrder.
  3. Use the received orderId to create a payment link.
  4. Proceed with the payment in the sandbox environment.
  5. Check the success/cancel endpoints (/success, /cancel) to verify payment status.

7. Conclusion

You have now successfully integrated PayPal with your Spring Boot application. This setup allows you to create PayPal orders, handle payment success/cancel actions, and potentially handle webhooks for more advanced payment processing. Remember to replace placeholders with your actual PayPal credentials and URLs.

Additional Resources:

Leave a Reply