Skip to content

Commit

Permalink
Add support for setting a payment method as default
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Oct 21, 2024
1 parent dd5ba5a commit 00582aa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/ClientGatewayTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,11 @@ public function destroy(DestroyClientGatewayTokenRequest $request, ClientGateway

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());
}
}
13 changes: 13 additions & 0 deletions app/Repositories/ClientGatewayTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ public function save(array $data, ClientGatewayToken $client_gateway_token): Cli

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();
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@

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']);
Expand Down
85 changes: 85 additions & 0 deletions tests/Feature/ClientGatewayTokenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,91 @@ protected function setUp(): void
$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 = [
Expand Down

0 comments on commit 00582aa

Please sign in to comment.