I am integrating paypal for the first time in laravel 5.2. I am using the PayPal SDK as the api but I have reached a point where I am stuck. when I submit a payment form I get the following error.
"PayPalConnectionException in PayPalHttpConnection.php line 176: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment."
I got the tutorial from this website and here is the code from my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
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\ExecutePayment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
use Session;
use Redirect;
use Config;
use URL;
use Redirects;
class IndexController extends Controller
{
private $_api_context;
public function __construct()
{
// setup PayPal api context
$paypal_conf = Config::get('paypal');
$this->_api_context = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
$this->_api_context->setConfig($paypal_conf['settings']);
}
public function paypalform()
{
return view('sponsors.paypalform');
}
public function postPayment()
{
$input = \Request::all();
$product = $input['product'];
$price = $input['price'];
$shipping = 2;
$total = $price + $shipping;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item_1 = new Item();
$item_1->setName($product) // item name
->setCurrency('USD')
->setQuantity(2)
->setPrice($price); // unit price
$item_list = new ItemList();
$item_list->setItems([$item_1]);
$details = new Details();
$details->setShipping($shipping)
->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($item_list)
->setDescription('Your transaction description')
->setInvoiceNumber(uniqid()); // added
$redirect_urls = new RedirectUrls();
$redirect_urls->setReturnUrl(URL::route('payment.status'))
->setCancelUrl(URL::route('payment.status'));
$payment = new Payment();
$payment->setIntent('Sale')
->setPayer($payer)
->setRedirectUrls($redirect_urls)
->setTransactions(array($transaction));
try {
$payment->create($this->_api_context);
} catch (\PayPal\Exception\PPConnectionException $ex) {
if (\Config::get('app.debug')) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
$err_data = json_decode($ex->getData(), true);
exit;
} else {
die('Some error occur, sorry for inconvenient');
}
}
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirect_url = $link->getHref();
break;
}
}
// add payment ID to session
Session::put('paypal_payment_id', $payment->getId());
if(isset($redirect_url)) {
// redirect to paypal
return Redirect::away($redirect_url);
}
return Redirect::route('original.route')
->with('error', 'Unknown error occurred');
}
}
I think the problems comes when redireciting to paypal website but I can't figure out what exactly is going wrong.
dd($payment->getId());beforeSession::put('paypal_payment_id', $payment->getId());and see what's its response./logs/paypal.logwhich path you can find in$paypal_conf['settings']. That will help to find out what exactly missing