我正在 PHP 中使用 REST API 来尝试创建 PayPal 购买,但每当运行它时,我只会收到一般的 400 错误。显然这意味着错误的请求,但几乎没有给出任何细节。这是我的代码的主要部分:
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();
代码的最后一部分输出:
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 https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment
我遇到了同样的问题,但最终通过捕获 Exception 而不是 PPConnectionException 发现了根本原因(根据欺骗我们俩的 SDK 示例代码)。一旦我这样做了,我就能够转储异常详细信息,显示 PayPal 抱怨我的交易金额格式不正确 - 小数点后只有一位数字。查看上面的代码,情况不太可能是这样,但是更改异常处理程序应该很快就能揭示真正的问题。
但将来您可能还容易受到其他值的格式问题的影响,您可以通过以下方法解决:
money_format('%!i', $amount)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)