4
4
5
5
use Payum \Core \Exception \LogicException ;
6
6
use Payum \Core \Exception \RuntimeException ;
7
- use Psr \Log \LoggerInterface ;
8
- use Psr \Log \NullLogger ;
9
7
use Symfony \Component \OptionsResolver \Options ;
10
8
use Symfony \Component \OptionsResolver \OptionsResolver ;
11
9
@@ -31,21 +29,6 @@ class Api
31
29
*/
32
30
private $ config ;
33
31
34
- /**
35
- * @var LoggerInterface
36
- */
37
- private $ logger ;
38
-
39
-
40
- /**
41
- * Constructor
42
- *
43
- * @param LoggerInterface $logger
44
- */
45
- public function __construct (LoggerInterface $ logger = null )
46
- {
47
- $ this ->logger = $ logger ?: new NullLogger ();
48
- }
49
32
50
33
/**
51
34
* Configures the api.
@@ -66,25 +49,19 @@ public function setConfig(array $config)
66
49
*/
67
50
public function getTransactionId ()
68
51
{
69
- $ path = $ this ->config ['trans_id_file_path ' ];
70
-
71
- // Create directory if not exists
72
- if (!is_dir (dirname ($ path ))) {
73
- mkdir (dirname ($ path ), 0777 , true );
74
- }
52
+ $ path = $ this ->getDirectoryPath () . 'transaction_id ' ;
75
53
76
54
// Create file if not exists
77
55
if (!file_exists ($ path )) {
78
56
touch ($ path );
79
57
}
80
58
81
- $ id = 1 ;
82
59
$ date = (new \DateTime ())->format ('Ymd ' );
83
60
$ fileDate = date ('Ymd ' , filemtime ($ path ));
84
61
$ isDailyFirstAccess = ($ date != $ fileDate );
85
62
86
63
// Open file
87
- $ handle = fopen ($ this -> config [ ' trans_id_file_path ' ] , 'r+ ' );
64
+ $ handle = fopen ($ path , 'r+ ' );
88
65
if (false === $ handle ) {
89
66
throw new RuntimeException ('Failed to open the transaction ID file. ' );
90
67
}
@@ -93,6 +70,7 @@ public function getTransactionId()
93
70
throw new RuntimeException ('Failed to lock the transaction ID file. ' );
94
71
}
95
72
73
+ $ id = 1 ;
96
74
// If not daily first access, read and increment the id
97
75
if (!$ isDailyFirstAccess ) {
98
76
$ id = (int )fread ($ handle , 6 );
@@ -107,10 +85,13 @@ public function getTransactionId()
107
85
flock ($ handle , LOCK_UN );
108
86
fclose ($ handle );
109
87
88
+ if ($ this ->config ['debug ' ]) {
89
+ $ id += 89000 ;
90
+ }
91
+
110
92
return str_pad ($ id , 6 , '0 ' , STR_PAD_LEFT );
111
93
}
112
94
113
-
114
95
/**
115
96
* Creates the request url.
116
97
*
@@ -144,13 +125,39 @@ public function parseResponseData(array $data)
144
125
return $ data ;
145
126
}
146
127
147
- public function checkResponseSignature (array $ data )
128
+ /**
129
+ * Checks the response signature.
130
+ *
131
+ * @param array $data
132
+ *
133
+ * @return bool
134
+ */
135
+ public function checkResponseIntegrity (array $ data )
148
136
{
149
137
if (!isset ($ data ['signature ' ])) {
150
138
return false ;
151
139
}
152
140
153
- return $ data ['signature ' ] === $ this ->generateSignature ($ data );
141
+ return $ data ['vads_site_id ' ] === (string )$ this ->config ['site_id ' ]
142
+ && $ data ['vads_ctx_mode ' ] === (string )$ this ->config ['ctx_mode ' ]
143
+ && $ data ['signature ' ] === $ this ->generateSignature ($ data );
144
+ }
145
+
146
+ /**
147
+ * Returns the directory path and creates it if not exists.
148
+ *
149
+ * @return string
150
+ */
151
+ private function getDirectoryPath ()
152
+ {
153
+ $ path = $ this ->config ['directory ' ];
154
+
155
+ // Create directory if not exists
156
+ if (!is_dir (dirname ($ path ))) {
157
+ mkdir (dirname ($ path ), 0700 , true );
158
+ }
159
+
160
+ return $ path . DIRECTORY_SEPARATOR ;
154
161
}
155
162
156
163
/**
@@ -176,22 +183,18 @@ private function createRequestData(array $data)
176
183
{
177
184
$ data = $ this
178
185
->getRequestOptionsResolver ()
179
- ->resolve (array_replace (
180
- [
181
- 'vads_site_id ' => $ this ->config ['site_id ' ],
182
- 'vads_ctx_mode ' => $ this ->config ['ctx_mode ' ],
183
- ],
184
- $ data ,
185
- [
186
- 'vads_page_action ' => 'PAYMENT ' ,
187
- 'vads_version ' => 'V2 ' ,
188
- ]
189
- ));
186
+ ->resolve (array_replace ($ data , [
187
+ 'vads_page_action ' => 'PAYMENT ' ,
188
+ 'vads_version ' => 'V2 ' ,
189
+ ]));
190
190
191
191
$ data = array_filter ($ data , function ($ value ) {
192
192
return null !== $ value ;
193
193
});
194
194
195
+ $ data ['vads_site_id ' ] = $ this ->config ['site_id ' ];
196
+ $ data ['vads_ctx_mode ' ] = $ this ->config ['ctx_mode ' ];
197
+
195
198
$ data ['signature ' ] = $ this ->generateSignature ($ data );
196
199
197
200
return $ data ;
@@ -249,12 +252,17 @@ private function getConfigResolver()
249
252
'site_id ' ,
250
253
'certificate ' ,
251
254
'ctx_mode ' ,
252
- 'trans_id_file_path ' ,
255
+ 'directory ' ,
256
+ 'debug ' ,
253
257
])
254
- ->setAllowedTypes ('site_id ' , ['string ' , 'int ' ])
255
- ->setAllowedTypes ('certificate ' , ['string ' , 'int ' ])
256
- ->setAllowedTypes ('trans_id_file_path ' , 'string ' )
257
- ->setAllowedValues ('ctx_mode ' , ['TEST ' , 'PRODUCTION ' ]);
258
+ ->setAllowedTypes ('site_id ' , 'string ' )
259
+ ->setAllowedTypes ('certificate ' , 'string ' )
260
+ ->setAllowedValues ('ctx_mode ' , ['TEST ' , 'PRODUCTION ' ])
261
+ ->setAllowedTypes ('directory ' , 'string ' )
262
+ ->setAllowedTypes ('debug ' , 'bool ' )
263
+ ->setNormalizer ('directory ' , function (Options $ options , $ value ) {
264
+ return rtrim ($ value , DIRECTORY_SEPARATOR );
265
+ });
258
266
259
267
return $ this ->configResolver = $ resolver ;
260
268
}
@@ -362,16 +370,13 @@ private function getRequestOptionsResolver()
362
370
])
363
371
->setRequired ([
364
372
'vads_amount ' ,
365
- 'vads_ctx_mode ' ,
366
373
'vads_currency ' ,
367
374
'vads_payment_config ' ,
368
- 'vads_site_id ' ,
369
375
'vads_trans_date ' ,
370
376
'vads_trans_id ' ,
371
377
'vads_version ' ,
372
378
])
373
379
->setAllowedValues ('vads_action_mode ' , ['SILENT ' , 'INTERACTIVE ' ])
374
- ->setAllowedValues ('vads_ctx_mode ' , ['TEST ' , 'PRODUCTION ' ])
375
380
->setAllowedValues ('vads_currency ' , $ this ->getCurrencyCodes ())
376
381
->setAllowedValues ('vads_language ' , $ this ->getLanguageCodes ())
377
382
->setAllowedValues ('vads_page_action ' , 'PAYMENT ' )
0 commit comments