public async Task <IActionResult> SuccessfulPayment(string authority = null, string status = null, int shoppingId = -1) { if (shoppingId == -1) { return(RedirectToAction(actionName: "ShoppingBasket")); } var shopping = await _db.Shoppings.AsNoTracking() .Where(c => c.Id.Equals(shoppingId)) .FirstOrDefaultAsync(); if (shopping.IsLocalPayment && shopping.Status.Equals(Status.WaitForRegister)) { return(RedirectToAction(actionName: "ShoppingBasket")); } if (shopping.IsLocalPayment) { return(View(model: shopping)); } var user = await _userManager.FindByNameAsync(HttpContext.User.Identity.Name); int amount = (int)shopping.TotalPrice; if (status.Equals("OK")) { var zp = new ZarinPalGateWay.PaymentGatewayImplementationServicePortTypeClient(); var result = await zp.PaymentVerificationAsync(MerchantID : "1e608052-d5c6-11e8-bfd3-005056a205be" , Authority : authority, Amount : amount); if (result.Body.Status.Equals(100) || result.Body.Status.Equals(101)) { //update shopping await UpdateShoppingAsync(shopping, result.Body.RefID.ToString(), Status.OnlinePaid); //update user: total buy value and buy number of this user user.BuyNumber++; user.TotalBuyValue += shopping.TotalPrice; await _userManager.UpdateAsync(user); //decrease discount await DecreaseDiscountNumberAsync(shopping); //change status of orders and change product sell number and product stock var orders = await _db.Orders .Where(c => c.UserId.Equals(user.Id) && c.Status.Equals(Status.WaitForRegister)) .ToListAsync(); var products = new List <Product>(); var specialDiscounts = new List <SpecialDiscount>(); foreach (var item in orders) { item.Status = Status.Registered; item.ShoppingId = shopping.Id; var product = await _db.Products.Where(c => c.Id.Equals(item.ProductId)) .Include(c => c.SpecialDiscount).FirstOrDefaultAsync(); product.SellNumber++; product.Stock--; products.Add(product); var specialDiscount = product.SpecialDiscount.Where(c => c.ProductId.Equals(product.Id) && c.ExpirationDate > System.DateTime.Now && c.ActivationDate <= System.DateTime.Now).FirstOrDefault(); if (specialDiscount != null) { specialDiscount.SellNumber++; specialDiscounts.Add(specialDiscount); } } await UpdateOrdersAsync(orders); await UpdateProductsAsync(products); await UpdateSpeicalDiscountsAsync(specialDiscounts); //Send SMS to customer try { Kavenegar.KavenegarApi api = new Kavenegar.KavenegarApi("677045782B79456B7242656C377A372B514A667157487031756D2B536E687954"); var verifysms = api.VerifyLookup(user.Mobile, user.FullName, shopping.TracingCode, shopping.TotalPrice.ToString(), "success"); } catch (Kavenegar.Exceptions.ApiException ex) { // در صورتی که خروجی وب سرویس 200 نباشد این خطارخ می دهد. } //Send SMS to Admin and Manager try { var webmasters = await _userManager.Users.Where(c => c.ClientRole.Equals(ClientRole.Admin) || c.ClientRole.Equals(ClientRole.Manager)).ToListAsync(); foreach (var item in webmasters) { Kavenegar.KavenegarApi api = new Kavenegar.KavenegarApi("677045782B79456B7242656C377A372B514A667157487031756D2B536E687954"); var verifysms = api.VerifyLookup(item.Mobile, shopping.TracingCode, shopping.TotalPrice.ToString(), shopping.ReceiverName, "order"); } } catch (Exception) { } return(View(model: shopping)); } } return(RedirectToAction(actionName: "ShoppingBasket")); }
public async Task <IActionResult> PaymentGateway(int shoppingId = -1) { var shopping = await _db.Shoppings.FindAsync(shoppingId); if (shopping == null) { return(RedirectToAction(actionName: "ShoppingBasket")); } var user = await _userManager.FindByNameAsync(HttpContext.User.Identity.Name); //if local payment is selected if (shopping.IsLocalPayment) { //update shopping await UpdateShoppingAsync(shopping, System.DateTime.Now.ToString("yyyyMMddmmss"), Status.Registered); //update user: update buy number and total buy value of this user user.BuyNumber++; user.TotalBuyValue += shopping.TotalPrice; await _userManager.UpdateAsync(user); //update discount remaining number and discount used number await DecreaseDiscountNumberAsync(shopping); var orders = await _db.Orders .Where(c => c.UserId.Equals(user.Id) && c.Status.Equals(Status.WaitForRegister)) .ToListAsync(); var products = new List <Product>(); var specialDiscounts = new List <SpecialDiscount>(); foreach (var item in orders) { item.Status = Status.Registered; item.ShoppingId = shopping.Id; var product = await _db.Products.Where(c => c.Id.Equals(item.ProductId)) .Include(c => c.SpecialDiscount).FirstOrDefaultAsync(); product.SellNumber++; product.Stock--; products.Add(product); var specialDiscount = product.SpecialDiscount.Where(c => c.ProductId.Equals(product.Id) && c.ExpirationDate > System.DateTime.Now && c.ActivationDate <= System.DateTime.Now).FirstOrDefault(); if (specialDiscount != null) { specialDiscount.SellNumber++; specialDiscounts.Add(specialDiscount); } } await UpdateOrdersAsync(orders); await UpdateProductsAsync(products); await UpdateSpeicalDiscountsAsync(specialDiscounts); //Send SMS to customer try { Kavenegar.KavenegarApi api = new Kavenegar.KavenegarApi("677045782B79456B7242656C377A372B514A667157487031756D2B536E687954"); var verifysms = api.VerifyLookup(user.Mobile, user.FullName, shopping.TracingCode, shopping.TotalPrice.ToString(), "success"); } catch (Kavenegar.Exceptions.ApiException ex) { // در صورتی که خروجی وب سرویس 200 نباشد این خطارخ می دهد. } //Send SMS to Admin and Manager try { var webmasters = await _userManager.Users.Where(c => c.ClientRole.Equals(ClientRole.Admin) || c.ClientRole.Equals(ClientRole.Manager)).ToListAsync(); foreach (var item in webmasters) { Kavenegar.KavenegarApi api = new Kavenegar.KavenegarApi("677045782B79456B7242656C377A372B514A667157487031756D2B536E687954"); var verifysms = api.VerifyLookup(item.Mobile, shopping.TracingCode, shopping.TotalPrice.ToString(), shopping.ReceiverName, "order"); } } catch (Exception) { } return(RedirectToAction(actionName: "SuccessfulPayment", routeValues: new { shoppingId = shopping.Id })); } else { //if online payment is selected send user to zarin pal gateway int amount = (int)shopping.TotalPrice; ZarinPalGateWay.PaymentGatewayImplementationServicePortTypeClient zp = new ZarinPalGateWay.PaymentGatewayImplementationServicePortTypeClient(); var paymentRequest = await zp.PaymentRequestAsync(MerchantID : "1e608052-d5c6-11e8-bfd3-005056a205be", Amount : amount, Description : "خرید محصول", Email : "", CallbackURL : "http://www.shopikardemo.ir/Shopping/SuccessfulPayment?shoppingId=" + shopping.Id , Mobile : user.Mobile); int status = paymentRequest.Body.Status; string authority = paymentRequest.Body.Authority; if (status.Equals(100) && authority.Length.Equals(36)) { return(Redirect("https://www.zarinpal.com/pg/StartPay/" + authority + "/ZarinGate ")); } } return(RedirectToAction("ConfirmAndPayment")); }