我正在尝试使用PayPal.NET RestApiSDK来存储信用卡并在他们的沙箱中付款。我在 MVC 项目中使用 .NET 4.5。
我遵循了此处的示例代码:https://developer.paypal.com/webapps/developer/docs/api/#store-a-credit-card
最初,事情很容易。第一天,我能够:-接受多次付款-存储几张卡-查找销售-退款销售- 将卡片存放在保险库中(基本上,他们示例代码中的所有内容)
从第一天(大约一周)开始,我就收到http 503"服务器不可用"错误。除非我在睡梦中改变了一些东西,否则我使用的是以前工作的确切代码。
我联系了PayPal支持人员,经过几次来回消息,他们让我知道,虽然他们无法查明我的代码中的错误,但错误一定在我这边,因为他们的服务器工作正常。
真正奇怪的是,我似乎能够做任何不改变数据的事情。例如,我可以打电话给付款。列表()。但是,我不能调用 creditCard.Create() 或付款。创建()。
此外,访问令牌的创建也很好。行 tokenCredential.GetAccessToken() 不返回任何服务器错误。当我调试代码时,它确实返回了正确的令牌。
问题:当我尝试存储卡或付款时,什么可能导致 http 503 错误?
下面是一些相关的代码。
控制器:
public JsonResult RunTestPayment()
{
string id = ConfigManager.Instance.GetProperties()["ClientID"];
string secret = ConfigManager.Instance.GetProperties()["ClientSecret"];
OAuthTokenCredential tokenCredential = new OAuthTokenCredential(id, secret);
string accessToken = tokenCredential.GetAccessToken();
PayPal.Api.Payments.Address billingAddress = new PayPal.Api.Payments.Address();
billingAddress.line1 = "52 N Main St";
billingAddress.city = "Johnstown";
billingAddress.country_code = "US";
billingAddress.postal_code = "43210";
billingAddress.state = "OH";
PayPal.Api.Payments.CreditCard creditCard = new PayPal.Api.Payments.CreditCard();
creditCard.number = "4417119669820331";
creditCard.type = "visa";
creditCard.expire_month = 11;
creditCard.expire_year = 2018;
creditCard.cvv2 = "874";
creditCard.first_name = "Joe";
creditCard.last_name = "Shopper";
creditCard.billing_address = billingAddress;
PayPal.Api.Payments.Details amountDetails = new PayPal.Api.Payments.Details();
amountDetails.subtotal = "7.51";
amountDetails.tax = "0.03";
amountDetails.shipping = "0.03";
PayPal.Api.Payments.Amount amount = new PayPal.Api.Payments.Amount();
amount.total = "7.56";
amount.currency = "USD";
amount.details = amountDetails;
PayPal.Api.Payments.Transaction transaction = new PayPal.Api.Payments.Transaction();
transaction.amount = amount;
transaction.description = "This is the payment transaction description.";
List<PayPal.Api.Payments.Transaction> transactions = new List<PayPal.Api.Payments.Transaction>();
transactions.Add(transaction);
PayPal.Api.Payments.FundingInstrument fundingInstrument = new PayPal.Api.Payments.FundingInstrument();
fundingInstrument.credit_card = creditCard;
List<PayPal.Api.Payments.FundingInstrument> fundingInstruments = new List<PayPal.Api.Payments.FundingInstrument>();
fundingInstruments.Add(fundingInstrument);
PayPal.Api.Payments.Payer payer = new PayPal.Api.Payments.Payer();
payer.funding_instruments = fundingInstruments;
payer.payment_method = "credit_card";
PayPal.Api.Payments.Payment payment = new PayPal.Api.Payments.Payment();
payment.intent = "sale";
payment.payer = payer;
payment.transactions = transactions;
PayPal.Api.Payments.Payment createdPayment = payment.Create(accessToken);
return Json(new JsonWrapper { Data = createdPayment });
}
单步执行时,错误出现在线路上
PayPal.Api.Payments.Payment createdPayment = payment.Create(accessToken);
确切的错误(作为 JSON 对象):
"ClassName":"PayPal.Exception.PayPalException","Message":"Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (503) Server Unavailable.","Data":null,"InnerException":{"ClassName":"PayPal.Exception.ConnectionException","Message":"Invalid HTTP response The remote server returned an error: (503) Server Unavailable.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at PayPal.HttpConnection.Execute(String payLoad, HttpWebRequest httpRequest)","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8nExecutenPayPalCoreSDK, Version=1.4.1.0, Culture=neutral, PublicKeyToken=nullnPayPal.HttpConnectionnSystem.String Execute(System.String, System.Net.HttpWebRequest)","HResult":-2146233088,"Source":"PayPalCoreSDK","WatsonBuckets":null},"HelpURL":null,"StackTraceString":" at PayPal.PayPalResource.ConfigureAndExecute[T](Dictionary`2 config, IAPICallPreHandler apiCallPreHandler, HttpMethod httpMethod, String resourcePath)rn at PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payload)rn at PayPal.Api.Payments.Payment.Create(APIContext apiContext)rn at PayPal.Api.Payments.Payment.Create(String accessToken)rn at Scout.Controllers.PaymentController.RequestPermissions() in e:\Scout\Scout\Controllers\PaymentController.cs:line 1105","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8nConfigureAndExecutenPayPalCoreSDK, Version=1.4.1.0, Culture=neutral, PublicKeyToken=nullnPayPal.PayPalResourcenT ConfigureAndExecute[T](System.Collections.Generic.Dictionary`2[System.String,System.String], PayPal.IAPICallPreHandler, PayPal.HttpMethod, System.String)","HResult":-2146233088,"Source":"PayPalCoreSDK","WatsonBuckets":null
web.config(API 密钥在此处被截断):
...
<configuration>
<configSections>
<section name="paypal" type="PayPal.Manager.SDKConfigHandler, PayPalCoreSDK" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
...
</configSections>
...
<paypal>
<settings>
<add name="endpoint" value="https://api.sandbox.paypal.com"/>
<add name="ClientID" value="AbayoRB3Eq6YxM6"/>
<add name="ClientSecret" value="EDWNfxDxnGZ3hWZW"/>
<add name="connectionTimeout" value="360000"/>
<!-- The number of times a request must be retried if the API endpoint is unresponsive -->
<add name="requestRetries" value="3"/>
</settings>
</paypal>
...
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="ScoutPaypalLog.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
如您所见,我已经配置了log4net,它正在记录我正在使用的另一个.dll(用于RavenDB)生成的数据,但是PayPal没有条目。
谢谢!
我终于卸载了两个nuget软件包RestApiSDK和PayPalCoreSDK。然后我重新启动了Visual Studio。最后,我重新安装了这两个软件包。
在不更改任何代码的情况下,它开始工作。