Below the error that i getting when i click the payment button in myweb application .what could be the solution to my problem i have looked on similar problem and non seems to solve my solution . Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. PayPal\Core\PayPalHttpConnection : ERROR: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. {"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"7abde5aa538aa"}
below is my controller PaypalController.php
<?php
namespace App\Http\Controllers;
use Gloudemans\Shoppingcart\Facades\Cart;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Item;
use PayPal\Api\WebProfile;
use PayPal\Api\ItemList;
use PayPal\Api\InputFields;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use Redirect;
use Session;
use URL;
use App\Invoice;
use App\Project;
use App\Suborder;
use Str;
//Use Request;
use Auth;
use Config;
use DB;
class PaypalController extends Controller
{
private $apiContext;
public function __construct()
{
# Main configuration in constructor
$paypalConfig = Config::get('paypal');
$this->apiContext = new ApiContext(new OAuthTokenCredential(
$paypalConfig['client_id'],
$paypalConfig['secret'])
);
$this->apiContext->setConfig($paypalConfig['settings']);
}
public function pay_amount(Request $request, $id)
{
$id =DB::table('projects')->where('id', $id)->get();
return view('projects.pay')
->with('id', $id);
}
public function payWithpaypal(Request $request, $id)
{
# We initialize the payer object and set the payment method to PayPal
$payer = new Payer();
$payer->setPaymentMethod('paypal');
# We insert a new order in the order table with the 'initialised' status
// $project = new Project();
// $project->user_name = Auth::user()->name;
// $project->invoice_id = null;
// $project->status = 'initialised';
// $project->save();
$project = Project::findOrFail($request->id);
//$project = new Project();
$project->update(['user_name' => (Auth::user()->name)]);
$project->update(['invoice_id' => (null)]);
$project->update(['status' => ('initialised')]);
$project->save();
# We need to update the order if the payment is complete, so we save it to the session
Session::put('projectId', $project->getKey());
# We get all the items from the cart and parse the array into the Item object
//dd($project->project_id);
$item = new Item();
$items[] = (new Item())
->setCurrency('USD')
->setQuantity(1)
->setPrice($project->amount);
//->setName($project->project_id)
//->setName($item->name)
//->setQuantity($item->qty)
//->setPrice($item->price);
//->setPrice($project->total_amount);
//->setPrice($request->get('amount'));
# We create a new item list and assign the items to it
$itemList = new ItemList();
$itemList->setItems(array($items));
# Disable all irrelevant PayPal aspects in payment
// $inputFields = new InputFields();
// $inputFields->setAllowNote(true)
// ->setNoShipping(1)
// ->setAddressOverride(0);
// $webProfile = new WebProfile();
// $webProfile->setName(uniqid())
// ->setInputFields($inputFields)
// ->setTemporary(true);
// $createProfile = $webProfile->create($this->apiContext);
# We get the total price of the cart
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($project->amount);
//->setTotal(Cart::subtotal());
$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setItemList($itemList)
->setDescription('Your transaction description');
$redirectURLs = new RedirectUrls();
$redirectURLs->setReturnUrl(URL::to('status'))
->setCancelUrl(URL::to('status'));
$payment = new Payment();
$payment->setIntent('Sale')
//$payment->setIntent('authorize')
->setPayer($payer)
->setRedirectUrls($redirectURLs)
->setTransactions(array($transaction));
//$payment->setExperienceProfileId($createProfile->getId());
//$payment->create($this->apiContext);
try {
$payment->create($this->apiContext);
$execution = new PaymentExecution(); $result = $payment->execute($execution, $apiContext);
} catch (\PayPal\Exception\PPConnectionException $ex) {
if (\Config::get('app.debug')) {
\Session::put('error', 'Connection timeout');
return Redirect::to('/');
} else {
\Session::put('error', 'Some error occur, sorry for inconvenient');
return Redirect::to('/');
}
}
foreach ($payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
# We store the payment ID into the session
Session::put('paypalPaymentId', $payment->getId());
if (isset($redirectURL)) {
return Redirect::away($redirectURL);
}
Session::put('error', 'There was a problem processing your payment. Please contact support.');
return Redirect::to('/client/projects');
}
public function getPaymentStatus()
{
/** Get the payment ID before session clear **/
$payment_id = Session::get('paypal_payment_id');
/** clear the session payment ID **/
Session::forget('paypal_payment_id');
if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
\Session::put('error', 'Payment failed');
return Redirect::to('/');
}
$payment = Payment::get($payment_id, $this->_api_context);
$execution = new PaymentExecution();
$execution->setPayerId(Input::get('PayerID'));
/**Execute the payment **/
$result = $payment->execute($execution, $this->_api_context);
if ($result->getState() == 'approved') {
\Session::put('success', 'Payment success');
return Redirect::to('/');
}
\Session::put('error', 'Payment failed');
return Redirect::to('/');
}
}```
The PayPal-PHP-SDK and v1/payments API are deprecated.
Instead use the Checkout-PHP-SDK for the v2/checkout/orders API, documented here: https://developer.paypal.com/docs/business/checkout/server-side-api-calls/#server-side-api-calls
You should make two routes, one for 'Create Order' and one for 'Capture Order'. These routes should return/output only JSON data (no HTML or text)
Pair those two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server