1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GGGGino\SkuskuCartBundle\Form; |
4
|
|
|
|
5
|
|
|
use Craue\FormFlowBundle\Event\FlowExpiredEvent; |
6
|
|
|
use Craue\FormFlowBundle\Form\FormFlow; |
7
|
|
|
use Craue\FormFlowBundle\Form\FormFlowEvents; |
8
|
|
|
use Craue\FormFlowBundle\Form\FormFlowInterface; |
9
|
|
|
use GGGGino\SkuskuCartBundle\Entity\CartForm; |
10
|
|
|
use GGGGino\SkuskuCartBundle\Event\PostPaymentCartEvent; |
11
|
|
|
use GGGGino\SkuskuCartBundle\Event\PostSubmitCartEvent; |
12
|
|
|
use GGGGino\SkuskuCartBundle\Event\PreSubmitCartEvent; |
13
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuCart; |
14
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuPayment; |
15
|
|
|
use GGGGino\SkuskuCartBundle\Service\CartManager; |
16
|
|
|
use GGGGino\SkuskuCartBundle\Service\OrderManager; |
17
|
|
|
use Payum\Core\Gateway; |
18
|
|
|
use Payum\Core\GatewayInterface; |
19
|
|
|
use Payum\Core\Payum; |
20
|
|
|
use Payum\Core\Request\Capture; |
21
|
|
|
use Symfony\Component\Form\FormInterface; |
22
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
23
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
27
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
28
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
29
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
30
|
|
|
|
31
|
|
|
class CartFlow extends CartFlowBase |
32
|
|
|
{ |
33
|
|
|
const PRE_SUBMIT = 'skusku_cart.pre_submit'; |
34
|
|
|
|
35
|
|
|
const POST_SUBMIT = 'skusku_cart.post_submit'; |
36
|
|
|
|
37
|
|
|
const POST_PAYMENT = 'skusku_cart.post_payment'; |
38
|
|
|
|
39
|
|
|
const TRANSITION_RESET_CART = 'emptycart'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CartManager |
43
|
|
|
*/ |
44
|
|
|
private $cartManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var RequestStack |
48
|
|
|
*/ |
49
|
|
|
private $requestStack; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Payum |
53
|
|
|
*/ |
54
|
|
|
private $payum; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var OrderManager |
58
|
|
|
*/ |
59
|
|
|
private $orderManager; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var TokenStorage |
63
|
|
|
*/ |
64
|
|
|
private $tokenStorage; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
private $allowAnonymous; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* CartFlowBase constructor. |
73
|
|
|
* @param array $configSteps |
74
|
|
|
* @param CartManager $cartManager |
75
|
|
|
* @param OrderManager $orderManager |
76
|
|
|
* @param RequestStack $requestStack |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
array $configSteps, |
80
|
|
|
CartManager $cartManager, |
81
|
|
|
OrderManager $orderManager, |
82
|
|
|
RequestStack $requestStack) |
83
|
|
|
{ |
84
|
|
|
parent::__construct($configSteps); |
85
|
|
|
|
86
|
|
|
$this->cartManager = $cartManager; |
87
|
|
|
$this->requestStack = $requestStack; |
88
|
|
|
$this->orderManager = $orderManager; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
protected function loadStepsConfig() |
95
|
|
|
{ |
96
|
|
|
$this->configSteps['payment']['skip'] = function($estimatedCurrentStepNumber, FormFlowInterface $flow) { |
97
|
|
|
/** @var CartForm $data */ |
98
|
|
|
$data = $flow->getFormData(); |
99
|
|
|
|
100
|
|
|
$paymentMethod = $data->getPaymentMethod(); |
101
|
|
|
|
102
|
|
|
// se non è settato vuol dire che non ci sono ancora arrivato |
103
|
|
|
if( !$paymentMethod ) |
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
|
106
|
|
|
/** @var GatewayInterface $gateway */ |
107
|
|
|
$gateway = $this->payum->getGateway($paymentMethod); |
|
|
|
|
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
return $this->configSteps; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $form |
117
|
|
|
* @param CartForm $formData |
118
|
|
|
* @return Response|void |
119
|
|
|
* |
120
|
|
|
* @throws \Payum\Core\Reply\ReplyInterface |
121
|
|
|
* @throws null |
122
|
|
|
*/ |
123
|
|
|
public function handleSubmit(&$form, $formData) |
124
|
|
|
{ |
125
|
|
|
if ($this->isValid($form)) { |
126
|
|
|
$this->saveCurrentStepData($form); |
127
|
|
|
|
128
|
|
|
if( !$this->allowAnonymous ) |
129
|
|
|
throw new AccessDeniedException("Anonymous users cannot buy"); |
130
|
|
|
|
131
|
|
|
// @todo done this because craue form flow doesn't permit to add a custom action |
132
|
|
|
if( $this->requestStack->getCurrentRequest()->request->get('flow_cart_transition') == self::TRANSITION_RESET_CART ){ |
133
|
|
|
$this->emptyCart($formData); |
134
|
|
|
$this->reset(); |
135
|
|
|
$form = $this->createForm(); |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($this->nextStep()) { |
140
|
|
|
// form for the next step |
141
|
|
|
$form = $this->createForm(); |
142
|
|
|
} else { |
143
|
|
|
// flow finished |
144
|
|
|
/** @var SkuskuCart $finalCart */ |
145
|
|
|
$finalCart = $formData->getCart(); |
146
|
|
|
|
147
|
|
|
if ($this->hasListeners(self::PRE_SUBMIT)) { |
148
|
|
|
$event = new PreSubmitCartEvent($this, $formData); |
|
|
|
|
149
|
|
|
$this->eventDispatcher->dispatch(self::PRE_SUBMIT, $event); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->cartManager->persistCart($finalCart); |
153
|
|
|
|
154
|
|
|
$storage = $this->payum->getStorage('GGGGino\SkuskuCartBundle\Model\SkuskuPayment'); |
155
|
|
|
|
156
|
|
|
$payment = new SkuskuPayment(); |
157
|
|
|
$payment->setNumber(uniqid()); |
158
|
|
|
$payment->setCurrencyCode($finalCart->getCurrency()->getIsoCode()); |
159
|
|
|
$payment->setTotalAmount($finalCart->getTotalPrice()); // 1.23 EUR |
160
|
|
|
$payment->setDescription($finalCart->getTotalQuantity()); |
161
|
|
|
$payment->setClientId($finalCart->getCustomer()); |
|
|
|
|
162
|
|
|
$payment->setClientEmail($finalCart->getCustomer()->getEmail()); |
163
|
|
|
|
164
|
|
|
$storage->update($payment); |
165
|
|
|
|
166
|
|
|
$captureToken = $this->payum->getTokenFactory()->createCaptureToken( |
167
|
|
|
$formData->getPaymentMethod(), |
168
|
|
|
$payment, |
169
|
|
|
'done' // the route to redirect after capture |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$finalCart->setPayment($payment); |
173
|
|
|
|
174
|
|
|
$this->cartManager->flushCart($finalCart); |
175
|
|
|
|
176
|
|
|
$this->reset(); // remove step data from the session |
177
|
|
|
|
178
|
|
|
if ($this->hasListeners(self::POST_SUBMIT)) { |
179
|
|
|
$event = new PostSubmitCartEvent($this, $formData); |
|
|
|
|
180
|
|
|
$this->eventDispatcher->dispatch(self::POST_SUBMIT, $event); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return new RedirectResponse($captureToken->getTargetUrl()); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Excecute all the listener tagged with "skusku_cart.post_payment" |
190
|
|
|
* Save the cart as order |
191
|
|
|
* |
192
|
|
|
* @param $payment |
193
|
|
|
* @return FormInterface |
194
|
|
|
*/ |
195
|
|
|
public function handleDone(SkuskuPayment $payment, $status) |
196
|
|
|
{ |
197
|
|
|
if ($this->hasListeners(self::POST_PAYMENT)) { |
198
|
|
|
$event = new PostPaymentCartEvent($this, $payment, $status); |
199
|
|
|
$this->eventDispatcher->dispatch(self::POST_PAYMENT, $event); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$order = $this->orderManager->buildOrderFromCart($payment->getCart()); |
203
|
|
|
$this->orderManager->saveOrder($order); |
204
|
|
|
|
205
|
|
|
// you have order and payment status |
206
|
|
|
// so you can do whatever you want for example you can just print status and payment details. |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function emptyCart(CartForm $formData) |
210
|
|
|
{ |
211
|
|
|
$this->cartManager->emptyCart($formData); |
212
|
|
|
$this->cartManager->persistCart($formData->getCart()); |
213
|
|
|
$this->cartManager->flushCart($formData->getCart()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param mixed $payum |
218
|
|
|
* @return CartFlow |
219
|
|
|
*/ |
220
|
|
|
public function setPayum($payum) |
221
|
|
|
{ |
222
|
|
|
$this->payum = $payum; |
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param TokenStorage $tokenStorage |
228
|
|
|
* @return CartFlow |
229
|
|
|
*/ |
230
|
|
|
public function setTokenStorage($tokenStorage) |
231
|
|
|
{ |
232
|
|
|
$this->tokenStorage = $tokenStorage; |
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param boolean $allowAnonymous |
238
|
|
|
* @return CartFlow |
239
|
|
|
*/ |
240
|
|
|
public function setAllowAnonymous($allowAnonymous) |
241
|
|
|
{ |
242
|
|
|
$this->allowAnonymous = $allowAnonymous; |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
} |