Search code examples
phplaravelpaypalsdkphp-8

Required parameter $path follows optional parameter $handlers paypal sdk


I really need your help on this problem, since I don't understand how to fix it or where to start looking for an answer. Here's the thing:

I'm trying to integrate paypal into Laravel 8 with PHP 8. the package I'm using is the paypal/rest-api-sdk-php v1. I know this is deprecated but I have not found any other solution to get working the v2.

Well, whenever I try to create a payment with the method $payment->create($this->api_context) -by the way, you can check the how-to guiide where I got those examples from below-the process gives me an error saying the following

ErrorException Required parameter $path follows optional parameter $handlers

I know this is because of an optional parameter being called before a required one. but honestly I've been working on this for weeks and I still can't figure it out.

Here some evidence: error-screen: enter image description here

class PagoController (where all magic starts to fail)

class PagoController extends Controller

{ private $api_context;

public function __construct(){

    $paypal_config = Config::get('paypal');

    $this->api_context = new ApiContext(
        new OAuthTokenCredential(
            $paypal_config['client_id'],
            $paypal_config['secret']
        )
    );

    $this->api_context->setConfig($paypal_config['settings']);

    //dd($this->api_context);
}

public function pagarConPaypal(Request $request, $id_conciliacion){

    //Obtener valores de la conciliación
    $registro_pago = Conciliacion::where('id_conciliacion', '=', $id_conciliacion)->first();
    //dd($registro_pago);

//--Crea un nuevo pago

    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $amount = new Amount();
    $amount->setTotal($registro_pago->monto);
    $amount->setCurrency('MXN');

    $transaction = new Transaction();
    $transaction->setAmount($amount);
    $transaction->setDescription($registro_pago->concepto);

    $callbackURL = url('/paypal/estado');
    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl($callbackURL)
        ->setCancelUrl($callbackURL);

    $payment = new Payment();
    $payment->setIntent('Sale')
        ->setPayer($payer)
        ->setTransactions(array($transaction))
        ->setRedirectUrls($redirectUrls);

    //dd($payment);

//--Aplica el pago creado
    try {
        $payment->create($this->api_context, null);
        // echo $payment;
        return redirect()->away($payment->getApprovalLink());
    }
    catch (PayPalConnectionException $ex) {
        // This will print the detailed information on the exception.
        //REALLY HELPFUL FOR DEBUGGING
        echo $ex->getData();
    }

Any help will be very appreciated. Thanks in advance


Solution

  • Don't use the deprecated SDK, there is no support for it.

    Ideally you also shouldn't use any redirects, as that is an old integration method, for old websites.

    Here is the best way to proceed: