Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dotpay CHK parameter support and Symfony version requirments. #3

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions Controller/CallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use JMS\Payment\CoreBundle\Plugin\Exception\FinancialException;
use ETS\Payment\DotpayBundle\Event\DotpayConfirmationReceivedEvent;
use ETS\Payment\DotpayBundle\Event\Events as DotpayEvents;
use JMS\Payment\CoreBundle\PluginController\Result;

/*
* Copyright 2012 ETSGlobal <e4-devteam@etsglobal.org>
Expand Down Expand Up @@ -49,6 +50,10 @@ public function urlcAction(Request $request, PaymentInstruction $instruction)

$client = $this->get('payment.dotpay.client.token');
$logger = $this->get('logger');
$ppc = $this->get('payment.plugin_controller');

$t_id = $request->request->get('t_id');
$amount = (float) $request->get('amount');

// Check the PIN
$control = md5(sprintf(
Expand All @@ -67,35 +72,49 @@ public function urlcAction(Request $request, PaymentInstruction $instruction)
));

if ($control !== $request->request->get('md5')) {
$logger->err('[Dotpay - URLC] pin verification failed');
$logger->err('[Dotpay - URLC - ' . $t_id .'] pin verification failed');

return new Response('FAIL', 500);
return new Response('FAIL SIGNATURE', 500);
}

// Handling payment:
if (null === $transaction = $instruction->getPendingTransaction()) {
$logger->err('[Dotpay - URLC] no pending transaction found for the payment instruction');

return new Response('FAIL', 500);
}
if ($instruction->getAmount() > $instruction->getDepositedAmount()) {

$logger->err('[Dotpay - URLC - ' . $t_id .'] no pending transaction found for the payment instruction, creating new one');
$payment = $ppc->createPayment($instruction->getId(), $amount);
$ppc->approveAndDeposit($payment->getId(), $amount);
$transaction = $payment->getPendingTransaction();

if (null === $transaction) {
$logger->err('[Dotpay - URLC - ' . $t_id .'] error while creating new transaction');

$amountParts = explode(' ', $request->get('orginal_amount')); // Yes, the right parameter is 'orginal_amount'
$amount = (float) $amountParts[0]; // there is a typo error in the DotPay API
return new Response('FAIL CREATING NEW TRANSACTION', 500);
}

} else {
$logger->err('[Dotpay - URLC - ' . $t_id .'] unable to create new transaction, all of amount has been deposited');

return new Response('FAIL, TRANSACTION IS COMPLETED', 500);
}
}

$transaction->getExtendedData()->set('t_status', $request->get('t_status'));
$transaction->getExtendedData()->set('t_id', $request->get('t_id'));
$transaction->getExtendedData()->set('amount', $amount);

try {
$this->get('payment.plugin_controller')->approveAndDeposit($transaction->getPayment()->getId(), $amount);
$ppc->approveAndDeposit($transaction->getPayment()->getId(), $amount);
} catch (\Exception $e) {
$logger->err(sprintf('[Dotpay - URLC] %s', $e->getMessage()));
$logger->err(sprintf('[Dotpay - URLC - %s] %s', $t_id, $e->getMessage()));

return new Response('FAIL', 500);
return new Response('FAIL APPROVE AND DEPOSIT', 500);
}

$this->getDoctrine()->getEntityManager()->flush();
$this->getDoctrine()->getManager()->flush();

$logger->info(sprintf('[Dotpay - URLC] Payment instruction %s successfully updated', $instruction->getId()));
$logger->info(sprintf('[Dotpay - URLC - %s] Payment instruction %s successfully updated', $t_id, $instruction->getId()));

return new Response('OK');
}
Expand Down
13 changes: 13 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ public function getConfigTreeBuilder()
->end()
->end()
->scalarNode('return_url')->defaultNull()->end()
->booleanNode('chk')
->defaultFalse()
->end()
->booleanNode('recipientChk')
->defaultFalse()
->end()
->booleanNode('onlineTransfer')
->defaultFalse()
->end()
->integerNode('expirationTime')
->defaultValue(0)
->end()
->end()

->end()
->end()
->end();
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/ETSPaymentDotpayExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('payment.dotpay.direct.url', $config['direct']['url']);
$container->setParameter('payment.dotpay.direct.type', $config['direct']['type']);
$container->setParameter('payment.dotpay.direct.return_url', $config['direct']['return_url']);
$container->setParameter('payment.dotpay.direct.chk', $config['direct']['chk']);
$container->setParameter('payment.dotpay.direct.recipientChk', $config['direct']['recipientChk']);
$container->setParameter('payment.dotpay.direct.onlineTransfer', $config['direct']['onlineTransfer']);
$container->setParameter('payment.dotpay.direct.expirationTime', $config['direct']['expirationTime']);
}
}
135 changes: 131 additions & 4 deletions Plugin/DotpayDirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,48 @@ class DotpayDirectPlugin extends AbstractPlugin
*/
protected $type;

/**
* @var boolean
*/
protected $chk;

/**
* @var boolean
*/
protected $recipientChk;

/**
* @var boolean
*/
protected $onlineTransfer;

/**
* @var integer
*/
protected $expirationTime;


