Skip to content

Commit

Permalink
Add Pint and apply changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Perafan committed Jul 28, 2024
1 parent c5fd626 commit a25a72a
Show file tree
Hide file tree
Showing 33 changed files with 303 additions and 303 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/BinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public function show($bin)

if ($binData = $this->getBinData($bin)) {
$this->cacheBinData($cacheKey, $binData);

return response()->json($binData);
}

if ($data = $this->binService->getBinInfo($bin)) {
$binData = $this->createBin($bin, $data);
$this->cacheBinData($cacheKey, $binData);

return response()->json($binData);
}

Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/ProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ class ProviderController extends Controller
public function index()
{
$providers = Provider::all();

return response()->json($providers);
}

public function toggle($id)
{
$provider = Provider::find($id);

if (!$provider) {
if (! $provider) {
return response()->json(['message' => 'Provider not found'], 404);
}

$provider->enabled = !$provider->enabled;
$provider->enabled = ! $provider->enabled;
$provider->save();

return response()->json($provider);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Bin extends Model
use HasFactory;

protected $fillable = [
'bin', 'type', 'brand', 'bank', 'country', 'provider_id'
'bin', 'type', 'brand', 'bank', 'country', 'provider_id',
];

public function provider()
Expand Down
14 changes: 7 additions & 7 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace App\Providers;

use App\Services\Adapters\IinListAdapter;
use App\Services\Adapters\BinCheckAdapter;
use App\Services\Adapters\BinCodesAdapter;
use App\Services\Adapters\BinListAdapter;
use App\Services\Adapters\GreipAdapter;
use App\Services\Adapters\IinListAdapter;
use App\Services\BinCheckService;
use App\Services\BinCodesService;
use App\Services\BinListService;
use App\Services\BinServiceInterface;
use App\Services\GreipService;
use App\Services\MultiBinService;
use App\Services\IinListService;
use App\Services\MultiBinService;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -25,11 +25,11 @@ public function register(): void
{
$this->app->singleton(BinServiceInterface::class, function ($app) {
return new MultiBinService([
'bincodes' => new BinCodesService(new BinCodesAdapter()),
'bincheck' => new BinCheckService(new BinCheckAdapter()),
'binlist' => new BinListService(new BinListAdapter()),
'greip' => new GreipService(new GreipAdapter()),
'iinlist' => new IinListService(new IinListAdapter()),
'bincodes' => new BinCodesService(new BinCodesAdapter),
'bincheck' => new BinCheckService(new BinCheckAdapter),
'binlist' => new BinListService(new BinListAdapter),
'greip' => new GreipService(new GreipAdapter),
'iinlist' => new IinListService(new IinListAdapter),
]);
});
}
Expand Down
1 change: 0 additions & 1 deletion app/Services/Adapters/BinAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ public function getBank(array $response): ?string;

public function getCountry(array $response): ?string;
}

1 change: 0 additions & 1 deletion app/Services/Adapters/BinCheckAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class BinCheckAdapter implements BinAdapterInterface
{

public function getType(array $response): ?string
{
return $response['BIN']['type'] ?? null;
Expand Down
1 change: 0 additions & 1 deletion app/Services/Adapters/GreipAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class GreipAdapter implements BinAdapterInterface
{

public function getType(array $response): ?string
{
return $response['data']['info']['scheme']['type'] ?? null;
Expand Down
1 change: 0 additions & 1 deletion app/Services/Adapters/IinListAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ public function getCountry(array $response): ?string
return $response['_embedded']['cards'][0]['account']['country']['code'] ?? null;
}
}

3 changes: 2 additions & 1 deletion app/Services/BaseBinService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
abstract class BaseBinService implements BinServiceInterface
{
protected Client $client;

protected BinAdapterInterface $adapter;

public function __construct(BinAdapterInterface $adapter)
{
$this->client = new Client();
$this->client = new Client;
$this->adapter = $adapter;
}

Expand Down
1 change: 1 addition & 0 deletions app/Services/BinCheckService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function getBinInfo(string $bin)
],
]
);

return json_decode($response->getBody()->getContents(), true);
}

Expand Down
1 change: 1 addition & 0 deletions app/Services/BinCodesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function getBinInfo(string $bin)
$response = $this->client->get(
"https://api.bincodes.com/bin/?format=json&api_key={$this->api_key()}&bin={$bin}"
);

return json_decode($response->getBody()->getContents(), true);
}

Expand Down
1 change: 1 addition & 0 deletions app/Services/BinListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BinListService extends BaseBinService
public function getBinInfo(string $bin)
{
$response = $this->client->get("https://lookup.binlist.net/{$bin}");

return json_decode($response->getBody()->getContents(), true);
}
}
6 changes: 3 additions & 3 deletions app/Services/GreipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public function getBinInfo(string $bin)
"https://greipapi.com/BINLookup?bin={$bin}",
[
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key()
]
'Authorization' => 'Bearer '.$this->api_key(),
],
]
);

