Hook into order completing process

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Hook into order completing process

Post by flomo » 20 Jan 2020, 11:01

As soon as an order has been paid, I need to make an API call to fetch a serial number, store the serial with the order and send it to the customer with the payment email.

I'm a bit lost:
1. Where can I hook into the order complete process to start my API calls (the order must be fully paid at this moment)?
2. Where would I store the serial to, so the user can view it in his account?

Ideally this should happen before the confirmation is sent to the customer, to include the serial in the email.

User avatar
aimeos
Administrator
Posts: 7873
Joined: 01 Jan 1970, 00:00

Re: Hook into order completing process

Post by aimeos » 21 Jan 2020, 09:46

The best way is to create a decorator for the payment service provider:
- https://aimeos.org/docs/Developers/Libr ... _decorator
- https://aimeos.org/docs/Developers/Library/Service

Then, in your decorator:
- add the updateSync() (called after redirecting the customer to the confirmation page) method
- add updatePush() (called by the payment server in an asynchon way if the payment status changes) methods
- check if the payment status is what you need (authorized or received)
- fetch the order base object (basket) with the services (payment and delivery)
- check if the serial number is already set as order service attribute
- make your API calls
- store the serial number as order service attribute
- store the order base object again

There are helper/support methods for almost everything documented. The rest (serial number is shown to the customers in their accounts and in their e-mails) is done automatically as long as you don't use a order service attribute type "hidden".
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Re: Hook into order completing process

Post by flomo » 21 Jan 2020, 09:59

Thank you very much! Will dive into this this week.

flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Re: Hook into order completing process

Post by flomo » 24 Jan 2020, 11:27

How would I get the basket/products? I was able to fetch the basket, but the products are always empty:

Code: Select all

    public function updateSync(\Psr\Http\Message\ServerRequestInterface $request, \Aimeos\MShop\Order\Item\Iface $orderItem)
    {
        $update = $this->getProvider()->updateSync($request, $orderItem);

        // Returns []
        dump($this->getProvider()->getOrderBase($orderItem->getBaseId())->getProducts());

        // Returns []
        dump($this->getOrderBase($orderItem->getBaseId())->getProducts());

        return $update;
    }

flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Re: Hook into order completing process

Post by flomo » 24 Jan 2020, 14:05

:? Nevermind. I just found out, that

Code: Select all

\Aimeos\MShop\Service\Provider\Base::getOrderBase()
accepts a second parameter that defines, which part of the order to load: https://aimeos.org/api/latest/class-Aim ... .Base.html

Using

Code: Select all

\Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL
or

Code: Select all

\Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT
(defined here https://aimeos.org/api/latest/class-Aim ... .Base.html) solves the problem.

flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Re: Hook into order completing process

Post by flomo » 24 Jan 2020, 16:05

Still having problems. I now have the Aimeos\MShop\Order\Item\Base\Product\Standard objects from the order, but the supplier ( getSupplierCode() ) and attributes ( getAttribute(), the function getAttributes() is even not available) are empty. How can I access this data?

User avatar
aimeos
Administrator
Posts: 7873
Joined: 01 Jan 1970, 00:00

Re: Hook into order completing process

Post by aimeos » 25 Jan 2020, 14:21

The supplier code is only added if it's passed when adding it to the basket. This isn't done by default because there can be several suppliers for one product.

These are the methods available for retrieving attributes of an order product item:
https://github.com/aimeos/aimeos-core/b ... #L289-L313
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Re: Hook into order completing process

Post by flomo » 25 Jan 2020, 17:11

I tried those methods, but getAttributeItem() returns null, and getAttributeItems() an empty array [] with this code:

Code: Select all

        $basket = $this->getOrderBase($orderItem->getBaseId(), OrderItemBaseBase::PARTS_ALL);
        $products = $basket->getProducts();
        foreach($products as $product) {
            $product->getAttributeItems());
            // []
        }
I had to use this code, to get the attribute value:

Code: Select all

        $basket = $this->getOrderBase($orderItem->getBaseId(), OrderItemBaseBase::PARTS_ALL);
        $products = $basket->getProducts();
        $baseManager = \Aimeos\MShop::create($this->getContext(), 'product');

        foreach ($products as $product) {
            $item = $baseManager->getItem($product->getProductId(), ['attribute']);
            $serialApi = $item->getRefItems('attribute', 'serialapi');
            if (!empty($serialApi)) {
                $serialApiValue = reset($serialApi)->getCode();
            }
        }
 
Is that ok this way?

User avatar
aimeos
Administrator
Posts: 7873
Joined: 01 Jan 1970, 00:00

Re: Hook into order completing process

Post by aimeos » 25 Jan 2020, 17:18

No, you didn't store the serial ID as order product attribute but as regular product referencing a regular attribute instead! That way, your serial ID is shared for all orders and if there will be two orders at the same time, they will both retrieve the same serial ID.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

flomo
Posts: 52
Joined: 26 Sep 2019, 15:11

Re: Hook into order completing process

Post by flomo » 25 Jan 2020, 17:27

The code in my previous post ist not about the serial yet. I store an API reference called "serialapi" (each product might get its serial from another API, depending on the brand) with each product. After payment I have to retrieve this reference to know, which API I have to contact.

So I think the idea is correct, I'm just not sure if my complicated code is correct (because $product->getAttributeItems() returns an empty array).

Post Reply