Created
April 29, 2021 18:20
-
-
Save ahosker/d73dffac46fa8ab944b855ef9b421ef2 to your computer and use it in GitHub Desktop.
Laravel verraest/revolut-php console implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Console\Commands; | |
| // This is an example Laravel Command | |
| // Just scaffolding for use of: | |
| // https://github.com/sverraest/revolut-php | |
| //include RevolutPHP with composer | |
| //Create this command. | |
| //Run it and follow instructions where prompted | |
| use Illuminate\Console\Command; | |
| use Illuminate\Support\Facades\Storage; | |
| use Carbon\Carbon; | |
| use RevolutPHP; | |
| class RevolutGet extends Command | |
| { | |
| protected $signature = 'banking:revolut'; | |
| protected $description = 'Display list of bank accounts in console.'; | |
| //Variables | |
| private $fileName = 'revToken.txt'; //Filename to store Tokens. | |
| private $clientId = ''; //Your Client ID from Revolut | |
| private $redirectUri = 'https://website.com/revolutOauthConsent'; //Your Auth URL to get Key (not implemented) | |
| private $privateKey = 'file:///home/forge/revolutKeys/privatekey.pem'; //Location of your generated Private Key | |
| private $isSandbox = true; | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| public function handle() | |
| { | |
| //Establish Client | |
| $RevolutClient = new \RevolutPHP\Client($this->getToken(), 'sandbox'); | |
| //Get All Accounts | |
| $accounts = $RevolutClient->accounts->all(); | |
| //Output Accounts | |
| dd($accounts); | |
| } | |
| private function getToken(){ | |
| // -- Create Auth Provider | |
| $authProvider = new \RevolutPHP\Auth\Provider(['clientId' => $this->clientId,'privateKey' => $this->privateKey,'redirectUri' => $this->redirectUri,'isSandbox' => $this->isSandbox]); | |
| //-- If no file generate NEW token | |
| if (Storage::disk('local')->missing($this->fileName)) { | |
| return $this->generateNewToken($authProvider); | |
| } | |
| //-- If TOKEN is older than 85 days, generate NEW token | |
| if(Carbon::now()->diffInSeconds(Carbon::createFromTimestamp(Storage::lastModified($this->fileName))) > (86400*85)){ | |
| return $this->generateNewToken($authProvider); | |
| } | |
| //-- Use an Old Token | |
| return $this->generateOldToken($authProvider, Storage::get($this->fileName)); | |
| } | |
| private function generateNewToken($authProvider){ | |
| //Generate re-authentication url | |
| $this->info('Go to:'."\n"."\n".$authProvider->getAuthorizationUrl()."\n"."\n"); | |
| sleep(2); | |
| $this->info('Copy the Auth Code from the URL.'); | |
| //Ask for Auth Code | |
| $authCode = $this->ask('What is the Auth Code?'); | |
| //Save new access token | |
| $accessToken = $authProvider->getAccessToken('authorization_code', ['code' => $authCode]); | |
| Storage::disk('local')->put($this->fileName, $accessToken->getRefreshToken()); | |
| return $accessToken; | |
| } | |
| private function generateOldToken($authProvider, $refreshToken){ | |
| return $authProvider->getAccessToken('refresh_token', ['refresh_token' => $refreshToken]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment