Extend
Request sending
This module assumes that the order notification service of a Dynamics BC installation is accessible via the POST
method. It is possible though to change the HTTP method that is used to access the endpoint, by injecting another request class into \TechDivision\PacemakerDynamicsBcEntityExportOrderNotification\Model\Command\CreateOrderNotification
. All request classes provided by the module TechDivision_DynamicsBcApiClient
can be used to access the endpoint. Custom request classes can be used, as long as they implement \TechDivision\DynamicsBcApiClient\Api\RequestInterface
.
Example
The Dynamics BC installation of your project requires the endpoint to be accessed via the PUT
method. In this case you would add the following to your project specific module’s di.xml
file:
<type name="TechDivision\PacemakerDynamicsBcEntityExportOrderNotification\Model\Command\CreateOrderNotification">
<arguments>
<argument name="request" xsi:type="object">TechDivision\DynamicsBcApiClient\Model\Request\Put</argument>
</arguments>
</type>
Make sure though, that your project specific module is loaded after the generic module!
Response validation
This module uses \TechDivision\DynamicsBcApiClient\Model\Service\ResponseValidator
to validate the response that is returned by Dynamics BC. Please refer to the documentation of the module TechDivision_DynamicsBcApiClient
for details of how to make this validation match your requirements.
Response validation is performed immediately after receiving it from Dynamics BC. Validation errors are therefore logged in the |
Response handling
This module registers a special response handler for the order export pipeline. The response handler reads the data that was sent to and received from Dynamics BC and passes them to an implementation of \TechDivision\PacemakerDynamicsBcEntityExportOrderNotification\Api\Command\HandleOrderNotificationResponseInterface
.
This allows to implement a project specific response handling by implementing said interface and setting this implementation as your projects preferred implementation. This module comes with a default implementation of the interface, that does absolutely nothing with the specified data. Its purpose is to keep that part away from you until you really need to handle the response.
Example
Upon notifying Dynamics BC, your project specific installation returns the identifier that Dynamics BC will use to store the order. Your project requires that this external id is stored in Magento for backwards reference purposes.
You can achieve this by implementing the following class.
declare(strict_types=1);
namespace MyVendor\MyModule\Model\Command;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use TechDivision\PacemakerDynamicsBcEntityExportOrderNotification\Api\Command\HandleOrderNotificationResponseInterface;
use TechDivision\PacemakerDynamicsBcEntityExportOrderNotification\Api\Data\OrderNotificationPayloadInterface;
class PersistExternalOrderId implements HandleOrderNotificationResponseInterface
{
private OrderRepositoryInterface $orderRepository;
private SerializerInterface $serializer;
public function __construct(
OrderRepositoryInterface $orderRepository,
SerializerInterface $serializer
) {
$this->orderRepository = $orderRepository;
$this-serializer = $serializer;
}
/**
* @inheritDoc
*
* @param \MyVendor\MyModule\Api\Data\OrderNotificationPayloadInterface & OrderNotificationPayloadInterface $payload
*/
public function execute(OrderNotificationPayloadInterface $payload, string $responseBody): void
{
$data = $this->serializer->unserialize($responseBody); (1)
$order = $this->orderRepository->get($payload->getId()); (2)
if (isset($data['externalOrderId'])) {
$order->setExtOrderId($data['externalOrderId']); (3)
$this->orderRepository->save($order); (4)
}
}
}
1 | Since the module is not aware of the data structure and format of the response, you have to process the response body as needed. Here we assume that a JSON format was returned and have it deserialized by a Magento serializer (with JSON being the preferred implementation). |
2 | Get hold of the order. |
3 | Use Magento’s external id feature to store the external id (no need to reinvent the wheel). |
4 | Persist the change. |