使用REST PHP的PayPal返回错误400 [英] PayPal with REST PHP returning error 400

查看:100
本文介绍了使用REST PHP的PayPal返回错误400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP中使用REST API尝试创建PayPal购买项目,但每次运行它时,我只会收到一般400错误.显然,这意味着要求不高,但是几乎没有给出任何详细信息.这是我的代码的主要部分:

I am using the REST API in PHP to try and create a PayPal purchase, but I only get a generic 400 error whenever I run it. Apparently this means bad request, but there are barely any details given. Here is the main portion of my code:

define("PP_CONFIG_PATH", "../vendor/");

$apiContext = new ApiContext(new OAuthTokenCredential('-redacted-', '-redacted-')); // id, secret

$addr = new Address();
$addr->setLine1($_POST['addr1']);
if(isset($_POST['addr2']) && !empty($_POST['addr2'])) $addr->setLine2($_POST['addr2']);
$addr->setCity($_POST['city']);
$addr->setCountry_code($_POST['country']);
$addr->setPostal_code($_POST['zip']);
$addr->setState($_POST['state']);
$addr->setPhone('9179261285'); // TODO put in actual phone

$card = new CreditCard();
$card->setNumber($_POST['card_num']);
$card->setExpire_month($_POST['expire_mon']);
$card->setExpire_year($_POST['expire_yr']);
$card->setCvv2($_POST['cvv2']);
$card->setFirst_name($_GET['fname']);
$card->setLast_name($_GET['lname']);
$card->setBilling_address($addr);

$fi = new FundingInstrument();
$fi->setCredit_card($card);

$payer = new Payer();
$payer->setPayment_method('credit_card');
$payer->setFunding_instruments(array($fi));

$cost = $_POST['plan'] == 1 ? '19.95' : '29.95';

$amountDetails = new AmountDetails();
$amountDetails->setSubtotal($cost);
$amountDetails->setTax('0.00');
$amountDetails->setShipping('0.00');

$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal($cost);
$amount->setDetails($amountDetails);

$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('MyTrustCo membership subscription.');

$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));

try {
    $response = $payment->create($apiContext);
} catch (PPConnectionException $e) {
    echo "<br />exception:<br />" . $e->getMessage() . "<br />";
}

echo "response: " . $response;
die();

代码的最后一部分输出:

The last part of the code outputs:

exception:
Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.
got response
response:

我不知道是什么原因导致此错误.我正在学习本教程: https://developer.paypal. com/webapps/developer/docs/api/#create-a-payment

I have no idea what could be causing this error. I was following this tutorial: https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment

推荐答案

我遇到了同样的问题,但是最终通过捕获Exception而不是PPConnectionException来发现了根本原因(按照SDK样本代码欺骗了我们俩).完成此操作后,我便可以转储异常详细信息,该异常详细信息表明PayPal抱怨我的交易金额格式不正确-小数点后只有一位数字.在上面的代码中,情况可能不太可能,但是更改异常处理程序应该可以迅速揭示出真正的问题.

I was having the same problem but ultimately uncovered the root cause by capturing Exception instead of PPConnectionException (as per the SDK sample code which fooled both of us). Once I did this, I was able to dump the exception details which revealed that PayPal was complaining that my transaction amount was not formatted correctly - I only had a single digit after the decimal point. Looking at your code above, this is not likely to be the case, but changing the exception handler should reveal the real problem quickly.

尽管将来您可能还会遇到其他值的格式问题,可以通过以下方法解决:

You may also be susceptible to the formatting problem for other values though in future, which you can resolve with the following:

money_format('%!i', $amount)

这篇关于使用REST PHP的PayPal返回错误400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