Skip to content

Commit

Permalink
feat: Add Paypal for payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
vodoquangduong committed Jul 6, 2024
1 parent a8a2bb9 commit 7ac77a3
Show file tree
Hide file tree
Showing 32 changed files with 1,400 additions and 13 deletions.
89 changes: 88 additions & 1 deletion backend/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Transactions;
Expand All @@ -7,6 +8,7 @@
using backend.Enums;
using backend.Helper;
using backend.Interfaces;
using backend.Payment_src.core.Payment.Service.Paypal.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -18,11 +20,14 @@ public class OrderController : ControllerBase
{
private readonly IOrderRepository _orderRepo;
private readonly IEmailSender _emailSender;
private readonly PaypalClient _paypalClient;
private readonly ITransactionRepository _transactionRepo;

public OrderController(IOrderRepository orderRepo, IEmailSender emailSender)
public OrderController(IOrderRepository orderRepo, IEmailSender emailSender, PaypalClient paypalClient)
{
_orderRepo = orderRepo;
_emailSender = emailSender;
_paypalClient = paypalClient;
}

[HttpGet]
Expand Down Expand Up @@ -99,5 +104,87 @@ [FromBody] UpdateOrderDTO order
}
return Ok(updatedOrder);
}

#region Paypal
[HttpPost("createPaypalOrder")]
public async Task<ActionResult> CreateOrderPaypal(CancellationToken cancellationToken, [FromBody] CreatePaypalOrderRequest createPaypalOrderRequest)
{
//Thong tin cua don hang gui qua paypal
if (createPaypalOrderRequest == null)
{
return BadRequest(new { Message = "Invalid request" });
}
if (string.IsNullOrEmpty(createPaypalOrderRequest.OrderId))
{
return BadRequest(new { Message = "OrderId is required" });
}
if (string.IsNullOrEmpty(createPaypalOrderRequest.Reference))
{
return BadRequest(new { Message = "TransactionId is required" });
}
var order = await _orderRepo.GetOrderByIdAsync(createPaypalOrderRequest.OrderId);
var transaction = await _transactionRepo.GetTransactionByIdAsync(createPaypalOrderRequest.Reference);
if (order == null)
{
return NotFound(new { Message = "Order not found" });
}
if (transaction == null)
{
return NotFound(new { Message = "Transaction not found" });
}
var amount = transaction.Amount.ToString("F2", CultureInfo.InvariantCulture);
Console.WriteLine("This amount: " + amount);
var currency = "USD";
try
{
var response = await _paypalClient.CreateOrder(amount, currency, createPaypalOrderRequest.Reference);
return Ok(response);
}
catch (Exception e)
{
var error = new { e.GetBaseException().Message };
return BadRequest(error);
}
}
public class CreatePaypalOrderRequest
{
public string Reference { get; set; }
public string OrderId { get; set; }
}
public class Reference
{
public string OrderId { get; set; }
}

[HttpPost("capturePaypalOrder")]
public async Task<ActionResult> CapturePaypalOrder(CancellationToken cancellationToken, [FromBody] Reference Reference)
{
try
{
var response = await _paypalClient.CaptureOrder(Reference.OrderId);
return Ok(response);
}
catch (Exception e)
{
var error = new { e.GetBaseException().Message };
return BadRequest(error);
}
}

[HttpPut("completePayment")]
public async Task<ActionResult> CompletePayment(CancellationToken cancellationToken, [FromBody] string orderId)
{
try
{
var response = await _orderRepo.CompleteOrderAsync(orderId);
return Ok(response);
}
catch (Exception e)
{
var error = new { e.GetBaseException().Message };
return BadRequest(error);
}
}
#endregion
}
}
13 changes: 13 additions & 0 deletions backend/Controllers/TransactionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ [FromBody] CreateTransactionDTO transactionDto
return Ok(newTransaction);
}

[HttpPut("completePayment")]
public async Task<IActionResult> CompletePayment(
[FromBody] string id
)
{
var transaction = await _transactionRepo.CompletePaymentAsync(id);
if (transaction == null)
{
return NotFound("Transaction not found.");
}
return Ok(transaction);
}


}
}
2 changes: 2 additions & 0 deletions backend/Interfaces/IOrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface IOrderRepository
Task<OrderResult?> GetAllOrdersAsync(OrderQuery query);
Task<Order?> GetOrderByIdAsync(string id);
Task<Order?> CreateOrderAsync(long customerId, CreateOrderDTO orderDto);
Task<Order?> CreateOrderPaypalAsync(long customerId, CreateOrderDTO orderDto);
Task<Order?> UpdateOrderAsync(string id, UpdateOrderDTO orderDto);
Task<Order?> CompleteOrderAsync(string id);
}
}
3 changes: 3 additions & 0 deletions backend/Interfaces/ITransactionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ public interface ITransactionRepository
{
Task<TransactionResult?> GetAllTransactionsAsync(TransactionQuery query);
Task<Transaction?> CreateTransactionAsync(CreateTransactionDTO transactionDto);

Task<Transaction?> GetTransactionByIdAsync(string id);
Task<Transaction?> CompletePaymentAsync(string id);
}
}
17 changes: 17 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public class Address
{
public string address_line_1 { get; set; }
public string address_line_2 { get; set; }
public string admin_area_2 { get; set; }
public string admin_area_1 { get; set; }
public string postal_code { get; set; }
public string country_code { get; set; }
}
}
13 changes: 13 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Amount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public class Amount
{
public string currency_code { get; set; }
public string value { get; set; }
}
}
22 changes: 22 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Capture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public class Capture
{
public string id { get; set; }
public string status { get; set; }
public Amount amount { get; set; }
public SellerProtection seller_protection { get; set; }
public bool final_capture { get; set; }
public string disbursement_mode { get; set; }
public SellerReceivableBreakdown seller_receivable_breakdown { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public List<Link> links { get; set; }
}

}
14 changes: 14 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Link.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public sealed class Link
{
public string href { get; set; }
public string rel { get; set; }
public string method { get; set; }
}
}
13 changes: 13 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Name.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public sealed class Name
{
public string given_name { get; set; }
public string surname { get; set; }
}
}
14 changes: 14 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Payer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public sealed class Payer
{
public Name name { get; set; }
public string email_address { get; set; }
public string payer_id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public sealed class PaymentSource
{
public Paypal paypal { get; set; }
}
}
12 changes: 12 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Payments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public sealed class Payments
{
public List<Capture> captures { get; set; }
}
}
14 changes: 14 additions & 0 deletions backend/Payment_src/core/Payment.Service/Paypal/Model/Paypal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace backend.Payment_src.core.Payment.Service.Paypal.Model
{
public sealed class Paypal
{
public Name name { get; set; }
public string email_address { get; set; }
public string account_id { get; set; }
}
}
Loading

0 comments on commit 7ac77a3

Please sign in to comment.