PayPal Plus integration @Laravel

Rudiko
Contributor
Contributor

Hey, 

 

i want to add PayPal Plus to our Payment methods. 
I´ve allready tested the standard PayPal integration and everything worked out. 

Now i´ve added a view

 

<script src="https://www.paypalobjects.com/webstatic/ppplus/ppplus.min.js"
type="text/javascript"></script>
<div id="ppplus"> </div>
<script type="application/javascript">
 var ppp = PAYPAL.apps.PPP({
   "approvalUrl": "<?php echo htmlspecialchars_decode($approval_url); ?>",
 "placeholder": "ppplus",
 "mode": "sandbox",
"country": "DE"
 });
</script>

Modified my Controller:

 

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payee;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\PaymentExecution;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use URL;
use Session;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;



class PaymentController extends Controller
{
  public function __construct()
      {
  /** 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 payWithpaypal(Request $request)
    {
      $payer = new Payer();
              $payer->setPaymentMethod('paypal');
      $item_1 = new Item();
      $item_1->setName('Item 1') /** item name **/
                  ->setCurrency('EUR')
                  ->setQuantity(1)
                  ->setPrice('52.00'); /** unit price **/
      $item_list = new ItemList();
              $item_list->setItems(array($item_1));
      $amount = new Amount();
              $amount->setCurrency('EUR')
                  ->setTotal('52.00');
      $transaction = new Transaction();
              $transaction->setAmount($amount)
                  ->setItemList($item_list)
                  ->setDescription('Your transaction description');
      $redirect_urls = new RedirectUrls();
              $redirect_urls->setReturnUrl(URL::route('status')) /** Specify return URL **/
                  ->setCancelUrl(URL::route('status'));
      $payment = new Payment();
              $payment->setIntent('Sale')
                  ->setPayer($payer)
                  ->setRedirectUrls($redirect_urls)
                  ->setTransactions(array($transaction));
              /** dd($payment->create($this->_api_context));exit; **/
              try {
      $payment->create($this->_api_context);
      } catch (\PayPal\Exception\PPConnectionException $ex) {
        if (\Config::get('app.debug')) {
        \Session::put('error', 'Connection timeout');
                        return Redirect::route('paywithpaypal');
        } else {
        \Session::put('error', 'Some error occur, sorry for inconvenient');
                        return Redirect::route('paywithpaypal');
        }
        }
        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 **/
      $payment = $payment->create($this->_api_context);
      $approval_url = $payment->getApprovalLink();
      $paypal_payment_id = $payment->getId();
      return view('payments.index', compact('approval_url', 'paypal_payment_id'));
                  // return Redirect::away($redirect_url);
      }
      \Session::put('error', 'Unknown error occurred');
              return Redirect::route('paywithpaypal');
  }


  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::route('/');
    }
    $payment = Payment::get($payment_id, $this->_api_context);
            $execution = new PaymentExecution();
            $payerID = Input::get('PayerID');
            $execution->setPayerId($payerID);
    /**Execute the payment **/
            // dd($payment, $execution);
            $result = $payment->execute($execution, $this->_api_context);

    if ($result->getState() == 'approved') {
    \Session::put('success', 'Payment success');
                return Redirect::route('kalender');
    }
    \Session::put('error', 'Payment failed');
            return Redirect::route('kalender');
  }
}

 

 

What i get is the following error

 

[28-08-2018 11:47:58] PayPal\Core\PayPalHttpConnection : ERROR: Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAY-19G26658PP300502MLOCSPCA/execute. {"name":"PAYMENT_NOT_APPROVED_FOR_EXECUTION","message":"Payer has not approved payment","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"44c31450d767b"}

 

The difference between the standard paypal and my actual version is the payment object, which has no payer object.

The new paypal plus only gets the payer id in execution. 
Any Suggestions?

Thanks!

 

 

 

Login to Me Too
3 REPLIES 3

tisuchi
Contributor
Contributor

I know it's kind of old discussion. However, if anyone else struggling with that, you can follow this post. 

http://laravel-school.com/posts/integrate-paypal-plus-on-laravel-8-79

Login to Me Too

andresjcolina
Contributor
Contributor

segui esse aqui, deu certinho no localhost, fiz os testes no servidor também, agora está dando erro, no log aparece Undefined index: links (View: /home/turismointerno/public_html/modules/Booking/Views/frontend/gateways/paypal-plus.blade.php)

 

na view ta

<input type="hidden" id="paypal_approval_url" value="{{$paymentData['links'][1]['href'] }}">
<input type="hidden" id="paypal_payment_id" value="{{ $paymentData['id'] }}">
 
alguem poderia me ajudar?
 
Login to Me Too

MTS_Stefan
Moderator
Moderator
Hi Rudiko,

It might be easier, if we use the respective terms, when describing things.
PayPal Plus is based on v1 REST payment flow.
This flow looks like this:
- oAuth Call
- Create Call
- req: amount / transaction object
- not req: addresses
-> you get the EC-Token and PayID from us
- If the buyer selects PayPal Plus as payment option, only then you should provide PayPal with the address information
- Patch call to provide shipping address and billing address, the billing address will be dismissed by us as we only use it to prefill the fields on the landing page of a guest checkout transaction
-> now redirect the buyer to us
-> If the buyer approves the transaction, then you receive the buyer back to your <return URL> ++ payID ++ payerID
- now you can call the payment execute with payID ++ payerID to get a transaction ID

If you were to attempt to execute a note approved transaction, you would cause an error. No buyers funding source can be charged by you without them allowing you to do so, aka, provide you with an approval.

Regards,
Stefan
Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.