Sendcloud bugfixes and improvements

This commit is contained in:
Andreas Palm
2022-10-29 21:38:28 +02:00
parent 1ddee4350c
commit 6a2a16c0a6
7 changed files with 383 additions and 214 deletions
@@ -29,7 +29,7 @@ class ParcelCreation extends ParcelBase
'customs_invoice_nr' => $this->CustomsInvoiceNr,
'customs_shipment_type' => $this->CustomsShipmentType,
'external_reference' => $this->ExternalReference,
'total_insured_value' => $this->TotalInsuredValue,
'total_insured_value' => $this->TotalInsuredValue ?? 0,
'parcel_items' => array_map(fn(ParcelItem $item)=>$item->toApiRequest(), $this->ParcelItems),
'is_return' => $this->IsReturn,
'length' => $this->Length,
+6 -11
View File
@@ -14,9 +14,8 @@ class ParcelItem
public string $Description;
public string $OriginCountry;
public float $Price;
public string $PriceCurrency;
public string $Sku;
public string $ProductId;
public ?string $Sku = null;
public ?string $ProductId = null;
public function toApiRequest(): array {
return [
@@ -24,13 +23,10 @@ class ParcelItem
'weight' => number_format($this->Weight / 1000, 3, '.', null),
'quantity' => $this->Quantity,
'description' => $this->Description,
'price' => [
'value' => $this->Price,
'currency' => $this->PriceCurrency,
],
'value' => $this->Price,
'origin_country' => $this->OriginCountry,
'sku' => $this->Sku,
'product_id' => $this->ProductId,
'sku' => $this->Sku ?? '',
'product_id' => $this->ProductId ?? '',
];
}
@@ -41,8 +37,7 @@ class ParcelItem
$obj->Weight = intval(floatval($data->weight)*1000);
$obj->Quantity = $data->quantity;
$obj->Description = $data->description;
$obj->Price = $data->price->value;
$obj->PriceCurrency = $data->price->currency;
$obj->Price = $data->value;
$obj->OriginCountry = $data->origin_country;
$obj->Sku = $data->sku;
$obj->ProductId = $data->product_id;
+8 -7
View File
@@ -63,19 +63,20 @@ class SendCloudApi
return array_map(fn($x) => ShippingProduct::fromApiResponse($x), $response ?? []);
}
/**
* @throws Exception
*/
public function CreateParcel(ParcelCreation $parcel): ParcelResponse|string|null
{
$uri = self::PROD_BASE_URI . '/parcels';
$response = $this->sendRequest($uri, null, true, [
'parcel' => $parcel->toApiRequest()
]);
if (isset($response->parcel))
return ParcelResponse::fromApiResponse($response->parcel);
if (isset($response->error))
return $response->error->message;
try {
if (isset($response->parcel))
return ParcelResponse::fromApiResponse($response->parcel);
if (isset($response->error))
return $response->error->message;
} catch (Exception $e) {
return $e->getMessage();
}
return null;
}