PayPal c# . net SDK信用卡支付示例空引用异常

本文关键字:引用 异常 net SDK 信用卡 PayPal | 更新日期: 2023-09-27 17:49:44

谢谢你看我的问题。

我正在尝试使用PayPal SDK。As soon As:

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的SDK样本,更具体地说是本页。

这是我的版本:

    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: 

相关网页。配置部分如下:

<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>

PayPal c# . net SDK信用卡支付示例空引用异常

您的网站。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也有一个配置的例子