Object of class Illuminate\Routing\Redirector could not be converted to string. srmklive/laravel-paypal

Viewed 3227

I am currently working on a paypal checkout using paypal and https://github.com/srmklive/laravel-paypal. I'm using the express checkout which I modified it a little bit to fit the requirements of the my project. During testing it is working in a couple of tries, paypal show and payment executes properly but when I tried to run the exact same code. I get this error I don't know what it means.

I tried to check my routes if it all of the errors happens to my routes but all of it are working properly. I also tried dump and die like dd("check") just to check if its really going to my controller and it does. I did this in the method "payCommission" (this where the I think the error happens)

This is my route for the controller

api.php

Route::get('service/commissionfee/payment' , 'api\service\ExpressPaymentController@payCommission');

Route::get('paypal/ec-checkout-success', 'api\service\ExpressPaymentController@payCommissionSuccess');

ExpressPaymentController.php


<?php

namespace App\Http\Controllers\api\service;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Srmklive\PayPal\Services\ExpressCheckout;

class ExpressPaymentController extends Controller
{
    protected $provider;

    public function __construct()
    {
        try {
            $this->provider = new ExpressCheckout();
        }
        catch(\Exception $e){
            dd($e);
        }

    }


    public function payCommission(Request $request)
    {



            $recurring = false;
            $cart = $this->getCheckoutData($recurring);

            try {
                $response = $this->provider->setExpressCheckout($cart, $recurring);

                return redirect($response['paypal_link']);

            } catch (\Exception $e) {

                dd($e);
                return response()->json(['code' => 'danger', 'message' => "Error processing PayPal payment"]);
            }

    }

    public function payCommissionSuccess(Request $request)
    {

        $recurring = false;

        $token = $request->get('token');
        $PayerID = $request->get('PayerID');

        $cart = $this->getCheckoutData($recurring);

        // ? Verify Express Checkout Token
        $response = $this->provider->getExpressCheckoutDetails($token);

        if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
            if ($recurring === true) {
                $response = $this->provider->createMonthlySubscription($response['TOKEN'], 9.99, $cart['subscription_desc']);
                if (!empty($response['PROFILESTATUS']) && in_array($response['PROFILESTATUS'], ['ActiveProfile', 'PendingProfile'])) {
                    $status = 'Processed';
                } else {
                    $status = 'Invalid';
                }
            } else {
                // ? Perform transaction on PayPal
                $payment_status = $this->provider->doExpressCheckoutPayment($cart, $token, $PayerID);
                $status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
            }

            return response()->json(['success' => "payment complete"]);
        }
    }

    private function getCheckoutData($recurring = false)
    {
        $data = [];

        $order_id = 1;

            $data['items'] = [
                [
                    'name'  => 'Product 1',
                    'price' => 9.99,
                    'qty'   => 1,
                ],

            ];

            $data['return_url'] = url('api/paypal/ec-checkout-success');



        // ! 

        $data['invoice_id'] = config('paypal.invoice_prefix').'_'.$order_id;
        $data['invoice_description'] = "Commission Fee payment";
        $data['cancel_url'] = url('/');

        $total = 0;
        foreach ($data['items'] as $item) {
            $total += $item['price'] * $item['qty'];
        }

        $data['total'] = $total;

        return $data;
    }
}


Error I am getting

Object of class Illuminate\Routing\Redirector could not be converted to string

Thank you in advance

1 Answers

you may just go to the config/paypal.php and edit 'invoice_prefix' => env('PAYPAL_INVOICE_PREFIX', 'Life_saver_'), you may use _ underline in this like Life_saver_, dont forget use underline at the end too.

Related