Skip to content

Commit c80faa6

Browse files
committed
"Hypercharge Payment Plugin" "2.2.6" : "log hypercharge orders and recover the canceled orders if they were created ok on Hypercharge // 2016-02-08"
1 parent 052b381 commit c80faa6

3 files changed

Lines changed: 165 additions & 35 deletions

File tree

Frontend/HyperchargePaymentWpf/Bootstrap.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function install() {
3939
$this->createEvents();
4040
$this->createCronJobs();
4141
$this->createEmails();
42+
$this->createTables();
4243

4344
return array('success' => true, 'invalidateCache' => array('backend', 'proxy'));
4445
}
@@ -80,6 +81,7 @@ public function update($version) {
8081
*/
8182
$this->createCronJobs();
8283
$this->createEmails();
84+
$this->createTables();
8385

8486
return array('success' => true, 'invalidateCache' => array('backend', 'proxy'));
8587
}
@@ -833,6 +835,28 @@ private function createEmails(){
833835
Shopware() -> Models() -> flush();
834836
}
835837

838+
/**
839+
* create Hypercharge tables
840+
*/
841+
private function createTables(){
842+
$sqls = array();
843+
$sqls[] = "CREATE TABLE IF NOT EXISTS `hypercharge_orders` (
844+
`id` int(11) NOT NULL AUTO_INCREMENT,
845+
`sessionId` varchar(255) collate utf8_unicode_ci NOT NULL default '',
846+
`transactionId` varchar(255) collate utf8_unicode_ci NOT NULL default '' ,
847+
`uniquePaymentId` varchar(255) collate utf8_unicode_ci NOT NULL default '',
848+
`uniqueId` varchar(255) collate utf8_unicode_ci NULL ,
849+
`status` int(11) NOT NULL default '0',
850+
`date` timestamp NOT NULL default CURRENT_TIMESTAMP,
851+
PRIMARY KEY (`id`),
852+
KEY `sessionId` (`sessionId`)
853+
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
854+
";
855+
856+
foreach ($sqls as $sql)
857+
Shopware()->Db()->exec($sql);
858+
}
859+
836860
/**
837861
* send warning email about the cancelled orders
838862
* @param Shopware_Components_Cron_CronJob $job

Frontend/HyperchargePaymentWpf/Controllers/frontend/Hyperchargewpf.php

Lines changed: 139 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public function indexAction() {
4040
}
4141
if (substr($payment_name, 0, 17) == 'hyperchargemobile') {
4242
//for Mobile API we already have the transaction, we just need to save the order
43-
$session = Shopware()->Session();
44-
$uniquePaymentId = $session->nfxUniquePaymentID;
45-
$transactionId = $session->nfxTransactionID;
46-
47-
$session->offsetUnset('nfxUniquePaymentID');
48-
$session->offsetUnset('nfxTransactionID');
43+
$sql = "SELECT * FROM hypercharge_orders WHERE sessionId = ? AND status = 0 ORDER BY id DESC LIMIT 1";
44+
$trn = Shopware()->Db()->fetchRow($sql, array(Shopware()->SessionID()));
45+
$uniquePaymentId = $trn["uniquePaymentId"];
46+
$transactionId = $trn["transactionId"];
4947
if (!$uniquePaymentId || !$transactionId) {
5048
$plugin->logAction("Invalid session params");
5149
//$this->redirect(array('controller' => 'checkout'));
@@ -113,6 +111,12 @@ public function indexAction() {
113111

114112
$uniquePaymentId = $this->createPaymentUniqueId();
115113
$transactionId = $this->createPaymentUniqueId();
114+
Shopware()->Db()->insert('hypercharge_orders',
115+
array(
116+
'sessionId' => Shopware()->SessionID(),
117+
'transactionId' => $transactionId,
118+
'uniquePaymentId' => $uniquePaymentId
119+
));
116120
$return_success_url = $router->assemble(
117121
array('action' => 'success', 'forceSecure' => true))
118122
. '?uniquePaymentID=' . $uniquePaymentId
@@ -165,6 +169,7 @@ public function indexAction() {
165169
'country' => $user['additional']['country']['countryiso']
166170
)
167171
);
172+
168173
if($user['billingaddress']['phone']){
169174
$paymentData['customer_phone'] = $user['billingaddress']['phone'];
170175
}
@@ -238,12 +243,7 @@ public function indexAction() {
238243
}
239244
$plugin->logAction("$payment_name response received\n"
240245
. print_r($paymentH, true));
241-
242-
if($config->transactionId == "uniqueId"){
243-
//we will change transactionID = uniqueID
244-
Shopware()->Session()->nfxUniqueID = $paymentH->unique_id;
245-
$plugin->logAction(sprintf('The transactionId will be changed from %s to %s', $transactionId, $paymentH->unique_id));
246-
}
246+
Shopware()->Db()->update("hypercharge_orders", array('uniqueId' => $paymentH->unique_id), "sessionId = '" . Shopware()->SessionID() . "' AND transactionId = '" . $transactionId . "' AND uniquePaymentId = '" . $uniquePaymentId . "'");
247247

248248
mb_internal_encoding($initial_encoding);
249249
// Redirect for WPF
@@ -294,28 +294,27 @@ public function successAction() {
294294
Shopware()->Session()->offsetUnset("nfxPayolutionAgree");
295295
$request = $this->Request();
296296
$plugin = $this->Plugin();
297+
$config = $plugin->Config();
297298
$plugin->logAction("SUCCESS:");
298299
foreach ($request->getParams() as $key => $value) {
299300
$plugin->logAction("\t$key => $value");
300301
}
301302
$transactionId = $request->getParam('transactionID');
302-
if(Shopware()->Session()->nfxUniqueID){
303-
$uniqueId = Shopware()->Session()->nfxUniqueID;
303+
$uniquePaymentId = $request->getParam('uniquePaymentID');
304+
$transaction_id_field = "transactionId";
305+
if($config->transactionId == "uniqueId"){
306+
$sql = "SELECT uniqueId FROM hypercharge_orders WHERE transactionId = ? AND uniquePaymentId = ?";
307+
$uniqueId = Shopware()->Db()->fetchOne($sql, array($transactionId, $uniquePaymentId));
304308
$plugin->logAction(sprintf('The transactionId is changed from %s to %s', $transactionId, $uniqueId));
305-
Shopware()->Session()->offsetUnset("nfxUniqueID");
306309
$transactionId = $uniqueId;
310+
$transaction_id_field = "uniqueId";
307311
}
308-
$this->saveOrder($transactionId, $request->getParam('uniquePaymentID'), null, true);
312+
$this->saveOrder($transactionId, $uniquePaymentId, null, true);
313+
Shopware()->Db()->update("hypercharge_orders", array('status' => 1), $transaction_id_field . " = '" . $transactionId . "' AND uniquePaymentId = '" . $uniquePaymentId . "'");
309314
$this->redirect(array('controller' => 'checkout',
310315
'action' => 'finish',
311-
'sUniqueID' => $request->getParam('uniquePaymentID'),
316+
'sUniqueID' => $uniquePaymentId,
312317
'sAGB' => 1));
313-
/*$this->forward("finish", "checkout", 'frontend', array(
314-
'sUniqueID' => $request->getParam('uniquePaymentID'),
315-
'sAGB' => 1,
316-
'appendSession' => true,
317-
'forceSecure' => true
318-
));*/
319318
}
320319

