Skip to content

Commit a734cba

Browse files
author
Max Görtz
committed
Change the flow of the payment with QuickPay
A generated orderId and the QuickPay paymentId are now used as Shopware transactionId and paymentUniqueId. Writing the order to the database was moved to the successAction to prevent premature ending of the the ordering process. Once a payment id is received from Quickpay it is stored in the session until the order is finished. Identification of the order in the callbackAction is accordingly done by using the order_id and id of the received payment object.
1 parent 7f1300e commit a734cba

File tree

2 files changed

+108
-50
lines changed

2 files changed

+108
-50
lines changed

Components/QuickPayService.php

+54-16
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,69 @@
22

33
namespace QuickPayPayment\Components;
44

5+
use Exception;
6+
use Shopware\Components\Random;
7+
use function Shopware;
8+
59
class QuickPayService
610
{
711
private $baseUrl = 'https://api.quickpay.net';
812

913
const METHOD_POST = 'POST';
1014
const METHOD_PUT = 'PUT';
1115
const METHOD_GET = 'GET';
16+
const METHOD_PATCH = 'PATCH';
1217

1318
/**
1419
* Create payment
1520
*
1621
* @param $orderId
17-
* @param $currency
22+
* @param $parameters
1823
* @return mixed
1924
*/
20-
public function createPayment($parameters)
25+
public function createPayment($orderId, $parameters)
2126
{
27+
$parameters['order_id'] = $orderId;
28+
2229
//Create payment
2330
$payment = $this->request(self::METHOD_POST, '/payments', $parameters);
2431

2532
return $payment;
2633
}
2734

35+
/**
36+
* Create payment
37+
*
38+
* @param $paymentId
39+
* @param $parameters
40+
* @return mixed
41+
*/
42+
public function updatePayment($paymentId, $parameters)
43+
{
44+
$resource = sprintf('/payments/%s', $paymentId);
45+
46+
//Update payment
47+
$payment = $this->request(self::METHOD_PATCH, $resource, $parameters);
48+
49+
return $payment;
50+
}
51+
52+
/**
53+
* Get payment information
54+
*
55+
* @param $paymentId
56+
* @return mixed
57+
*/
58+
public function getPayment($paymentId)
59+
{
60+
$resource = sprintf('/payments/%s', $paymentId);
61+
62+
//Get payment
63+
$payment = $this->request(self::METHOD_GET, $resource);
64+
65+
return $payment;
66+
}
67+
2868
/**
2969
* Create payment link
3070
*
@@ -87,14 +127,14 @@ private function request($method = self::METHOD_POST, $resource, $params = [], $
87127

88128
//Validate reponsecode
89129
if (! in_array($responseCode, [200, 201, 202])) {
90-
throw new \Exception('Invalid gateway response ' . $result);
130+
throw new Exception('Invalid gateway response ' . $result);
91131
}
92132

93133
$response = json_decode($result);
94134

95135
//Check for JSON errors
96136
if (! $response || (json_last_error() !== JSON_ERROR_NONE)) {
97-
throw new \Exception('Invalid json response');
137+
throw new Exception('Invalid json response');
98138
}
99139

100140
return $response;
@@ -124,18 +164,6 @@ private function getApiKey()
124164
return Shopware()->Config()->getByNamespace('QuickPayPayment', 'public_key');
125165
}
126166

127-
/**
128-
* Create payment token
129-
*
130-
* @param float $amount
131-
* @param int $customerId
132-
* @return string
133-
*/
134-
public function createPaymentToken($amount, $customerId)
135-
{
136-
return md5(implode('|', [$amount, $customerId]));
137-
}
138-
139167
/**
140168
* Get language code
141169
*
@@ -147,4 +175,14 @@ private function getLanguageCode()
147175

148176
return substr($locale, 0, 2);
149177
}
178+
179+
/**
180+
* Creates a unique order id
181+
*
182+
* @return string
183+
*/
184+
public function createOrderId()
185+
{
186+
return Random::getAlphanumericString(20);
187+
}
150188
}

Controllers/Frontend/QuickPay.php

+54-34
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@ public function redirectAction()
1212
$service = $this->container->get('quickpay_payment.quickpay_service');
1313

