PayPal C#.Net SDK信用卡付款样本空引用异常

时间:2015-05-19 21:39:59

标签: c# paypal

感谢您查看我的问题。

我正在尝试使用PayPal SDK。 一旦:

payment.Create(apiContext)

执行,它崩溃说明:

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.

我意识到这是一个标准的Null引用异常,我只是无法弄清楚错误是什么,因为它在代码示例中起作用。 此代码直接来自Paypal's SDK Samples,更具体地说是page

这是我的版本:

    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();
    }

完整的例外情况如下:

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: 

相关的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>

1 个答案:

答案 0 :(得分:0)

您的 web.config 文件看起来格格不入。文件中的每个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>

PayPal .NET SDK wiki也有一个配置示例。