mollie / mollie-api-php

Mollie API client for PHP
http://www.mollie.com
BSD 2-Clause "Simplified" License
552 stars 191 forks source link

How to refresh access token? #512

Closed EricKarijo closed 3 years ago

EricKarijo commented 3 years ago

Specifications

Describe the issue

I want to working example of how to refresh the accesstoken. This is my code to connect to Mollie:

// Connect to Mollie public function connect() { return Socialite::with('mollie') ->scopes(['profiles.read','profiles.write','payments.read','payments.write']) // Additional permission: profiles.read ->redirect(); }

My login callback is: public function login_callback() {

    if ( isset( $_GET['error'] )) {
        $error = $_GET['error'];
        $err_desc = $_GET['error_description'];

        if ( $error && $err_desc ) {                
            return redirect()->route('finances.owner')->with( 'error', $error . ': ' . $err_desc  );
        }
    }

    try {

        // get current mollie account
        $mollie_user = Socialite::with('mollie')->user();

        // current restorant owner
        $current_user = auth()->user();

        $code = $_GET['code'];      // auth code
        $state = $_GET['state'];    // state

        $token = $mollie_user->token;  // access token
        $refreshToken = $mollie_user->refreshToken; // not always provided
        $expiresIn = $mollie_user->expiresIn;

       ... storing above variables in db

    }
    catch {
    }

This is working fine.

After this when a customer want to make a payment I want to refresh the accesstoken.

This is how payment is created:

public function createPayment( Order $order) {

      // REFRESH ACCESSTOKEN IF NEEDED
       How to this part ??

       $token = $restorant->user->mollie_authToken;

        Mollie::api()->setAccessToken( $token);

        $ord_total = sprintf("%.2f", floatval($order->delivery_price + $order->order_price + $order->static_fee ));

        $applicationFee = [
        "amount"        => [
                "currency"  => "EUR",
                "value"     => (string)sprintf("%.2f", floatval($restorant->static_fee )), // You must send the correct number of decimals, thus we enforce the use of strings
        ],
        "description"    => "Platform fee bestelling #" . $order->id . " bij restaurant '" . $restorant->name . "'",
        ];

        $payment = Mollie::api()->payments->create([
            "amount"        => [
                "currency"  => "EUR",
                "value"     => $ord_total, // You must send the correct number of decimals, thus we enforce the use of strings
            ],
            "description"   => "Bestelling #" . $order->id . " bij restaurant '" . $restorant->name . "'",
            "redirectUrl"   => 'https://' . $host . '/mollie/webhooks/mollie/redirected/',  // route('mollie.webhooks.redirect'),  // route('orders.inde//x'),
            "webhookUrl"    => route('mollie.webhooks.normal'), // route('webhooks.mollie'),
            "metadata"      => [
                    "order_id"  => $order->id,
                    "profile"   => $restorant->user->mollie_profileId,
                ],
            // "applicationFee"    => $applicationFee,    
            "profileId"         => $restorant->user->mollie_profileId,
        ]);

}

Before creating the payment I want to refresh the accesstoken see // REFRESH ACCESSTOKEN IF NEEDED in the code

How to do that ?

sandervanhooft commented 3 years ago
  1. Make sure to store the refresh token on initial authentication
  2. Do something like this:
use Laravel\Socialite\Facades\Socialite;

$accessToken = Socialite::with('mollie')->getRefreshTokenResponse($refreshToken);