Skip to content

Commit

Permalink
introduces a unique id in order to prevent 'fingerprint cannot be use…
Browse files Browse the repository at this point in the history
…d twice' when the payment was canceled before
  • Loading branch information
wd-chofer committed Apr 15, 2016
1 parent 39ee729 commit 87a0862
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Controller/Checkout/Back.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public function execute()

$this->_logger->debug(__METHOD__ . ':' . print_r($this->_request->getPost()->toArray(), true));

$this->_cart->getCustomerSession()->unsUniqueId();

if (!$this->_request->isPost()) {
throw new \Exception('Not a post request');
}
Expand Down Expand Up @@ -247,4 +249,4 @@ public function execute()
}


}
}
46 changes: 46 additions & 0 deletions Model/AbstractPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function initPaymentByCart(CheckoutCart $cart, $urls, \Magento\Framework\

$orderId = $quote->getReservedOrderId();
$init->setOrderReference(sprintf('%010d', $orderId));
$init->uniqueId = $this->_getUniqueId($cart);

$precision = 2;
$init->setAmount(round($cart->getQuote()->getBaseGrandTotal(), $precision))
Expand Down Expand Up @@ -295,6 +296,51 @@ protected function getUserDescription($quote)
$quote->getCustomerLastname());
}

/**
* Returns uniqueId - required for duplicate request check, if the transaction was canceled
*
* @param CheckoutCart $cart
*
* @return string
*/
protected function _getUniqueId($cart)
{
$uniqueId = $cart->getCustomerSession()->getUniqueId();
if (!strlen($uniqueId)) {
$uniqueId = $this->_generateUniqString();
$cart->getCustomerSession()->setUniqueId($uniqueId);
}

return $uniqueId;
}

/**
* returns a uniq String with default length 10.
*
* @param int $length
*
* @return string
*/
private function _generateUniqString($length = 10)
{
$tid = '';
$alphabet = "023456789abcdefghikmnopqrstuvwxyzABCDEFGHIKMNOPQRSTUVWXYZ";
for ($i = 0; $i < $length; $i ++) {
$c = substr($alphabet, mt_rand(0, strlen($alphabet) - 1), 1);
if (( ( $i % 2 ) == 0 ) && !is_numeric($c)) {
$i --;
continue;
}
if (( ( $i % 2 ) == 1 ) && is_numeric($c)) {
$i --;
continue;
}
$alphabet = str_replace($c, '', $alphabet);
$tid .= $c;
}
return $tid;
}

/**
* @param Quote $quote
*
Expand Down

0 comments on commit 87a0862

Please sign in to comment.