Created
April 11, 2025 21:07
-
-
Save Giovasdf/8bed6a15aeb49106af971c1f891086a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace WebpayApi; | |
| public class CommitTransactionRequest | |
| { | |
| public string Token { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace WebpayApi; | |
| public class CreateTransactionRequest | |
| { | |
| public decimal Amount { get; set; } | |
| public string SessionId { get; set; } | |
| public string BuyOrder { get; set; } | |
| public string ReturnUrl { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.AspNetCore.Builder; | |
| using Microsoft.AspNetCore.Http.Json; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Hosting; | |
| using Transbank.Common; | |
| using Transbank.Webpay.Common; | |
| using Transbank.Webpay.WebpayPlus; | |
| var builder = WebApplication.CreateBuilder(args); | |
| builder.Services.AddCors(); | |
| builder.Services.AddControllers(); | |
| builder.Services.Configure<JsonOptions>(options => | |
| { | |
| options.SerializerOptions.PropertyNamingPolicy = null; | |
| }); | |
| var app = builder.Build(); | |
| app.UseCors(policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); | |
| app.MapControllers(); | |
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.AspNetCore.Mvc; | |
| using Transbank.Webpay.WebpayPlus; | |
| using Transbank.Webpay.WebpayPlus.Responses; | |
| namespace WebpayApi.Controllers; | |
| [ApiController] | |
| [Route("api")] | |
| public class TransactionController : ControllerBase | |
| { | |
| private static readonly Transaction tx = new Transaction(new Options( | |
| "597055555532", // Código de comercio | |
| "579B532A7440BB0C9079DED94D31EA1615BACEB56610332264630D42D0A36B1C", // API Key | |
| IntegrationType.Test // Ambiente de integración | |
| )); | |
| [HttpPost("create-transaction")] | |
| public async Task<IActionResult> CreateTransaction([FromBody] CreateTransactionRequest request) | |
| { | |
| if (request == null || request.Amount <= 0 || string.IsNullOrWhiteSpace(request.SessionId) | |
| || string.IsNullOrWhiteSpace(request.BuyOrder) || string.IsNullOrWhiteSpace(request.ReturnUrl)) | |
| { | |
| return BadRequest(new { error = "Faltan datos requeridos" }); | |
| } | |
| try | |
| { | |
| var response = await tx.Create(request.BuyOrder, request.SessionId, request.Amount, request.ReturnUrl); | |
| return Ok(response); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new { error = "Error al crear la transacción", details = ex.Message }); | |
| } | |
| } | |
| [HttpPost("commit-transaction")] | |
| public async Task<IActionResult> CommitTransaction([FromBody] CommitTransactionRequest request) | |
| { | |
| try | |
| { | |
| var response = await tx.Commit(request.Token); | |
| var isRejected = response.ResponseCode < 0 || response.Status != "AUTHORIZED"; | |
| if (isRejected) | |
| { | |
| return Ok(new | |
| { | |
| status = "rejected", | |
| response.BuyOrder, | |
| responseCode = response.ResponseCode, | |
| responseMessage = response.ResponseMessage ?? "Transacción rechazada" | |
| }); | |
| } | |
| return Ok(new | |
| { | |
| status = "approved", | |
| response.BuyOrder, | |
| response.Amount, | |
| authorizationCode = response.AuthorizationCode, | |
| paymentTypeCode = response.PaymentTypeCode, | |
| cardNumber = response.CardDetail.CardNumber, | |
| transactionDate = response.TransactionDate | |
| }); | |
| } | |
| catch (Exception ex) | |
| { | |
| return StatusCode(500, new | |
| { | |
| status = "rejected", | |
| error = "Error al confirmar transacción", | |
| details = ex.Message | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment