Невозможно создать вариант с SquareConnect - PullRequest
0 голосов
/ 16 октября 2018

Используя PHP SquareConnect Sdk, я пытаюсь создать очень простой вариант продукта, используя их API.

`

require('connect-php-sdk-master/autoload.php');
$access_token="SECRETACCESS TOKEN";
$location_id="LOCATION ID"; //only need the one
// Configure OAuth2 access token for authorization: oauth2
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);

$api_instance = new SquareConnect\Api\CatalogApi();
$object_id = "OBJECTIDTHATWORKS"; // string 
$include_related_objects = true; // 

//print out the objectid.  Works perfectly!
try {
   $result = $api_instance->retrieveCatalogObject($object_id,$include_related_objects);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CatalogApi->retrieveCatalogObject: ', $e->getMessage(), PHP_EOL;
}

//now create the variant and it will fail

    $var_api = new \SquareConnect\Api\V1ItemsApi();
    $variation = new \SquareConnect\Model\V1Variation(); // \SquareConnect\Model\V1Variation | An object containing the fields to POST for the request.  See the corresponding object definition for field details.
    $variation->setName("JERSUB");
    $variation->setSku("JERSUPPERSKU");



      try {
          $meresult = $var_api->createVariation($location_id, $object_id, $variation);
          print_r($meresult);
      } catch (Exception $e) {
          echo 'Exception when calling V1ItemsApi->createVariation: ', $e->getMessage(), PHP_EOL;
      }

`

Независимо от того, что я делаю, я всегда получаю400 Плохой запрос.Исключение при вызове V1ItemsApi-> createVariation: [HTTP / 1.1 400 Bad Request] {"type": "bad_request", "message": "BadRequest"}

Я пытался просто передать пустой объект варианта, напримердокументация, но она все еще не работает.Как мне обойти или диагностировать ошибку?

1 Ответ

0 голосов
/ 17 октября 2018

Я использовал более старую версию API V1.Я нашел ОО способ сделать это, который был на самом деле умнее.Ниже приведен фрагмент, который, надеюсь, должен кому-то помочь.

require('connect-php-sdk-master/autoload.php');
$access_token="SECRETACCESS TOKEN";
$location_id="LOCATION ID"; //only need the one
// Configure OAuth2 access token for authorization: oauth2
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);

$api_instance = new SquareConnect\Api\CatalogApi();
$object_id = "OBJECTIDTHATWORKS"; // string 
$include_related_objects = true; // 

try {
   $result = $api_instance->retrieveCatalogObject($object_id,$include_related_objects);
   print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling CatalogApi->retrieveCatalogObject: ', $e->getMessage(), PHP_EOL;
}
$clone=$results[0];
$clone->setId("#TEMP123"); //change it to a temp id
$clone->setType("ITEM_VARIATION");
$id=$clone->getId();
$var_data=$clone->getItemVariationData();
//now clear/update the cloned object
$var_data->setName(UPC_FIELD_NAME);
$var_data->setSku("SUPPERSKU_TEST2"); //update sku
$var_data->setPricingType("VARIABLE_PRICING");
$var_data->setTrackInventory(null);
$var_data->setLocationOverrides(null); //got to remove location ovverides or it will track.
$var_data->setPriceMoney(null);
$clone->setItemVariationData($var_data);
//upsert it
$upsert=new \SquareConnect\Model\UpsertCatalogObjectRequest();
//set unique key
$upsert->setIdempotencyKey(md5(time()));
$upsert->setObject($clone);
//fire the update
$update_result=$api_instance->upsertCatalogObject($upsert);
...