1414
try {
15-
$user = $this->getUser();
16-
$billing = $user['billingaddress'];
17-
18-
$paymentId = $this->createPaymentUniqueId();
19-
$token = $service->createPaymentToken($this->getAmount(), $billing['customernumber']);
20-
21-
//Save order and grab ordernumber
22-
$orderNumber = $this->saveOrder($paymentId, $token, \Shopware\Models\Order\Status::PAYMENT_STATE_OPEN);
23-
24-
//Save orderNumber to session
25-
Shopware()->Session()->offsetSet('quickpay_order_id', $orderNumber);
26-
Shopware()->Session()->offsetSet('quickpay_order_token', $token);
27-
2815
$paymentParameters = [
29-
'order_id' => $orderNumber,
3016
'currency' => $this->getCurrencyShortName(),
31-
'variables' => [
32-
'payment_id' => $paymentId,
33-
'token' => $token
34-
],
3517
];
36-
37-
//Create payment
38-
$payment = $service->createPayment($paymentParameters);
39-
18+
19+
//Save order and grab ordernumber
20+
$paymentId = Shopware()->Session()->offsetGet('quickpay_payment_id');
21+
if(empty($paymentId))
22+
{
23+
//Create new QuickPay payment
24+
$orderId = $service->createOrderId();
25+
26+
$payment = $service->createPayment($orderId, $paymentParameters);
27+
}
28+
else
29+
{
30+
//Update existing QuickPay payment
31+
$payment = $service->updatePayment($paymentId, $paymentParameters);
32+
}
33+
34+
// Save ID to session
35+
Shopware()->Session()->offsetSet('quickpay_payment_id', $payment->id);
36+
4037
$user = $this->getUser();
4138
$email = $user['additional']['user']['email'];
4239

@@ -49,14 +46,7 @@ public function redirectAction()
4946
$this->getCancelUrl(),
5047
$this->getCallbackUrl()
5148
);
52-
53-
$repository = Shopware()->Models()->getRepository(\Shopware\Models\Order\Order::class);
54-
$order = $repository->findOneBy(array(
55-
'number' => $orderNumber
56-
));
57-
$order->getAttribute()->setQuickpayPaymentLink($paymentLink);
58-
Shopware()->Models()->flush($order->getAttribute());
59-
49+
6050
$this->redirect($paymentLink);
6151
} catch (\Exception $e) {
6252
die($e->getMessage());
@@ -90,17 +80,17 @@ public function callbackAction()
9080
if (!$testmode && ($response->test_mode === true)) {
9181

9282
//Set order as cancelled
93-
$this->savePaymentStatus($response->variables->payment_id, $response->variables->token, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
83+
$this->savePaymentStatus($response->order_id, $response->id, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
9484
Shopware()->PluginLogger()->info("Order attempted paid with testcard while testmode was disabled");
9585
return;
9686
}
9787

9888
//Set order as reserved
99-
$this->savePaymentStatus($response->variables->payment_id, $response->variables->token, \Shopware\Models\Order\Status::PAYMENT_STATE_RESERVED);
89+
$this->savePaymentStatus($response->order_id, $response->id, \Shopware\Models\Order\Status::PAYMENT_STATE_RESERVED);
10090
}
10191
} else {
10292
//Cancel order
103-
$this->savePaymentStatus($response->variables->payment_id, $response->variables->token, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
93+
$this->savePaymentStatus($response->order_id, $response->id, \Shopware\Models\Order\Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED);
10494
Shopware()->PluginLogger()->info('Checksum mismatch');
10595
}
10696
}
@@ -111,6 +101,36 @@ public function callbackAction()
111101
*/
112102
public function successAction()
113103
{
104+
/** @var \QuickPayPayment\Components\QuickPayService $service */
105+
$service = $this->container->get('quickpay_payment.quickpay_service');
106+
107+
$paymentId = Shopware()->Session()->offsetGet('quickpay_payment_id');
108+
109+
if(empty($paymentId))
110+
{
111+
$this->redirect(['controller' => 'checkout', 'action' => 'confirm']);
112+
return;
113+
}
114+
115+
$payment = $service->getPayment($paymentId);
116+
if(empty($payment) || !isset($payment->order_id))
117+
{
118+
$this->redirect(['controller' => 'checkout', 'action' => 'confirm']);
119+
return;
120+
}
121+
122+
$orderNumber = $this->saveOrder($payment->order_id, $payment->id, \Shopware\Models\Order\Status::PAYMENT_STATE_OPEN);
123+
124+
$repository = Shopware()->Models()->getRepository(\Shopware\Models\Order\Order::class);
125+
$order = $repository->findOneBy(array(
126+
'number' => $orderNumber
127+
));
128+
$order->getAttribute()->setQuickpayPaymentLink($payment->link->url);
129+
Shopware()->Models()->flush($order->getAttribute());
130+
131+
//Remove ID from session
132+
Shopware()->Session()->offsetUnset('quickpay_payment_id');
133+
114134
//Redirect to finish
115135
$this->redirect(['controller' => 'checkout', 'action' => 'finish']);
116136

@@ -122,7 +142,7 @@ public function successAction()
122142
*/
123143
public function cancelAction()
124144
{
125-
$this->redirect(['controller' => 'checkout', 'action' => 'cancel']);
145+
$this->redirect(['controller' => 'checkout', 'action' => 'confirm']);
126146
}
127147

128148
/**

0 commit comments

Comments
 (0)