Merge pull request #131 from exciler/sendcloud_errors

Improve Sendcloud error handling, prevent multiple form submission
This commit is contained in:
OpenXE-ERP 2024-03-27 12:08:59 +01:00 committed by GitHub
commit 520fab99db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 50 additions and 4 deletions

View File

@ -8,6 +8,9 @@
namespace Xentral\Carrier\SendCloud\Data; namespace Xentral\Carrier\SendCloud\Data;
/**
* Documents object for a parcel
*/
class Document class Document
{ {
public const TYPE_LABEL = 'label'; 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; namespace Xentral\Carrier\SendCloud\Data;
/**
* Error returned during parcel creation
*/
class ParcelCreationError class ParcelCreationError
{ {
public int $Code; 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 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]); $response = $this->sendRequest($uri, null, true, ['parcel' => $parcel->toApiRequest()], [200,400]);
switch ($response['code']) { switch ($response['code']) {
case 200: case 200:
@ -91,8 +91,8 @@ class SendCloudApi
} }
break; break;
case 400: case 400:
if (isset($response->error)) if (isset($response['body']->error))
return $response->error->message; return $response['body']->error->message;
break; break;
} }
throw SendcloudApiException::fromResponse($response); throw SendcloudApiException::fromResponse($response);

View File

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

View File

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