GuillermoAzFz's avatar

league/omnipay paypal package integration / usage

Hi. I am trying to enable Paypal payments on Laravel 9 using league/omnipay omnipay/paypal package. However, I get the following error when calling the charge method:

"Omnipay\Common\Item::__construct(): Argument #1 ($parameters) must be of type ?array, string given, called in ...\vendor\omnipay\common\src\Common\ItemBag.php on line 72"

These are my __construct and charge methods:

public function __construct() {
    $this->gateway = Omnipay::create('PayPal_Rest');
    $this->gateway->setClientId(env('PAYPAL_CLIENT_ID'));
    $this->gateway->setSecret(env('PAYPAL_CLIENT_SECRET'));
    $this->gateway->setTestMode(true); // false in production server
}


public function charge(Request $request) {
    if($request->method() == 'POST') {
        try {
            $response = $this->gateway->purchase(array(
                'amount' => $request->input('amount'),
                'items' => array(
                    'name' => $request->input('title'),
                    'price' => $request->input('amount'),
                ),
                'currency' => env('PAYPAL_CURRENCY'),
                'returnUrl' => url('success'),
                'cancelUrl' => url('error'),
            ))->send();

            if ($response->isRedirect()) {
                $response->redirect();
            } 
            else {
                $response->getMessage();
            }
        } catch(Exception $e) {
            return $e->getMessage();
        }
    }
}

I followed this tutorial: https://youtu.be/M9rk1ZB94u4 but can't get it to work. Any idea on where is the array required as param? Would appreciate any help.

0 likes
3 replies
rodrigo.pedra's avatar
Level 56

As in the acompaning article of the video you shared:

https://artisansweb.net/paypal-payment-gateway-integration-in-laravel/

The items key should be an array of items, where each item is defined as an associative array.

Try this:

$response = $this->gateway->purchase(
    [
        'amount' => $request->input('amount'),
        'items' => [ // note a new pair of brackets wrapping this item
            ['name' => $request->input('title'), 'price' => $request->input('amount')],
        ],
        'currency' => env('PAYPAL_CURRENCY'),
        'returnUrl' => url('success'),
        'cancelUrl' => url('error'),
    ],
)->send();
1 like

Please or to participate in this conversation.