From 4a87cca794abecab284a6952b3bbfa7a429441a2 Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Fri, 20 Dec 2024 17:03:41 +0100 Subject: [PATCH] feat: throw relevant error when no pdfs are received INT-787 --- src/Helper/MyParcelCollection.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Helper/MyParcelCollection.php b/src/Helper/MyParcelCollection.php index 31c6eb4e..9f47c9a2 100644 --- a/src/Helper/MyParcelCollection.php +++ b/src/Helper/MyParcelCollection.php @@ -16,6 +16,7 @@ use BadMethodCallException; use Closure; +use GuzzleHttp\Exception\BadResponseException; use InvalidArgumentException; use MyParcelNL\Sdk\src\Adapter\ConsignmentAdapter; use MyParcelNL\Sdk\src\Concerns\HasUserAgent; @@ -467,7 +468,20 @@ public function setPdfOfLabels($positions = self::DEFAULT_A4_POSITION): self ) ->sendRequest('GET', MyParcelRequest::REQUEST_TYPE_RETRIEVE_LABEL); - $this->label_pdf = $request->getResult(); + /** + * When account needs to pay upfront, an array is returned with payment information, + * instead of the actual pdf’s. It will throw an unintelligible error when not handled here. + */ + $result = $request->getResult(); + + if (!is_string($result) || !preg_match('/^%PDF-1./', $result)) { + if (is_array($result) && isset($result['data']['payment_instructions'])) { + throw new ApiException('Received payment link instead of pdf. Check your MyParcel account status.'); + } + throw new ApiException('Did not receive expected pdf response. Please contact MyParcel.'); + } + + $this->label_pdf = $result; } $this->setLatestData();