billbeeio / billbee-php-sdk

🔌 The official Billbee API SDK for PHP 💻
MIT License
23 stars 25 forks source link

Creating new Article/Product result in a 500 Internal Server Error. #21

Closed TiBianMod closed 5 years ago

TiBianMod commented 5 years ago

Like the Title indicates, creating new Article/Product result in a 500 Internal Server Error.

// Let's keep the example very simple.
use BillbeeDe\BillbeeAPI\Client;

$user = Config::get('BILLBEE_USER');
$apiPassword = Config::get('BILLBEE_API_PASSWORD');
$apiKey = Config::get('BILLBEE_API_KEY');

$client = new Client($user, $apiPassword, $apiKey);

// Based on the server responses the minimum field requirement are the following fields.
$data = [
    'form_params' => [
        'Title' => 'Product Title',
        'VatIndex' => 1,
        'Price' => 5.29,
        'Vat1Rate' => 0,
        'Vat2Rate' => 0,
        'Type' => 1,
        // Another issue: will not accept boolean (This is the reason why i use string here.)
        'IsDigital' => 'false', 
        // Another issue: will not accept boolean (This is the reason why i use string here.)
        'IsCustomizable' => 'false',
    ]
];

$client->post('products', $data);

Here is the response from the server.

Fatal error: Uncaught GuzzleHttp\Exception\ServerException: Server error: `POST https://app.billbee.io/api/v1/products` resulted in a `500 Internal Server Error` response: {"ErrorMessage":"Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.","ErrorCode":8,"Data":null}

Is ok if i open another issues here?

devtronic commented 5 years ago

This endpoint is not implemented in the SDK at the moment. You can extend the client class and write your own method:

<?php

use BillbeeDe\BillbeeAPI\Client;
use BillbeeDe\BillbeeAPI\Model\Product;
use BillbeeDe\BillbeeAPI\Model\TranslatableText;
use BillbeeDe\BillbeeAPI\Response\BaseResponse;

$user = Config::get('BILLBEE_USER');
$apiPassword = Config::get('BILLBEE_API_PASSWORD');
$apiKey = Config::get('BILLBEE_API_KEY');

class MyClient extends Client
{
    public function createProduct(Product $product)
    {
        return $this->requestPOST(
            'products', // Products endpoint
            $this->jom->serialize($product), // Serialize the product to json
            BaseResponse::class // response class
        );
    }
}

$myClient = new MyClient($user, $apiPassword, $apiKey);

$myProduct = new Product();
$myProduct->title = [new TranslatableText('Product Title', 'DE')];
$myProduct->vatIndex = 1;
$myProduct->price = 5.29;
$myProduct->stockReduceItemsPerSale = 1.0;
$myProduct->isDigital = false;
$myProduct->isCustomizable = false;

// This fields have wrong values in the current version of the SDK so we need to change them
$myProduct->stocks = [];
$myProduct->weight = 0;
$myProduct->weightNet = 0;
$myProduct->unitsPerItem = 1.0;
$myProduct->soldSumGross = 0.0;
$myProduct->soldSumNet = 0.0;
$myProduct->soldSumNetLast30Days = 0.0;
$myProduct->soldSumGrossLast30Days = 0.0;
$myProduct->soldAmountLast30Days = 0.0;

var_dump($myClient->createProduct($myProduct));
devtronic commented 5 years ago

The createProduct call will be available in the next version of the SDK

TiBianMod commented 5 years ago

Very Nice news, Thanks :)