Seraching for a exact product

Hello,
I am trying to create a link that i can send data to from another site to create the company, client and invoice with products and return data from the request.

However, i am not finding a way to get a existing product. I tried where, and the api (it says no store).

Here is the code that i am playing with.


Route::get('/test', function (Request $request) {
 

    $account = Account::factory()->create();


    $settings = CompanySettings::defaults();

    $settings->name = isset($this->request['name']) ? $this->request['name'] : '';

    $company = new Company();
    $company->account_id = $account->id;
    $company->company_key = $this->createHash();
    $company->ip = request()->ip();
    $company->settings = $settings;
    $company->db = config('database.default');
    $company->enabled_modules = config('ninja.enabled_modules');
    $company->subdomain = isset($this->request['subdomain']) ? $this->request['subdomain'] : '';
    $company->custom_fields = new \stdClass;
    $company->default_password_timeout = 1800000;

    if(Ninja::isHosted())
        $company->subdomain = MultiDB::randomSubdomainGenerator();
    else
        $company->subdomain = '';

    $company->save();

    $ret['account_id'] = $company->account_id;

    $user = User::whereEmail($request['user_email'])->first();

    if (! $user) {
        $user = User::factory()->create([
                                            'account_id' => $account->id,
                                            'email' => $request['user_email'],
                                            'confirmation_code' => $this->createDbHash(config('database.default')),
                                        ]);
    }
    $ref['user_id'] = $user->id;

    $company_token = new CompanyToken;
    $company_token->user_id = $user->id;
    $company_token->company_id = $company->id;
    $company_token->account_id = $account->id;
    $company_token->name = 'test token';
    $company_token->token = Str::random(64);
    $company_token->is_system = true;

    $company_token->save();

    $user->companies()->attach($company->id, [
        'account_id' => $account->id,
        'is_owner' => 1,
        'is_admin' => 1,
        'is_locked' => 0,
        'notifications' => CompanySettings::notificationDefaults(),
        // 'permissions' => '',
        'settings' => null,
    ]);

    /***********************
    // FAILS HERE
     ***********************/
    $product = ???
    
    if (!$product) {
        $product = new Product();
        $product->custom_value1 = $request->desc   ;
        $product->cost = $request['price'];
        $product->price = $request['price'];
        $product->quantity = 1;
        $product->save();
    }

    $invoice = new Invoice();
    $invoice->status_id = Invoice::STATUS_DRAFT;
    $invoice->number = null;
    $invoice->discount = 0;
    $invoice->is_amount_discount = true;
    $invoice->po_number = '';
    $invoice->footer = '';
    $invoice->terms = '';
    $invoice->public_notes = '';
    $invoice->private_notes = '';
    $invoice->date = null;
    $invoice->due_date = Carbon::now();
    $invoice->partial_due_date = null;
    $invoice->is_deleted = false;
    $invoice->line_items = json_encode([]);
    $invoice->tax_name1 = '';
    $invoice->tax_rate1 = 0;
    $invoice->tax_name2 = '';
    $invoice->tax_rate2 = 0;
    $invoice->tax_name3 = '';
    $invoice->tax_rate3 = 0;
    $invoice->custom_value1 = '';
    $invoice->custom_value2 = '';
    $invoice->custom_value3 = '';
    $invoice->custom_value4 = '';
    $invoice->amount =  $product->price;
    $invoice->balance =  $product->price;
    $invoice->paid_to_date = 0;
    $invoice->partial = 0;
    $invoice->user_id = $user_id;
    $invoice->company_id = $company_id;
    $invoice->recurring_id = null;
    $invoice->save();

/*
 * todo
 * trigger invoice email, and get invoice client link
 * 
 * return company_id, client_id, Invoice_id, client link
 */

});

Hi,

How are you developing this? We can try to help if you’re using the API but it looks like you may be modifying the code directly.

I was just putting a test route in for testing, then was planning to move it to a module.

Its is possible with the api? every api i have tried just says store disabled.

i need to create company, create client, create invoice, create product, email invoice, and return the id so i can track it on the other site for if it is paid or not. I am looking at the events as a way to be notified the other site that the invoice has been paid.

v5 is an API first application, everything is supported in the API.

‘store disabled’ sounds like you may be using the storefront API, that’s a limited API. Are you creating API tokens on Settings > Account Management?

no i wasnt, it likely was the store and all the api points were using a store url.

do you have any code examples on how to use the api?

The docs are here:

https://app.swaggerhub.com/apis/invoiceninja/invoiceninja

1 Like