首页 > 解决方案 > 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'
    ),
];

标签: phplaravel-5paypal

解决方案


事实证明——正如我所料——由于意外的类型转换,计算存在一些问题。但是,主要问题出在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;

推荐阅读