-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaymentController.php
More file actions
231 lines (194 loc) · 9.39 KB
/
PaymentController.php
File metadata and controls
231 lines (194 loc) · 9.39 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace AppBundle\Controller;
use PayoneBundle\Ecommerce\PaymentManager\BsPayone;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\Payment\Datatrans;
use Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\Payment\PayPal;
use Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\Payment\QPay;
use Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\Payment\WirecardSeamless;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class PaymentController extends AbstractCartAware
{
/**
* @inheritDoc
*/
public function onKernelController(FilterControllerEvent $event)
{
parent::onKernelController($event);
//navigation and breadcrumbs
$this->view->hideNav = true;
$translator = $this->get('translator');
$placeholder = $this->get('pimcore.templating.view_helper.placeholder');
/** @var callable $placeholder */
$placeholder('addBreadcrumb')->append([
'parentId' => $this->document->getId(),
'id' => 'cart',
'label' => $translator->trans('general.mycart'),
'url' => $this->generateUrl('cart', ['action' => 'list'])
]);
$placeholder('addBreadcrumb')->append([
'parentId' => 'cart',
'id' => 'checkout',
'label' => $translator->trans('general.checkout')
]);
$cart = $this->getCart();
$this->view->cart = $cart;
}
/**
* payment page with iframe
*/
public function paymentAction()
{
$this->view->currentStep = 'payment';
$checkoutManager = Factory::getInstance()->getCheckoutManager($this->getCart());
$paymentStep = $checkoutManager->getCheckoutStep('confirm');
$trackingManager = Factory::getInstance()->getTrackingManager();
$trackingManager->trackCheckoutStep($paymentStep, $this->getCart(), 3, 'payment');
//needed for sidebar
$checkoutManager = Factory::getInstance()->getCheckoutManager($this->getCart());
$deliveryAddress = $checkoutManager->getCheckoutStep('deliveryaddress');
$this->view->deliveryAddress = $deliveryAddress->getData();
}
/**
* payment iframe
*/
public function paymentFrameAction(Request $request)
{
// init
$cart = $this->getCart();
$checkoutManager = Factory::getInstance()->getCheckoutManager($cart);
if ($checkoutManager->isCommitted()) {
throw new \Exception('Cart already committed');
}
$paymentInformation = $checkoutManager->startOrderPayment();
$payment = $checkoutManager->getPayment();
$language = substr($request->getLocale(), 0, 2);
// payment config
if ($payment instanceof WirecardSeamless) {
// wirecard seamless
$config = [
'view' => $this->view,
'orderIdent' => $paymentInformation->getInternalPaymentId()
];
} elseif ($payment instanceof QPay) {
// wirecard
$url = $request->getSchemeAndHttpHost() . $this->generateUrl('action', ['controller' => 'payment', 'action' => 'payment-status', 'prefix' => $language]) . '?mode=';
$config = [
'language' => $language,
'successURL' => $url . 'success',
'cancelURL' => $url . 'cancel',
'failureURL' => $url . 'failure',
'serviceURL' => $url . 'service',
'confirmURL' => $request->getSchemeAndHttpHost() . $this->generateUrl('action', ['controller' => 'handle-payment', 'action' => 'server-side-q-pay', 'prefix' => $language, 'elementsclientauth' => 'disabled']),
'confirmMail' => 'christian.fasching@pimcore.com',
'orderDescription' => 'My Order at pimcore.org',
'imageURL' => 'https://www.pimcore.org/static/css/skins/default/logo.png',
'orderIdent' => $paymentInformation->getInternalPaymentId()
];
} elseif ($payment instanceof PayPal) {
// paypal
$returnUrl = $request->getSchemeAndHttpHost() . $this->generateUrl('action', ['controller' => 'payment', 'action' => 'payment-status', 'mode' => 'success', 'prefix' => $language]);
$cancelUrl = $request->getSchemeAndHttpHost() . $this->generateUrl('checkout', ['action' => 'payment', 'error' => 'cancel', 'language' => $language]) . '?mode=';
$config = [
'ReturnURL' => $returnUrl,
'CancelURL' => $cancelUrl . 'payment?error=cancel',
'OrderDescription' => 'My Order at pimcore.org',
'cpp-header-image' => '111b25',
'cpp-header-border-color' => '111b25',
'cpp-payflow-color' => 'f5f5f5',
'cpp-cart-border-color' => 'f5f5f5',
'cpp-logo-image' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/static/images/logo_paypal.png',
'InvoiceID' => $paymentInformation->getInternalPaymentId()
];
} elseif ($payment instanceof Datatrans) {
// datatrans
$url = $request->getSchemeAndHttpHost() . $this->generateUrl('action', ['controller' => 'payment', 'action' => 'payment-status', 'prefix' => $language]) . '?mode=';
$config = [
// checkout config
'language' => $language,
'refno' => $paymentInformation->getInternalPaymentId(),
'useAlias' => true,
'reqtype' => 'CAA', // payment direkt ausführen
// system
'successUrl' => $url . 'success',
'errorUrl' => $url . 'error',
'cancelUrl' => $url . 'cancel'
];
}elseif($payment instanceof BsPayone) {
// payone
$payment->setPaymentInformation($paymentInformation);
$config['orderIdent'] = $paymentInformation->getInternalPaymentId();
$config['cart'] = $cart;
$config['language'] = substr($request->getLocale(), 0, 2);
}
else {
throw new \Exception('Unknown Payment configured.');
}
// init payment
$this->view->payment = $payment->initPayment($cart->getPriceCalculator()->getGrandTotal(), $config);
}
/**
* got response from payment provider
*/
public function paymentStatusAction(Request $request)
{
// init
$cart = $this->getCart();
$checkoutManager = Factory::getInstance()->getCheckoutManager($cart);
$language = substr($request->getLocale(), 0, 2);
if ($request->get('mode') == 'cancel') {
try {
$checkoutManager->cancelStartedOrderPayment();
} catch (\Exception $e) {
//it seems that payment already canceled due to server side call.
}
$this->view->goto = $this->generateUrl('checkout', ['action' => 'confirm', 'language' => $language, 'error' => strip_tags($request->get('mode'))]);
return;
}
if ($request->get('mode') == 'pending') {
$this->view->goto = $this->generateUrl('checkout', ['action' => 'pending', 'language' => $language]);
return;
}
$params = array_merge($request->query->all(), $request->request->all());
// add additional data for paypal
$payment = $checkoutManager->getPayment();
if ($payment instanceof PayPal) {
$price = $cart->getPriceCalculator()->getGrandTotal();
$params['amount'] = $price->getAmount()->asString();
$params['currency'] = $price->getCurrency()->getShortName();
}
try {
$order = $checkoutManager->handlePaymentResponseAndCommitOrderPayment($params);
// optional to clear payment
// if this call is necessary depends on payment provider and configuration.
// its possible to execute this later (e.g. when shipment is done)
// $payment = $checkoutManager->getPayment();
// $paymentStatus = $payment->executeDebit();
// $orderAgent = Factory::getInstance()->getOrderManager()->createOrderAgent($order);
// $orderAgent->updatePayment($paymentStatus);
if ($order && $order->getOrderState() == $order::ORDER_STATE_COMMITTED) {
$session = $this->get("session");
$session->set("last_order_id", $order->getId());
$this->view->goto = $this->generateUrl('checkout', ['action' => 'completed', 'language' => $language]);
} else {
$this->view->goto = $this->generateUrl('checkout', ['action' => 'confirm', 'language' => $language, 'error' => strip_tags($request->get('mode'))]);
}
} catch (\Exception $e) {
$this->view->goto = $this->generateUrl('checkout', ['action' => 'confirm', 'language' => $language, 'error' => $e->getMessage()]);
return;
}
}
}