1
感謝您查看我的問題。PayPal C#.Net SDK信用卡支付示例空引用異常
我正在嘗試使用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.
我意識到,這是一個標準的空引用異常,我無法弄清楚什麼是錯的,因爲它工作在代碼示例。 此代碼直接從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 = "",
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 = "",
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 = "[email protected]"
}
};
// 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>