mirror of
https://github.com/anikeen-com/id.git
synced 2026-11-03 14:19:31 +00:00
Ship orders: shipments with delivery notes
$order->shipments()->create([...]) records a delivery of a paid order's goods on Anikeen ID, which adds its delivery note to the user's order documents and sends order.shipped. $shipment->deliveryNote() downloads the PDF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
60b4b80f17
commit
5349ee81f6
5 files changed
+219
No files matched your search
@@ -101,6 +101,19 @@ class Order extends BaseResource
|
||||
->request('DELETE', sprintf('v1/orders/%s', $this->id))->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deliveries of this order's goods; `create()` ships them (see Shipments::create()).
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function shipments(): Shipments
|
||||
{
|
||||
return Shipments::builder(fn() => $this->billable->anikeenId()
|
||||
->request('GET', sprintf('v1/orders/%s/shipments', $this->id)))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get order items from given order.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Concerns\HasParent;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
* @property string $number delivery note number, e.g. LS-2026-0001
|
||||
* @property string $order_id
|
||||
* @property null|string $carrier
|
||||
* @property null|string $tracking_number
|
||||
* @property null|string $tracking_url
|
||||
* @property array $items order_item_id, name, description, units
|
||||
* @property object $shipping_address
|
||||
* @property null|string $note
|
||||
* @property string $shipped_at
|
||||
*/
|
||||
class Shipment extends BaseResource
|
||||
{
|
||||
use HasBillable;
|
||||
use HasParent;
|
||||
|
||||
/**
|
||||
* The delivery note as PDF (binary string).
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function deliveryNote(): string
|
||||
{
|
||||
$result = $this->billable->anikeenId()
|
||||
->request('GET', sprintf('v1/orders/%s/shipments/%s/delivery-note', $this->order_id, $this->id));
|
||||
|
||||
return (string) $result->response?->getBody();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Anikeen\Id\Resources;
|
||||
|
||||
use Anikeen\Id\Concerns\HasBillable;
|
||||
use Anikeen\Id\Concerns\HasParent;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Deliveries of an order's physical goods. Each one adds a delivery note to the customer's
|
||||
* order documents on Anikeen ID.
|
||||
*/
|
||||
class Shipments extends BaseCollection
|
||||
{
|
||||
use HasBillable;
|
||||
use HasParent;
|
||||
|
||||
/**
|
||||
* Ship goods of a paid order. Only the app that created the order (or a trusted app) can.
|
||||
*
|
||||
* Without `items`, everything physical that hasn't been shipped yet is shipped. Items and
|
||||
* the shipping address are stored as they are now; the user sees them on the delivery note.
|
||||
*
|
||||
* @param array{
|
||||
* carrier?: string,
|
||||
* tracking_number?: string,
|
||||
* tracking_url?: string,
|
||||
* note?: string,
|
||||
* shipped_at?: string,
|
||||
* items?: array<array{order_item_id: string, units?: float|int|string}>
|
||||
* } $attributes
|
||||
* - carrier: e.g. "DHL"
|
||||
* - tracking_url: http(s) link to track the parcel, shown to the user
|
||||
* - shipped_at: when it was sent (default: now, not in the future)
|
||||
* - items: physical items of the order and how many of them (default: all left)
|
||||
*
|
||||
* Errors (ResourceException with the HTTP status): 409 when the order isn't paid or nothing
|
||||
* is left to ship, 422 for items that aren't physical items of the order or exceed what's left.
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function create(array $attributes = []): Shipment
|
||||
{
|
||||
return (new Shipment(fn() => $this->billable->anikeenId()
|
||||
->request('POST', sprintf('v1/orders/%s/shipments', $this->parent->id), $attributes)))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this->parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function find(string $id): ?Shipment
|
||||
{
|
||||
return (new Shipment(fn() => $this->billable->anikeenId()
|
||||
->request('GET', sprintf('v1/orders/%s/shipments/%s', $this->parent->id, $id))))
|
||||
->setBillable($this->billable)
|
||||
->setParent($this->parent);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user