1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eo\KeyClient; |
4
|
|
|
|
5
|
|
|
use Eo\KeyClient\Exception\KeyClientException; |
6
|
|
|
use Eo\KeyClient\Payment\PaymentRequestInterface; |
7
|
|
|
use Eo\KeyClient\Payment\PaymentResponse; |
8
|
|
|
use Eo\KeyClient\Payment\PaymentResponseInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Client |
13
|
|
|
*/ |
14
|
|
|
class Client |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $endpoint = 'https://ecommerce.nexi.it/ecomm/ecomm/DispatcherServlet'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $alias; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $secret; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class constructor |
33
|
|
|
* |
34
|
|
|
* @param string $alias Store identification code given by KeyClient |
35
|
|
|
* @param string $secret |
36
|
|
|
*/ |
37
|
|
|
public function __construct($alias, $secret) |
38
|
|
|
{ |
39
|
|
|
$this->alias = $alias; |
40
|
|
|
$this->secret = $secret; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create paymentUrl |
45
|
|
|
* |
46
|
|
|
* @param PaymentRequestInterface $payment |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function createPaymentUrl(PaymentRequestInterface $payment) |
50
|
|
|
{ |
51
|
|
|
$params = $payment->toArray(); |
52
|
|
|
$params = array_merge($params, array( |
53
|
|
|
'alias' => $this->alias, |
54
|
|
|
'mac' => $this->calculateMac($payment) |
55
|
|
|
)); |
56
|
|
|
|
57
|
|
|
return sprintf('%s?%s', $this->endpoint, http_build_query($params)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Parse paymentResponse |
62
|
|
|
* |
63
|
|
|
* @param Request $request |
64
|
|
|
* @return PaymentResponse |
65
|
|
|
*/ |
66
|
|
|
public function parsePaymentResponse(Request $request = null) |
67
|
|
|
{ |
68
|
|
|
$response = new PaymentResponse($request); |
69
|
|
|
|
70
|
|
|
// Verify mac |
71
|
|
|
if ($this->verifyMac($response) !== true) { |
72
|
|
|
throw new KeyClientException('Mac can not be verified'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Calculate mac |
80
|
|
|
* |
81
|
|
|
* @param PaymentRequestInterface $paymentRequest |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function calculateMac(PaymentRequestInterface $paymentRequest) |
85
|
|
|
{ |
86
|
|
|
$mac = strtr('codTrans={transactionCode}divisa={currency}importo={amount}{secret}', array( |
87
|
|
|
'{transactionCode}' => $paymentRequest->get('codTrans'), |
88
|
|
|
'{currency}' => $paymentRequest->get('divisa'), |
89
|
|
|
'{amount}' => $paymentRequest->get('importo'), |
90
|
|
|
'{secret}' => $this->secret |
91
|
|
|
)); |
92
|
|
|
|
93
|
|
|
return sha1($mac); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Verify mac |
98
|
|
|
* |
99
|
|
|
* @param PaymentResponseInterface $paymentResponse |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function verifyMac(PaymentResponseInterface $paymentResponse) |
103
|
|
|
{ |
104
|
|
|
$mac = strtr('codTrans={transactionCode}esito={result}importo={amount}divisa={currency}data={date}orario={time}codAut={authCode}{secret}', array( |
105
|
|
|
'{transactionCode}' => $paymentResponse->get('codTrans'), |
106
|
|
|
'{result}' => $paymentResponse->get('esito'), |
107
|
|
|
'{amount}' => $paymentResponse->get('importo'), |
108
|
|
|
'{currency}' => $paymentResponse->get('divisa'), |
109
|
|
|
'{date}' => $paymentResponse->get('data'), |
110
|
|
|
'{time}' => $paymentResponse->get('orario'), |
111
|
|
|
'{authCode}' => $paymentResponse->get('codAut'), |
112
|
|
|
'{secret}' => $this->secret |
113
|
|
|
)); |
114
|
|
|
|
115
|
|
|
$generated = sha1($mac); |
116
|
|
|
$given = $paymentResponse->get('mac'); |
117
|
|
|
|
118
|
|
|
return $generated === $given; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|