Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from heidelpay/develop
Browse files Browse the repository at this point in the history
php api
  • Loading branch information
devheidelpay authored Oct 26, 2016
2 parents 3a87b0e + 4cc653d commit eb8a84a
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 50 deletions.
3 changes: 2 additions & 1 deletion Controller/HgwAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Heidelpay\Gateway\Controller;
/**
* Abstract controller class
*
*
* @license Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
* @copyright Copyright © 2016-present Heidelberger Payment GmbH. All rights reserved.
* @link https://dev.heidelpay.de/magento
Expand All @@ -12,6 +12,7 @@
* @subpackage Magento2
* @category Magento2
*/

use Heidelpay\Gateway\Helper\Payment AS HeidelpayHelper;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
Expand Down
9 changes: 5 additions & 4 deletions Controller/Index/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @subpackage Magento2
* @category Magento2
*/

use Heidelpay\PhpApi\Response AS HeidelpayResponse;

class Redirect extends \Heidelpay\Gateway\Controller\HgwAbstract
{
Expand Down Expand Up @@ -49,8 +49,9 @@ public function execute()
$this->_logger->error('Heidelpay Redirect load transactions fail. '.$e->getMessage());
}

$HeidelpayResponse = new HeidelpayResponse($data);

if ($data !== NULL && $data['PROCESSING_RESULT'] == 'ACK'){
if ($data !== NULL && $HeidelpayResponse->isSuccess()){

/*
* Set Parameters for Success page
Expand Down Expand Up @@ -78,8 +79,8 @@ public function execute()

/* set QouteIds */
$session->setLastQuoteId($quoteId)
->setLastSuccessQuoteId($quoteId)
->clearHelperData();
->setLastSuccessQuoteId($quoteId);
//->clearHelperData();

/* set OrderIds */
$session->setLastOrderId($order->getId())
Expand Down
87 changes: 54 additions & 33 deletions Controller/Index/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

/**
* Notification handler for the payment response
*
*
* The heidelpay payment server will call this page directly after the payment
* process to send the result of the payment to your shop. Please make sure
* that this page is reachable form the Internet without any authentication.
* that this page is reachable form the Internet without any authentication.
*
* The controller use cryptographic methods to protect your shop in case of
* fake payment responses. The plugin can not take care of man in the middle attacks,
* so please make sure that you use https for the checkout process.
* The controller use cryptographic methods to protect your shop in case of
* fake payment responses. The plugin can not take care of man in the middle attacks,
* so please make sure that you use https for the checkout process.
*
* @license Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
* @copyright Copyright © 2016-present Heidelberger Payment GmbH. All rights reserved.
Expand All @@ -23,39 +23,57 @@
*/

use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Heidelpay\PhpApi\Response AS HeidelpayResponse;

class Response extends \Heidelpay\Gateway\Controller\HgwAbstract
{
protected $resultPageFactory;
protected $logger;


public function execute()
{
$Request = $this->getRequest();
$data = array();

/**
* Quit processing on an empty post response
*/

$data['PROCESSING_RESULT'] = $Request->getPOST('PROCESSING_RESULT');

$data['CRITERION_SECRET'] = $Request->getPost('CRITERION_SECRET');

$data['IDENTIFICATION_TRANSACTIONID'] = $Request->getPOST('IDENTIFICATION_TRANSACTIONID');


$HeidelpayResponse = new HeidelpayResponse($data);



$secret = $this->_encryptor->exportKeys();
$identificationTransactionId = $HeidelpayResponse->getIdentification()->getTransactionId();

$this->_logger->addDebug('Heidelpay response postdata : '.print_r($HeidelpayResponse,1));
$this->_logger->addDebug('Heidelpay $secret: '.print_r($secret,1));
$this->_logger->addDebug('Heidelpay $$identificationTransactionId: '.print_r($identificationTransactionId,1));
/*
* validate Hash to prevent manipulation
*/
$refernceHash = $this->_encryptor->getHash($data['IDENTIFICATION_TRANSACTIONID'].$this->_encryptor->exportKeys());
if (empty($data['IDENTIFICATION_TRANSACTIONID']) or empty($data['CRITERION_SECRET']) or $refernceHash !== $data['CRITERION_SECRET']) {
echo $this->_url->getUrl ( 'hgw/index/redirect', array (
'_forced_secure' => true,
'_store_to_url' => true,
'_nosid' => true
));
$this->_logger->critical("Heidelpay response form server " . $Request->getServer('REMOTE_ADDR') . " with an invalid hash. This could be some kind of manipulation.");
$this->_logger->critical('Heidelpay reference hash '.$refernceHash);
return;
};


$data['PROCESSING_RESULT'] = ($Request->getPOST('PROCESSING_RESULT') == 'ACK') ? 'ACK' : 'NOK';

try {
$HeidelpayResponse->verifySecurityHash($secret,$identificationTransactionId);
} catch (\Exception $e) {
$this->_logger->critical("Heidelpay response object fail " . $e->getMessage());
$this->_logger->critical("Heidelpay response object form server " . $Request->getServer('REMOTE_ADDR') . " with an invalid hash. This could be some kind of manipulation.");
$this->_logger->critical('Heidelpay reference object hash '.$HeidelpayResponse->getCriterion()->getSecretHash());
echo $this->_url->getUrl ( 'hgw/index/redirect', array (
'_forced_secure' => true,
'_store_to_url' => true,
'_nosid' => true
));
return ;
}

$data['IDENTIFICATION_TRANSACTIONID'] = (int)$Request->getPOST('IDENTIFICATION_TRANSACTIONID');
$data['PROCESSING_STATUS_CODE'] = (int)$Request->getPOST('PROCESSING_STATUS_CODE');
$data['PROCESSING_RETURN'] = $Request->getPOST('PROCESSING_RETURN');
Expand All @@ -77,8 +95,6 @@ public function execute()

$PaymentCode = $this->_paymentHelper->splitPaymentCode ($data['PAYMENT_CODE']);

$data['PAYMENT_METHODE'] = $PaymentCode[0];
$data['PAYMENT_TYPE'] = $PaymentCode[1];
$data['SOURCE'] = 'RESPONSE';

if ($data['PAYMENT_CODE'] == "PP.PA") {
Expand All @@ -88,15 +104,23 @@ public function execute()

}

$this->_logger->addDebug('Heidelpay response postdata : '.print_r($data,1));
$HeidelpayResponse->splitArray($data);


$paymentMethode = $PaymentCode[0];
$paymentType = $PaymentCode[1];

$this->_logger->addDebug('Heidelpay response postdata : '.print_r($HeidelpayResponse,1));



if ($data['PROCESSING_RESULT'] == 'ACK'){

if ($HeidelpayResponse->isSuccess()){

try {
$quote = $this->_objectManager->create('Magento\Quote\Model\Quote')->load($data['IDENTIFICATION_TRANSACTIONID']);
$quote->collectTotals();
//$this->_quoteObject()->save($quote);

/** in case of quest checkout */
if($data['CRITERION_GUEST'] === 'true') {
Expand All @@ -120,6 +144,8 @@ public function execute()
);
$order->save();



}

$url = $this->_url->getUrl ( 'hgw/index/redirect', array (
Expand All @@ -131,10 +157,10 @@ public function execute()
$this->_logger->addDebug('Heidelpay respose url : '.$url);
echo $url;

try {
try {
$model = $this->_objectManager->create('Heidelpay\Gateway\Model\Transaction');
$model->setData('payment_methode', $data['PAYMENT_METHODE']);
$model->setData('payment_type', $data['PAYMENT_TYPE']);
$model->setData('payment_methode', $paymentMethode);
$model->setData('payment_type', $paymentType);
$model->setData('transactionid', $data['IDENTIFICATION_TRANSACTIONID']);
$model->setData('uniqeid', $data['IDENTIFICATION_UNIQUEID']);
$model->setData('shortid', $data['IDENTIFICATION_SHORTID']);
Expand All @@ -146,13 +172,8 @@ public function execute()
$model->setData('source', $data['SOURCE']);
$model->save();
} catch (\Exception $e) {
$this->_logger->error('Heidelpay Response save transaction. '.$e->getMessage());
$this->_logger->error('Heidelpay Response save transaction error. '.$e->getMessage());
}






}
}
8 changes: 7 additions & 1 deletion PaymentMethodes/HeidelpayAbstractPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ public function getHeidelpayUrl($quote) {
$this->_encryptor->exportKeys() // A secret passphrase from your application
);

/** Magento Version
* @todo replace fixed shop version and plugin version
* */
$this->_heidelpayPaymentMethod->getRequest()->getCriterion()->set('SHOP.TYPE', 'Magento 2.x');
$this->_heidelpayPaymentMethod->getRequest()->getCriterion()->set('SHOPMODULE.VERSION', 'Heidelpay Gateway - 16.10.27');

/** @todo should be removed after using heidelpay php-api for every payment method */
$this->_heidelpayPaymentMethod->getRequest()->getCriterion()->set('secret',$this->_encryptor->getHash($quote->getId().$this->_encryptor->exportKeys()));
//$this->_heidelpayPaymentMethod->getRequest()->getCriterion()->set('secret',$this->_encryptor->getHash($quote->getId().$this->_encryptor->exportKeys()));

/** Force PhpApi to just generate the request instead of sending it directly */
$this->_heidelpayPaymentMethod->_dryRun=TRUE;
Expand Down
91 changes: 91 additions & 0 deletions PaymentMethodes/HeidelpayCreditCardPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
namespace Heidelpay\Gateway\PaymentMethodes ;
/**
* Heidelpay credit card payment method
*
* This is the payment class for heidelpay credit card
*
*
* @license Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
* @copyright Copyright © 2016-present Heidelberger Payment GmbH. All rights reserved.
* @link https://dev.heidelpay.de/magento
* @author Jens Richter
*
* @package Heidelpay
* @subpackage Magento2
* @category Magento2
*/
use \Heidelpay\PhpApi\PaymentMethodes\CreditCardPaymentMethod as HeidelpayPhpApiCreditCard;
use \Heidelpay\Gateway\PaymentMethodes\HeidelpayAbstractPaymentMethod;

class HeidelpayCreditCardPaymentMethod extends HeidelpayAbstractPaymentMethod
{
/**
* Payment Code
* @var string PayentCode
*/
const CODE = 'hgwcc';
/**
* Payment Code
* @var string PayentCode
*/
protected $_code = 'hgwcc';
/**
* isGateway
* @var boolean
*/
protected $_isGateway = true;
/**
* canAuthorize
* @var boolean
*/
protected $_canAuthorize = true;

/**
* Active redirect
*
* This function will return false, if the used payment method needs additional
* customer payment data to pursue.
* @return boolean
*/

public function activeRedirct() {
return false ;
}

/**
* Initial Request to heidelpay payment server to get the form / iframe url
* {@inheritDoc}
* @see \Heidelpay\Gateway\PaymentMethodes\HeidelpayAbstractPaymentMethod::getHeidelpayUrl()
*/

public function getHeidelpayUrl($quote) {

$this->_heidelpayPaymentMethod = new HeidelpayPhpApiCreditCard();

parent::getHeidelpayUrl($quote);

/** Force PhpApi to just generate the request instead of sending it directly */
$this->_heidelpayPaymentMethod->_dryRun = TRUE;


$url = explode( '/', $this->urlBuilder->getUrl('/', array('_secure' => true)));
$PaymentFrameOrigin = $url[0].'//'.$url[2];
$PreventAsyncRedirect = 'FALSE';
$CssPath = $this->_scopeConfig->getValue ("payment/hgwmain/default_css", \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->getStore() );


/** Set payment type to debit */
$this->_heidelpayPaymentMethod->debit($PaymentFrameOrigin, $PreventAsyncRedirect, $CssPath);

/** Prepare and send request to heidelpay */
$request = $this->_heidelpayPaymentMethod->getRequest()->prepareRequest();
$response = $this->_heidelpayPaymentMethod->getRequest()->send($this->_heidelpayPaymentMethod->getPaymentUrl(), $request);

return $response[0];



}

}
Loading

0 comments on commit eb8a84a

Please sign in to comment.