1 <?php
2 3 4
5 namespace Team3\PayU\SignatureCalculator;
6
7 use Psr\Log\LoggerInterface;
8 use Team3\PayU\Configuration\Credentials\CredentialsInterface;
9 use Team3\PayU\Order\Model\OrderInterface;
10 use Team3\PayU\SignatureCalculator\Encoder\Algorithms\AlgorithmInterface;
11 use Team3\PayU\SignatureCalculator\Encoder\EncoderInterface;
12 use Team3\PayU\SignatureCalculator\ParametersSorter\ParametersSorterInterface;
13
14 15 16 17 18 19
20 class OrderSignatureCalculator implements OrderSignatureCalculatorInterface
21 {
22 const SIGNATURE_FORMAT = 'signature=%s;algorithm=%s;sender=%s';
23
24 25 26
27 private $signatureCalculator;
28
29 30 31
32 private $parametersSorter;
33
34 35 36
37 private $logger;
38
39 40 41 42 43
44 public function __construct(
45 EncoderInterface $encoder,
46 ParametersSorterInterface $parametersSorter,
47 LoggerInterface $logger
48 ) {
49 $this->signatureCalculator = new SignatureCalculator($encoder);
50 $this->parametersSorter = $parametersSorter;
51 $this->logger = $logger;
52 }
53
54 55 56 57 58 59 60 61 62 63 64 65
66 public function calculate(
67 OrderInterface $order,
68 CredentialsInterface $credentials,
69 AlgorithmInterface $algorithm
70 ) {
71 $signature = $this->signatureCalculator->calculate(
72 $this->getSortedParameters($order),
73 $credentials,
74 $algorithm
75 );
76
77 $signature = sprintf(
78 self::SIGNATURE_FORMAT,
79 $signature,
80 $algorithm->getName(),
81 $credentials->getMerchantPosId()
82 );
83
84 $this->logCalculatedSignature($order, $signature);
85
86 return $signature;
87 }
88
89 90 91 92 93
94 private function getSortedParameters(OrderInterface $order)
95 {
96 return $this->parametersSorter->getSortedParameters($order);
97 }
98
99 100 101 102
103 private function logCalculatedSignature(OrderInterface $order, $signature)
104 {
105 $this
106 ->logger
107 ->debug(sprintf(
108 'Signature for order with id %s was calculated to "%s"',
109 $order->getOrderId(),
110 $signature
111 ));
112 }
113 }
114