diff --git a/app/Factory/ClientGatewayTokenFactory.php b/app/Factory/ClientGatewayTokenFactory.php index 2aa382e7ceb1..ed8f09049b1c 100644 --- a/app/Factory/ClientGatewayTokenFactory.php +++ b/app/Factory/ClientGatewayTokenFactory.php @@ -11,14 +11,14 @@ namespace App\Factory; +use App\Models\ClientGatewayToken; use Illuminate\Support\Str; class ClientGatewayTokenFactory { - public static function create(int $company_id, int $user_id) :ClientGatewayToken + public static function create(int $company_id) :ClientGatewayToken { $client_gateway_token = new ClientGatewayToken; - $client_gateway_token->user_id = $user_id; $client_gateway_token->company_id = $company_id; $client_gateway_token->is_default = false; $client_gateway_token->meta = ''; diff --git a/app/Http/Controllers/ClientGatewayTokenController.php b/app/Http/Controllers/ClientGatewayTokenController.php index 2e9cb5eb0766..a83988c8995e 100644 --- a/app/Http/Controllers/ClientGatewayTokenController.php +++ b/app/Http/Controllers/ClientGatewayTokenController.php @@ -320,7 +320,9 @@ class ClientGatewayTokenController extends BaseController */ public function create(CreateClientGatewayTokenRequest $request) { - $client_gateway_token = ClientGatewayTokenFactory::create(auth()->user()->company()->id, auth()->user()->id); + $client_gateway_token = ClientGatewayTokenFactory::create(auth()->user()->company()->id); + + $client_gateway_token = $this->client_gateway_token_repo->save($request->all(), $client_gateway_token); return $this->itemResponse($client_gateway_token); } @@ -366,7 +368,9 @@ class ClientGatewayTokenController extends BaseController */ public function store(StoreClientGatewayTokenRequest $request) { + $client_gateway_token = ClientGatewayTokenFactory::create(auth()->user()->company()->id); + $client_gateway_token = $this->client_gateway_token_repo->save($request->all(), $client_gateway_token); return $this->itemResponse($client_gateway_token); } diff --git a/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php b/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php index 26efee9291f8..ca5340d69e66 100644 --- a/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php +++ b/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php @@ -32,7 +32,7 @@ class StoreClientGatewayTokenRequest extends Request */ public function authorize() : bool { - return auth()->user()->isAdmin() + return auth()->user()->isAdmin(); } public function rules() @@ -40,6 +40,8 @@ class StoreClientGatewayTokenRequest extends Request $rules = [ 'client_id' => 'required', 'company_gateway_id' => 'required', + 'gateway_type_id' => 'required|integer', + 'meta' => 'required', ]; @@ -53,7 +55,6 @@ class StoreClientGatewayTokenRequest extends Request $input = $this->decodePrimaryKeys($input); - $this->replace($input); } diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php index 5e59e5327659..c4666534a313 100644 --- a/app/Http/Requests/Request.php +++ b/app/Http/Requests/Request.php @@ -106,6 +106,10 @@ class Request extends FormRequest $input['project_id'] = $this->decodePrimaryKey($input['project_id']); } + if (array_key_exists('company_gateway_id', $input) && is_string($input['company_gateway_id'])) { + $input['company_gateway_id'] = $this->decodePrimaryKey($input['company_gateway_id']); + } + if (isset($input['client_contacts'])) { foreach ($input['client_contacts'] as $key => $contact) { if (! array_key_exists('send_email', $contact) || ! array_key_exists('id', $contact)) { diff --git a/app/Models/ClientGatewayToken.php b/app/Models/ClientGatewayToken.php index 9f4c9b227ade..b1c6e1953454 100644 --- a/app/Models/ClientGatewayToken.php +++ b/app/Models/ClientGatewayToken.php @@ -29,6 +29,14 @@ class ClientGatewayToken extends BaseModel 'hashed_id', ]; + protected $fillable = [ + 'token', + 'routing_number', + 'gateway_customer_reference', + 'gateway_type_id', + 'meta', + ]; + public function getEntityType() { return self::class; @@ -66,9 +74,9 @@ class ClientGatewayToken extends BaseModel * @param null $field * @return Model|null */ - // public function resolveRouteBinding($value, $field = null) - // { - // return $this - // ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); - // } + public function resolveRouteBinding($value, $field = null) + { + return $this + ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); + } } diff --git a/app/Repositories/ClientGatewayTokenRepository.php b/app/Repositories/ClientGatewayTokenRepository.php index 6da85b82947b..311096a62a21 100644 --- a/app/Repositories/ClientGatewayTokenRepository.php +++ b/app/Repositories/ClientGatewayTokenRepository.php @@ -11,9 +11,21 @@ namespace App\Repositories; +use App\Models\ClientGatewayToken; + /** * Class for ClientGatewayTokenRepository . */ class ClientGatewayTokenRepository extends BaseRepository { + + public function save(array $data, ClientGatewayToken $client_gateway_token) :ClientGatewayToken + { + + $client_gateway_token->fill($data); + $client_gateway_token->save(); + + return $client_gateway_token->fresh(); + } + } diff --git a/routes/api.php b/routes/api.php index d34fa8199cd6..649e59ed8529 100644 --- a/routes/api.php +++ b/routes/api.php @@ -37,6 +37,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::put('clients/{client}/upload', 'ClientController@upload')->name('clients.upload'); Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk'); + Route::resource('client_gateway_tokens', 'ClientGatewayTokenController'); + Route::post('connected_account', 'ConnectedAccountController@index'); Route::post('connected_account/gmail', 'ConnectedAccountController@handleGmailOauth'); diff --git a/tests/Feature/ClientGatewayTokenApiTest.php b/tests/Feature/ClientGatewayTokenApiTest.php new file mode 100644 index 000000000000..4751b9c8f4bb --- /dev/null +++ b/tests/Feature/ClientGatewayTokenApiTest.php @@ -0,0 +1,181 @@ +withoutMiddleware( + ThrottleRequests::class + ); + + if (! config('ninja.testvars.stripe')) { + $this->markTestSkipped('Skip test no company gateways installed'); + } + + $this->faker = \Faker\Factory::create(); + + Model::reguard(); + + $this->makeTestData(); + + $this->withoutExceptionHandling(); + + CompanyGateway::whereNotNull('id')->delete(); + + $data = []; + $data[1]['min_limit'] = -1; + $data[1]['max_limit'] = -1; + $data[1]['fee_amount'] = 0.00; + $data[1]['fee_percent'] = 2; + $data[1]['fee_tax_name1'] = 'GST'; + $data[1]['fee_tax_rate1'] = 10; + $data[1]['fee_tax_name2'] = 'GST'; + $data[1]['fee_tax_rate2'] = 10; + $data[1]['fee_tax_name3'] = 'GST'; + $data[1]['fee_tax_rate3'] = 10; + $data[1]['adjust_fee_percent'] = true; + $data[1]['fee_cap'] = 0; + $data[1]['is_enabled'] = true; + + $data[2]['min_limit'] = -1; + $data[2]['max_limit'] = -1; + $data[2]['fee_amount'] = 0.00; + $data[2]['fee_percent'] = 1; + $data[2]['fee_tax_name1'] = 'GST'; + $data[2]['fee_tax_rate1'] = 10; + $data[2]['fee_tax_name2'] = 'GST'; + $data[2]['fee_tax_rate2'] = 10; + $data[2]['fee_tax_name3'] = 'GST'; + $data[2]['fee_tax_rate3'] = 10; + $data[2]['adjust_fee_percent'] = true; + $data[2]['fee_cap'] = 0; + $data[2]['is_enabled'] = true; + + //disable ach here + $json_config = json_decode(config('ninja.testvars.stripe')); + + $this->cg = new CompanyGateway; + $this->cg->company_id = $this->company->id; + $this->cg->user_id = $this->user->id; + $this->cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $this->cg->require_cvv = true; + $this->cg->require_billing_address = true; + $this->cg->require_shipping_address = true; + $this->cg->update_details = true; + $this->cg->config = encrypt(json_encode($json_config)); + $this->cg->fees_and_limits = $data; + $this->cg->save(); + } + + public function testClientGatewayPostPost() + { + $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, + ])->post('/api/v1/client_gateway_tokens', $data); + + $response->assertStatus(200); + $arr = $response->json(); + + $this->assertNotNull($arr['data']['token']); + + } + + public function testClientPut() + { + + + $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, + ])->post('/api/v1/client_gateway_tokens', $data); + + $arr = $response->json(); + + $response->assertStatus(200); + + $data = [ + 'token' => 'a_testy_token', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/client_gateway_tokens/'.$arr['data']['id'], $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals('a_testy_token', $arr['data']['token']); + } + + public function testClientGet() + { + + $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, + ])->post('/api/v1/client_gateway_tokens', $data); + + $arr = $response->json(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/client_gateway_tokens/'.$arr['data']['id']); + + $response->assertStatus(200); + } + +}