调用payment.Create()

时间:2016-05-24 15:11:20

标签: paypal paypal-sandbox

动机

我想接受我网站上的信用卡。我之所以选择PayPal是因为它们似乎是最不麻烦且不差的价格,我想尽快开始接受信用卡。我选择了信用卡付款,因为我不希望我的网站用户访问PayPal网站;我希望他们留在我的网站上。这看起来更令人印象深刻和专业。

我做了什么

我从Paypal下载了PayPal.SDK.NET451PayPal.SDK.Sample.VS.2013。我在PayPal.dll版本1.7.3.0上运行我在沙箱上创建了developer account,但是对于这个实验,他们建议只使用随附的clientIdclientSecret示例项目,以排除我的帐户或PayPal上托管的应用程序定义的任何问题。

问题

在以下代码中,Configuration.GetAPIContext()card.Create(apiContext)成功。我跟Fiddler进行了跟踪,可以看到2xx响应,并且没有异常被抛出。

但是,在调用payment.Create(apiContext)时会抛出异常:

PayPal.PaymentsException was unhandled by user code   
HResult=-2146233088   
Message=The remote server returned an error: (400) Bad Request.   
Response={"name":"UNKNOWN_ERROR","message":"An unknown error has occurred", "information_link":"https://developer.paypal.com/webapps/developer/docs/api/#UNKNOWN_ERROR", "debug_id":"2ae8b36c6ea98"}   
Source=PayPal   
StackTrace:      
   at PayPal.Api.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payload, String endpoint, Boolean setAuthorizationHeader) in C:\Documents\Downloads\PayPal-NET-SDK-develop\PayPal-NET-SDK-develop\Source\SDK\Api\PayPalResource.cs:line 208      
   at PayPal.Api.Payment.Create(APIContext apiContext, Payment payment) in C:\Documents\Downloads\PayPal-NET-SDK-develop\PayPal-NET-SDK-develop\Source\SDK\Api\Payment.cs:line 140      
   at PayPal.Api.Payment.Create(APIContext apiContext) in C:\Documents\Downloads\PayPal-NET-SDK-develop\PayPal-NET-SDK-develop\Source\SDK\Api\Payment.cs:line 124      

更多信息

异常中没有信息表明原因。请注意,同样的功能似乎也在PayPal's developer site失败了!以及从示例应用程序本身运行时失败。

守则

using PayPal.Api;
using PayPal.Sample;

protected void TestCreditCardPayment()
{
  var apiContext = Configuration.GetAPIContext();

  // First vault a credit card.
  var card = new CreditCard
  {
    expire_month = 11,
    expire_year = 2018,
    number = "4877274905927862",
    type = "visa",
    cvv2 = "874"
  };
  var createdCard = card.Create(apiContext);

  // Next, create the payment authorization using the vaulted card as the funding instrument.
  var payment = new Payment
  {
    intent = "authorize",
    payer = new Payer
    {
      payment_method = "credit_card",
      funding_instruments = new List<FundingInstrument>
    {
        new FundingInstrument
        {
            credit_card_token = new CreditCardToken
            {
                credit_card_id = createdCard.id,
                expire_month = createdCard.expire_month,
                expire_year = createdCard.expire_year
            }
        }
    }
    },
    transactions = new List<Transaction>
    {
        new Transaction
        {
            amount = new Amount
            {
                currency = "USD",
                total = "1.00"
            },
            description = "This is the payment transaction description."
        }
    }
  };
  var createdPayment = payment.Create(apiContext);
}

1 个答案:

答案 0 :(得分:0)

问题是沙箱如何处理特定的卡号。您应该可以使用步骤4生成新的测试卡号:https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1413&expand=true&locale=en_US

只要卡通过LUHN算法,您应该没问题,可以使用其他测试卡号生成器。