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\Action\Api; |
13
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Entity\SubscriptionInterface; |
15
|
|
|
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface; |
16
|
|
|
use BitBag\SyliusMolliePlugin\Request\Api\CreateRecurringSubscription; |
17
|
|
|
use BitBag\SyliusMolliePlugin\Request\StateMachine\StatusRecurringSubscription; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Mollie\Api\Exceptions\ApiException; |
20
|
|
|
use Mollie\Api\Resources\Customer; |
21
|
|
|
use Mollie\Api\Resources\Subscription; |
22
|
|
|
use Mollie\Api\Types\MandateMethod; |
23
|
|
|
use Payum\Core\Action\ActionInterface; |
24
|
|
|
use Payum\Core\ApiAwareInterface; |
25
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
26
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
27
|
|
|
use Payum\Core\GatewayAwareInterface; |
28
|
|
|
use Payum\Core\GatewayAwareTrait; |
29
|
|
|
use SM\Factory\FactoryInterface as SateMachineFactoryInterface; |
30
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
31
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
32
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
33
|
|
|
|
34
|
|
|
final class CreateRecurringSubscriptionAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface |
35
|
|
|
{ |
36
|
|
|
use GatewayAwareTrait; |
37
|
|
|
|
38
|
|
|
/** @var FactoryInterface */ |
39
|
|
|
private $subscriptionFactory; |
40
|
|
|
|
41
|
|
|
/** @var EntityManagerInterface */ |
42
|
|
|
private $subscriptionManager; |
43
|
|
|
|
44
|
|
|
/** @var SateMachineFactoryInterface */ |
45
|
|
|
private $subscriptionSateMachineFactory; |
46
|
|
|
|
47
|
|
|
/** @var OrderRepositoryInterface */ |
48
|
|
|
private $orderRepository; |
49
|
|
|
|
50
|
|
|
/** @var MollieLoggerActionInterface */ |
51
|
|
|
private $loggerAction; |
52
|
|
|
|
53
|
|
|
public function __construct( |
54
|
|
|
FactoryInterface $subscriptionFactory, |
55
|
|
|
EntityManagerInterface $subscriptionManager, |
56
|
|
|
SateMachineFactoryInterface $subscriptionSateMachineFactory, |
57
|
|
|
OrderRepositoryInterface $orderRepository, |
58
|
|
|
MollieLoggerActionInterface $loggerAction |
59
|
|
|
) { |
60
|
|
|
$this->subscriptionFactory = $subscriptionFactory; |
61
|
|
|
$this->subscriptionManager = $subscriptionManager; |
62
|
|
|
$this->subscriptionSateMachineFactory = $subscriptionSateMachineFactory; |
63
|
|
|
$this->orderRepository = $orderRepository; |
64
|
|
|
$this->loggerAction = $loggerAction; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @param CreateRecurringSubscription $request */ |
68
|
|
|
public function execute($request): void |
69
|
|
|
{ |
70
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
71
|
|
|
|
72
|
|
|
$model = ArrayObject::ensureArrayObject($request->getModel()); |
73
|
|
|
|
74
|
|
|
if (true === isset($model['subscription_id'])) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
/** @var Customer $customer */ |
80
|
|
|
$customer = $this->mollieApiClient->customers->get($model['customer_mollie_id']); |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
$this->loggerAction->addNegativeLog(sprintf('Error with get customer from mollie with: %s', $e->getMessage())); |
83
|
|
|
|
84
|
|
|
throw new ApiException(sprintf('Error with get customer from mollie with: %s', $e->getMessage())); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @var Subscription $subscriptionApiResult */ |
88
|
|
|
$subscriptionApiResult = $customer->createSubscription([ |
89
|
|
|
'amount' => $model['amount'], |
90
|
|
|
'interval' => $model['interval'], |
91
|
|
|
'description' => $model['description'], |
92
|
|
|
'method' => MandateMethod::DIRECTDEBIT, |
93
|
|
|
'webhookUrl' => $model['webhookUrl'], |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
/** @var SubscriptionInterface $subscription */ |
97
|
|
|
$subscription = $this->subscriptionFactory->createNew(); |
98
|
|
|
|
99
|
|
|
/** @var OrderInterface $order */ |
100
|
|
|
$order = $this->orderRepository->find($model['metadata']['order_id']); |
101
|
|
|
|
102
|
|
|
$subscription->setSubscriptionId($subscriptionApiResult->id); |
103
|
|
|
$subscription->setCustomerId($model['customer_mollie_id']); |
104
|
|
|
$subscription->setOrder($order); |
105
|
|
|
|
106
|
|
|
$this->subscriptionManager->persist($subscription); |
107
|
|
|
|
108
|
|
|
$model['subscription_mollie_id'] = $subscriptionApiResult->id; |
109
|
|
|
|
110
|
|
|
$this->loggerAction->addLog(sprintf('Create requrring subscription with id: %s', $subscriptionApiResult->id)); |
111
|
|
|
|
112
|
|
|
$this->gateway->execute(new StatusRecurringSubscription($subscription)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function supports($request): bool |
116
|
|
|
{ |
117
|
|
|
return |
118
|
|
|
$request instanceof CreateRecurringSubscription && |
119
|
|
|
$request->getModel() instanceof \ArrayAccess |
120
|
|
|
; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|