1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
7
|
|
|
* an email on [email protected]. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\EventListener; |
13
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Client\MollieApiClient; |
15
|
|
|
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory; |
16
|
|
|
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType; |
17
|
|
|
use Mollie\Api\Exceptions\ApiException; |
18
|
|
|
use Mollie\Api\Resources\Order; |
19
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
20
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
21
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
22
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
23
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
24
|
|
|
use Webmozart\Assert\Assert; |
25
|
|
|
|
26
|
|
|
final class ShipmentShipEventListener |
27
|
|
|
{ |
28
|
|
|
/** @var Session */ |
29
|
|
|
private $session; |
30
|
|
|
|
31
|
|
|
/** @var MollieApiClient */ |
32
|
|
|
private $apiClient; |
33
|
|
|
|
34
|
|
|
public function __construct(MollieApiClient $apiClient) |
35
|
|
|
{ |
36
|
|
|
$this->apiClient = $apiClient; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function shipAll(GenericEvent $event): void |
40
|
|
|
{ |
41
|
|
|
/** @var ShipmentInterface $shipment */ |
42
|
|
|
$shipment = $event->getSubject(); |
43
|
|
|
Assert::isInstanceOf($shipment, ShipmentInterface::class); |
44
|
|
|
|
45
|
|
|
/** @var OrderInterface $order */ |
46
|
|
|
$order = $shipment->getOrder(); |
47
|
|
|
$payment = $order->getLastPayment(); |
48
|
|
|
|
49
|
|
|
if (null === $payment) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
54
|
|
|
$paymentMethod = $payment->getMethod(); |
55
|
|
|
|
56
|
|
|
$factoryName = $paymentMethod->getGatewayConfig()->getFactoryName() ?? null; |
|
|
|
|
57
|
|
|
|
58
|
|
|
if (!isset($payment->getDetails()['order_mollie_id']) || MollieGatewayFactory::FACTORY_NAME !== $factoryName) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$modusKey = $this->getModus($paymentMethod->getGatewayConfig()->getConfig()); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
/** @var Order $order */ |
66
|
|
|
$this->apiClient->setApiKey($modusKey); |
67
|
|
|
$order = $this->apiClient->orders->get($payment->getDetails()['order_mollie_id']); |
68
|
|
|
$order->shipAll(); |
69
|
|
|
} catch (ApiException $e) { |
70
|
|
|
$this->session->getFlashBag()->add('error', $e->getMessage()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function getModus(array $config): string |
75
|
|
|
{ |
76
|
|
|
if ($config['environment']) { |
77
|
|
|
return $config[MollieGatewayConfigurationType::API_KEY_LIVE]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $config[MollieGatewayConfigurationType::API_KEY_TEST]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.