Skip to content

Commit

Permalink
Payment Synchronous communication and Security Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
NibrasAmmar01 committed Dec 24, 2024
1 parent 105ba80 commit 1368650
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion order/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
container_name: db_order
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: fadizoe1212
POSTGRES_PASSWORD: nebras2001
POSTGRES_DB: db_order
PGDATA: /var/lib/postgresql/data
ports:
Expand Down
5 changes: 5 additions & 0 deletions order/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.microservices.order_service.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http

.csrf(csrf -> csrf.disable())


.cors(Customizer.withDefaults())


.authorizeHttpRequests(authz -> authz

.requestMatchers("/order/**").permitAll()
.anyRequest().permitAll()
)

.httpBasic(Customizer.withDefaults())
.formLogin(Customizer.withDefaults());

return http.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.microservices.order_service.model.Order;
import com.microservices.order_service.service.InventoryService;
import com.microservices.order_service.service.OrderService;
import com.microservices.order_service.service.PaymentService;
import com.microservices.order_service.service.PricingService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -106,4 +108,12 @@ public ResponseEntity<List<CartItem>> RecieveCart(@RequestBody List<CartItem> ca
return null;

}
@Autowired
private PaymentService paymentService;

@PostMapping("/pay")
public PaymentResponseDTO payForOrder(@RequestBody List<CartItem> cartItems, @RequestBody PaymentRequestDTO paymentRequestDTO) {
// Call processPayment to handle both pricing and payment
return paymentService.processPayment(cartItems, paymentRequestDTO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.microservices.order_service.dto;
import com.microservices.order_service.domain.OrderStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.UUID;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderUpdateRequestDTO {
private UUID orderId;
private OrderStatus status;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.microservices.order_service.service;
import com.microservices.order_service.dto.CartItem;
import com.microservices.order_service.dto.PaymentRequestDTO;
import com.microservices.order_service.dto.PaymentResponseDTO;
import com.microservices.order_service.dto.cartResponse;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
import java.util.List;


@Service
Expand All @@ -16,15 +24,33 @@ public class PaymentService {
public PaymentService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://localhost:8085/payments").build();
}
@Autowired
private PricingService pricingService;
public PaymentResponseDTO processPayment(List<CartItem> cartItems, @NotNull PaymentRequestDTO paymentRequestDTO) {

cartResponse pricingResponse = pricingService.checkPrice(cartItems);
BigDecimal totalAmount = pricingResponse.getTotalAfterDiscount();

public PaymentResponseDTO checkPrice(PaymentRequestDTO paymentRequestDTO) {

paymentRequestDTO.setAmount(totalAmount);
return sendForPayment(paymentRequestDTO);
}


public PaymentResponseDTO sendForPayment(PaymentRequestDTO paymentRequestDTO) {
return webClient.post()
.uri("http://localhost:8085/payments")
.bodyValue(paymentRequestDTO)
.retrieve()
.onStatus(status -> status.is4xxClientError(), clientResponse -> {
return clientResponse.createException()
.flatMap(error -> Mono.error(new RuntimeException("Client Error: " + error.getMessage())));
})
.onStatus(status -> status.is5xxServerError(), clientResponse -> {
return clientResponse.createException()
.flatMap(error -> Mono.error(new RuntimeException("Server Error: " + error.getMessage())));
})
.bodyToMono(new ParameterizedTypeReference<PaymentResponseDTO>() {})
.block(); // Communication synchrone
.block(); // Communication is synchronous here
}
}
5 changes: 5 additions & 0 deletions order/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ spring:
enabled: false
baseline-on-migrate: true
locations: classpath:db/migration
http:
host: 0.0.0.0
cors:
enabled: true
origins: "*"
logging:
level:
org:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE clients (
phone VARCHAR(50) -- Phone number
);
*/
DELETE FROM flyway_schema_history WHERE version = '2';
/**DELETE FROM flyway_schema_history WHERE version = '2';
Expand Down

0 comments on commit 1368650

Please sign in to comment.