php - PayPal:出现错误:“访问 https://api.sandbox.paypal.com/v1/payments/payment 时出现 Http 响应代码 400。”
问题描述
遵循GitHub 示例中的代码以及此处的一些建议(附加错误消息和错误方法),没有什么能让我了解:$payment->create($this->_api_context);
进入 try 块的异常处理,或者超出一般错误消息:
访问https://api.sandbox.paypal.com/v1/payments/payment时的 Http 响应码 400 。
这是我正在使用的方法:
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 (PayPal\Exception\PayPalConnectionException $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;
推荐阅读
- android - 为什么我的听众没有在这个 Firestore 集中点击这里
- java - 意外的记录类型(org.apache.poi.hssf.record.HyperlinkRecord)
- html - 每当访问应用程序时,如何在 vue js 上显示动画?
- tsql - 如何将下一个查询变成 Power Query,我只能执行第一部分
- swift - 如何使 ObservableObject 符合 Codable 协议?
- javascript - 防止两个事件在彼此之后立即触发
- laravel - 从 Vagrant 访问 Lumen 公用文件夹
- excel - 在满足条件的情况下将单元格复制到另一张工作表循环
- java - 我可以使用动态编程降低时间复杂度吗?
- python-3.x - 如何在不指向文件路径的情况下打开文件?