dimanche 29 juillet 2018

PayPal REST API using OmniPay gives "AUTHENTICATION_FAILURE" on Laravel

I'm trying to integrate PayPal REST API using OmniPay and OmniPay PayPal on Laravel 5.6.

My gateway() function:

public function gateway()
{
    // Create a gateway for the PayPal RestGateway
    $gateway = Omnipay::create('PayPal_Rest');

    // Initialise the gateway
    $gateway->initialize(array(
        'clientId' => config('services.paypal.clientid'),
        'secret' => config('services.paypal.secret'),
        'testMode' => config('services.paypal.testmode'), // Or false when you are ready for live transactions
    ));


    return $gateway;
}

Purchase function in the same model:

public function purchase(array $parameters)
{
    $response = $this->gateway()
        ->purchase($parameters)
        ->send();

    return $response;
}

Checkout function in controller:

public function checkout($order_id, Request $request)
{
    $order = Order::findOrFail(decrypt($order_id));

    $paypal = new PayPal;

    $response = $paypal->purchase([
        'amount' => $paypal->formatAmount($order->amount),
        'transactionId' => $order->id,
        'currency' => 'USD',
        'cancelUrl' => $paypal->getCancelUrl($order),
        'returnUrl' => $paypal->getReturnUrl($order),
    ]);

    if ($response->isRedirect()) {
        $response->redirect();
    }

    return redirect()->back()->with([
        'message' => $response->getMessage(),
    ]);
}

The clientId and Secret are set correctly (tried changing and reentering it multiple times and the testMode is set to true in .env file.

Each time I try to submit the request I get this error:

Authentication failed due to invalid authentication credentials or a missing Authorization header.

What could possibly be the problem here?

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire