Hi
The following code creates an order, attaches contacts and order lines where required and processes the order using a credit card. The code is being used within a Sitefinity 3.7x based website using .Net 4
This code works fine in the following situations:
- Debug on my machine. Windows 7 64bit, VS2010
- Dev server. Windows 2003 32bit SP2
This code fails in the following situation:
- Staging server. Windows 2008 64bit SP1
Older .NET2 code that is very similar to this code works fine in the following situation:
- Production server: Windows 2008 64Bit SP2.
In all cases the same iMIS system and Database are being used.
The code fails with the following error. Line 800 is the order.Save() line. In the code that this is based on the error occurs during the payment property setting although where it occurs seems to vary between the various properties.
Object reference not set to an instance of an object. ----
at Asi.iBO.Financials.CAccountingEvent.StandardOrderProFormaFullPurchase(DataServer server, CStandardOrder order, CBatch batch, CPayment payment) at Asi.iBO.Commerce.CStandardOrder.Save(DataServer server, Boolean validate, Boolean useTransaction) at Asi.iBO.Commerce.CStandardOrder.Save() at Cplqld.Org.Au.CplWebsite.Web.CplExtensions.Controls.CplCommerce.Checkout.Checkout.TestPurchase_Button_OnClick(Object sender, EventArgs e) in C:\Users\sharrap\Documents\Visual Studio 2010\Workspace\Projects\CplWebSite\Main\Src\CplWebsite\CplExtensions\Controls\CplCommerce\Checkout\Checkout.ascx.cs:line 800
There are 2 points in this code that require assistance of order methods:
- ImisHelper.ManagerUser is supplied at the end
- ImisHelper.GetBatch creates or retrieves a batch. It has proved its selve many times and works in all cases.
Can anyone help? - this is the most frustrating bug I have yet had to deal with. It it wanders between lines, accross servers and between .Net versions.Thank you so much to anyone who can put a silver bullet in this.
Cheers,
Steven.
The code follows:
<asp:Button runat="server" ID="TestPurchase_Button" Text="[Do test purchase]" OnClick="TestPurchase_Button_OnClick" />
<asp:Literal runat="server" ID="TestPurchase_Literal" Text="[]" />
protected void TestPurchase_Button_OnClick(object sender, EventArgs e)
{
const string firstName = "Firstname";
const string lastName = "Lastname";
const string email = firstnamelastname@somewhere.com;
CContact contact;
var sqlParams = new SqlParameter[3];
sqlParams[0] = new SqlParameter("@FIRST_NAME", firstName);
sqlParams[1] = new SqlParameter("@LAST_NAME", lastName);
sqlParams[2] = new SqlParameter("@EMAIL", email);
var contacts = CContact.GetContacts(
ImisHelper.ManagerUser,
string.Empty,
"AND Upper(FIRST_NAME) = Upper(@FIRST_NAME) AND Upper(LAST_NAME) = Upper(@LAST_NAME) AND Upper(EMAIL) = Upper(@EMAIL)",
sqlParams);
if (contacts.Length == 0)
{
contact = new CContact(ImisHelper.ManagerUser)
{
FirstName = firstName,
LastName = lastName,
EmailAddress = email,
CustomerTypeCode = "INDIV"
};
}
else
{
contact = contacts[0];
}
contact.DefaultAddress.Address1 = "55 Street st";
contact.DefaultAddress.County = "Mycounty";
contact.DefaultAddress.StateProvince = "Queensland";
contact.DefaultAddress.PostalCode = "4000";
contact.DefaultAddress.Country = "Australia";
contact.DefaultAddress.AddressPurpose = "Home";
contact.InstituteName = "Company Ltd";
contact.WorkPhone = string.Empty;
contact.HomePhone = string.Empty;
contact.TollFreePhone = "0121121212";
contact.Prefix = "Mr";
if (contact.Validate())
{
contact.Save();
}
else
{
throw new Exception("Error getting/creating contact");
}
var imisLog = new ImisLog();
var batch = ImisHelper.GetBatch(
imisLog,
this.BatchDescription,
this.BatchCreatedBy,
this.BatchGLCashAccount,
this.BatchCashAccountCode);
if (imisLog.Result != ImisLog.ImisLogResult.Success)
{
throw new Exception("Error getting/creating batch");
}
var billingAddress = contact.NewAddress();
billingAddress.InstituteName = contact.InstituteName;
billingAddress.Address1 = contact.DefaultAddress.Address1;
billingAddress.County = contact.DefaultAddress.County;
billingAddress.StateProvince = contact.DefaultAddress.StateProvince;
billingAddress.PostalCode = contact.DefaultAddress.PostalCode;
billingAddress.Country = contact.DefaultAddress.Country;
var order = new CStandardOrder(ImisHelper.ManagerUser)
{
BillToContactId = contact.ContactId,
ShipToContactId = contact.ContactId,
FirstName = contact.FirstName,
LastName = contact.LastFirst,
ShipToAddress = contact.DefaultAddress,
BatchNumber = batch.BatchNumber,
CashAccountCode = this.BatchCashAccountCode,
TermsCode = "0",
OrderTypeCode = "WEB",
UseMemberPrice = false
};
var line = order.NewLineItem("ADVCD", 1);
if (line == null || !line.Validate())
{
throw new Exception("Error creating line");
}
var payment = order.Payment;
payment.Amount = order.TotalCharges;
payment.PaymentType = EnumPaymentType.CreditCard;
payment.CreditCardExpiration = "01/12";
payment.CreditDebitCardHoldersName = "Mr First J Last";
payment.CreditCardSecurityCode = "123";
payment.CashAccountCode = "W_VISA";
payment.CreditCardNumber = "4111111111111111";
if (payment.Validate() && order.Validate())
{
var paymentResponse = payment.ProcessPayment(billingAddress);
if (!paymentResponse.IsSuccess)
{
throw new Exception("Error processing payment");
}
if (!order.Save())
{
throw new Exception("Error saving order");
}
}
else if (!payment.Validate())
{
throw new Exception("Error validationg payment");
}
else if (!order.Validate())
{
throw new Exception("Error validating order");
}
TestPurchase_Literal.Text = "Wooot!";
}
}
public class ImisHelper
{
private static IiMISUser managerUser = null;
public static IiMISUser ManagerUser
{
get
{
if (managerUser == null)
{
managerUser = CStaffUser.Login(
CStaffUser.DefaultStaffUserID, "a password");
}
return managerUser;
}
}
}
Can you update the staging server to SP2?
-