/// <summary> /// Initializes the payment.</summary> /// <param name="paymentSystem">The payment System.</param> /// <param name="paymentArgs">The payment arguments.</param> public override void Invoke(DomainModel.Payments.PaymentSystem paymentSystem, PaymentArgs paymentArgs) { Assert.IsNotNull(HttpContext.Current, "Http context is null"); Assert.IsNotNull(paymentSystem, "Payment system is null"); Assert.IsNotNull(paymentArgs.ShoppingCart, "Shopping cart is null"); Assert.IsNotNull(paymentArgs.ShoppingCart.Totals, "Shopping cart totals were not set"); Assert.IsNotNull(paymentArgs.ShoppingCart.Currency, "Shopping cart currency was not set"); Assert.IsNotNull(paymentArgs.ShoppingCart.CustomerInfo, "Customer information was not set"); Assert.IsNotNull(paymentArgs.ShoppingCart.CustomerInfo.ShippingAddress, "Customer shipping address was not set"); Assert.IsNotNull(paymentArgs.ShoppingCart.CustomerInfo.BillingAddress, "Customer billing address was not set"); Assert.IsNotNull(paymentArgs.Description, "Description is empty"); Assert.IsNotNullOrEmpty(paymentSystem.Code, "Payment system code was not set"); Assert.IsNotNullOrEmpty(paymentSystem.Username, "Payment system user name was not set"); Assert.IsNotNullOrEmpty(paymentSystem.Password, "Payment system password was not set"); Assert.IsNotNullOrEmpty(paymentArgs.ShoppingCart.OrderNumber, "Order number was not set"); Assert.IsNotNullOrEmpty(paymentArgs.PaymentUrls.ReturnPageUrl, "Return page was not set"); Assert.IsNotNullOrEmpty(paymentSystem.PaymentUrl, "Payment url was not set"); Assert.IsTrue(paymentArgs.ShoppingCart.Totals.TotalPriceIncVat > 0, "Price is empty"); }
/// <summary> /// Handles the Load event of the Page control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> /// <exception cref="ValidatorException">Order could not be created.</exception> protected void Page_Load(object sender, EventArgs e) { DomainModel.Carts.ShoppingCart shoppingCart = Sitecore.Ecommerce.Context.Entity.GetInstance <DomainModel.Carts.ShoppingCart>(); if (string.IsNullOrEmpty(shoppingCart.PaymentSystem.Code)) { return; } PaymentUrlResolver paymentUrlResolver = new PaymentUrlResolver(); PaymentArgs paymentArgs = new PaymentArgs { PaymentUrls = paymentUrlResolver.Resolve(), ShoppingCart = shoppingCart }; ITransactionData transactionData = Sitecore.Ecommerce.Context.Entity.Resolve <ITransactionData>(); PaymentProvider paymentProvider = Sitecore.Ecommerce.Context.Entity.Resolve <PaymentProvider>(shoppingCart.PaymentSystem.Code); DomainModel.Payments.PaymentSystem paymentSystem = shoppingCart.PaymentSystem; try { paymentProvider.ProcessCallback(paymentSystem, paymentArgs); } catch (Exception exception) { IOrderManager <Order> orderManager = Sitecore.Ecommerce.Context.Entity.Resolve <IOrderManager <Order> >(); shoppingCart.OrderNumber = orderManager.GenerateOrderNumber(); transactionData.DeletePersistentValue(shoppingCart.OrderNumber); HttpContext.Current.Session["paymentErrorMessage"] = exception.Message; this.Response.Redirect(paymentArgs.PaymentUrls.FailurePageUrl); return; } switch (paymentProvider.PaymentStatus) { case PaymentStatus.Succeeded: { IOrderManager <Order> orderManager = Sitecore.Ecommerce.Context.Entity.Resolve <IOrderManager <Order> >(); Order order = orderManager.CreateOrder(shoppingCart); if (order != null) { // Redirect to order confirmation page string orderId = shoppingCart.OrderNumber; ICheckOut checkOut = Sitecore.Ecommerce.Context.Entity.Resolve <ICheckOut>(); if (checkOut is CheckOut) { ((CheckOut)checkOut).ResetCheckOut(); } if (MainUtil.IsLoggedIn()) { orderId = string.Format("orderid={0}", orderId); } else { string encryptKey = Crypto.EncryptTripleDES(orderId, "5dfkjek5"); orderId = string.Format("key={0}", Uri.EscapeDataString(encryptKey)); } this.StartOrderCreatedPipeline(order.OrderNumber); if (!string.IsNullOrEmpty(orderId)) { UrlString url = new UrlString(paymentArgs.PaymentUrls.SuccessPageUrl); url.Append(new UrlString(orderId)); this.Response.Redirect(url.ToString()); } } else { IOrderManager <Order> orderProvider = Sitecore.Ecommerce.Context.Entity.Resolve <IOrderManager <Order> >(); shoppingCart.OrderNumber = orderProvider.GenerateOrderNumber(); transactionData.DeletePersistentValue(shoppingCart.OrderNumber); HttpContext.Current.Session["paymentErrorMessage"] = "Order could not be created."; this.Response.Redirect(paymentArgs.PaymentUrls.FailurePageUrl); } break; } case PaymentStatus.Canceled: { IOrderManager <Order> orderProvider = Sitecore.Ecommerce.Context.Entity.Resolve <IOrderManager <Order> >(); shoppingCart.OrderNumber = orderProvider.GenerateOrderNumber(); transactionData.DeletePersistentValue(shoppingCart.OrderNumber); HttpContext.Current.Session["paymentErrorMessage"] = "Payment has been aborted by user."; this.Response.Redirect(paymentArgs.PaymentUrls.CancelPageUrl); break; } case PaymentStatus.Failure: { IOrderManager <Order> orderProvider = Sitecore.Ecommerce.Context.Entity.Resolve <IOrderManager <Order> >(); shoppingCart.OrderNumber = orderProvider.GenerateOrderNumber(); transactionData.DeletePersistentValue(shoppingCart.OrderNumber); this.Response.Redirect(paymentArgs.PaymentUrls.FailurePageUrl); break; } } }