PayWithStripe is a Java Spring Boot backend service designed to facilitate integration with the Stripe Payment Gateway. This project implements backend APIs for creating, capturing, refunding payment intents, and fetching a list of payment intents.
- Dockerized Application
- Prerequisites
- Build and Run the Application
- Requirements
- API Endpoints
- Service Implementation
- Testing
- Configuration
- References
- Postman Collection
This repository contains a Dockerized application named "PayWithStripe." The application can be built and run using the provided Makefile.
Before running the application, ensure you have the following prerequisites:
- Java Development Kit (JDK) installed.
- Maven for building the Spring Boot project.
- Docker for containerization.
To build and run the application, follow these steps:
-
Build the Spring Boot project using Maven:
make build-project
-
Build the Docker image:
make build
-
Run the Docker container:
make run
This will start the application and expose it on port 8000.
-
To stop and remove the Docker container:
make stop
Alternatively, you can perform all the above actions in a single command:
make build-and-run
Make sure you have Docker installed and the necessary dependencies for building the project.
Note: The make build-and-run
command performs all actions, including building the project, building the Docker image, and running the container. Make sure to run this command to execute all necessary steps.
For more details on individual targets, you can refer to the provided Makefile.
- Implement backend APIs for Stripe Payment Gateway integration using Java Spring Boot.
- Create an account on Stripe to obtain access and secret keys.
- Implement the following APIs:
-
Create Intent for Payment
- Endpoint:
POST /api/v1/create_intent
- Request Body: PaymentDetails
- Endpoint:
-
Capture the Created Intent
- Endpoint:
POST /api/v1/capture_intent/{id}
- Path Variable:
id
- ID of the created intent
- Endpoint:
-
Create a Refund for the Created Intent
- Endpoint:
POST /api/v1/create_refund/{id}
- Path Variable:
id
- ID of the created intent
- Endpoint:
-
Get a List of All Intents
- Endpoint:
GET /api/v1/get_intents
- Endpoint:
{
"your_payment_details": "go_here"
}
The StripeServiceImplement
class is a Spring Service responsible for interacting with the Stripe API to handle payment intents. It implements the StripeServiceInterface
for creating, capturing, refunding payment intents, and fetching a list of payment intents.
createIntent
: Create a payment intent based on the provided PaymentDetails.captureIntent
: Capture a payment intent with the specified ID.createRefund
: Create a refund for a payment intent with the specified ID.getIntents
: Get a list of PaymentIntents with a specified limit.
The Stripe secret key is injected into the service from the application configuration.
# application.properties or application.yml
stripe.secret.key=your_stripe_secret_key
The service uses SLF4J for logging. Logs include information about successful operations and errors.
To use the service, inject StripeServiceInterface
and call the appropriate methods.
Example:
@RestController
@RequestMapping("/api/v1")
public class PaymentController {
@Autowired
private StripeServiceInterface stripeService;
@PostMapping("/create_intent")
public ResponseEntity<String> createIntent(@RequestBody PaymentDetails paymentDetails) {
return stripeService.createIntent(paymentDetails);
}
// Other controller methods for capture, refund, and get_intents
}
The PayWithStripeApplicationTests
class contains test cases for the functionality implemented in the Spring Boot application. These tests use JUnit and Mockito for unit testing.
createIntent_Success
: Verify that thecreateIntent
method returns the expected success message.captureIntent_Success
: Verify that thecaptureIntent
method returns the expected success message.createRefund_Success
: Verify that thecreateRefund
method returns the expected success message.getIntents_Success
: Verify that thegetIntents
method returns the expected success message.
The test class is annotated with @SpringBootTest
, indicating that it is a Spring Boot test. It also uses @MockBean
to create a mock instance of StripeServiceImplement
for testing.
@SpringBootTest
class PayWithStripeApplicationTests {
// Test methods...
}
Ensure that you have configured your Stripe secret key in the application.properties
or application.yml
file.
- Stripe API Docs
- Payment Intents
- Stripe Java SDK
- Setting up a Simple Backend in Spring Boot
- Testing in Spring Boot
Additional reference links for building and testing REST APIs:
For testing the APIs, you can use the following Postman collection: PayWithStripe Postman Collection