<?php
namespace Laravel\Ui\Tests\AuthBackend;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Logout;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Routing\Pipeline;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Testing\TestResponse;
use Illuminate\Validation\ValidationException;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\Test;
#[WithMigration]
class AuthenticatesUsersTest extends TestCase
{
use AuthenticatesUsers, RefreshDatabase;
protected function tearDown(): void
{
Auth::logout();
parent::tearDown();
}
#[Test]
public function it_can_authenticate_a_user()
{
Event::fake();
$user = UserFactory::new()->create();
$request = Request::create('/login', 'POST', [
'email' => $user->email,
'password' => 'password',
], [], [], [
'HTTP_ACCEPT' => 'application/json',
]);
$response = $this->handleRequestUsing($request, function ($request) {
return $this->login($request);
})->assertStatus(204);
Event::assertDispatched(function (Attempting $event) {
return $event->remember === false;
});
}
#[Test]
public function it_can_deauthenticate_a_user()
{
Event::fake();
$user = UserFactory::new()->create();
$this->actingAs($user);
$request = Request::create('/logout', 'POST', [], [], [], [
'HTTP_ACCEPT' => 'application/json',
]);
$response = $this->handleRequestUsing(
$request, fn ($request) => $this->logout($request)
)->assertStatus(204);
Event::assertDispatched(fn (Logout $event) => $user->is($event->user));
}
#[Test]
public function it_can_authenticate_a_user_with_remember_as_false()
{
Event::fake();
$user = UserFactory::new()->create();
$request = Request::create('/login', 'POST', [
'email' => $user->email,
'password' => 'password',
'remember' => false,
], [], [], [
'HTTP_ACCEPT' => 'application/json',
]);
$response = $this->handleRequestUsing($request, function ($request) {
return $this->login($request);
})->assertStatus(204);
Event::assertDispatched(function (Attempting $event) {
return $event->remember === false;
});
}
#[Test]
public function it_can_authenticate_a_user_with_remember_as_true()
{
Event::fake();
$user = UserFactory::new()->create();
$request = Request::create('/login', 'POST', [
'email' => $user->email,
'password' => 'password',
'remember' => true,
], [], [], [
'HTTP_ACCEPT' => 'application/json',
]);
$response = $this->handleRequestUsing($request, function ($request) {
return $this->login($request);
})->assertStatus(204);
Event::assertDispatched(function (Attempting $event) {
return $event->remember === true;
});
}
#[Test]
public function it_cant_authenticate_a_user_with_invalid_password()
{
$user = UserFactory::new()->create();
$request = Request::create('/login', 'POST', [
'email' => $user->email,
'password' => 'invalid-password',
], [], [], [
'HTTP_ACCEPT' => 'application/json',
]);
$response = $this->handleRequestUsing($request, function ($request) {
return $this->login($request);
})->assertUnprocessable();
$this->assertInstanceOf(ValidationException::class, $response->exception);
$this->assertSame([
'email' => [
'These credentials do not match our records.',
],
], $response->exception->errors());
}
#[Test]
public function it_cant_authenticate_unknown_credential()
{
$request = Request::create('/login', 'POST', [
'email' => 'taylor@laravel.com',
'password' => 'password',
], [], [], [
'HTTP_ACCEPT' => 'application/json',
]);
$response = $this->handleRequestUsing($request, function ($request) {
return $this->login($request);
})->assertUnprocessable();
$this->assertInstanceOf(ValidationException::class, $response->exception);
$this->assertSame([
'email' => [
'These credentials do not match our records.',
],
], $response->exception->errors());
}
/**
* Handle Request using the following pipeline.
*
* @param \Illuminate\Http\Request $request
* @param callable $callback
* @return \Illuminate\Testing\TestResponse
*/
protected function handleRequestUsing(Request $request, callable $callback)
{
return new TestResponse(
(new Pipeline($this->app))
->send($request)
->through([
\Illuminate\Session\Middleware\StartSession::class,
])
->then($callback)
);
}
}
Initiates a new payment transaction.
create-order
| Parameter | Type | Details |
|---|---|---|
| amount | decimal | Your Amount , Must be rounded at 2 precision. |
| currency | string | Currency Code, Must be in Upper Case (Alpha-3 code) |
| success_url | string | Enter your return or success URL |
| cancel_url | string (optional) | Enter your cancel or failed URL |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url.'create-order', [
'headers' => [
'Authorization' => 'Bearer '. $authorizationToken,
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'amount' => '$amount',
'currency' => 'currency',
'success_url' => 'success_url',
'cancel_url' => 'cancel_url',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Order created successfully."
]
},
"data": {
"redirect_url":"https://example.com/login/OISADFDFSDFSF",
"order_details":{
"amount" : "10",
"fixed_charge" : 2,
"percent_charge" : 1,
"total_charge" : 3,
"total_payable" : 13,
"currency" : "USD",
"expiry_time": "2024-04-25T06:48:35.984285Z",
"success_url": "http://127.0.0.1/nfcpay/user/transaction/success",
"cancel_url": "http://127.0.0.1/nfcpay/user/transaction/cancel"
}
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid token."
]
},
"data": null,
"type": "error"
}