PayPal RestApiSDK .NET http 503 Server Unavailable

2019-09-06 16:10发布

问题:

I am trying to use the PayPal .NET RestApiSDK to store credit cards and take payments in their sandbox. I am using .NET 4.5 in an MVC project.

I followed the example code here: https://developer.paypal.com/webapps/developer/docs/api/#store-a-credit-card

Initially, things were very easy. On day one, I was able to: -take several payments -store several cards -look up sales -refund sales -store cards in the vault (basically, everything in their example code)

Ever since day one (about a week), I have been getting an http 503 "Server Unavailable" error. Unless I changed something in my sleep, I am using the exact code that worked before.

I contacted PayPal support, and after several back and forth messages they have let me know that while they can't pinpoint an error in my code, the error must be on my side, because their servers are working fine.

What is really strange, is that I seem to be able to do anything that doesn't change data. For instance, I can call payments.List(). However, I can't call creditCard.Create() or payment.Create().

Also, the access token is being created just fine. The line tokenCredential.GetAccessToken() does not return any server error. When I debug the code, it has indeed returned with a proper token.

Question: What could possibly be causing an http 503 error when I try to store a card or take a payment?

Here is some relevant code.

controller:

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

When stepping through, the error occors on the line

        PayPal.Api.Payments.Payment createdPayment = payment.Create(accessToken);

the exact error (as a Json Object):

"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":"8\nExecute\nPayPalCoreSDK, Version=1.4.1.0, Culture=neutral, PublicKeyToken=null\nPayPal.HttpConnection\nSystem.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)\r\n   at PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payload)\r\n   at PayPal.Api.Payments.Payment.Create(APIContext apiContext)\r\n   at PayPal.Api.Payments.Payment.Create(String accessToken)\r\n   at Scout.Controllers.PaymentController.RequestPermissions() in e:\\Scout\\Scout\\Controllers\\PaymentController.cs:line 1105","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nConfigureAndExecute\nPayPalCoreSDK, Version=1.4.1.0, Culture=neutral, PublicKeyToken=null\nPayPal.PayPalResource\nT 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 keys are truncated here):

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

As you can see, I have configured log4net, and it is recording data generated by another .dll I'm using (for RavenDB), but there are no entries made by PayPal.

Thanks!

回答1:

I finally uninstalled the two nuget packages RestApiSDK and PayPalCoreSDK. I then restarted Visual Studio. Finally, I re-installed those same two packages.

Without changing any code, it started working.