package com.paymentlink.controller.api; import com.paymentlink.service.TaxService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/api/tax") public class TaxApiController { private final TaxService taxService; public TaxApiController(TaxService taxService) { this.taxService = taxService; } /** * GET /api/tax/calculate - Calculate tax */ @GetMapping("/calculate") public ResponseEntity> calculateTax( @RequestParam Long subtotal, @RequestParam(required = false) String country, @RequestParam(required = false) String state) { try { long taxAmount = taxService.calculateTax(subtotal, country, state); double taxRate = taxService.getTaxRate(country, state); Map response = new HashMap<>(); response.put("success", true); response.put("taxAmount", taxAmount); response.put("taxRate", taxRate); return ResponseEntity.ok(response); } catch (Exception e) { Map error = new HashMap<>(); error.put("success", false); error.put("error", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); } } /** * GET /api/tax/rate - Get tax rate */ @GetMapping("/rate") public ResponseEntity> getTaxRate( @RequestParam(required = false) String country, @RequestParam(required = false) String state) { try { double taxRate = taxService.getTaxRate(country, state); Map response = new HashMap<>(); response.put("success", true); response.put("taxRate", taxRate); return ResponseEntity.ok(response); } catch (Exception e) { Map error = new HashMap<>(); error.put("success", false); error.put("error", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); } } }