Hi Team
Are there any examples in asp.net core uses payfast on ecommerce? Where users can add to cart and checkout but be prompt to first login and later continue to pay using payfast.
Hi Team
Are there any examples in asp.net core uses payfast on ecommerce? Where users can add to cart and checkout but be prompt to first login and later continue to pay using payfast.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 27, 2024, 2:38 AM EDT
Hi,
Integrating PayFast into an ASP.NET Core e-commerce application is feasible and can enhance the payment experience for users. To achieve this, you can utilize the PayFast SDK or API to handle payment transactions securely.
Step 1: Set Up Your ASP.NET Core Project Step 2: Install Necessary Packages
Install packages that might help in building your e-commerce functionalities such as Entity Framework Core for database operations.
Step 3: Create User Authentication
Implement user authentication to prompt users to log in before checking out.
Configure authentication in StartUp.cs
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity() .AddEntityFrameworkStores(); services.Configure(options => { // Password settings. options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; // Lockout settings. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.AddControllersWithViews(); services.AddRazorPages(); }Step 4: Create Models for Products and Orders
Create models for products and orders.
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } public class Order { public int Id { get; set; } public string UserId { get; set; } public ICollection Items { get; set; } public decimal TotalPrice { get; set; } } public class OrderItem { public int Id { get; set; } public int ProductId { get; set; } public Product Product { get; set; } public int Quantity { get; set; } }Step 5: Create the Cart Functionality
Implement the logic to add items to the cart and check out.
public class CartItem { public int ProductId { get; set; } public int Quantity { get; set; } } public class Cart { public List Items { get; set; } = new List(); }Step 6: Implement the Checkout Process with PayFast
Add PayFast integration. PayFast requires generating a payment URL and redirecting the user to that URL.
public class PayFastService { private readonly IConfiguration _configuration; public PayFastService(IConfiguration configuration) { _configuration = configuration; } public string GeneratePaymentUrl(Order order) { var merchantId = _configuration["PayFast:MerchantId"]; var merchantKey = _configuration["PayFast:MerchantKey"]; var returnUrl = _configuration["PayFast:ReturnUrl"]; var notifyUrl = _configuration["PayFast:NotifyUrl"]; var cancelUrl = _configuration["PayFast:CancelUrl"]; var data = new Dictionary { { "merchant_id", merchantId }, { "merchant_key", merchantKey }, { "return_url", returnUrl }, { "notify_url", notifyUrl }, { "cancel_url", cancelUrl }, { "amount", (order.TotalPrice * 100).ToString("F0") }, // Amount in cents { "item_name", $"Order {order.Id}" }, { "item_description", "Description of the items purchased" } }; var queryString = string.Join("&", data.Select(kvp => $"{kvp.Key}={kvp.Value}")); var paymentUrl = $"https://www.payfast.co.za/eng/process?{queryString}"; return paymentUrl; } }Step 7: Handle Payment Notifications
Handle payment notifications from PayFast.
[HttpPost] public IActionResult PaymentNotification() { // Read and validate the notification data from PayFast // Update the order status in the database accordingly return Ok(); }Step 8: Create the Views
Create views for the cart, checkout, and payment process.
Cart View:
Checkout View:
@model Order Checkout Pay with PayFastStep 9: Redirect to PayFast
In the checkout action, generate the payment URL and redirect the user.
[HttpPost] public IActionResult Complete(Order order) { var paymentUrl = _payFastService.GeneratePaymentUrl(order); return Redirect(paymentUrl); }Step 10: Test Your Application
Run your application, add items to the cart, proceed to checkout, log in if prompted, and complete the payment via PayFast.