【问题标题】:PayPal: Getting error: "Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment."PayPal:出现错误:“访问 https://api.sandbox.paypal.com/v1/payments/payment 时出现 Http 响应代码 400。”
【发布时间】:2018-12-26 03:33:02
【问题描述】:

遵循GitHub example 中的代码和此处的一些建议(additional error messages 和an error method),没有什么能让我了解:$payment->create($this->_api_context); 并进入 try 块的异常处理,或超出一般错误消息:

访问时Http响应码400 https://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 (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'
    ),
];

【问题讨论】:

    标签: php laravel-5 paypal


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      响应代码为400 Bad Request,因此您的请求无法正常工作。

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

      编辑:
      试试:

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

      PS:尽管您添加了实例化事件,但它并不是解决此问题所需的所有信息。

      【讨论】:

      • 我已经定义了这些参数,所以我从代码中添加了__construct() 来演示。
      • 你还没有为沙盒配置你的 apiContext,看我的编辑。
      • 我将:$this-&gt;_api_context-&gt;setConfig($configPayPal['settings']); 替换为:$this-&gt;_api_context-&gt;setConfig(["mode" =&gt; "sandbox"]); 并得到与以前完全相同的错误。所以我然后编辑了“config/paypal.php”文件并将:'mode' =&gt; env('PAYPAL_MODE')更改为:'mode' =&gt; 'sandbox',这也给了我同样的错误。
      猜你喜欢
      • 2020-11-20
      • 2015-05-23
      • 2014-06-11
      • 2021-02-28
      • 2020-11-25
      • 2019-12-04
      • 2017-01-07
      • 2019-06-26
      • 2019-08-07
      相关资源
      最近更新 更多