【问题标题】:How do I solve this error "Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. "如何解决此错误“访问 https://api.sandbox.paypal.com/v1/payments/payment 时出现 Http 响应代码 400。”
【发布时间】:2020-11-20 02:55:30
【问题描述】:

我正在 Laravel 7 中集成 PayPal 支付。我已经关注了这个 process 和其他问题,包括 thisthis 等等,但我似乎没有得到帮助。这是我创建的付款代码

    <?php

namespace App\Http\Controllers;

use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\PaymentExecution;
use Illuminate\Http\Request;
use App\Project;

class PaypalController extends Controller
{
    public function makePayment(){
        return view('paypal');
    }
    public function payForProject($id){
        $project = Project::find($id);
        return view('paypal',['project'=>$project]);
    }
    public function create(Request $request){
        
        // dd($request->all());
        // $project = Project::find($request->project_id);
        $apiContext = new \PayPal\Rest\ApiContext(
            new \PayPal\Auth\OAuthTokenCredential(
                '',//Client ID
                ''//Client Secret
            )
        );

        $payer = new Payer();
        $payer->setPaymentMethod("paypal");

        $item1 = new Item();
        $item1->setName("Project". $request->project_id)
                ->setCurrency('USD')
                ->setQuantity(1)
                ->setSku($request->project_id)
                ->setPrice($request->budget);

        $itemList = new ItemList();
        $itemList->setItems(array($item1));

        $details = new Details();
        $details->setShipping(0)
                ->setTax(0)
                ->setSubtotal($request->budget);


        $amount = new Amount();
        $amount->setCurrency("USD")
                ->setTotal($request->budget)
                ->setDetails($details);

        $transaction = new Transaction();
        $transaction->setAmount($amount)
                ->setItemList($itemList)
                ->setDescription("Payment for project No." .$request->project_id)
                ->setInvoiceNumber(uniqid());

        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl("http://localhost/execute-payment")
                ->setCancelUrl("http://localhost/cancel");


        $payment = new Payment();
        $payment->setIntent("deposit")
                ->setPayer($payer)
                ->setRedirectUrls($redirectUrls)
                ->setTransactions(array($transaction));

        // return $payment;// Tried this return statement which gave the below response.

        // try {
            // $payment->create($apiContext);
        // } catch (PayPal\Exception\PayPalConnectionException $ex) {
            // echo $ex->getCode(); // Prints the Error Code
         //    echo $ex->getData(); // Prints the detailed error message 
         //    die($ex);
            // echo $ex;
            // exit(1);
        // }
                // $payment->create($apiContext);
        try {
            $payment->create($apiContext);
        } catch (PayPal\Exception\PayPalConnectionException $ex) {
            echo $ex->getCode(); // Prints the Error Code
            echo $ex->getData(); // Prints the detailed error message 
            die($ex);
        } catch (Exception $ex) {
            die($ex);
        }
        // return 1;
        return redirect($payment->getApprovalLink());
         
    }
}

$payment->create($apiContext);访问 https://api.sandbox.paypal.com/v1/payments/payment 时给出 Got Http 响应代码 400。

当我使用return $payment 时,我得到以下响应。

{ "intent": "deposit", "payer": { "payment_method": "paypal" }, "redirect_urls": { "return_url": "http://localhost/execute-payment", "cancel_url": "http://localhost/cancel" }, "transactions": [ { "amount": { "currency": "USD", "total": "9781", "details": { "shipping": "0", "tax": "0", "subtotal": "9781" } }, "item_list": { "items": [ { "name": "Project301", "currency": "USD", "quantity": 1, "sku": "301", "price": "9781" } ] }, "description": "Payment for project No.301", "invoice_number": "5f22a904096d7" } ] }

我做错了什么,我该如何解决这个问题?我已输入正确的客户端和机密 ID。

【问题讨论】:

    标签: php laravel paypal


    【解决方案1】:

    400 转换为错误请求。确保您的请求参数格式正确且 api 密钥有效。

    【讨论】:

      猜你喜欢
      • 2018-12-26
      • 2015-05-23
      • 2014-06-11
      • 2021-02-28
      • 2020-11-25
      • 2019-08-07
      • 2019-12-04
      • 2017-01-07
      • 2019-06-26
      相关资源
      最近更新 更多