/**
* @param Router $router The router
* @param Token $token The client token
* @param String $stringTools The String tool package
* @param string $url The urlc
* @param integer $type The type
* @param string $returnUrl The return url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the parameter to the param list

* @param boolean $returnUrl Using DotPay CHK parameter, by default false
*/
public function __construct(Router $router, Token $token, String $stringTools, $url, $type, $returnUrl)
public function __construct(Router $router, Token $token, String $stringTools, $url, $type, $returnUrl, $chk = false, $recipientChk = false, $onlineTransfer = false, $expirationTime = 0)
{
$this->router = $router;
$this->token = $token;
$this->stringTools = $stringTools;
$this->returnUrl = $returnUrl;
$this->url = $url;
$this->type = $type;
$this->chk = $chk;
$this->recipientChk = $recipientChk;
$this->onlineTransfer = $onlineTransfer;
$this->expirationTime = $expirationTime;
}

/**
Expand Down Expand Up @@ -148,19 +174,29 @@ public function createDotpayRedirectActionException(FinancialTransactionInterfac

$datas = array(
'id' => $this->token->getId(),
'url' => $this->getReturnUrl($extendedData),
'URL' => $this->getReturnUrl($extendedData),
'URLC' => $urlc,
'type' => $this->type,
'onlinetransfer' => $this->onlineTransfer ? 1 : 0,

'amount' => $transaction->getRequestedAmount(),
'currency' => $instruction->getCurrency(),
'description' => sprintf('Payment Instruction #%d', $instruction->getId()),

'data_waznosci' => $this->expirationTime > 0 ? date('Y-m-d H:i:s', time() + $this->expirationTime * 60) : null,
'data_zapadalnosci' => null
);

$additionalDatas = array(
'street', 'phone', 'postcode', 'lastname',
'firstname', 'email', 'country', 'city', 'grupykanalow',
'street', 'phone', 'postcode', 'lastname', 'firstname',
'email', 'country', 'city', 'grupykanalow', 'street_n1', 'street_n2', 'description'
);
if ($this->recipientChk) {
$additionalDatas = array_merge($additionalDatas, array(
'recipientAccountNumber', 'recipientCompany', 'recipientFirstName', 'recipientLastName', 'recipientAddressStreet',
'recipientAddressBuilding', 'recipientAddressApartment', 'recipientAddressPostcode', 'recipientAddressCity'
));
}

foreach ($additionalDatas as $value) {
if ($extendedData->has($value)) {
Expand All @@ -172,10 +208,101 @@ public function createDotpayRedirectActionException(FinancialTransactionInterfac
$datas['lang'] = substr($extendedData->get('lang'), 0, 2);
}

if ($this->recipientChk) {
$datas['recipientChk'] = $this->generateRecipientChk($datas, $this->token->getPin());
}

if ($this->chk) {
$datas['chk'] = $this->generateChk($datas, $this->token->getPin());
}


$actionRequest->setAction(new VisitUrl($this->url . '?' . http_build_query($datas)));

return $actionRequest;
}

/**
* This method generates chk parameter user to sign request to dotpay
*
* @param array $datas
* @param string $pin
*/
protected function generateChk(array $datas, $pin)
{
$key = $datas['id'];
$key .= number_format($datas['amount'], 2, '.', '');
$key .= $datas['currency'];
$key .= rawurlencode($datas['description']);

if (isset($datas['control'])) {
$key .= $datas['control'];
}

$key .= $pin;

if (isset($datas['channel'])) {
$key .= $datas['channel'];

if (isset($datas['chlock'])) {
$key .= $datas['chlock'];
}
}

if (isset($datas['data_waznosci'])) {

if (isset($datas['data_zapadalnosci'])) {
$key .= $datas['data_zapadalnosci'];
}

$key .= $datas['data_waznosci'];
}

if (isset($datas['recipientChk'])) {
$key .= $datas['recipientChk'];
}


return md5($key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a blank line before the return statement

}

/**
* This method generates recipientChk parameter user to sign request with recipient data to dotpay
*
* @param array $datas
* @param string $pin
*/
protected function generateRecipientChk(array $datas, $pin)
{
$key = $datas['id'];
$key .= number_format($datas['amount'], 2, '.', '');
$key .= $datas['currency'];

if (isset($datas['control'])) {
$key .= $datas['control'];
}

$recipientFields = array(
'recipientAccountNumber',
'recipientCompany',
'recipientFirstName',
'recipientLastName',
'recipientAddressStreet',
'recipientAddressBuilding',
'recipientAddressApartment',
'recipientAddressPostcode',
'recipientAddressCity'
);
foreach ($recipientFields as $f) {
if (isset($datas[$f])) {
$key .= $datas[$f];
}
}

$key .= $pin;

return hash( 'sha256', $key);
}

/**
* This method executes an approve transaction.
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<argument>%payment.dotpay.direct.url%</argument>
<argument>%payment.dotpay.direct.type%</argument>
<argument>%payment.dotpay.direct.return_url%</argument>
<argument>%payment.dotpay.direct.chk%</argument>
<argument>%payment.dotpay.direct.recipientChk%</argument>
<argument>%payment.dotpay.direct.onlineTransfer%</argument>
<argument>%payment.dotpay.direct.expirationTime%</argument>
<tag name="payment.plugin" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion Tools/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class String
*/
public function normalize($text)
{
return preg_replace('/\pM*/u', '', normalizer_normalize($text, \Normalizer::FORM_D));
return preg_replace('/\pM*/u', '', \Normalizer::normalize($text, \Normalizer::FORM_D));
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": ">=2.0,<2.2-dev",
"symfony/framework-bundle": ">=2.0,<2.4",
"jms/payment-core-bundle": "dev-master"
},
"require-dev": {
Expand All @@ -23,4 +23,4 @@
"psr-0": { "ETS\\Payment\\DotpayBundle": "" }
},
"target-dir": "ETS/Payment/DotpayBundle"
}
}