c# - SagePay 直接支付集成 C#

标签 c# asp.net .net e-commerce opayo

我正在尝试使用 SagePay 在我的网站上实现信用卡支付解决方案,这样我就可以在我的网站上进行付款。我正在使用 ASP.NET Web 窗体和 C#。这是我的代码:

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");

当执行结果对象调用时,我得到 Object reference is Null。

知道我哪里做错了吗?

堆栈跟踪

[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

最佳答案

您没有为付款请求设置足够的属性,因此在尝试在内部构建请求对象时抛出 NullReferenceException

您可以通过调用 Validation 方法检查您是否至少设置了最小值:

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

本例中的错误对象会告诉您缺少以下值:

VendorTxCode

这是您的交易标识符,例如订单号。它必须是您进行的每笔付款的唯一值。如果您设置该值,错误应该会消失。

奖励:如果您真的对 .Net 的 SagePay 集成工具包感到困惑,您可以在我的 GitHub repository 中查看它的源代码。 .

关于c# - SagePay 直接支付集成 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37441493/

相关文章:

c# - 我想使用 aspx 在网络上旋转图像

c# - "Non-nullable event must contain a non-null value when exiting constructor"

asp.net - UpdatePanel 中的按钮需要单击两次才能触发

c# - 使用默认属性的 DataContractSerializer

c# - 部分观点的论证是错误的类型——这怎么可能?

c# - 用于验证城市/州/ zip 的正则表达式

c# - 我可以从 C# 中另一个类的构造函数调用构造函数吗?

c# - 如何在 WPF 中绑定(bind) Ctrl 和数字键盘/(正斜杠)?

c# - 为什么我的{get; private set} 变量返回一个空字典?

c# - .NET Framework 中 MSIL 源代码的编译器位于何处?