Goal : How to implement Webhook and call Prestashop REST API from Magic xpi when Order status is changed
Prerequisite :
- Prestashop up and running (i have used an AWS image with prestashop already configured from Intuz )
- Credentials
- FTP access to upload the Magic xpi module (not mandatory)
1/ Webhook
In our case, we want to trigger a Magic xpi Flow when an Order is validated in the Shopping site
To implement a webhook to trigger Magic xpi Flow, you have to Write a Prestashop module in PHP.
The result of this step will be
To get this result, navigate to https://validator.prestashop.com/generator to generate a basic module and fill the information like below
Click Next
Click Next and type ActionOrderStatusUpdate
Refer to https://devdocs.prestashop-project.org/8/modules/concepts/hooks/list-of-hooks/actionorderstatusupdate for documentation
After clicking Next, you will get a zip file containing all the necessary files to implement the module.
Replace the logo.png file with a 32*32 pixel png file (magic logo)
2/ Modify the Magicxpi.php file like below
Register the module to the Hook ActionOrderStatusUpdate
Define a Configuration URL variable that will hold the Magic xpi Trigger URL
Create a function hookActionOrderStatusUpdate($params) and write the code like below
We will receive, in the Magic xpi Flow, Order_Id in the URL and Order_ID + newOrderStatus in the body in JSON
Save your file and zip the Magic xpi folder to get a new Magicxpi.zip
3/ Sign in with your admin user and Upload your Magic xpi module in the Prestashop website
If successfull, the result will be
Click on Configure to fill the magic xpi URL
(*) We will discuss below the Magic xpi URL structure
4/ Create a Magic xpi Service to get the order
Create a RESTful API service and set the URL to : http://localhost:6443/xpiREST
Define a Path with the structure : /order/{id}
5/ Create a Magic xpi Flow with a RESTful trigger
Catch the Id parameter in a context variable (C.ExternalObjectID) and the Body in a context variable (C.RequestPayload)
6/ Get the order detail using Prestashop REST API
To call the Prestashop REST API, you have to define an API Key and set the rights on the objects.
Set the permissions on the objects (minimum you have to tick GET on the object “orders”)
7/ Define a Magic xpi REST resource
We choose JSON format for the output and full option in the query
8/ Drag and drop REST Client connector
catch JSON Prestashop Order detail in a context variable C.OrderDetail_eCommerce
9/ Make a break point after this step
10/ From now, you are going to be able to test your webhook
My prestashop system and My Magic xpi system are located in the same AWS VPC.
My prestashop system has a public IP with Firewall rule.
To test and simulate the call, you can use Postman
The JSON request body corresponds to what Prestashop Module is going to send.
You should receive an HTTP 200 code
11/ Magic xpi monitor
You should see in the Blob, the order detail in JSON format.
This order corresponds to :
11/ You can use this implementation to automatically create the order in any back end system (ERP, WMS, …) such as SAP ECC6, D365FinanceOperation, …
you can use this mechanism on any prestashop objects with any webhooks that are supported.