Custom E-Commerce One-Page Checkout web part issues

Keith Donnell asked on September 14, 2015 23:51

Hello there,

I am working on a custom single-page checkout web part, and I have the billing address, shipping address, and shipping provider working. I am now working on getting payments (using the built-in Authorize.NET provider) to work, and I'm having some trouble figuring out the order of operations, as the code I need is spread out all over the place. Here is what I have cobbled together so far:

        ShoppingCart.ShoppingCartPaymentOptionID = PaymentOptionInfoProvider.GetPaymentOptionInfo("CreditCard", CurrentSiteName).PaymentOptionID;

        ShoppingCart.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_NUMBER] = txtCardNumber.Text;
        ShoppingCart.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_CCV] = txtCVV.Text;
        DateTime expiry = new DateTime(Convert.ToInt32(ddlExpiryYear.SelectedValue), Convert.ToInt32(ddlExpiryMonth.SelectedValue), 1);
        ShoppingCart.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_EXPIRATION] = expiry;

        ShoppingCartInfoProvider.SetOrder(ShoppingCart);
        ShoppingCartInfoProvider.SetShoppingCartInfo(ShoppingCart);

        PaymentGatewayProvider.OrderId = ShoppingCart.OrderId;
        PaymentGatewayProvider.ShoppingCartInfoObj = ShoppingCart;

        // Validate data if web part is not placed in wizard
        if (!String.IsNullOrEmpty(PaymentGatewayProvider.ValidateCustomData()))
        {
            // Do not continue if validation failed
            return;
        }

        PaymentGatewayProvider.ProcessCustomData();

        // Skip payment when already paid or user is not authorized
        if (!PaymentGatewayProvider.IsPaymentCompleted &&
            PaymentGatewayProvider.IsUserAuthorizedToFinishPayment(MembershipContext.AuthenticatedUser, ShoppingCart, !IsLiveSite))
        {
            // Process payment 
            PaymentGatewayProvider.ProcessPayment();
        }

        // Show info message
        if (PaymentGatewayProvider.InfoMessage != "")
        {
            lblInfo.Visible = true;
            lblInfo.Text = PaymentGatewayProvider.InfoMessage;
        }

        // Show error message
        if (PaymentGatewayProvider.ErrorMessage != "")
        {
            ShowError(PaymentGatewayProvider.ErrorMessage);
        }

        // Redirect to next sibling if payment is complete
        if (PaymentGatewayProvider.IsPaymentCompleted)
        {
            URLHelper.Redirect(CurrentDocument.Parent.Children.OrderBy(c => c.NodeOrder).First(c => c.NodeOrder > CurrentDocument.NodeOrder).RelativeURL);
        }

Most of this code (and the comments) was pulled from various code-behinds of a few web parts. I have made it past a couple errors regarding missing data (no order associated with the cart yet) and missing payment gateway url (config; my fault), but now the line PaymentGatewayProvider.ProcessPayment() gives me a null reference exception. I have checked the ShoppingCartInfoObj reference, and it has the PaymentGatewayCustomData hashtable populated appropriately.

Here is a quick summary of my issues/questions:

  1. Null reference exception on PaymentGatewayProvider.ProcessPayment()
  2. When should ShoppingCartInfoProvider.SetOrder(ShoppingCart) be called? Currently, successive calls to this method generates new order records, even if the cart is unchanged.
  3. Once the payment is successful, how do I clear out the cart session?
  4. Once an order is paid, is the best practice to use the Order GUID to reference it (i.e. for the receipt page order querystring param)?
  5. Is there any documentation or sample code out there that would be able to help me with this? I can't seem to find anything about custom checkout.

Recent Answers


Joshua Adams answered on September 15, 2015 21:20

