1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\AffiliateNetwork\Networks; |
4
|
|
|
|
5
|
|
|
use Padosoft\AffiliateNetwork\DealsResultset; |
6
|
|
|
use Padosoft\AffiliateNetwork\Transaction; |
7
|
|
|
use Padosoft\AffiliateNetwork\Merchant; |
8
|
|
|
use Padosoft\AffiliateNetwork\Stat; |
9
|
|
|
use Padosoft\AffiliateNetwork\Deal; |
10
|
|
|
use Padosoft\AffiliateNetwork\AbstractNetwork; |
11
|
|
|
use Padosoft\AffiliateNetwork\NetworkInterface; |
12
|
|
|
use Padosoft\AffiliateNetwork\NetAffiliationEx; |
13
|
|
|
use Padosoft\AffiliateNetwork\ProductsResultset; |
14
|
|
|
|
15
|
|
|
if (!defined('COOKIES_BASE_DIR')){ |
16
|
|
|
define('COOKIES_BASE_DIR',public_path('upload/report')); |
17
|
|
|
} |
18
|
|
|
/** |
19
|
|
|
* Class NetAffiliation |
20
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
21
|
|
|
*/ |
22
|
|
|
class NetAffiliation extends AbstractNetwork implements NetworkInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
private $_network = null; |
28
|
|
|
private $_username = ''; |
29
|
|
|
private $_password = ''; |
30
|
|
|
private $_idSite = ''; |
31
|
|
|
private $_logged = false; |
32
|
|
|
protected $_tracking_parameter = 'argsite'; |
33
|
|
|
protected $_merchants = array(); // To avoid repeated calls to getMerchants() |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @method __construct |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $username, string $password,string $idSite='') |
39
|
|
|
{ |
40
|
|
|
$this->_network = new NetAffiliationEx(); |
41
|
|
|
$this->_username = $username; |
42
|
|
|
$this->_password = $password; |
43
|
|
|
if (trim($idSite)!=''){ |
44
|
|
|
$this->addAllowedSite($idSite); |
45
|
|
|
} |
46
|
|
|
$this->login( $this->_username, $this->_password ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function addAllowedSite($idSite){ |
50
|
|
|
if (trim($idSite)!=''){ |
51
|
|
|
$this->_network->addAllowedSite($idSite); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function login(string $username, string $password,string $idSite=''): bool |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if (trim($idSite)!=''){ |
58
|
|
|
$this->addAllowedSite($idSite); |
59
|
|
|
} |
60
|
|
|
$this->_logged = false; |
61
|
|
|
if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
$this->_username = $username; |
66
|
|
|
$this->_password = $password; |
67
|
|
|
$credentials = array(); |
68
|
|
|
$credentials["user"] = $this->_username; |
69
|
|
|
$credentials["password"] = $this->_password; |
70
|
|
|
$credentials["apiPassword"] = $this->_password; |
71
|
|
|
$this->_network->login( $credentials ); |
72
|
|
|
//$this->_apiClient = $this->_network->getApiClient(); |
73
|
|
|
if ($this->_network->checkConnection()) { |
74
|
|
|
$this->_logged = true; |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->_logged; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function checkLogin() : bool |
85
|
|
|
{ |
86
|
|
|
return $this->_logged; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array of Merchants |
91
|
|
|
*/ |
92
|
|
|
public function getMerchants() : array |
93
|
|
|
{ |
94
|
|
|
if (!$this->checkLogin()) { |
95
|
|
|
return array(); |
96
|
|
|
} |
97
|
|
|
$arrResult = array(); |
98
|
|
|
$merchantList = $this->_network->getMerchantList(); |
99
|
|
|
foreach($merchantList as $merchant) { |
100
|
|
|
$Merchant = Merchant::createInstance(); |
101
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
102
|
|
|
$Merchant->name = $merchant['name']; |
103
|
|
|
// Added more info - 2018-04-23 <PN> |
104
|
|
|
$Merchant->status = $merchant['status']; |
105
|
|
|
$Merchant->url = $merchant['url']; |
106
|
|
|
$Merchant->status = $merchant['status']; |
107
|
|
|
if (!empty($merchant['launch_date'])) { |
108
|
|
|
$date = new \DateTime($merchant['launch_date']); |
|
|
|
|
109
|
|
|
//TODO check date format |
110
|
|
|
//$Merchant->launch_date = $date; |
111
|
|
|
} |
112
|
|
|
$arrResult[] = $Merchant; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $arrResult; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param int|null $merchantID |
120
|
|
|
* @param int $page |
121
|
|
|
* @param int $items_per_page |
122
|
|
|
* |
123
|
|
|
* @return DealsResultset |
124
|
|
|
*/ |
125
|
|
|
public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset |
126
|
|
|
{ |
127
|
|
|
$result = DealsResultset::createInstance(); |
128
|
|
|
|
129
|
|
|
$url = 'http://flux.netaffiliation.com/rsscp.php?mode=x&sec=' . $_ENV['NETAFFILIATION_SECKEY']; |
130
|
|
|
$xml = file_get_contents($url); |
131
|
|
|
$arrResult = array(); |
132
|
|
|
$arrResponse = xml2array($xml); |
133
|
|
|
if(!is_array($arrResponse) || count($arrResponse) <= 0) { |
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$arrItems = $arrResponse['listing']['item']; |
138
|
|
|
foreach($arrItems as $item) { |
139
|
|
|
if (!isset($item['idcamp'])){ |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
$Deal = Deal::createInstance(); |
143
|
|
|
$Deal->merchant_ID = $item['idcamp']; |
144
|
|
|
$Deal->code = $item['code']; |
145
|
|
|
$Deal->name = $item['title']; |
146
|
|
|
$Deal->start_date = $item['startdate']; |
147
|
|
|
if (is_array($item['enddate'])) { |
148
|
|
|
if (count($item['enddate'] == 0)) { |
149
|
|
|
$Deal->end_date = '0000-00-00 00:00:00'; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
else { |
153
|
|
|
$Deal->end_date = $item['enddate']; |
154
|
|
|
} |
155
|
|
|
$Deal->description = $item['description']; |
156
|
|
|
$Deal->default_track_uri = $item['link']; |
157
|
|
|
$Deal->deal_ID = md5($item['link']); // Use link to generate a unique deal ID |
|
|
|
|
158
|
|
|
if($merchantID > 0) { |
159
|
|
|
if($merchantID == $item['idcamp']) { |
160
|
|
|
$arrResult[] = $Deal; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
else { |
164
|
|
|
$arrResult[] = $Deal; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
$result->deals[]=$arrResult; |
168
|
|
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param \DateTime $dateFrom |
173
|
|
|
* @param \DateTime $dateTo |
174
|
|
|
* @param int $merchantID |
|
|
|
|
175
|
|
|
* @return array of Transaction |
176
|
|
|
*/ |
177
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
if (!$this->checkLogin()) { |
181
|
|
|
return array(); |
182
|
|
|
} |
183
|
|
|
$arrResult = array(); |
184
|
|
|
if (count( $arrMerchantID ) < 1) { |
185
|
|
|
if (count($this->_merchants) == 0) { |
186
|
|
|
$this->_merchants = $this->getMerchants(); |
187
|
|
|
} |
188
|
|
|
foreach ($this->_merchants as $merchant) { |
189
|
|
|
$arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$transcationList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo); |
194
|
|
|
foreach($transcationList as $transaction) { |
195
|
|
|
try { |
196
|
|
|
$Transaction = Transaction::createInstance(); |
197
|
|
|
array_key_exists_safe( $transaction, |
198
|
|
|
'currency' ) ? $Transaction->currency = $transaction['currency'] : $Transaction->currency = ''; |
199
|
|
|
array_key_exists_safe( $transaction, |
200
|
|
|
'status' ) ? $Transaction->status = $transaction['status'] : $Transaction->status = ''; |
201
|
|
|
array_key_exists_safe( $transaction, |
202
|
|
|
'amount' ) ? $Transaction->amount = $transaction['amount'] : $Transaction->amount = ''; |
|
|
|
|
203
|
|
|
array_key_exists_safe( $transaction, |
204
|
|
|
'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
205
|
|
|
array_key_exists_safe( $transaction, |
206
|
|
|
'title' ) ? $Transaction->title = $transaction['title'] : $Transaction->title = ''; |
207
|
|
|
array_key_exists_safe( $transaction, |
208
|
|
|
'unique_id' ) ? $Transaction->unique_ID = $transaction['unique_id'] : $Transaction->unique_ID = ''; |
209
|
|
|
array_key_exists_safe( $transaction, |
210
|
|
|
'transaction_id' ) ? $Transaction->transaction_ID = $transaction['transaction_id'] : $Transaction->transaction_ID = ''; |
211
|
|
|
array_key_exists_safe( $transaction, |
212
|
|
|
'commission' ) ? $Transaction->commission = $transaction['commission'] : $Transaction->commission = 0; |
|
|
|
|
213
|
|
|
$date = new \DateTime( $transaction['date'] ); |
214
|
|
|
$Transaction->date = $date; // $date->format('Y-m-d H:i:s'); |
|
|
|
|
215
|
|
|
array_key_exists_safe( $transaction, |
216
|
|
|
'merchantId' ) ? $Transaction->merchant_ID = $transaction['merchantId'] : $Transaction->merchant_ID = ''; |
|
|
|
|
217
|
|
|
array_key_exists_safe( $transaction, |
218
|
|
|
'approved' ) ? $Transaction->approved = $transaction['approved'] : $Transaction->approved = ''; |
|
|
|
|
219
|
|
|
$arrResult[] = $Transaction; |
220
|
|
|
} catch (\Exception $e) { |
221
|
|
|
//echo "stepE "; |
222
|
|
|
echo "<br><br>errore transazione NetAffiliation, id: ".$transaction->unique_ID." msg: ".$e->getMessage()."<br><br>"; |
223
|
|
|
//var_dump($e->getTraceAsString()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
} catch (\Exception $e) { |
229
|
|
|
throw new \Exception($e->getMessage()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $arrResult; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param \DateTime $dateFrom |
237
|
|
|
* @param \DateTime $dateTo |
238
|
|
|
* @param int $merchantID |
239
|
|
|
* @return array of Stat |
240
|
|
|
*/ |
241
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
242
|
|
|
{ |
243
|
|
|
return array(); |
244
|
|
|
/* |
245
|
|
|
$this->_apiClient->setConnectId($this->_username); |
246
|
|
|
$this->_apiClient->setSecretKey($this->_password); |
247
|
|
|
$dateFromIsoEngFormat = $dateFrom->format('Y-m-d'); |
248
|
|
|
$dateToIsoEngFormat = $dateTo->format('Y-m-d'); |
249
|
|
|
$response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat); |
250
|
|
|
$arrResponse = json_decode($response, true); |
251
|
|
|
$reportItems = $arrResponse['reportItems']; |
252
|
|
|
$Stat = Stat::createInstance(); |
253
|
|
|
$Stat->reportItems = $reportItems; |
254
|
|
|
|
255
|
|
|
return array($Stat); |
256
|
|
|
*/ |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param array $params |
262
|
|
|
* |
263
|
|
|
* @return ProductsResultset |
264
|
|
|
*/ |
265
|
|
|
public function getProducts(array $params = []): ProductsResultset |
266
|
|
|
{ |
267
|
|
|
// TODO: Implement getProducts() method. |
268
|
|
|
throw new \Exception("Not implemented yet"); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function getTrackingParameter(){ |
272
|
|
|
return $this->_tracking_parameter; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.