<?php
namespace Illuminate\Testing\Concerns;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\ParallelTesting;
use Illuminate\Support\Facades\Schema;
trait TestDatabases
{
/**
* Indicates if the test database schema is up to date.
*
* @var bool
*/
protected static $schemaIsUpToDate = false;
/**
* Boot a test database.
*
* @return void
*/
protected function bootTestDatabase()
{
ParallelTesting::setUpProcess(function () {
$this->whenNotUsingInMemoryDatabase(function ($database) {
if (ParallelTesting::option('recreate_databases')) {
Schema::dropDatabaseIfExists(
$this->testDatabase($database)
);
}
});
});
ParallelTesting::setUpTestCase(function ($testCase) {
$uses = array_flip(class_uses_recursive(get_class($testCase)));
$databaseTraits = [
Testing\DatabaseMigrations::class,
Testing\DatabaseTransactions::class,
Testing\DatabaseTruncation::class,
Testing\RefreshDatabase::class,
];
if (Arr::hasAny($uses, $databaseTraits) && ! ParallelTesting::option('without_databases')) {
$this->whenNotUsingInMemoryDatabase(function ($database) use ($uses) {
[$testDatabase, $created] = $this->ensureTestDatabaseExists($database);
$this->switchToDatabase($testDatabase);
if (isset($uses[Testing\DatabaseTransactions::class])) {
$this->ensureSchemaIsUpToDate();
}
if ($created) {
ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
}
});
}
});
ParallelTesting::tearDownProcess(function () {
$this->whenNotUsingInMemoryDatabase(function ($database) {
if (ParallelTesting::option('drop_databases')) {
Schema::dropDatabaseIfExists(
$this->testDatabase($database)
);
}
});
});
}
/**
* Ensure a test database exists and returns its name.
*
* @param string $database
* @return array
*/
protected function ensureTestDatabaseExists($database)
{
$testDatabase = $this->testDatabase($database);
try {
$this->usingDatabase($testDatabase, function () {
Schema::hasTable('dummy');
});
} catch (QueryException $e) {
$this->usingDatabase($database, function () use ($testDatabase) {
Schema::dropDatabaseIfExists($testDatabase);
Schema::createDatabase($testDatabase);
});
return [$testDatabase, true];
}
return [$testDatabase, false];
}
/**
* Ensure the current database test schema is up to date.
*
* @return void
*/
protected function ensureSchemaIsUpToDate()
{
if (! static::$schemaIsUpToDate) {
Artisan::call('migrate');
static::$schemaIsUpToDate = true;
}
}
/**
* Runs the given callable using the given database.
*
* @param string $database
* @param callable $callable
* @return void
*/
protected function usingDatabase($database, $callable)
{
$original = DB::getConfig('database');
try {
$this->switchToDatabase($database);
$callable();
} finally {
$this->switchToDatabase($original);
}
}
/**
* Apply the given callback when tests are not using in memory database.
*
* @param callable $callback
* @return void
*/
protected function whenNotUsingInMemoryDatabase($callback)
{
$database = DB::getConfig('database');
if ($database !== ':memory:') {
$callback($database);
}
}
/**
* Switch to the given database.
*
* @param string $database
* @return void
*/
protected function switchToDatabase($database)
{
DB::purge();
$default = config('database.default');
$url = config("database.connections.{$default}.url");
if ($url) {
config()->set(
"database.connections.{$default}.url",
preg_replace('/^(.*)(\/[\w-]*)(\??.*)$/', "$1/{$database}$3", $url),
);
} else {
config()->set(
"database.connections.{$default}.database",
$database,
);
}
}
/**
* Returns the test database name.
*
* @return string
*/
protected function testDatabase($database)
{
$token = ParallelTesting::token();
return "{$database}_test_{$token}";
}
}
Get access token to initiates payment transaction.
generate-token
| Parameter | Type | Comments |
|---|---|---|
| client_id | string | Enter merchant API client/primary key |
| secret_id | string | Enter merchant API secret key |
| env | string | Enter merchant API environment |
| merchant_id | string | Enter merchant API merchant id |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url. 'v1/generate-token', [
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'client_id' => '$client_id',
'secret_id' => 'secret_id',
'env' => 'env',
'merchant_id' => 'merchant_id',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Successfully token is generated"
]
},
"data": {
"token":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9PSIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtnVkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2xoU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtcG9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRiQ0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3JzbnIyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYzkzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid credentials."
]
},
"data": null,
"type": "error"
}