Have you installed a default kentico site and looked at how the one page checkout is done in that? The dancing goat site has a good example I believe. If you are truly trying to make a complete one page checkout, then you will have to make sure that when you save the billing address, or customer details, and modify any of the other controls, that it updates the order and reloads the other controls. The order has to be created before ever going into the processpayment method. Check the ShoppingCart properties and see if any of the necessary items are missing. Is your customer null, is the cart missing the payment method, is shipping set? kentico's one page cart has some limitation to it, which depending on how your page is set up may be causing the issue. Are you using their webparts out of the box, have you customized any of them?

0 votesVote for this answer Mark as a Correct answer

Keith Donnell answered on September 15, 2015 21:50

@Joshua I am not using any of the web parts out of the box - we need full control of the markup, and we gave up trying to configure the installed web parts to suit our needs after a few hours of getting nowhere. So far I have been able to piece the above code together from various payment-related web parts. I have omitted the (working) billing address, shipping address, and shipping method code, which was also pulled from various out-of-the-box web parts - those values are correctly applied to the ShoppingCart, and the Order (once SetOrder() is called).

I have inspected the ShoppingCart and PaymentGatewayProvider objects, and found that almost nothing was null, so whatever it is is probably buried in a child object somewhere. Unfortunately, it's bombing out outside of source I have access to.

I had been working off of the E-Commerce starter site as an example, and haven't checked out Dancing Goat (I just started an install including that one - thanks for the tip on that).

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 15, 2015 22:19

Have you verified that the orderid is populated, the billingaddress is not null, the payment method is not null or 0, the customer isn't null, and that the shipping has been set or that the cart says its not needed?

These would be where i would be checking. Unfortunately since its a custom solution, its hard for others to dive in without seeing the code and being able to see everything thats going on. Hopefully one of those is the issue. If not, you may need to give us some more information to work with.

0 votesVote for this answer Mark as a Correct answer

Keith Donnell answered on September 15, 2015 22:39

FYI: I checked dancing goat - unfortunately when you select the "Credit Card" payment option, it skips the payment step completely.

I'll inspect what you mention above and update here.

0 votesVote for this answer Mark as a Correct answer

Keith Donnell answered on September 16, 2015 16:13 (last edited on September 16, 2015 16:13)

Have you verified that the orderid is populated, the billingaddress is not null, the payment method is not null or 0, the customer isn't null, and that the shipping has been set or that the cart says its not needed?

ShoppingCart.OrderID:  13
ShoppingCart.ShoppingCartBillingAddress:  Populated with a complete address
ShoppingCart.Order.OrderBillingAddress:  Populated with same address
PaymentGatewayProvider.OrderID:  13
PaymentGatewayProvider.Order.OrderBillingAddress:  Populated with same address
PaymentGatewayProvider.Order.OrderPaymentOptionID:  5 (Credit Card)
PaymentGatewayProvider.Order.OrderCustomerID:  1
PaymentGatewayProvider.Order.OrderShippingAddress:  Populated with a complete address
PaymentGatewayProvider.Order.OrderShippingOptionID:  8

Everything mentioned is accounted for. I'm not sure where to go next on this.

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 16, 2015 16:31

How is your payment method set up? Using a custom class or the default Kentico payment gateway? Where do you get the error, when the credit card form is loading or after when the payment is being submitted?

0 votesVote for this answer Mark as a Correct answer

Keith Donnell answered on September 16, 2015 16:40

We are using the Authorize.net (out of the box) payment gateway. I have configured the gateway with appropriate test keys (that work with other ecommerce platforms), and have set the payment method's Payment Gateway URL to https://secure2.authorize.net/gateway/transact.dll (I have also tried https://secure.authorize.net/gateway/transact.dll wit the same result).

We get the error the line PaymentGatewayProvider.ProcessPayment();

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 16, 2015 18:58

What about the shoppingcart.shoppingcartcontrol is that coming in as null? The issue has to be since you are trying to create a one page checkout. Kentico has a specific reason for why their one page cart isn't actually one page, that it passes you to the payment form on a separate page. You may have to add a hash from the order and when your other information is saved, to add that to the current page.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.