Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
Add UpdateOrderRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 29, 2020
1 parent 77d8651 commit 76b27e7
Show file tree
Hide file tree
Showing 26 changed files with 427 additions and 105 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 21 additions & 16 deletions examples/110_CreateAndSubmitOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
$request->from->geoId = $_SERVER['YANDEX_SENDER_GEOID'];
$request->to->location = 'Новосибирск, ул. Державина, 5';

$request->dimensions->length = $length = 10;
$request->dimensions->length = 10;
$request->dimensions->width = 20;
$request->dimensions->height = 30;
$request->dimensions->weight = 1.25;
Expand All @@ -100,23 +100,12 @@
$logger->addFile('delivery-options-request.json');
$logger->addFile('delivery-options-response.json');

$response = $client->sendDeliveryOptionsRequest($request);
$deliveryMethods = $client->sendDeliveryOptionsRequest($request);

if (!\count($response)) {
if (!\count($deliveryMethods)) {
return;
}

/**
* @var \Traversable<\YDeliverySDK\Responses\Types\DeliveryOption> $response
*/
$deliveryMethods = \iterator_to_array($response, false);

/*
\usort($deliveryMethods, function (\YDeliverySDK\Responses\Types\DeliveryOption $a, \YDeliverySDK\Responses\Types\DeliveryOption $b) {
return $a->cost->deliveryForSender <=> $b->cost->deliveryForSender;
});
*/

/** @var \YDeliverySDK\Responses\Types\DeliveryOption $deliveryMethod */
foreach ($deliveryMethods as $deliveryMethod) {
echo \join("\t", [
Expand All @@ -127,13 +116,29 @@
$deliveryMethod->delivery->calculatedDeliveryDateMin->format('Y-m-d'),
$deliveryMethod->delivery->calculatedDeliveryDateMax->format('Y-m-d'),
]), "\n";
break;
}

$deliveryMethod = $deliveryMethods->getFirstTagged($deliveryMethods::TAG_CHEAPEST);

if (!$deliveryMethod) {
return;
}

echo "\nCheapest delivery method:\n";

echo \join("\t", [
$deliveryMethod->delivery->type,
$deliveryMethod->tariffId,
$deliveryMethod->tariffName ?? 'Без названия',
$deliveryMethod->cost->deliveryForSender,
$deliveryMethod->delivery->calculatedDeliveryDateMin->format('Y-m-d'),
$deliveryMethod->delivery->calculatedDeliveryDateMax->format('Y-m-d'),
]), "\n";

/**
* Отправим заказ.
*/
$requestBuilder = new CreateOrderRequest\Builder($deliveryMethod, $location);
$requestBuilder = CreateOrderRequest::builder($deliveryMethod, $location);
$requestBuilder->setPostalCode($postalCode);
$request = $requestBuilder->build();

Expand Down
File renamed without changes.
220 changes: 220 additions & 0 deletions examples/130_UpdateOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<?php
/**
* This code is licensed under the MIT License.
*
* Copyright (c) 2018-2020 Alexey Kopytko <alexey@kopytko.com> and contributors
* Copyright (c) 2018 Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

declare(strict_types=1);

use Tests\YDeliverySDK\Integration\DebuggingLogger;
use YDeliverySDK\Requests\CreateOrderRequest;
use YDeliverySDK\Requests\DeliveryOptionsRequest;
use YDeliverySDK\Requests\UpdateOrderRequest;

include_once 'vendor/autoload.php';

$logger = new DebuggingLogger();

$builder = new \YDeliverySDK\ClientBuilder();
$builder->setToken($_SERVER['YANDEX_DELIVERY_TOKEN'] ?? '');
$builder->setLogger($logger);
/** @var \YDeliverySDK\Client $client */
$client = $builder->build();

/**
* Получим данные по адресу получателя.
*/
$response = $client->makeLocationRequest('Новосибирская область, Новосибирск');

\var_dump(\count($response));

foreach ($response as $location) {
echo "{$location->geoId}\t{$location->address}\n";

foreach ($location->addressComponents as $component) {
echo "- {$component->kind}: {$component->name}\n";
}
break;
}

/**
* Получим почтовый индекс.
*/
$response = $client->makePostalCodeRequest('Новосибирская область, Новосибирск, ул. Державина, 5');

\var_dump(\count($response));

foreach ($response as $postalCode) {
echo "$postalCode\n";

break;
}

/**
* Получим тарифы.
*/
$request = new DeliveryOptionsRequest();

$request->senderId = $_SERVER['YANDEX_SHOP_ID'];

$request->from->geoId = $_SERVER['YANDEX_SENDER_GEOID'];
$request->to->location = 'Новосибирск, ул. Державина, 5';

$request->dimensions->length = 10;
$request->dimensions->width = 20;
$request->dimensions->height = 30;
$request->dimensions->weight = 1.25;
$dimensions = $request->dimensions;

$request->deliveryType = $request::DELIVERY_TYPE_COURIER;

// $request->shipment->date = new DateTime('next Monday');
$request->shipment->type = $request->shipment::TYPE_WITHDRAW;

$request->cost->assessedValue = 1000;
$request->cost->itemsSum = 1000;
$request->cost->fullyPrepaid = true;

// $request->tariffId = 333333333;

$logger->addFile('delivery-options-request.json');
$logger->addFile('delivery-options-response.json');

$deliveryMethods = $client->sendDeliveryOptionsRequest($request);

if (!\count($deliveryMethods)) {
return;
}

$deliveryMethod = $deliveryMethods->getFirstTagged($deliveryMethods::TAG_CHEAPEST);

if (!$deliveryMethod) {
return;
}

/**
* Отправим заказ.
*/
$requestBuilder = CreateOrderRequest::builder($deliveryMethod, $location);
$requestBuilder->setPostalCode($postalCode);
$request = $requestBuilder->build();

$request->shipment->warehouseFrom = (int) $_SERVER['YANDEX_WAREHOUSE_ID'];
$request->senderId = (int) $_SERVER['YANDEX_SHOP_ID'];
$request->comment = 'Доставки не будет - тестовый заказ';
// $request->externalId = '426';

$request->recipient->firstName = 'Василий';
$request->recipient->lastName = 'Юрочкин';
$request->recipient->phone = '+79266056128';

$request->recipient->address->apartment = '43';
$request->recipient->address->house = '5';
$request->recipient->address->housing = '';
$request->recipient->address->street = 'ул. Державина';
//$request->recipient->pickupPointId = 10000018299;

$place = $request->addPlace($dimensions);
// $place->externalId = '427';
$item = $place->addItem();
$item->externalId = '428';
$item->name = 'HELP';
$item->count = 2;
$item->price = 500;
$item->assessedValue = 500;
// $item->tax = $item::TAX_NO_VAT;

$request->cost->assessedValue = 1000;
$request->cost->fullyPrepaid = true;
$request->cost->paymentMethod = $request->cost::PAYMENT_METHOD_PREPAID;

$contact = $request->addContact();

$contact->phone = '+79266056128';
$contact->firstName = 'Василий';
$contact->lastName = 'Юрочкин';

$logger->addFile('create-order-request.json');
$logger->addFile('create-order-response.json');

$response = $client->sendCreateOrderRequest($request);

if ($response->hasErrors()) {
// Обрабатываем ошибки
foreach ($response->getMessages() as $message) {
if ($message->getErrorCode() !== '') {
// Это ошибка
echo "{$message->getErrorCode()}: {$message->getMessage()}\n";
}
}

return;
}

$requestBuilder = UpdateOrderRequest::builder($response->id, $deliveryMethod, $location);
$requestBuilder->setPostalCode($postalCode);
$request = $requestBuilder->build();

$request->shipment->warehouseFrom = (int) $_SERVER['YANDEX_WAREHOUSE_ID'];
$request->senderId = (int) $_SERVER['YANDEX_SHOP_ID'];
$request->comment = 'Доставки не будет - тестовый заказ';
// $request->externalId = '426';

$request->recipient->firstName = 'Василий';
$request->recipient->lastName = 'Юрочкин';
$request->recipient->phone = '+79266056128';

$request->recipient->address->apartment = '43';
$request->recipient->address->house = '5';
$request->recipient->address->housing = '';
$request->recipient->address->street = 'ул. Державина';
//$request->recipient->pickupPointId = 10000018299;

$place = $request->addPlace($dimensions);
// $place->externalId = '427';
$item = $place->addItem();
$item->externalId = '428';
$item->name = 'HELP';
$item->count = 2;
$item->price = 500;
$item->assessedValue = 500;
// $item->tax = $item::TAX_NO_VAT;

$request->cost->assessedValue = 1000;
$request->cost->fullyPrepaid = true;
$request->cost->paymentMethod = $request->cost::PAYMENT_METHOD_PREPAID;

$contact = $request->addContact();

$contact->phone = '+79266056128';
$contact->firstName = 'Василий';
$contact->lastName = 'Юрочкин';

$request->comment = 'Доставки не будет - тестовый заказ (обновлено)';

$logger->addFile('update-order-request.json');
$logger->addFile('update-order-response.json');

$response = $client->sendUpdateOrderRequest($request);

echo "\n\nUpdated: {$response->id}\n\n";
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

declare(strict_types=1);

namespace YDeliverySDK\Requests\CreateOrderRequest;
namespace YDeliverySDK\Requests\Builders;

use YDeliverySDK\Requests\CreateOrderRequest;
use YDeliverySDK\Requests\Templates\OrderRequest;
Expand All @@ -36,13 +36,18 @@
use YDeliverySDK\Responses\Types\PostalCode;

/**
* CreateOrderRequest builder.
* OrderRequestRequest builder.
*
* @psalm-template T of OrderRequest
*
* @see https://yandex.ru/dev/delivery-3/doc/dg/reference/post-orders.html
*/
final class Builder
final class OrderRequestBuilder
{
/** @var CreateOrderRequest */
/**
* @var OrderRequest
* @psalm-var T
*/
private $request;

/** @var DeliveryOption */
Expand All @@ -60,9 +65,12 @@ final class Builder
/** @var PostalCode|null */
private $postalCode;

public function __construct(DeliveryOption $deliveryOption, Location $location, CreateOrderRequest $request = null)
/**
* @psalm-param T $request
*/
public function __construct(DeliveryOption $deliveryOption, Location $location, OrderRequest $request)
{
$this->request = $request ?? new CreateOrderRequest(new OrderRequest\DeliveryOption($deliveryOption->services));
$this->request = $request;
$this->deliveryOption = $deliveryOption;
$this->location = $location;
}
Expand All @@ -74,7 +82,12 @@ public function setPostalCode(PostalCode $postalCode): self
return $this;
}

public function build(): CreateOrderRequest
/**
* @psalm-return T
*
* @return OrderRequest
*/
public function build()
{
$this->request->deliveryType = $this->deliveryOption->delivery->type;
$this->request->shipment->date = $this->deliveryOption->shipments[$this->shipment]->date;
Expand Down
18 changes: 16 additions & 2 deletions src/Requests/CreateOrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
namespace YDeliverySDK\Requests;

use CommonSDK\Concerns\RequestCore;
use YDeliverySDK\Requests\Builders\OrderRequestBuilder;
use YDeliverySDK\Requests\Templates\OrderRequest;
use YDeliverySDK\Responses\CreateOrderResponse;
use YDeliverySDK\Responses\OrderResponse;
use YDeliverySDK\Responses\Types as ResponsesTypes;

/**
* CreateOrderRequest.
Expand All @@ -44,5 +46,17 @@ final class CreateOrderRequest extends OrderRequest
private const METHOD = 'POST';
private const ADDRESS = '/orders';

protected const RESPONSE = CreateOrderResponse::class;
protected const RESPONSE = OrderResponse::class;

/**
* @return OrderRequestBuilder<CreateOrderRequest>
*/
public static function builder(
ResponsesTypes\DeliveryOption $deliveryOption,
ResponsesTypes\Location $location
) {
return new OrderRequestBuilder($deliveryOption, $location, new static(
new OrderRequest\DeliveryOption($deliveryOption->services)
));
}
}
1 change: 1 addition & 0 deletions src/Requests/Shortcuts.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @method Responses\IntervalsResponse|Responses\Types\Interval[] sendImportIntervalsRequest(ImportIntervalsRequest $request)
* @method Responses\DeliveryOptionsResponse|Responses\Types\DeliveryOption[] sendDeliveryOptionsRequest(DeliveryOptionsRequest $request)
* @method Responses\OrderResponse sendCreateOrderRequest(CreateOrderRequest $request)
* @method Responses\OrderResponse sendUpdateOrderRequest(UpdateOrderRequest $request)
* @method Responses\SubmitOrderResponse|Responses\Types\SubmittedOrder[] sendSubmitOrderRequest(SubmitOrderRequest $request)
* @method Responses\OrdersSearchResponse|Responses\Types\Order[] sendOrdersSearchRequest(OrdersSearchRequest $request)
* @method Contracts\Response sendDeleteOrderRequest(DeleteOrderRequest $request)
Expand Down

0 comments on commit 76b27e7

Please sign in to comment.