public async Task PostProcessPaymentAsync(PostProcessPaymentRequest postProcessPaymentRequest) { Order order = postProcessPaymentRequest.Order; Customer customer = await _customerService.GetCustomerByIdAsync(postProcessPaymentRequest.Order.CustomerId); var total = Convert.ToInt32(Math.Round(order.OrderTotal, 2)); if (_zarinPalPaymentSettings.RialToToman) { total = total / 10; } string phoneOfUser = String.Empty; var billingAddress = await _addressService.GetAddressByIdAsync(order.BillingAddressId); var shippingAddress = await _addressService.GetAddressByIdAsync(order.ShippingAddressId ?? 0); if (_customerSettings.PhoneEnabled)// Default Phone number of the Customer { phoneOfUser = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.PhoneAttribute); } if (string.IsNullOrEmpty(phoneOfUser))//Phone number of the BillingAddress { phoneOfUser = billingAddress.PhoneNumber; } if (string.IsNullOrEmpty(phoneOfUser))//Phone number of the ShippingAddress { phoneOfUser = string.IsNullOrEmpty(shippingAddress?.PhoneNumber) ? phoneOfUser : $"{phoneOfUser} - {shippingAddress.PhoneNumber}"; } var name = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.FirstNameAttribute); var family = await _genericAttributeService.GetAttributeAsync <string>(customer, NopCustomerDefaults.LastNameAttribute); string fullName = $"{name ?? ""} {family ?? ""}".Trim(); string urlToRedirect = ""; string zarinGate = _zarinPalPaymentSettings.UseZarinGate ? _zarinPalPaymentSettings.ZarinGateType.ToString() : null; //string description = $"{_storeService.GetStoreByIdAsync(order.StoreId).Name}{(string.IsNullOrEmpty(NameFamily) ? "" : $" - {NameFamily}")} - {customer.Email}{(string.IsNullOrEmpty(phoneOfUser) ? "" : $" - {phoneOfUser}")}"; string description = "asdsadsadsad"; string callbackURL = string.Concat(_webHelper.GetStoreLocation(), "Plugins/PaymentZarinpal/ResultHandler", "?OGUId=" + postProcessPaymentRequest.Order.OrderGuid); string storeAddress = _webHelper.GetStoreLocation(); if (_zarinPalPaymentSettings.Method == EnumMethod.SOAP) { if (_zarinPalPaymentSettings.UseSandbox) { using (ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient zpalSr = new ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient()) { ServiceReferenceZarinpalSandBox.PaymentRequestResponse resp = zpalSr.PaymentRequestAsync( _zarinPalPaymentSettings.MerchantID, total, description, customer.Email, phoneOfUser, callbackURL ).Result; urlToRedirect = ZarinpalHelper.ProduceRedirectUrl(storeAddress, resp.Body.Status, _zarinPalPaymentSettings.UseSandbox, resp.Body.Authority, zarinGate); } } else { using (ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient zpalSr = new ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient()) { var resp = zpalSr.PaymentRequestAsync ( _zarinPalPaymentSettings.MerchantID, total, description, customer.Email, phoneOfUser, callbackURL ).Result; urlToRedirect = ZarinpalHelper.ProduceRedirectUrl ( storeAddress, resp.Body.Status, _zarinPalPaymentSettings.UseSandbox, resp.Body.Authority, zarinGate ); } } } else if (_zarinPalPaymentSettings.Method == EnumMethod.REST) { var url = $"https://{(_zarinPalPaymentSettings.UseSandbox ? "sandbox" : "www")}.zarinpal.com/pg/rest/WebGate/PaymentRequest.json"; var values = new Dictionary <string, string> { { "MerchantID", _zarinPalPaymentSettings.MerchantID }, //Change This To work, some thing like this : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx { "Amount", total.ToString() }, //Toman { "CallbackURL", callbackURL }, { "Mobile", phoneOfUser }, { "Email", customer.Email }, { "Description", description } }; var paymentRequestJsonValue = JsonConvert.SerializeObject(values); var content = new StringContent(paymentRequestJsonValue, Encoding.UTF8, "application/json"); var response = ClientZarinPal.PostAsync(url, content).Result; var responseString = response.Content.ReadAsStringAsync().Result; var restRequestModel = JsonConvert.DeserializeObject <RestRequestModel>(responseString); urlToRedirect = ZarinpalHelper.ProduceRedirectUrl(storeAddress, restRequestModel?.Status, _zarinPalPaymentSettings.UseSandbox, restRequestModel.Authority, zarinGate); } var uri = new Uri(urlToRedirect); _httpContextAccessor.HttpContext.Response.Redirect(uri.AbsoluteUri); }
public ActionResult ResultHandler(string Status, string Authority, string OGUID) { if (!(_paymentPluginManager.LoadPluginBySystemName("Payments.Zarinpal") is ZarinPalPaymentProcessor processor) || !_paymentPluginManager.IsPluginActive(processor)) { throw new NopException("ZarinPal module cannot be loaded"); } Guid orderNumberGuid = Guid.Empty; try { orderNumberGuid = new Guid(OGUID); } catch { } Order order = _orderService.GetOrderByGuid(orderNumberGuid); var total = Convert.ToInt32(Math.Round(order.OrderTotal, 2)); if (_ZarinPalPaymentSettings.RialToToman) { total = total / 10; } if (string.IsNullOrEmpty(Status) == false && string.IsNullOrEmpty(Authority) == false) { long _refId = 0; System.Net.ServicePointManager.Expect100Continue = false; int _status = -1; var storeScope = _storeContext.ActiveStoreScopeConfiguration; var ZarinPalSettings = _settingService.LoadSetting <ZarinpalPaymentSettings>(storeScope); if (_ZarinPalPaymentSettings.UseSandbox) { using (ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient ZpalSr = new ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient()) { var res = ZpalSr.PaymentVerificationAsync( ZarinPalSettings.MerchantID, Authority, total).Result; //test _status = res.Body.Status; _refId = res.Body.RefID; } } else { using (ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient ZpalSr = new ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient()) { var res = ZpalSr.PaymentVerificationAsync( ZarinPalSettings.MerchantID, Authority, total).Result; //test _status = res.Body.Status; _refId = res.Body.RefID; } } var result = ZarinpalHelper.StatusToMessage(_status); order.OrderNotes.Add(new OrderNote() { Note = string.Concat( "پرداخت ", (result.IsOk ? "" : "نا"), "موفق", " - ", "پیغام درگاه : ", result.Message, result.IsOk ? string.Concat(" - ", "کد پی گیری : ", _refId.ToString()) : "" ), DisplayToCustomer = true, CreatedOnUtc = DateTime.UtcNow }); _orderService.UpdateOrder(order); if (result.IsOk && _orderProcessingService.CanMarkOrderAsPaid(order)) { order.AuthorizationTransactionId = _refId.ToString(); _orderService.UpdateOrder(order); _orderProcessingService.MarkOrderAsPaid(order); return(RedirectToRoute("CheckoutCompleted", new { orderId = order.Id })); } } return(RedirectToRoute("orderdetails", new { orderId = order.Id })); }
/// <summary> /// Post process payment (used by payment gateways that require redirecting to a third-party URL) /// </summary> /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param> public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest) { var total = Convert.ToInt32(Math.Round(postProcessPaymentRequest.Order.OrderTotal, 2)); if (_ZarinPalPaymentSettings.RialToToman) { total = total / 10; } string urlToRedirect = ""; Customer cm = _customerService.GetCustomerById(postProcessPaymentRequest.Order.CustomerId); if (_ZarinPalPaymentSettings.UseSandbox) { using (ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient ZpalSr = new ServiceReferenceZarinpalSandBox.PaymentGatewayImplementationServicePortTypeClient()) { ServiceReferenceZarinpalSandBox.PaymentRequestResponse resp = ZpalSr.PaymentRequestAsync( _ZarinPalPaymentSettings.MerchantID, total, string.Concat(_storeService.GetStoreById(postProcessPaymentRequest.Order.StoreId).Name, " - ", cm.Email), cm.Email, _workContext.CurrentCustomer.Addresses.FirstOrDefault().PhoneNumber ?? "", string.Concat(_webHelper.GetStoreLocation(), "Plugins/PaymentZarinpal/ResultHandler", "?OGUId=" + postProcessPaymentRequest.Order.OrderGuid) ).Result; urlToRedirect = (resp.Body.Status == 100) ? string.Concat("https://sandbox.zarinpal.com/pg/StartPay/", resp.Body.Authority) : string.Concat(_webHelper.GetStoreLocation(), "Plugins/PaymentZarinpal/ErrorHandler", "?Error=", resp.Body.Status); _httpContextAccessor.HttpContext.Response.Redirect(urlToRedirect); } } else { using (ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient ZpalSr = new ServiceReferenceZarinpal.PaymentGatewayImplementationServicePortTypeClient()) { ServiceReferenceZarinpal.PaymentRequestResponse resp = ZpalSr.PaymentRequestAsync( _ZarinPalPaymentSettings.MerchantID, total, string.Concat(_storeService.GetStoreById(postProcessPaymentRequest.Order.StoreId).Name, " - ", cm.Email), cm.Email, _workContext.CurrentCustomer.Addresses.FirstOrDefault().PhoneNumber ?? "", string.Concat(_webHelper.GetStoreLocation(), "Plugins/PaymentZarinpal/ResultHandler", "?OGUId=" + postProcessPaymentRequest.Order.OrderGuid) ).Result; urlToRedirect = (resp.Body.Status == 100) ? string.Concat("https://zarinpal.com/pg/StartPay/", resp.Body.Authority) : string.Concat(_webHelper.GetStoreLocation(), "Plugins/PaymentZarinpal/ErrorHandler", "?Error=", "Error : ", ZarinpalHelper.StatusToMessage(resp.Body.Status)); _httpContextAccessor.HttpContext.Response.Redirect(urlToRedirect); } } }