PHPackages                             fintreen/laravel-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. fintreen/laravel-client

ActiveLibrary[API Development](/categories/api)

fintreen/laravel-client
=======================

A Laravel client for interacting with Fintreen.com API.

1.0.5(2y ago)017MITPHPPHP ^8.0

Since Dec 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/fintreen/laravel-client)[ Packagist](https://packagist.org/packages/fintreen/laravel-client)[ RSS](/packages/fintreen-laravel-client/feed)WikiDiscussions master Synced yesterday

READMEChangelog (5)Dependencies (2)Versions (7)Used By (0)

Laravel client for fintreen.com.
================================

[](#laravel-client-for-fintreencom)

Fintreen.com laravel client for crypto-payment gateway API

[![MIT License](https://camo.githubusercontent.com/ccf868bd300b5f35fefe83aa4c571a3f35a8ab009ebd8ff94282ad0e220c8676/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f74686e65696c647265772f426573742d524541444d452d54656d706c6174652e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/fintreen/laravel-client/blob/main/LICENCE.txt)

### Fintreen.com laravel client

[](#fintreencom-laravel-client)

 [**Explore the docs on apiary »**](https://fintreen.docs.apiary.io/)

 [Report Bug](https://github.com/fintreen/laravel-client/issues) · [Request Feature](https://github.com/fintreen/laravel-client/issues) .

 [&gt;&gt;&gt; PHP Client &lt;&lt;&lt;](https://github.com/fintreen/php-client)

Tested on php 8.0, 8.1, 8.2. Should be also good with php 8.3. Tested on laravel 9, 10.

Use composer2 for package installation.

Installation:
-------------

[](#installation)

#### 1. Add next env variables to your .env file

[](#1-add-next-env-variables-to-your-env-file)

```
#set true if you are using Laravel Backpack
FINTREEN_USE_BAKCPACK=false
FINTREEN_TEST_MODE=false
FINTREEN_TOKEN="yourfintreentoken"
FINTREEN_EMAIL="yourfintreenemail"
```

#### 2. Install the client

[](#2-install-the-client)

```
composer req fintreen/laravel-client

```

#### 3. Publish config

[](#3-publish-config)

```
php artisan vendor:publish --tag=config
```

#### 4. Migrate

[](#4-migrate)

```
php artisan migrate
```

### Laravel BackPack + Settings integration

[](#laravel-backpack--settings-integration)

If you are using [Laravel BackPack](https://github.com/Laravel-Backpack/CRUD) and [Settings](https://github.com/Laravel-Backpack/Settings) add-on you can use `FINTREEN_USE_BAKCPACK=true` at your .env. It will add `fintreen_token` and `fintreen_email` setting from the `FINTREEN_TOKEN` and `FINTREEN_EMAIL` to database, and default client initialization (without params) will use them. But if no setting were found (db records are empty) even if you are using FINTREEN\_USE\_BAKCPACK flag, it will fallback to configuration values from .env. Otherwise you can use own params on `initClient` method.

Usage:
------

[](#usage)

### 0. General usage

[](#0-general-usage)

You can use all methods from [php-client](https://github.com/fintreen/php-client/issues)with `getClient()` method. Example:

```
$fintreen = new FintreenModel();
$fintreen->initClient();
$this->data['fintreenCurrencies'] = $fintreen->getClient()->getCurrenciesList();
$this->data['statusesList'] = $fintreen->getClient()->getOrderStatusList();
$this->data['calculation'] = $fintreen->getClient()->calculate($amount, $cryptoCodes);

/*####....*/

$fintreen->getClient()->createTransaction($amount, $cryptoCode)

/* you can also use:
checkTransaction(), getTransactionsList() and sendRequest() methods, just like in the php client with getClient() method.
*/

/* Alternatively you can use createTransaction and getCurrenciesList without getClient*/

$fintreen->getCurrenciesList(); // This has cache wrapper with TTL of 10 minutes by default
// but you can use raw no cache method via client  $fintreen->getClient()->getCurrenciesList();

[$linkToRedirect, $createdTransaction] = $fintreen->createTransaction($amount, $cryptoCode);
```

### 1. Client initialization

[](#1-client-initialization)

As far as this is just a laravel wrapper, you can initialize the client with no params. This will load config from config/fintreen.php file which should be published (installation point 3) which takes env variables.

Bit still you can init client with own params.

```
$fintreen = new FintreenModel();
$fintreen->initClient($token, $email, $isTest);
```

### 2. Calculation

[](#2-calculation)

You can use existing route to calculate with `{{route('fintreen.calculate')}}` which goes to `/fintreen/calculate` by default. The POST params you need to set are `currency` (CryptoCode) and `amount` (Fiat in EUR).

Exaple of using this with jQuery

```
$request = $.ajax({
    url: "{{route('fintreen.calculate')}}",
    method: 'POST',
    data: {
        amount: amount,
        currency: currency,
    },
    beforeSend: function() {
        $('#submit-btn').addClass('hidden');
    },
    success: function(response) {
        // Handle success response
        if(response.status === 1) {
            $('#approx-to-pay').css('color', 'green').html(response.message);
            $('#submit-btn').removeClass('hidden');
        } else {
            $('#approx-to-pay').css('color', 'red').html(response.message);
            $('#submit-btn').addClass('hidden');
        }
    },
    error: function(error) {
        // Handle error response
        $('#request-response').css('color', 'red').text('Error calculation!');
    }
});
```

But for sure its better to use custom route. Take a look on `calculateAction` in `./vendor/fintreen/laravel-client/src/app/Http/Controllers/FintreenController.php`

```
use Fintreen\Laravel\app\Models\Fintreen\FintreenModel;

....

$fintreen = new FintreenModel();
$fintreen->initClient();
$calcData = $fintreen->getClient()->calculate($amount, $cryptoCodes);
```

Note that in custom variant you can set parameters for initClient and use comma-separated crypto codes calculate method.

### 3. Create transaction

[](#3-create-transaction)

```
[$linkToRedirect, $createdTransaction] = $fintreen->createTransaction($amount, $cryptoCode);
return redirect($linkToRedirect); // Redirect user to gateway link
```

Example with validation in real project:

```
use Fintreen\Laravel\app\Exceptions\FintreenApiException;
use Fintreen\Laravel\app\Models\Fintreen\FintreenModel;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request;

// .........

$fintreen = new FintreenModel();
$fintreen->initClient();
$this->data['fintreenCurrencies'] = $fintreen->getCurrenciesList();

if ($request->isMethod('post')) {
    $error = false;
    $token = $request->post('g-recaptcha-response');
    $cryptoCode = $request->post('crypto-code');
    $amount = (float)$request->post('amount');
    $captchaValidaion = $this->validateRecaptcha($token, 'submit');
    if (!$captchaValidaion) {
        $error = true;
        $this->data['validationErrors'][] = 'Google recaaptcha check failed';
    }
    if (!$amount) {
        $error = true;
        $this->data['validationErrors'][] = 'Minimum amount is ' . FintreenModel::MIN_EURO_AMOUNT_DEFAULT;
    }
    if (isset($this->data['fintreenCurrencies']['crypto'])) {
        $exists = false;
        foreach ($this->data['fintreenCurrencies']['crypto'] as $fintreenCryptoCurrency) {
            if ($fintreenCryptoCurrency['code'] == $cryptoCode) {
                if ( $fintreenCryptoCurrency['min_eur_api'] > $amount) {
                    $error = true;
                    $this->data['validationErrors'][] = 'Minimum amount is ' . $fintreenCryptoCurrency['min_eur_api'];
                }
                $exists = true;
                break;
            }

        }
        if (!$exists) {
            $error = true;
            $this->data['validationErrors'][] = 'Crypto code validation error';
        }
    } else {
        $error = true;
        $this->data['validationErrors'][] = 'Crypto code validation error';
    }

    if ($error === false) {
        try {
            [$linkToRedirect, $createdTransaction] = $fintreen->createTransaction($amount, $cryptoCode);
            return redirect($linkToRedirect);
        } catch (FintreenApiException $e) {
            Log::channel('fintreen_deposit')->debug($e->getMessage(), ['context' => $e]);
            $this->data['validationErrors'][] = 'Something went wrong';
        } catch (\Exception $e) {
            Log::channel('fintreen_deposit')->debug($e->getMessage(), ['context' => $e]);
            $this->data['validationErrors'][] = 'Something went wrong';
        }
    }
}
```

### 4. Transaction list and check

[](#4-transaction-list-and-check)

#### 4.1 Transactions list

[](#41-transactions-list)

List transactions are just like with php client.

The params to filter can be found here

```
use Fintreen\Laravel\app\Models\Fintreen\FintreenModel;

$fintreenModel = new FintreenModel();
$fintreenModel->initClient();

$filters = [];
$fintreenModel->getClient()->getTransactionsList($filters); // returns array

// Or use raw
$fintreenModel->getClient()->sendRequest('transactions', 'GET', $filters); // returns json string
```

#### 4.2 Transaction check

[](#42-transaction-check)

To check if transactions are paid you can use console command fintreen:transactions:check

```
fintreen:transactions:check {$localId?} {--limit=}
```

It run the `check` mehod on `FintreenModel`.

Also you can call this method from instance with callback.

```
use Fintreen\Laravel\app\Models\Fintreen\FintreenModel;

//***

$fintreenModel = new FintreenModel();

$modelItem = $fintreenModel->find(1); // Use local id here
$modelItem->initClient(); //
$modelItem->check();

// ! Or use callable

// Callback usage in the application
$modelItem->check(function ($successfullTransaction) {
    // Code to deposit amount to user balance
});
```

!! Note, that callback fires only if transaction is successful.

### 5. How to handle transaction paid event (deposit balance in your app if transaction is successful)

[](#5-how-to-handle-transaction-paid-event-deposit-balance-in-your-app-if-transaction-is-successful)

**This is most dangerous one. Please be careful and avoid double checking and depositing balances.**

The `check` method on `FintreenModel` can get callback. In case that transaction is paid it will:

1. update status in table
2. then dispatch event `FintreenTransactionIsSuccess`
3. if callback is set it will fire the callback.

There are 2 ways - use custom check with callable as described above and events subscribtion.

**Callback method:**

```
$fintreenModel->check(function ($successfullTransaction) use ($someData) {
    // Code to deposit amount to user balance
});
```

!! Note, that callback fires only if transaction is successful.

**Event method:**

When transaction is successful `FintreenTransactionIsSuccess` event is triggered.

```
use Fintreen\Laravel\app\Events\FintreenTransactionIsSuccess;

Event::listen(FintreenTransactionIsSuccess::class, function ($event) {
    // Handle the event, e.g., update user balance
});
```

To create it read

```
php artisan make:listener FintreenTransactionIsSuccessListener
```

Example of listiner

```