321320
/**
@@ -361,6 +360,7 @@ public function failedAction() {
361360
//$this->View()->nfxErrorMessage = Shopware()->Session()->nfxErrorMessage;
362361
}
363362
}
363+
Shopware()->Db()->update("hypercharge_orders", array('status' => -1), "sessionId = '" . Shopware()->SessionID() . "' AND status = 0");
364364
//we want to display the error message above the payment method
365365
Shopware()->Session()->nfxFailedAction = true;
366366
$this->redirect(array('controller' => 'checkout', 'action' => 'confirm','forceSecure' => true));
@@ -424,7 +424,7 @@ public function notifyAction() {
424424
// Identify notification channel
425425
$transactionH = $notification->getTransaction();
426426
$plugin->logAction(sprintf('transaction type: %s', $transactionH->transaction_type));
427-
427+
428428
$try = 1;
429429
//sometimes Hypercharge is faster than SW => we try more times to check the order
430430
while($try<4){
@@ -463,11 +463,33 @@ public function notifyAction() {
463463
}
464464
}
465465

466+
if (!$orderId) {
467+
//check if it is a cancelled order
468+
$sql = "SELECT o.id AS orderId
469+
FROM hypercharge_orders AS ho
470+
JOIN s_order o ON ho.sessionId = o.temporaryID
471+
WHERE ho.transactionId = ?
472+
AND ho.uniquePaymentId = ?
473+
AND ho.uniqueId = ?
474+
AND o.status = -1";
475+
$orderId = Shopware()->Db()->fetchOne($sql, array($transactionId, $paymentId, $uniqueId));
476+
if($orderId){
477+
$plugin->logAction(sprintf('The order having payment id %s , transaction id %s and unique id %s is a cancelled order', $paymentId, $transactionId, $uniqueId));
478+
if($config->transactionId == "uniqueId"){
479+
$plugin->logAction(sprintf('The transactionId is changed from %s to %s', $transactionId, $uniqueId));
480+
$transaction_id_field = "uniqueId";
481+
}
482+
$orderId = $this->convertOrder($orderId, ($transaction_id_field == "uniqueId")? $uniqueId: $transactionId, $paymentId);
483+
if($orderId){
484+
Shopware()->Db()->update("hypercharge_orders", array('status' => 1), "transactionId = '" . $transactionId . "' AND uniquePaymentId = '" . $paymentId . "'");
485+
}
486+
}
487+
}
466488
if (!$orderId) {
467489
if($config->transactionId == "uniqueId"){
468-
$plugin->logAction(sprintf('The order having payment id %s and transaction %s or %s does not exist', $paymentId, $transactionId, $uniqueId));
490+
$plugin->logAction(sprintf('The order having payment id %s and transaction id %s or %s does not exist', $paymentId, $transactionId, $uniqueId));
469491
} else {
470-
$plugin->logAction(sprintf('The order having payment id %s and transaction %s does not exist', $paymentId, $transactionId));
492+
$plugin->logAction(sprintf('The order having payment id %s and transaction id %s does not exist', $paymentId, $transactionId));
471493
}
472494
exit();
473495
}
@@ -591,6 +613,12 @@ public function hyperchargeMobileAction() {
591613

592614
$uniquePaymentId = $this->createPaymentUniqueId();
593615
$transactionId = $this->createPaymentUniqueId();
616+
Shopware()->Db()->insert('hypercharge_orders',
617+
array(
618+
'sessionId' => Shopware()->SessionID(),
619+
'transactionId' => $transactionId,
620+
'uniquePaymentId' => $uniquePaymentId
621+
));
594622
$hyperchargeTransactionId = $transactionId . ' ' . $uniquePaymentId;
595623
$hyperchargeTransactionId = \Hypercharge\Helper::appendRandomId($hyperchargeTransactionId);
596624
$paymentData = array(
@@ -613,6 +641,7 @@ public function hyperchargeMobileAction() {
613641
'country' => $user['additional']['country']['countryiso']
614642
)
615643
);
644+
616645
if($user['billingaddress']['phone']){
617646
$paymentData['customer_phone'] = $user['billingaddress']['phone'];
618647
}
@@ -669,14 +698,7 @@ public function hyperchargeMobileAction() {
669698
$plugin->logAction("$payment_name response received\n"
670699
. print_r($paymentH, true));
671700

672-
Shopware()->Session()->nfxUniquePaymentID = $uniquePaymentId;
673-
Shopware()->Session()->nfxTransactionID = $transactionId;
674-
if($config->transactionId == "uniqueId"){
675-
//we will change transactionID = uniqueID
676-
Shopware()->Session()->nfxUniqueID = $paymentH->unique_id;
677-
$plugin->logAction(sprintf('The transactionId will be changed from %s to %s', $transactionId, $paymentH->unique_id));
678-
}
679-
701+
Shopware()->Db()->update("hypercharge_orders", array('uniqueId' => $paymentH->unique_id), "sessionId = '" . Shopware()->SessionID() . "' AND transactionId = '" . $transactionId . "' AND uniquePaymentId = '" . $uniquePaymentId . "'");
680702
echo json_encode(array(
681703
"success" => true,
682704
"redirect_url" => $paymentH->redirect_url,
@@ -771,6 +793,89 @@ public static function normalizeExport($val) {
771793
}
772794
return iconv(mb_internal_encoding(), 'utf-8', $val);
773795
}
796+
797+
/**
798+
* convert a cancelled order into a real order
799+
* @param null $orderId
800+
* @param type $transactionId
801+
* @param type $temporaryId
802+
* @return null
803+
* @throws Exception
804+
*/
805+
private function convertOrder($orderId, $transactionId, $temporaryId){
806+
try{
807+
$plugin = $this->Plugin();
808+
Shopware()->Models()->clear();
809+
// Get user, shipping and billing
810+
$builder = Shopware()->Models()->createQueryBuilder();
811+
$builder->select(array('orders', 'customer', 'billing', 'payment', 'shipping'))
812+
->from('Shopware\Models\Order\Order', 'orders')
813+
->leftJoin('orders.customer', 'customer')
814+
->leftJoin('orders.payment', 'payment')
815+
->leftJoin('customer.billing', 'billing')
816+
->leftJoin('customer.shipping', 'shipping')
817+
->where("orders.id = ?1")
818+
->setParameter(1, $orderId);
819+
820+
$result = $builder->getQuery()->getArrayResult();
821+
// Check requiered fields
822+
if (empty($result) || $result[0]['customer'] === null || $result[0]['customer']['billing'] === null) {
823+
throw new Exception('Could not get required customer data');
824+
}
825+
// Get ordernumber
826+
$numberRepository = Shopware()->Models()->getRepository('Shopware\Models\Order\Number');
827+
$numberModel = $numberRepository->findOneBy(array('name' => 'invoice'));
828+
if ($numberModel === null) {
829+
throw new Exception('Could not get ordernumber');
830+
}
831+
$newOrderNumber = $numberModel->getNumber() + 1;
832+
833+
// Set new ordernumber
834+
$numberModel->setNumber($newOrderNumber);
835+
836+
// set new ordernumber to the order
837+
$orderModel = Shopware()->Models()->find('Shopware\Models\Order\Order', $orderId);
838+
$orderModel->setNumber($newOrderNumber);
839+
840+
// set new ordernumber to order details
841+
$orderDetailRepository = Shopware()->Models()->getRepository('Shopware\Models\Order\Detail');
842+
$orderDetailModel = $orderDetailRepository->findOneBy(array('orderId' => $orderId));
843+
$orderDetailModel->setNumber($newOrderNumber);
844+
845+
// If there is no shipping address, set billing address to be the shipping address
846+
if ($result[0]['customer']['shipping'] === null) {
847+
$result[0]['customer']['shipping'] = $result[0]['customer']['billing'];
848+
}
849+
850+
// Create new entry in s_order_billingaddress
851+
$billingModel = new Shopware\Models\Order\Billing();
852+
$billingModel->fromArray($result[0]['customer']['billing']);
853+
$billingModel->setCountry(Shopware()->Models()->find('Shopware\Models\Country\Country', $result[0]['customer']['billing']['countryId']));
854+
$billingModel->setCustomer(Shopware()->Models()->find('Shopware\Models\Customer\Customer', $result[0]['customer']['billing']['customerId']));
855+
$billingModel->setOrder($orderModel);
856+
Shopware()->Models()->persist($billingModel);
857+
858+
// Create new entry in s_order_shippingaddress
859+
$shippingModel = new Shopware\Models\Order\Shipping();
860+
$shippingModel->fromArray($result[0]['customer']['shipping']);
861+
$shippingModel->setCountry(Shopware()->Models()->find('Shopware\Models\Country\Country', $result[0]['customer']['shipping']['countryId']));
862+
$shippingModel->setCustomer(Shopware()->Models()->find('Shopware\Models\Customer\Customer', $result[0]['customer']['shipping']['customerId']));
863+
$shippingModel->setOrder($orderModel);
864+
Shopware()->Models()->persist($shippingModel);
865+
866+
// Finally set the order to be a regular order
867+
$statusModel = Shopware()->Models()->find('Shopware\Models\Order\Status', 0);
868+
$orderModel->setOrderStatus($statusModel);
869+
$orderModel->setTransactionId($transactionId);
870+
$orderModel->setTemporaryId($temporaryId);
871+
872+
Shopware()->Models()->flush();
873+
} catch (Exception $ex) {
874+
$orderId = null;
875+
$plugin->logAction("ERROR: " . $ex->getMessage());
876+
}
877+
return $orderId;
878+
}
774879

775880
/**
776881
* return an instance to HyperchargePaymentWpf plugin

Frontend/HyperchargePaymentWpf/plugin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
"support": "support@nfxmedia.de",
1111
"description": "<p><img src='data:image/png;base64,<%IMG%>' /></p><p style='font-size:12px; font-weight: bold;'>Hypercharge WebPaymentForm allows you to use a variety of payment methods, both online and off-line. Any sensitive information is safely aquired an processed on out platform</p>",
1212

13-
"currentVersion": "2.2.5",
13+
"currentVersion": "2.2.6",
1414

1515
"changelog": {
1616
"de": {
1717
},
1818
"en": {
19+
"2.2.6" : "log hypercharge orders and recover the canceled orders if they were created ok on Hypercharge // 2016-02-08",
1920
"2.2.5" : "imporve the logging // 2016-02-01",
2021
"2.2.4" : "create weekly emails about the cancelled orders // 2016-01-29",
2122
"2.2.3" : "check many times the order existence in notification // 2016-01-28",

0 commit comments

Comments
 (0)