Expand All @@ -20,6 +20,6 @@ public function getBinInfo(string $bin)

private function api_key()
{
return config ('services.greip.key');
return config('services.greip.key');
}
}
5 changes: 3 additions & 2 deletions app/Services/IinListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public function getBinInfo(string $bin)
"https://api.iinlist.com/cards?bin={$bin}",
[
'headers' => [
'X-API-Key' => $this->api_key()
]
'X-API-Key' => $this->api_key(),
],
]
);

return json_decode($response->getBody()->getContents(), true);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Services/MultiBinService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getBinInfo(string $bin): ?array
$service = $this->services[$provider->name];
try {
$data = $service->getBinInfo($bin);
if (!empty($data)) {
if (! empty($data)) {
return array_merge(
$service->response($data),
['provider_id' => $provider->id]
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"require-dev": {
"fakerphp/faker": "^1.23",
"laravel/pint": "^1.13",
"laravel/pint": "^1.17",
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion database/factories/BinFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function definition(): array
'country' => fake()->countryCode,
'provider_id' => function () {
return Provider::factory()->create()->id;
}
},
];
}
}
2 changes: 1 addition & 1 deletion database/seeders/BinSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Database\Seeders;

use App\Models\Provider;
use App\Models\Bin;
use App\Models\Provider;
use Illuminate\Database\Seeder;

class BinSeeder extends Seeder
Expand Down
1 change: 0 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Database\Seeders;

use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

Expand Down
3 changes: 3 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "laravel"
}
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BinController;
use App\Http\Controllers\ProviderController;
use Illuminate\Support\Facades\Route;

Route::get('/bin/{bin}', [BinController::class, 'show']);
Route::get('/providers', [ProviderController::class, 'index']);
Expand Down
3 changes: 1 addition & 2 deletions tests/Feature/Controllers/BinControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

uses(RefreshDatabase::class);


it('fetches bin data from database if not cached and caches it', function () {
$bin = Bin::factory()->create(['bin' => '123456', 'type' => 'debit']);

Expand Down Expand Up @@ -55,7 +54,7 @@
});

it('returns not found if bin data is unavailable', function () {
$response = $this->getJson("/api/bin/999999");
$response = $this->getJson('/api/bin/999999');

$response->assertNotFound()
->assertJson(['message' => 'BIN not found']);
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/Controllers/ProviderControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@
$response->assertStatus(200)
->assertJson(['enabled' => false]);


expect($provider->fresh()->enabled)->toBeFalse();
});
2 changes: 1 addition & 1 deletion tests/Feature/Services/BinCheckServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$client->shouldReceive('post')
->andReturn(new Response(200, [], json_encode($response)));

$service = new BinCheckService(new BinCheckAdapter());
$service = new BinCheckService(new BinCheckAdapter);
$reflection = new ReflectionClass($service);
$property = $reflection->getProperty('client');
$property->setValue($service, $client);
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Services/BinCodesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$client->shouldReceive('get')
->andReturn(new Response(200, [], json_encode($response)));

$service = new BinCodesService(new BinCodesAdapter());
$service = new BinCodesService(new BinCodesAdapter);
$reflection = new ReflectionClass($service);
$property = $reflection->getProperty('client');
$property->setValue($service, $client);
Expand All @@ -29,6 +29,6 @@
->and($response['type'])->toBe($data['type'])
->and($response['brand'])->toBe($data['card'])
->and($response['bank'])->toBe($data['bank'])
->and($response['country'])->toBe($data['countrycode'] );
->and($response['country'])->toBe($data['countrycode']);

})->with('bin codes success responses');
4 changes: 2 additions & 2 deletions tests/Feature/Services/BinListServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$client->shouldReceive('get')
->andReturn(new Response(200, [], json_encode($response)));

$service = new BinListService(new BinListAdapter());
$service = new BinListService(new BinListAdapter);
$reflection = new ReflectionClass($service);
$property = $reflection->getProperty('client');
$property->setValue($service, $client);
Expand All @@ -29,6 +29,6 @@
->and($response['type'])->toBe($data['type'])
->and($response['brand'])->toBe($data['scheme'])
->and($response['bank'])->toBe($data['bank']['name'] ?? null)
->and($response['country'])->toBe($data['country']['alpha2'] );
->and($response['country'])->toBe($data['country']['alpha2']);

})->with('bin list success responses');
Loading

0 comments on commit a25a72a

Please sign in to comment.