Merge pull request #3965 from turbo124/v2

Fees for fees and limits
This commit is contained in:
David Bomba 2020-08-05 21:49:16 +10:00 committed by GitHub
commit d605fd422e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 220 additions and 6 deletions

View File

@ -0,0 +1,41 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers\Shop;
use App\Http\Controllers\BaseController;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\Product;
use App\Transformers\ProductTransformer;
use App\Transformers\Shop\CompanyShopProfileTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
class ProfileController extends BaseController
{
use MakesHash;
protected $entity_type = Company::class;
protected $entity_transformer = CompanyShopProfileTransformer::class;
public function show(Request $request)
{
$company = Company::where('company_key', $request->header('X-API-COMPANY-KEY'))->first();
if(!$company->enable_shop_api)
return response()->json(['message' => 'Shop is disabled', 'errors' => []],403);
return $this->itemResponse($company);
}
}

View File

@ -475,8 +475,12 @@ class Client extends BaseModel implements HasLocalePreference
$valid_gateways = $gateways->filter(function ($method) use ($amount) {
if(isset($method->fees_and_limits))
$fees_and_limits = $method->fees_and_limits->{"1"};
if(isset($method->fees_and_limits)){
//sometimes the key value of the fees and limits object are not static,
//we have to harvest the key value as follows
$properties = array_keys(get_object_vars($method->fees_and_limits));
$fees_and_limits = $method->fees_and_limits->{$properties[0]};
}
else
return true;

View File

@ -0,0 +1,111 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Transformers\Shop;
use App\Models\Account;
use App\Models\Activity;
use App\Models\Client;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\CompanyLedger;
use App\Models\CompanyToken;
use App\Models\CompanyUser;
use App\Models\Credit;
use App\Models\Design;
use App\Models\Expense;
use App\Models\GroupSetting;
use App\Models\Payment;
use App\Models\PaymentTerm;
use App\Models\Product;
use App\Models\Project;
use App\Models\Quote;
use App\Models\Task;
use App\Models\TaxRate;
use App\Models\User;
use App\Models\Webhook;
use App\Transformers\CompanyLedgerTransformer;
use App\Transformers\CompanyTokenHashedTransformer;
use App\Transformers\CompanyTokenTransformer;
use App\Transformers\CreditTransformer;
use App\Transformers\EntityTransformer;
use App\Transformers\PaymentTermTransformer;
use App\Transformers\TaskTransformer;
use App\Transformers\WebhookTransformer;
use App\Utils\Traits\MakesHash;
/**
* Class CompanyShopProfileTransformer.
*/
class CompanyShopProfileTransformer extends EntityTransformer
{
use MakesHash;
/**
* @var array
*/
protected $defaultIncludes = [
];
/**
* @var array
*/
protected $availableIncludes = [
];
/**
* @param Company $company
*
* @return array
*/
public function transform(Company $company)
{
$std = new \stdClass;
return [
'company_key' => (string)$company->company_key ?: '',
'settings' => $this->trimCompany($company),
];
}
private function trimCompany($company)
{
$std = new \stdClass;
$trimmed_company_settings = [
'custom_fields' => $company->custom_fields ?: $std,
'custom_value1' => $company->settings->custom_value1,
'custom_value2' => $company->settings->custom_value2,
'custom_value3' => $company->settings->custom_value3,
'custom_value4' => $company->settings->custom_value4,
'name' => $company->settings->name,
'company_logo' => $company->settings->company_logo,
'website' => $company->settings->website,
'address1' => $company->settings->address1,
'address2' => $company->settings->address2,
'city' => $company->settings->city,
'state' => $company->settings->state,
'postal_code' => $company->settings->postal_code,
'phone' => $company->settings->phone,
'email' => $company->settings->email,
'country_id' => $company->settings->country_id,
'vat_number' => $company->settings->vat_number,
];
$new_settings = new \stdClass;
foreach($trimmed_company_settings as $key => $value)
$new_settings->{$key} = $value;
return $new_settings;
}
}

View File

@ -10,5 +10,5 @@ Route::group(['middleware' => ['company_key_db','locale'], 'prefix' => 'api/v1']
Route::get('shop/client/{contact_key}', 'Shop\ClientController@show');
Route::get('shop/invoice/{invitation_key}', 'Shop\InvoiceController@show');
Route::get('shop/product/{product_key}', 'Shop\ProductController@show');
Route::get('shop/profile', 'Shop\ProfileController@show');
});

View File

@ -63,7 +63,11 @@ class CompanyGatewayTest extends TestCase
$cg->save();
$this->assertNotNull($cg->fees_and_limits);
$this->assertNotNull($cg->fees_and_limits->{"1"});
$properties = array_keys(get_object_vars($cg->fees_and_limits));
$fees_and_limits = $cg->fees_and_limits->{$properties[0]};
$this->assertNotNull($fees_and_limits);
//confirm amount filtering works
@ -83,8 +87,10 @@ class CompanyGatewayTest extends TestCase
public function checkSieve($cg, $amount)
{
if(isset($cg->fees_and_limits))
$fees_and_limits = $cg->fees_and_limits->{"1"};
if(isset($cg->fees_and_limits)){
$properties = array_keys(get_object_vars($cg->fees_and_limits));
$fees_and_limits = $cg->fees_and_limits->{$properties[0]};
}
else
$passes = true;

View File

@ -0,0 +1,52 @@
<?php
namespace Tests\Unit\Shop;
use App\Factory\InvoiceFactory;
use App\Factory\InvoiceItemFactory;
use App\Helpers\Invoice\InvoiceSum;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers \App\Http\Controllers\Shop\ProfileController
*/
class ShopProfileTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
public function setUp() :void
{
parent::setUp();
$this->makeTestData();
}
public function testProfileDisplays()
{
$this->company->enable_shop_api = true;
$this->company->save();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-COMPANY-KEY' => $this->company->company_key
])->get('/api/v1/shop/profile');
$response->assertStatus(200);
$arr = $response->json();
$this->assertArrayHasKey('custom_value1', $arr['data']['settings']);
$this->assertEquals($this->company->company_key, $arr['data']['company_key']);
}
}