Skip to content

Commit

Permalink
checking response signature
Browse files Browse the repository at this point in the history
  • Loading branch information
vladdnepr committed Jul 1, 2019
1 parent 561c7d4 commit bc168bf
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 63 deletions.
8 changes: 8 additions & 0 deletions src/Exception/SignatureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php


namespace WayForPay\SDK\Exception;

class SignatureException extends \RuntimeException
{
}
50 changes: 43 additions & 7 deletions src/Request/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use WayForPay\SDK\Domain\Reason;
use WayForPay\SDK\Endpoint\ApiEndpoint;
use WayForPay\SDK\Exception\ApiException;
use WayForPay\SDK\Exception\SignatureException;

abstract class ApiRequest implements TransactionRequestInterface
{
Expand Down Expand Up @@ -44,25 +45,33 @@ public function getTransactionData()
return array(
'transactionType' => $this->getTransactionType(),
'merchantAccount' => $this->credential->getAccount(),
'merchantSignature' => $this->getSignature(),
'merchantSignature' => $this->getSignature(
$this->getRequestSignatureFieldsRequired(),
$this->getRequestSignatureFieldsValues()
),
'apiVersion' => self::API_VERSION,
);
}

public function getSignatureFieldsRequired()
public function getRequestSignatureFieldsRequired()
{
return array(
'merchantAccount',
);
}

public function getSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
public function getRequestSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
{
return array(
'merchantAccount' => $this->credential->getAccount(),
);
}

public function getResponseSignatureFieldsRequired()
{
return array();
}

/**
* @return EndpointInterface|ApiEndpoint
*/
Expand Down Expand Up @@ -121,14 +130,41 @@ public function send()
}
}

abstract public function getResponseClass();

/**
* @return string
* @param array $data
* @return ResponseInterface
* @throws \Exception
*/
public function getSignature()
public function getResponse(array $data)
{
$fieldsRequired = $this->getSignatureFieldsRequired();
$fieldsValues = $this->getSignatureFieldsValues();
if ($signatureRequired = $this->getResponseSignatureFieldsRequired()) {
$expected = $this->getSignature(
$signatureRequired,
array_intersect_key($data, array_flip($signatureRequired))
);

if (!isset($data['merchantSignature'])
|| $expected !== $data['merchantSignature']
) {
throw new SignatureException(
'Response signature mismatch: expected ' . $expected . ', got ' . $data['merchantSignature']
);
}
}

$class = $this->getResponseClass();
return new $class($data);
}

