Add support for setting a payment method as default

This commit is contained in:
David Bomba 2024-10-22 06:57:36 +11:00
parent dd5ba5a240
commit 00582aa8db
4 changed files with 107 additions and 0 deletions

View File

@ -419,4 +419,11 @@ class ClientGatewayTokenController extends BaseController
return $this->itemResponse($client_gateway_token->fresh());
}
public function setAsDefault(UpdateClientGatewayTokenRequest $request, ClientGatewayToken $client_gateway_token)
{
$client_gateway_token = $this->client_gateway_token_repo->setDefault($client_gateway_token);
return $this->itemResponse($client_gateway_token->fresh());
}
}

View File

@ -25,4 +25,17 @@ class ClientGatewayTokenRepository extends BaseRepository
return $client_gateway_token->fresh();
}
public function setDefault(ClientGatewayToken $client_gateway_token): ClientGatewayToken
{
ClientGatewayToken::withTrashed()
->where('company_id', $client_gateway_token->company_id)
->where('client_id', $client_gateway_token->client_id)
->update(['is_default' => false]);
$client_gateway_token->is_default = true;
$client_gateway_token->save();
return $client_gateway_token->fresh();
}
}

View File

@ -184,7 +184,9 @@ Route::group(['middleware' => ['throttle:api', 'api_db', 'token_auth', 'locale']
Route::post('filters/{entity}', [FilterController::class, 'index'])->name('filters');
Route::resource('client_gateway_tokens', ClientGatewayTokenController::class);
Route::post('client_gateway_tokens/{client_gateway_token}/setAsDefault', [ClientGatewayTokenController::class, 'setAsDefault'])->name('client_gateway_tokens.set_as_default');
Route::post('connected_account', [ConnectedAccountController::class, 'index']);
Route::post('connected_account/gmail', [ConnectedAccountController::class, 'handleGmailOauth']);

View File

@ -97,6 +97,91 @@ class ClientGatewayTokenApiTest extends TestCase
$this->cg->save();
}
public function testClientGatewaySetDefault()
{
$data = [
'client_id' => $this->client->hashed_id,
'company_gateway_id' => $this->cg->hashed_id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
'meta' => '{}',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/client_gateway_tokens', $data);
$response->assertStatus(200);
$arr = $response->json();
$t1 = $arr['data']['id'];
$this->assertNotNull($arr['data']['token']);
$data = [
'client_id' => $this->client->hashed_id,
'company_gateway_id' => $this->cg->hashed_id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
'is_default' => false,
'meta' => '{}',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/client_gateway_tokens', $data);
$response->assertStatus(200);
$arr = $response->json();
$t2 = $arr['data']['id'];
$this->assertNotNull($arr['data']['token']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson("/api/v1/client_gateway_tokens/{$t2}/setAsDefault", []);
$response->assertStatus(200);
$this->assertTrue($response->json()['data']['is_default']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson("/api/v1/client_gateway_tokens/{$t1}/setAsDefault", []);
$response->assertStatus(200);
$this->assertTrue($response->json()['data']['is_default']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->getJson("/api/v1/clients/{$this->client->hashed_id}", []);
$response->assertStatus(200);
$arr = $response->json();
$this->assertCount(2,$arr['data']['gateway_tokens']);
foreach($arr['data']['gateway_tokens'] as $token)
{
if($token['id'] == $t1){
$this->assertTrue($token['is_default']);
}
else {
$this->assertFalse($token['is_default']);
}
}
}
public function testClientGatewayPostPost()
{
$data = [