1
votes

I am trying to implement a credit card payment solution on my website using SagePay, where I can take payment on my website. I am using ASP.NET Web forms and C#. Here is my code:

var objSagePay = new SagePayIntegration();
var request = objSagePay.DirectPaymentRequest();

if ((request != null)) {
    request.Vendor = "testVendor";
    request.CardHolder = "Customer Name";
    request.CardNumber = "4900000000000";
    request.CardType = CardType.VISA;
    request.ExpiryDate = "0119";
    request.Cv2 = "123";
    request.Currency = "GBP";
    request.Amount = 10.0;
    request.BillingAddress1 = "Test Address 1";
    request.BillingAddress2 = "";
    request.BillingPostCode = "412";
    request.BillingCountry = "";
}

var result = objSagePay.ProcessDirectPaymentRequest(request, "https://test.sagepay.com/gateway/service/vspdirect-register.vsp");

When the result object call is executed, I get Object reference is Null.

Any idea where i am doing things wrong?

Stack Trace

[NullReferenceException: Object reference not set to an instance of an object.] SagePay.IntegrationKit.SagePayIntegration.GetPropertyTextValue(Object request, String propertyName) in C:\dev\dotnet\VspDotNetKitAPI\SagePayIntegration.cs:93 SagePay.IntegrationKit.SagePayIntegration.ProcessDirectPaymentRequest(IDirectPayment paymentRequest, String directPaymentUrl) in C:\dev\dotnet\VspDotNetKitAPI\SagePayIntegration.cs:449 orders_payment_Default.btnProceedCreditCard_Click(Object sender, EventArgs e) in Default.aspx.cs:93 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9692746 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562

1
Can you post the full stack trace?DavidG
I have edited the question and included the stack traceJohn Sheedy
Looks like you aren't setting enough properties. Can you also try setting the VendorTxCode? This is your order number for example.DavidG

1 Answers

3
votes

You aren't setting enough properties on your payment request so it is throwing the NullReferenceException when trying to build the request object internally.

You can check if you have set at least the minimum values by calling the Validation method:

var errors = objSagePay.Validation(SagePay.IntegrationKit.ProtocolMessage.DIRECT_PAYMENT,
    typeof(SagePay.IntegrationKit.Messages.IDirectPayment), 
    request, 
    SagePay.IntegrationKit.ProtocolVersion.V_300);

The errors object in this case will tell you that you are missing the following value:

VendorTxCode

This is your transaction identifier, for example, an order number. It needs to be a unique value for every payment you make. If you set that value, the error should go away.

Bonus: If you get really stuck with the SagePay Integration Kit for .Net, you can see the source code for it in my GitHub repository.