簡體   English   中英

如何解決此錯誤“訪問 https://api.sandbox.paypal.com/v1/payments/payment 時收到 Http 響應代碼 400。 ”

[英]How do I solve this error “Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. ”

我在 Laravel 7 中集成 PayPal 付款。我已經遵循了這個過程和其他問題,包括這個這個等等,但我似乎沒有得到幫助。 這是我的創建付款代碼

    <?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。

400 轉換為錯誤請求。 確保您的請求參數格式正確並且 api 密鑰有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM