Checkout Step by Step
Traditionally checkout is done after the customer has put his desired products to the cart and reviewed the content on a basket page. In this article we'll show you how to build the checkout step by step. In modern application it's usually done as a single page checkout flow, but multiple steps are possible as well. The APIs of Ucommerce support both scenarios and in fact, the APIs and the order is the same regardless of your choise of checkout flow.
TransactionLibrary is used during checkout. You need it to do the needed operations. It can be retrieved like shown below.
ITransactionLibrary transactionLibrary = Ucommerce.Infrastructure.ObjectFactory.Instance.Resolve<Ucommerce.Api.ITransactionLibrary>();
Update Billing and Shipping information
First thing you need to collect billing and shipping information. The most important information you are collecting is the country your customer is billed and shipping to. This will be used to narrow down available shipping and payment methods.
transactionLibrary.EditBillingInformation( firstName: "Joe", lastName: "Developer", emailAddress: "[email protected]", phoneNumber: "12345678", mobilePhoneNumber:"12345678", company: "Ucommerce", line1: "line 1", line2: "line2", postalCode: "8000", city: "city", state: "state", attention: "Joe developer", countryId: 1);
transactionLibrary.EditShippingInformation( firstName: "Joe", lastName: "Developer", emailAddress: "[email protected]", phoneNumber: "12345678", mobilePhoneNumber:"12345678", company: "Ucommerce", line1: "line 1", line2: "line2", postalCode: "8000", city: "city", state: "state", attention: "Joe developer", countryId: 1);
Create a Shipment
After collecting address information you need to select what shipping method to use for your customer. The API accepts a country to narrow down available methods by store and country. It is up to you if you want to use the country attached to billing or shiping method.
ICollection<ShippingMethod> shippingMethods = transactionLibrary.GetShippingMethods();
//address name is specified in case you're dealing with multiple shipments. Usually you're good with just defaultshipmentname as //the likelyness of having more shipments per checkout is rare. transactionLibrary.CreateShipment(shippingMethodId: 1, Constants.DefaultShipmentAddressName);
Create a Payment
Collecting payment methods is done using the following approach.
ICollection<PaymentMethod> paymentMethods = transactionLibrary.GetPaymentMethods(); transactionLibrary.CreatePayment( paymentMethodId:1, amount: -1, requestPayment:false, overwriteExisting:true);
When you're ready to have the customer enter his creditcard details (or place order for invoice), use the following approach to proceed. This will use the redirect url configured on on the payment method you are using.
string url = transactionLibrary.GetPaymentPageUrl(transactionLibrary.GetBasket(false).Payments.First()); System.Web.HttpContext.Current.Response.Redirect(url); //or return Redirect(url) in MVC
Confirmation page and Emails
When checkout is complete your user will be redirected to a confirmation page and a confirmation email will be sent out. In both situations you want to show some content from the order. This can be done with the following code-snippet:
A common pit-fall here is that when you call GetBasket() on either of theese pages, your customer will be presented with a new basket as the basket was just converted and no longer available.
string orderGuidParameterFromQueryString = System.Web.HttpContext.Current.Request.QueryString["OrderGuid"]; transactionLibrary.GetPurchaseOrder(Guid.Parse(orderGuidParameterFromQueryString));