ASPX templates
Version 3.x > ASPX templates > Custom Ecommerce form? View modes: 
User avatar
Member
Member
moodybull-hotmail - 5/29/2009 4:51:55 PM
   
Custom Ecommerce form?
I want to do a custom ecommerce form. It is a sign-up for an event. I want it to be all one form - name, address, phone, credit card details, hit a button and you're done.

After a lot of poking around the forums and looking at the various ecommerce code files I have come up something.

First, the code will add some items to the in-memory shopping cart. I have tested that part and it works.

Second, the code will try to create a new payment gateway object and use it to process payment for the items in the cart. This is the part where I don't know what I am doing. I don't even know how to write the completed order details to the database.

Can some of you more experienced Kentico developers look at this and tell me if I'm even on the right track? Is it even possible to do this? Can you tell me what I'm doing wrong and what I should do instead?

I'm surprised that nobody else has ever had to do this, there's no end-to-end sample code for a single-page payment form anywhere on the forums or the KB.

Here's the code:

protected void Button1_Click(object sender, EventArgs e)
{

cart = CMSContext.CurrentShoppingCart;
int[] aryProductOptions = new int[1] { 20 };
cart.AddShoppingCartItem(17, 1, aryProductOptions);
cart.ShoppingCartBillingAddressID = 1;
cart.ShoppingCartCustomerID = CMSContext.CurrentUser.CustomerInfo.CustomerID;
cart.ShoppingCartPaymentOptionID = 5;
cart.UserInfoObj.UserID = CMSContext.CurrentUser.UserID;
cart.ShoppingCartShippingAddressID = 1;
cart.TotalItemsPrice = 25;
cart.TotalShipping = 0;
Label1.Text = cart.OrderId.ToString();
foreach (ShoppingCartItemInfo item in cart.CartItems)
{
Label2.Text = Label2.Text + item.SKUID;
}

cart.PaymentGatewayProvider.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_NUMBER] = txtCreditCardNumber.Text;
cart.PaymentGatewayProvider.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_CCV] = txtCreditCardCCV.Text;
cart.PaymentGatewayProvider.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_EXPIRATION] = txtCreditCardExpiration.Text;

CMSPaymentGatewayProvider provider = new CMSAuthorizeNetProvider();
cart.PaymentGatewayProvider = provider;

provider.ProcessCustomData();
provider.ProcessPayment();

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

// Show error message
if (cart.PaymentGatewayProvider.ErrorMessage != "")
{
lblError.Visible = true;
lblError.Text = cart.PaymentGatewayProvider.ErrorMessage;
return false;
}

if (cart.PaymentGatewayProvider.IsPaymentCompleted)
{
// Raise payment completed event
cart.RaisePaymentCompletedEvent();
return true;
}
}

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 6/8/2009 7:22:35 AM
   
RE:Custom Ecommerce form?
Hello,

1. You should use a code like this:
cart.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_NUMBER] = txtCreditCardNumber.Text;


instead of:

cart.PaymentGatewayProvider.PaymentGatewayCustomData[AuthorizeNetParameters.CARD_NUMBER] = txtCreditCardNumber.Text;


2. You should create an order before payment process.
Before the code:

provider.ProcessCustomData();
provider.ProcessPayment();


you could insert a code like this:

// Create and initialize new order
OrderInfo oi = new OrderInfo();
oi.OrderCustomerID = ShoppingCartInfoObj.ShoppingCartCustomerID;
oi.OrderCompletedByUserID = ShoppingCartInfoObj.ShoppingCartUserID;
oi.OrderCurrencyID = ShoppingCartInfoObj.ShoppingCartCurrencyID;
oi.OrderDate = DateTime.Now;
oi.OrderBillingAddressID = ShoppingCartInfoObj.ShoppingCartBillingAddressID;
oi.OrderPaymentOptionID = ShoppingCartInfoObj.ShoppingCartPaymentOptionID;
oi.OrderShippingAddressID = ShoppingCartInfoObj.ShoppingCartShippingAddressID;
oi.OrderShippingOptionID = ShoppingCartInfoObj.ShoppingCartShippingOptionID;
oi.OrderSiteID = ShoppingCartInfoObj.ShoppingCartSiteID;
oi.OrderNote = txtNote.Text.Trim();
oi.OrderDiscountCouponID = ShoppingCartInfoObj.ShoppingCartDiscountCouponID;
oi.OrderCompanyAddressID = ShoppingCartInfoObj.ShoppingCartCompanyAddressID;
oi.OrderCustomData.LoadData(ShoppingCartInfoObj.ShoppingCartCustomData.GetData());

OrderStatusInfo osi = OrderStatusInfoProvider.GetFirstEnabledStatus();

oi.OrderStatusID = (osi != null) ? osi.StatusID : 0;
oi.OrderTotalPrice = ShoppingCartInfoObj.RoundedTotalPrice;
oi.OrderTotalShipping = ShoppingCartInfoObj.TotalShipping;
oi.OrderTotalTax = ShoppingCartInfoObj.TotalTax;

try
{
OrderInfoProvider.SetOrderInfo(oi);
oi.OrderInvoiceNumber = oi.OrderID.ToString(); //.PadLeft(8, '0');
OrderInfoProvider.SetOrderInfo(oi);

// Create and initialize order items
foreach (ShoppingCartItemInfo item in ShoppingCartInfoObj.CartItems)
{
if (item.SKUObj != null)
{
OrderItemInfo oii = new OrderItemInfo();

oii.OrderItemSKUID = item.SKUID;
oii.OrderItemOrderID = oi.OrderID;
oii.OrderItemSKUName = item.SKUObj.SKUName;
oii.OrderItemUnitCount = item.CartItemUnits;
oii.OrderItemUnitPrice = item.SKUObj.SKUPrice;
oii.OrderItemCustomData.LoadData(item.CartItemCustomData.GetData());
oii.OrderItemGUID = item.CartItemGUID;
oii.OrderItemParentGUID = item.CartItemParentGUID;

try
{
OrderItemInfoProvider.SetOrderItemInfo(oii);
}
catch (Exception ex)
{
OrderInfoProvider.DeleteOrderInfo(oi.OrderID);
throw ex;
}
}
}


This is a core which should work.


Best regards,
Helena Grulichova