/**
* @param array $fieldsRequired
* @param array $fieldsValues
* @return string
*/
protected function getSignature(array $fieldsRequired, array $fieldsValues)
{
$data = array();
$error = array();

Expand Down
32 changes: 20 additions & 12 deletions src/Request/ChargeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use DateTime;
use WayForPay\SDK\Collection\ProductCollection;
use WayForPay\SDK\Contract\ResponseInterface;
use WayForPay\SDK\Credential\AccountSecretCredential;
use WayForPay\SDK\Domain\Card;
use WayForPay\SDK\Domain\CardToken;
Expand Down Expand Up @@ -200,9 +199,9 @@ public function __construct(
$this->socialUri = strval($socialUri);
}

public function getSignatureFieldsRequired()
public function getRequestSignatureFieldsRequired()
{
return array_merge(parent::getSignatureFieldsRequired(), array(
return array_merge(parent::getRequestSignatureFieldsRequired(), array(
'merchantDomainName',
'orderReference',
'orderDate',
Expand All @@ -212,9 +211,9 @@ public function getSignatureFieldsRequired()
));
}

public function getSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
public function getRequestSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
{
return array_merge(parent::getSignatureFieldsValues($charset), array(
return array_merge(parent::getRequestSignatureFieldsValues($charset), array(
'merchantDomainName' => $this->merchantDomainName,
'orderReference' => $this->orderReference,
'orderDate' => $this->orderDate->getTimestamp(),
Expand All @@ -224,6 +223,20 @@ public function getSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
));
}

public function getResponseSignatureFieldsRequired()
{
return array(
'merchantAccount',
'orderReference',
'amount',
'currency',
'authCode',
'cardPan',
'transactionStatus',
'reasonCode',
);
}

public function getTransactionType()
{
return 'CHARGE';
Expand Down Expand Up @@ -277,13 +290,8 @@ public function getTransactionData()
return array_filter($data);
}

/**
* @param array $data
* @return ResponseInterface|ChargeResponse
* @throws \Exception
*/
public function getResponse(array $data)
public function getResponseClass()
{
return new ChargeResponse($data);
return ChargeResponse::getClass();
}
}
32 changes: 20 additions & 12 deletions src/Request/CheckRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace WayForPay\SDK\Request;

use WayForPay\SDK\Contract\ResponseInterface;
use WayForPay\SDK\Credential\AccountSecretCredential;
use WayForPay\SDK\Response\CheckResponse;

Expand All @@ -25,20 +24,34 @@ public function __construct(AccountSecretCredential $credential, $orderReference
$this->orderReference = $orderReference;
}

public function getSignatureFieldsRequired()
public function getRequestSignatureFieldsRequired()
{
return array_merge(parent::getSignatureFieldsRequired(), array(
return array_merge(parent::getRequestSignatureFieldsRequired(), array(
'orderReference',
));
}

public function getSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
public function getRequestSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
{
return array_merge(parent::getSignatureFieldsValues($charset), array(
return array_merge(parent::getRequestSignatureFieldsValues($charset), array(
'orderReference' => $this->orderReference,
));
}

public function getResponseSignatureFieldsRequired()
{
return array(
'merchantAccount',
'orderReference',
'amount',
'currency',
'authCode',
'cardPan',
'transactionStatus',
'reasonCode',
);
}

public function getTransactionType()
{
return 'CHECK_STATUS';
Expand All @@ -51,13 +64,8 @@ public function getTransactionData()
));
}

/**
* @param array $data
* @return ResponseInterface|CheckResponse
* @throws \Exception
*/
public function getResponse(array $data)
public function getResponseClass()
{
return new CheckResponse($data);
return CheckResponse::getClass();
}
}
10 changes: 2 additions & 8 deletions src/Request/Complete3DSRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace WayForPay\SDK\Request;

use WayForPay\SDK\Contract\ResponseInterface;
use WayForPay\SDK\Credential\AccountSecretCredential;
use WayForPay\SDK\Response\Complete3DSResponse;

Expand Down Expand Up @@ -53,13 +52,8 @@ public function getTransactionData()
);
}

/**
* @param array $data
* @return ResponseInterface|Complete3DSResponse
* @throws \Exception
*/
public function getResponse(array $data)
public function getResponseClass()
{
return new Complete3DSResponse($data);
return Complete3DSResponse::getClass();
}
}
28 changes: 16 additions & 12 deletions src/Request/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace WayForPay\SDK\Request;

use WayForPay\SDK\Contract\ResponseInterface;
use WayForPay\SDK\Credential\AccountSecretCredential;
use WayForPay\SDK\Response\RufundResponse;

Expand Down Expand Up @@ -48,24 +47,34 @@ public function __construct(
$this->comment = $comment;
}

public function getSignatureFieldsRequired()
public function getRequestSignatureFieldsRequired()
{
return array_merge(parent::getSignatureFieldsRequired(), array(
return array_merge(parent::getRequestSignatureFieldsRequired(), array(
'orderReference',
'amount',
'currency'
));
}

public function getSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
public function getRequestSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
{
return array_merge(parent::getSignatureFieldsValues($charset), array(
return array_merge(parent::getRequestSignatureFieldsValues($charset), array(
'orderReference' => $this->orderReference,
'amount' => $this->amount,
'currency' => $this->currency
));
}

public function getResponseSignatureFieldsRequired()
{
return array(
'merchantAccount',
'orderReference',
'transactionStatus',
'reasonCode',
);
}

public function getTransactionType()
{
return 'REFUND';
Expand All @@ -81,13 +90,8 @@ public function getTransactionData()
));
}

/**
* @param array $data
* @return ResponseInterface|RufundResponse
* @throws \Exception
*/
public function getResponse(array $data)
public function getResponseClass()
{
return new RufundResponse($data);
return RufundResponse::getClass();
}
}
18 changes: 6 additions & 12 deletions src/Request/TransactionListRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace WayForPay\SDK\Request;

use DateTime;
use WayForPay\SDK\Contract\ResponseInterface;
use WayForPay\SDK\Credential\AccountSecretCredential;
use WayForPay\SDK\Response\TransactionListResponse;

Expand Down Expand Up @@ -32,17 +31,17 @@ public function __construct(AccountSecretCredential $credential, DateTime $dateB
$this->dateEnd = $dateEnd;
}

public function getSignatureFieldsRequired()
public function getRequestSignatureFieldsRequired()
{
return array_merge(parent::getSignatureFieldsRequired(), array(
return array_merge(parent::getRequestSignatureFieldsRequired(), array(
'dateBegin',
'dateEnd',
));
}

public function getSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
public function getRequestSignatureFieldsValues($charset = self::DEFAULT_CHARSET)
{
return array_merge(parent::getSignatureFieldsValues($charset), array(
return array_merge(parent::getRequestSignatureFieldsValues($charset), array(
'dateBegin' => $this->dateBegin->getTimestamp(),
'dateEnd' => $this->dateEnd->getTimestamp(),
));
Expand All @@ -61,13 +60,8 @@ public function getTransactionData()
));
}

/**
* @param array $data
* @return ResponseInterface|TransactionListResponse
* @throws \Exception
*/
public function getResponse(array $data)
public function getResponseClass()
{
return new TransactionListResponse($data);
return TransactionListResponse::getClass();
}
}
8 changes: 8 additions & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ public function getReason()
{
return $this->reason;
}

/**
* @return string
*/
public static function getClass()
{
return get_called_class();
}
}

0 comments on commit bc168bf

Please sign in to comment.