PayPal:收到错误:"访问 https://api.sandbox.paypal.com/v1/payments/payment 时 HTTP 响应代码 400。



遵循了GitHub示例中的代码,以及这里的一些建议(额外的错误消息和错误方法(,没有什么能让我远离:$payment->create($this->_api_context);,进入try块的异常处理,或者超出通用错误消息:

Http访问时的响应代码400https://api.sandbox.paypal.com/v1/payments/payment.

这是我使用的方法:

public function __construct() {
// PayPal API context.
$configPayPal = Config::get('paypal');
$this->_api_context = new ApiContext(
new OAuthTokenCredential(
$configPayPal['client_id'],
$configPayPal['secret']
)
);
$this->_api_context->setConfig($configPayPal['settings']);
}
public function makePayment(Request $request) {
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$orderForPayment = $request->get('orderForPayment');
$orderTotal = $request->get('orderTotal');
$orderTax = $request->get('orderTax');
$shipping = 0.66;
$items = [];
foreach ($orderForPayment as $index => $item):
$items[$index] = new Item();
$items[$index]->setName($item['name'])
->setCurrency('GBP')
->setQuantity($item['qty'])
->setPrice($item['subtotal']);
endforeach;
$itemsList = new ItemList();
$itemsList->setItems($items);
$details = new Details();
$details->setShipping($shipping)
->setTax($orderTax)
->setSubtotal($orderTotal);
$amount = new Amount();
$amount->setCurrency('GBP')
->setTotal($orderTotal + $orderTax + $shipping)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemsList)
->setDescription("Your transaction description.");
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(URL::route('getPaymentStatus'))
->setCancelUrl(URL::route('getPaymentStatus'));
$payment = new Payment();
$payment->setIntent("Sale")
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
//dd($payment->create($this->_api_context)); exit;
try {
$payment->create($this->_api_context);
} catch (PayPalExceptionPayPalConnectionException $ex) {
// Prints the Error Code
echo $ex->getCode();
// Prints the detailed error message
echo $ex->getData();
echo $this->PayPalError($ex);
} catch (Exception $ex) {
echo $this->PayPalError($ex);
}
foreach ($payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
// Add payment ID to the session.
Session::put('paypal_payment_id', $payment->getId());
if (isset($redirect_url)) {
// Redirect to PayPal.
return Redirect::away($redirect_url);
}
Session::put('error', "Unknown error occurred");
return Redirect::route('makePayment');
}

我已经通过该方法检查了这些值,以确保它们是整数而不是字符串。我试过在英镑和美元之间交换货币。

我以前没有使用过PayPal的API,所以我可能有一些错误,但一般错误正在扼杀调试过程。

在测试时,我决定发送到项目:

$itemsList->setItems(array());

它奏效了。

所以,如果我发送项目,我会收到一个错误,但如果我不发送,我会成功。

我可能错了,但这似乎是沙盒本身的错误。

在"config/paypal.php"文件中,我有:

<?php
return [
'client_id' => env('PAYPAL_CLIENT_ID'),
'secret' => env('PAYPAL_SECRET'),
'settings' => array(
'mode' => env('PAYPAL_MODE'),
'http.ConnectionTimeOut' => 3000,
'log.LogEnabled' => true,
'log.FileName' => storage_path() . '/logs/paypal.log',
'log.LogLevel' => 'ERROR'
),
];

正如我所料,由于意外的类型转换,计算出现了一些问题。然而,主要问题出现在foreach()函数中,其中$index变量是相关产品的Shopify API ID,这很讽刺。

所以,最终,代码是:

$x = 0;
foreach ($orderForPayment as $index => $item):
$items[$x] = new Item();
$items[$x]->setName($item['name'])
->setCurrency('GBP')
->setQuantity($item['qty'])
->setPrice($item['subtotal']);
$++;
endforeach;

响应代码显示400 Bad Request,因此您的请求不起作用。

但我看不到您对$_apiContext的实例化
我猜您还没有为沙箱配置上下文
Mabye你应该阅读以下文章:PayPal PHP SDK:首次调用

编辑:
尝试:

$this->_api_context->setConfig(["mode" => "sandbox"]);

PS:事件虽然你已经添加了实例化,但它并不是解决这个问题所需的所有信息。

最新更新