[英]How do I solve this error “Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. ”
I am integrating PayPal payment in Laravel 7. I have followed this process and other questions including this , this and many more but I dont seem to get help.我在 Laravel 7 中集成 PayPal 付款。我已经遵循了这个过程和其他问题,包括这个, 这个等等,但我似乎没有得到帮助。 This is my create payment code
这是我的创建付款代码
<?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);
$payment->create($apiContext); gives Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment .
访问https://api.sandbox.paypal.com/v1/payments/payment时给出 Got Http 响应代码 400。
When I use return $payment
I get the following response.当我使用
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" } ] }
What am I doing wrong and how do I solve this issue?我做错了什么,我该如何解决这个问题? I have input the correct client and secret ID's.
我输入了正确的客户和秘密 ID。
400 translates to bad request. 400 转换为错误请求。 Make sure your request params are well formatted and api key is valid.
确保您的请求参数格式正确并且 api 密钥有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.