first of all, Thank you to take the time to read this.
I am making a Payment Test app, with Java and Spring boot, but to take the ad data I am making a form with the data of that ad in my HTML template of the ad in which the user will click on the pay button .
This is the HTML so you can understand it better.
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Detalle del Anuncio</title>
<h1 th:text="${anuncio.titulo}">Título del Anuncio</h1>
<img th:src="@{${anuncio.imageUrl}}" alt="Imagen del anuncio">
<p>Categoría: <span th:text="${anuncio.categoria}"></span></p>
<p>Estado: <span th:text="${anuncio.estado}"></span></p>
<p>Precio: <span th:text="${anuncio.precio}"></span></p>
<p>Descripción: <span th:text="${anuncio.descripcion}"></span></p>
<button id="btn">Comprar</button>
<input type="text" name="amount" th:value="${anuncio.precio}">
<input type="text" name="descripcion" th:value="${anuncio.titulo}">
<button type="submit">comprar</button>
public String pay(@PathVariable("id") Long id, @RequestParam("amount") Long amount, @RequestParam("descripcion")String description) {
Anuncio anuncio = anuncioService.findAdById(id);
if(anuncio == null) {
//THIS IS A EXAMPLE ETROR HANDLING. ITS JUST A EXAMPLE
//SOLO ES UN EJEMPLO SOBRE EL MANEJO DE ERRORES, ESTO ES AUN UNA PRUEBA.
return "redirect:/nullAnonce";
} else {
Long originalPrice = anuncio.getPrecio();
//JUST A EXAMPLE TAKING ONLY THE PRICE, IN A REAL CASE I WILL BE TAKING ALL.
//ES SOLO UN EJEMPLO TOMANDO SOLO EL PRECIO, EN UN CASO REAL TOMARIAMOS NO SOLO EL PRECIO TAMBIEN LA DESCRIPCION Y ESAS COSAS.
if(amount != originalPrice) {
//JUST A EXAMPLE TAKING ONLY THE PRICE, IN A REAL CASE I WILL BE TAKING ALL.
return "redirect:/PriceNotCorrect";
} else {
try {
// Configurar las credenciales de PayPal
String clientId = "CLIENTID";
String clientSecret = "CLIENTSECRET";
String mode = "sandbox"; // O "live" para producción
// Crear el objeto APIContext
APIContext apiContext = new APIContext(clientId, clientSecret, mode);
// Configurar la cantidad y la transacción
Amount paymentAmount = new Amount();
paymentAmount.setCurrency("USD");
paymentAmount.setTotal("500");
Transaction transaction = new Transaction();
transaction.setDescription("Descripción del pago");
transaction.setAmount(paymentAmount);
List<Transaction> transactions = new ArrayList<>();
transactions.add(transaction);
// Configurar el pagador
Payer payer = new Payer();
payer.setPaymentMethod("paypal");
// Configurar el pago
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setTransactions(transactions);
// Configurar las URLs de redirección
RedirectUrls redirectUrls = new RedirectUrls();
redirectUrls.setCancelUrl("https://localhost:3000/cancel");
redirectUrls.setReturnUrl("https://localhost:3000/rooms");
payment.setRedirectUrls(redirectUrls);
// Crear el pago
Payment createdPayment = payment.create(apiContext);
// Obtener la URL de aprobación y redirigir al usuario
String approvalUrl = createdPayment.getLinks().stream()
.filter(link -> "approval_url".equals(link.getRel()))
.findFirst()
.orElseThrow(() -> new PayPalRESTException("Approval URL not found"))
.getHref();
return "redirect:" + approvalUrl;
} catch (PayPalRESTException e) {
e.printStackTrace();
return "redirect:/error";
}
}
}
}`
Sorry if it seems a bit ‘stupid’…
What we are trying to do is that even if a user changes the value to the price, it will be compared with the price in the database, if it is correct, good, and if not then he is redirected.
Luka Varga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.