-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathSetSubscriptionDataOnBuyRequest.php
81 lines (67 loc) · 2.28 KB
/
SetSubscriptionDataOnBuyRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Mollie\Payment\Observer\SalesQuoteItemSetProduct;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Quote\Api\Data\CartItemInterface;
class SetSubscriptionDataOnBuyRequest implements ObserverInterface
{
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(
SerializerInterface $serializer
) {
$this->serializer = $serializer;
}
public function execute(Observer $observer)
{
/** @var ProductInterface $product */
$product = $observer->getData('product');
if (!$product->getData('mollie_subscription_product')) {
return;
}
$table = $product->getData('mollie_subscription_table');
if (!$table) {
return;
}
$buyRequest = $this->getBuyRequest($observer->getData('quote_item'));
$value = $this->serializer->unserialize($buyRequest->getValue());
if (isset($value['mollie_metadata'])) {
return;
}
$data = $this->serializer->unserialize($table);
$default = $this->getDefault($data);
$value['mollie_metadata'] = [
'purchase' => 'subscription',
'recurring_metadata' => [
'option_id' => $default['identifier'],
],
];
$buyRequest->setValue($this->serializer->serialize($value));
}
private function getDefault(array $data): array
{
foreach ($data as $row) {
if (isset($row['isDefault']) && $row['isDefault']) {
return $row;
}
}
return array_shift($data);
}
private function getBuyRequest(CartItemInterface $item): OptionInterface
{
/** @var OptionInterface[] $options */
$options = $item->getOptions();
foreach ($options as $option) {
if ($option->getCode() == 'info_buyRequest') {
return $option;
}
}
throw new NotFoundException(__('No info_buyRequest option found'));
}
}