|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GGGGino\SkuskuCartBundle\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Craue\FormFlowBundle\Form\FormFlowInterface; |
|
6
|
|
|
use GGGGino\SkuskuCartBundle\Entity\CartForm; |
|
7
|
|
|
use GGGGino\SkuskuCartBundle\Event\PostPaymentCartEvent; |
|
8
|
|
|
use GGGGino\SkuskuCartBundle\Event\PostSubmitCartEvent; |
|
9
|
|
|
use GGGGino\SkuskuCartBundle\Event\PrePersistOrderEvent; |
|
10
|
|
|
use GGGGino\SkuskuCartBundle\Event\PreSubmitCartEvent; |
|
11
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuCart; |
|
12
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuPayment; |
|
13
|
|
|
use GGGGino\SkuskuCartBundle\Service\CartManager; |
|
14
|
|
|
use GGGGino\SkuskuCartBundle\Service\OrderManager; |
|
15
|
|
|
use Payum\Core\GatewayInterface; |
|
16
|
|
|
use Payum\Core\Payum; |
|
17
|
|
|
use Symfony\Component\Form\FormInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
|
22
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
23
|
|
|
|
|
24
|
|
|
class CartFlow extends CartFlowBase |
|
25
|
|
|
{ |
|
26
|
|
|
const PRE_SUBMIT = 'skusku_cart.pre_submit'; |
|
27
|
|
|
|
|
28
|
|
|
const POST_SUBMIT = 'skusku_cart.post_submit'; |
|
29
|
|
|
|
|
30
|
|
|
const POST_PAYMENT = 'skusku_cart.post_payment'; |
|
31
|
|
|
|
|
32
|
|
|
const PRE_PERSIST_ORDER = 'skusku_cart.pre_persist_order'; |
|
33
|
|
|
|
|
34
|
|
|
const TRANSITION_RESET_CART = 'emptycart'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var CartManager |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $cartManager; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var RequestStack |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $requestStack; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Payum |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $payum; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var OrderManager |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $orderManager; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var TokenStorage |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $tokenStorage; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $allowAnonymous; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $cartMode; |
|
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|null |
|
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
|
|
|
|
|
129
|
|
|
if( !$this->allowAnonymous ) |
|
130
|
|
|
throw new AccessDeniedException("Anonymous users cannot buy"); |
|
131
|
|
|
|
|
132
|
|
|
// @todo done this because craue form flow doesn't permit to add a custom action |
|
133
|
|
|
if( $this->requestStack->getCurrentRequest()->request->get('flow_cart_transition') == self::TRANSITION_RESET_CART ) { |
|
134
|
|
|
$this->emptyCart($formData); |
|
135
|
|
|
$this->reset(); |
|
136
|
|
|
$form = $this->createForm(); |
|
137
|
|
|
return null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
if ($this->nextStep() && $this->cartMode != 'single_page') { |
|
142
|
|
|
// form for the next step |
|
143
|
|
|
$form = $this->createForm(); |
|
144
|
|
|
} else { |
|
145
|
|
|
// flow finished |
|
146
|
|
|
/** @var SkuskuCart $finalCart */ |
|
147
|
|
|
$finalCart = $formData->getCart(); |
|
148
|
|
|
|
|
149
|
|
|
if ($this->hasListeners(self::PRE_SUBMIT)) { |
|
150
|
|
|
$event = new PreSubmitCartEvent($this, $finalCart); |
|
151
|
|
|
$this->eventDispatcher->dispatch(self::PRE_SUBMIT, $event); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->cartManager->persistCart($finalCart); |
|
155
|
|
|
|
|
156
|
|
|
$storage = $this->payum->getStorage('GGGGino\SkuskuCartBundle\Model\SkuskuPayment'); |
|
157
|
|
|
|
|
158
|
|
|
$payment = new SkuskuPayment(); |
|
159
|
|
|
$payment->setNumber(uniqid()); |
|
160
|
|
|
$payment->setCurrencyCode($finalCart->getCurrency()->getIsoCode()); |
|
161
|
|
|
$payment->setTotalAmount($finalCart->getTotalPrice()); // 1.23 EUR |
|
162
|
|
|
$payment->setDescription($finalCart->getTotalQuantity()); |
|
163
|
|
|
$payment->setClientId($finalCart->getCustomer()); |
|
|
|
|
|
|
164
|
|
|
$payment->setClientEmail($finalCart->getCustomer()->getEmail()); |
|
165
|
|
|
|
|
166
|
|
|
$storage->update($payment); |
|
167
|
|
|
|
|
168
|
|
|
$captureToken = $this->payum->getTokenFactory()->createCaptureToken( |
|
169
|
|
|
$formData->getPaymentMethod(), |
|
170
|
|
|
$payment, |
|
171
|
|
|
'done' // the route to redirect after capture |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
$finalCart->setPayment($payment); |
|
175
|
|
|
|
|
176
|
|
|
$requestFields = $this->requestStack->getCurrentRequest()->request->get('choosePayment'); |
|
177
|
|
|
if(isset($requestFields['additionalFields']) && count($requestFields['additionalFields']) > 0 ) { |
|
178
|
|
|
$finalCart->setAdditionalFields(json_encode($requestFields['additionalFields'])); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->cartManager->flushCart($finalCart); |
|
182
|
|
|
|
|
183
|
|
|
$this->reset(); // remove step data from the session |
|
184
|
|
|
|
|
185
|
|
|
if ($this->hasListeners(self::POST_SUBMIT)) { |
|
186
|
|
|
$event = new PostSubmitCartEvent($this, $finalCart); |
|
187
|
|
|
$this->eventDispatcher->dispatch(self::POST_SUBMIT, $event); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return new RedirectResponse($captureToken->getTargetUrl()); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Excecute all the listener tagged with "skusku_cart.post_payment" |
|
197
|
|
|
* Save the cart as order |
|
198
|
|
|
* |
|
199
|
|
|
* @param $payment |
|
200
|
|
|
* @return FormInterface |
|
201
|
|
|
*/ |
|
202
|
|
|
public function handleDone(SkuskuPayment $payment, $status) |
|
203
|
|
|
{ |
|
204
|
|
|
if ($this->hasListeners(self::POST_PAYMENT)) { |
|
205
|
|
|
$event = new PostPaymentCartEvent($this, $payment, $status); |
|
206
|
|
|
$this->eventDispatcher->dispatch(self::POST_PAYMENT, $event); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$order = $this->orderManager->buildOrderFromCart($payment->getCart()); |
|
210
|
|
|
|
|
211
|
|
|
if ($this->hasListeners(self::PRE_PERSIST_ORDER)) { |
|
212
|
|
|
$event = new PrePersistOrderEvent($this, $order); |
|
213
|
|
|
$this->eventDispatcher->dispatch(self::PRE_PERSIST_ORDER, $event); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$this->orderManager->saveOrder($order); |
|
217
|
|
|
|
|
218
|
|
|
// you have order and payment status |
|
219
|
|
|
// so you can do whatever you want for example you can just print status and payment details. |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
protected function emptyCart(CartForm $formData) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->cartManager->emptyCart($formData); |
|
225
|
|
|
$this->cartManager->persistCart($formData->getCart()); |
|
226
|
|
|
$this->cartManager->flushCart($formData->getCart()); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param mixed $payum |
|
231
|
|
|
* @return CartFlow |
|
232
|
|
|
*/ |
|
233
|
|
|
public function setPayum($payum) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->payum = $payum; |
|
236
|
|
|
return $this; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param TokenStorage $tokenStorage |
|
241
|
|
|
* @return CartFlow |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setTokenStorage($tokenStorage) |
|
244
|
|
|
{ |
|
245
|
|
|
$this->tokenStorage = $tokenStorage; |
|
246
|
|
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param boolean $allowAnonymous |
|
251
|
|
|
* @return CartFlow |
|
252
|
|
|
*/ |
|
253
|
|
|
public function setAllowAnonymous($allowAnonymous) |
|
254
|
|
|
{ |
|
255
|
|
|
$this->allowAnonymous = $allowAnonymous; |
|
256
|
|
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @param boolean $cartMode |
|
261
|
|
|
* @return CartFlow |
|
262
|
|
|
*/ |
|
263
|
|
|
public function setCartMode($cartMode) |
|
264
|
|
|
{ |
|
265
|
|
|
$this->cartMode = $cartMode; |
|
|
|
|
|
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
} |