简体   繁体   English

PayPal C#.Net SDK信用卡付款示例null参考异常

[英]PayPal C# .Net SDK Credit Card payment sample null reference exception

Thank you for looking at my question. 谢谢您看我的问题。

I'm trying to make use of the PayPal SDK. 我正在尝试使用PayPal SDK。 As soon as: 立刻:

payment.Create(apiContext)

executes, it crashes stating: 执行,它崩溃说明:

An exception of type 'PayPal.PayPalException' occurred in PayPal.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

I realize that this is a standard Null reference exception, I just can't figure out what's wrong because it works in the code sample. 我意识到这是一个标准的Null引用异常,我只是想不出什么错,因为它在代码示例中有效。 This code was taken directly from Paypal's SDK Samples , more specifically this page . 该代码直接取自Paypal的SDK样本 ,更具体地说是此页面

Here is my version of it: 这是我的版本:

    Dictionary<string, string> _paypalConfig;
    Dictionary<string, string> PaypalConfig
    {
        get
        {
            if (_paypalConfig == null)
            {
                _paypalConfig = ConfigManager.Instance.GetProperties();
            }
            return _paypalConfig;
        }
    }

    String _paypalAccessToken;
    String PaypalAccessToken
    {
        get
        {
            if (_paypalAccessToken == null)
            {
                _paypalAccessToken = new OAuthTokenCredential(PaypalConfig).GetAccessToken();
            }
            return _paypalAccessToken;
        }
    }

    APIContext _paypalApiContext;
    APIContext PaypalApiContext
    {
        get
        {
            if (_paypalApiContext == null)
            {
                _paypalApiContext = new APIContext(PaypalAccessToken);
            }
            return _paypalApiContext;
        }
    }

    public ActionResult Pay()
    {
        var apiContext = PaypalApiContext;

        // A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. 
        var transaction = new Transaction()
        {
            amount = new Amount()
            {
                currency = "USD",
                total = "7",
                details = new Details()
                {
                    shipping = "1",
                    subtotal = "5",
                    tax = "1"
                }
            },
            description = "This is the payment transaction description.",
            item_list = new ItemList()
            {
                items = new List<Item>()
                {
                    new Item()
                    {
                        name = "Item Name",
                        currency = "USD",
                        price = "1",
                        quantity = "5",
                        sku = "sku"
                    }
                },
                shipping_address = new ShippingAddress
                {
                    city = "Johnstown",
                    country_code = "US",
                    line1 = "52 N Main ST",
                    postal_code = "43210",
                    state = "OH",
                    recipient_name = "Joe Buyer"
                }
            }
        };

        // A resource representing a Payer that funds a payment.
        var payer = new Payer()
        {
            payment_method = "credit_card",
            funding_instruments = new List<FundingInstrument>()
            {
                new FundingInstrument()
                {
                    credit_card = new CreditCard()
                    {
                        billing_address = new Address()
                        {
                            city = "Johnstown",
                            country_code = "US",
                            line1 = "52 N Main ST",
                            postal_code = "43210",
                            state = "OH"
                        },
                        cvv2 = "874",
                        expire_month = 11,
                        expire_year = 2018,
                        first_name = "Joe",
                        last_name = "Shopper",
                        number = "4877274905927862",
                        type = "visa"
                    }
                }
            },
            payer_info = new PayerInfo
            {
                email = "test@email.com"
            }
        };

        // A Payment resource; create one using the above types and intent as `sale` or `authorize`
        var payment = new Payment()
        {
            intent = "sale",
            payer = payer,
            transactions = new List<Transaction>() { transaction }
        };

        // Create a payment using a valid APIContext
        var createdPayment = payment.Create(apiContext);

        return View();
    }

The full exception is as follows: 完整的例外如下:

PayPal.PayPalException was unhandled by user code
  HResult=-2146233088
  Message=Object reference not set to an instance of an object.
  Source=PayPal
  StackTrace:
   at PayPal.PayPalResource.ConfigureAndExecute[T](Dictionary`2 config, IAPICallPreHandler apiCallPreHandler, HttpMethod httpMethod, String resourcePath)
   at PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payload)
   at PayPal.Api.Payment.Create(APIContext apiContext, Payment payment)
   at PayPal.Api.Payment.Create(APIContext apiContext)
   at PaypalTest.Controllers.HomeController.Pay() in c:\Users\Rian\Documents\Visual Studio 2013\Projects\PaypalTest\PaypalTest\Controllers\HomeController.cs:line 138
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
  InnerException: System.NullReferenceException
   HResult=-2147467261
   Message=Object reference not set to an instance of an object.
   Source=PayPal
   StackTrace:
        at PayPal.Api.RESTAPICallPreHandler.GetProcessedEndPoint()
        at PayPal.Api.RESTAPICallPreHandler.GetEndpoint()
        at PayPal.PayPalResource.ConfigureAndExecute[T](Dictionary`2 config, IAPICallPreHandler apiCallPreHandler, HttpMethod httpMethod, String resourcePath)
   InnerException: 

The relevant web.config sections are as follows: 相关的web.config部分如下:

<configuration>
<configSections>
    <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<configuration>

<paypal>
<settings>
  <add name="mode" value="sandbox"/>
  <add name="connectionTimeout" value="360000"/>
  <add name="requestRetries" value="1"/>
  <add name="clientId" value="AUASNhD7YM7dc5Wmc5YE9pEsC0o4eVOyYWO9ezXWBu2XTc63d3Au_s9c-v-U"/>
  <add name="clientSecret" value="EBq0TRAE-4R9kgCDKzVh09sm1TeNcuY-xJirid7LNtheUh5t5vlOhR0XSHt3"/>
</settings>
</paypal>

Your web.config file looks to be malformed. 您的web.config文件看起来格式错误。 Every XML element in the file should be inside the root <configuration> XML element: 文件中的每个XML元素都应位于根<configuration> XML元素内:

<configuration>
    <configSections>
        <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
    </configSections>

    <paypal>
        <settings>
            <add name="mode" value="sandbox"/>
            <add name="connectionTimeout" value="360000"/>
            <add name="requestRetries" value="1"/>
            <add name="clientId" value="AUASNhD7YM7dc5Wmc5YE9pEsC0o4eVOyYWO9ezXWBu2XTc63d3Au_s9c-v-U"/>
            <add name="clientSecret" value="EBq0TRAE-4R9kgCDKzVh09sm1TeNcuY-xJirid7LNtheUh5t5vlOhR0XSHt3"/>
        </settings>
    </paypal>
</configuration>

The PayPal .NET SDK wiki also has an example of the config. PayPal .NET SDK Wiki也包含配置示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM