Improve Sendcloud error handling, prevent multiple form submission

This commit is contained in:
Andreas Palm 2024-03-27 00:14:39 +01:00
parent d284b8e254
commit 59c567aa00
8 changed files with 50 additions and 4 deletions

View File

@ -8,6 +8,9 @@
namespace Xentral\Carrier\SendCloud\Data;
/**
* Documents object for a parcel
*/
class Document
{
public const TYPE_LABEL = 'label';

View File

@ -0,0 +1,13 @@
<?php
namespace Xentral\Carrier\SendCloud\Data;
/**
* Label object for a parcel
*/
class Label
{
public string $labelPrinter;
/** @var string[] $normalPrinter */
public array $normalPrinter;
}

View File

@ -8,6 +8,9 @@
namespace Xentral\Carrier\SendCloud\Data;
/**
* Error returned during parcel creation
*/
class ParcelCreationError
{
public int $Code;

View File

@ -0,0 +1,12 @@
<?php
namespace Xentral\Carrier\SendCloud\Data;
/**
* Shipping method object for a parcel
*/
class Shipment
{
public int $id;
public int $name;
}

View File

@ -0,0 +1,12 @@
<?php
namespace Xentral\Carrier\SendCloud\Data;
/**
* Status object for a parcel
*/
class Status
{
public int $id;
public string $message;
}

View File

@ -79,7 +79,7 @@ class SendCloudApi
*/
public function CreateParcel(ParcelCreation $parcel): ParcelResponse|string|null
{
$uri = self::PROD_BASE_URI . '/parcels';
$uri = self::PROD_BASE_URI . '/parcels?errors=verbose-carrier';
$response = $this->sendRequest($uri, null, true, ['parcel' => $parcel->toApiRequest()], [200,400]);
switch ($response['code']) {
case 200:
@ -91,8 +91,8 @@ class SendCloudApi
}
break;
case 400:
if (isset($response->error))
return $response->error->message;
if (isset($response['body']->error))
return $response['body']->error->message;
break;
}
throw SendcloudApiException::fromResponse($response);

View File

@ -469,6 +469,7 @@ abstract class Versanddienstleister
CustomsInfo::CUSTOMS_TYPE_RETURN => 'Rücksendung'
];
$json['messages'] = [];
$json['submitting'] = false;
$json['form']['services'] = [
Product::SERVICE_PREMIUM => false
];

View File

@ -229,7 +229,7 @@ SPDX-License-Identifier: LicenseRef-EGPL-3.1
</table>
</div>
<div>
<input class="btnGreen" type="submit" value="{|Paketmarke drucken|}" name="drucken">&nbsp;
<input class="btnGreen" type="submit" value="{|Paketmarke drucken|}" name="drucken" :disabled="submitting">&nbsp;
<!--<input type="button" value="{|Andere Versandart auswählen|}" name="anders">&nbsp;-->
</div>
</div>
@ -261,12 +261,14 @@ SPDX-License-Identifier: LicenseRef-EGPL-3.1
methods: {
submit: function () {
let app = this;
app.submitting = true;
let xhr = new XMLHttpRequest();
xhr.open('POST', location.href, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
let json = JSON.parse(this.response);
app.messages = json.messages;
app.submitting = false;
}
xhr.send(JSON.stringify($.extend({submit:'print'}, this.form)));
},