1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\AffiliateNetwork\Networks; |
4
|
|
|
|
5
|
|
|
use Padosoft\AffiliateNetwork\Transaction; |
6
|
|
|
use Padosoft\AffiliateNetwork\Merchant; |
7
|
|
|
use Padosoft\AffiliateNetwork\Stat; |
8
|
|
|
use Padosoft\AffiliateNetwork\Deal; |
9
|
|
|
use Padosoft\AffiliateNetwork\AbstractNetwork; |
10
|
|
|
use Padosoft\AffiliateNetwork\NetworkInterface; |
11
|
|
|
use Padosoft\AffiliateNetwork\DealsResultset; |
12
|
|
|
use Padosoft\AffiliateNetwork\ProductsResultset; |
13
|
|
|
use Padosoft\AffiliateNetwork\AffilinetEx; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Affilinet |
17
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
18
|
|
|
*/ |
19
|
|
|
class Affilinet extends AbstractNetwork implements NetworkInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var object |
23
|
|
|
*/ |
24
|
|
|
private $_network = null; |
25
|
|
|
// private $_apiClient = null; |
|
|
|
|
26
|
|
|
private $_username = ''; |
27
|
|
|
private $_password = ''; |
28
|
|
|
private $_passwordApi = ''; |
29
|
|
|
private $_website_id = ''; |
30
|
|
|
protected $_tracking_parameter = 'subid'; // Default value |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @method __construct |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function __construct(string $username, string $passwordApi, string $idSite='') |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$this->_network = new AffilinetEx; |
38
|
|
|
$this->_username = $username; |
39
|
|
|
$this->_password = $passwordApi; |
40
|
|
|
$this->_passwordApi = $passwordApi; |
41
|
|
|
$this->_website_id = $idSite; |
42
|
|
|
$this->login( $this->_username, $this->_password ,$idSite); |
43
|
|
|
// $this->_apiClient = \ApiClient::factory(PROTOCOL_JSON); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function login(string $username, string $password,string $idSite=''): bool |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->_logged = false; |
|
|
|
|
52
|
|
|
if (isNullOrEmpty( $username ) && isNullOrEmpty( $password )) { |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
$this->_username = $username; |
57
|
|
|
$this->_password = $password; |
58
|
|
|
$this->_passwordApi= $password; |
59
|
|
|
$credentials = array(); |
60
|
|
|
$credentials["user"] = $this->_username; |
61
|
|
|
$credentials["password"] = $this->_username; |
62
|
|
|
$credentials["apipassword"] = $this->_passwordApi; |
63
|
|
|
$this->_network->login($credentials); |
64
|
|
|
if ($this->_network->checkConnection()) { |
65
|
|
|
$this->_logged = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->_logged; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function checkLogin() : bool |
75
|
|
|
{ |
76
|
|
|
return $this->_logged; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array of Merchants |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function getMerchants(): array |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$arrResult = array(); |
85
|
|
|
$merchantList = $this->_network->getMerchantList(); |
86
|
|
|
foreach ($merchantList as $merchant) { |
87
|
|
|
$Merchant = Merchant::createInstance(); |
88
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
89
|
|
|
$Merchant->name = $merchant['name']; |
90
|
|
|
$arrResult[] = $Merchant; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $arrResult; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $merchantID |
98
|
|
|
* @return array of Deal |
99
|
|
|
*/ |
100
|
|
|
public function getDeals($merchantID = NULL, int $page = 0, int $items_per_page = 10): DealsResultset |
101
|
|
|
{ |
102
|
|
|
$result = DealsResultset::createInstance(); |
103
|
|
|
|
104
|
|
|
$arrResult = array(); |
105
|
|
|
$arrVouchers = $this->_network->getVouchers(); |
106
|
|
|
|
107
|
|
|
foreach($arrVouchers as $obj_voucher) { |
108
|
|
|
|
109
|
|
|
$voucher = json_decode(json_encode($obj_voucher), true); |
110
|
|
|
|
111
|
|
|
if ($merchantID > 0) { |
112
|
|
|
if ($voucher['ProgramId'] != $merchantID) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$partnershipStatus = $voucher['PartnershipStatus']; |
118
|
|
|
if ($partnershipStatus == 'NoRestriction' || $partnershipStatus == 'Accepted') { |
119
|
|
|
// Get only approved or without restriction |
120
|
|
|
$Deal = Deal::createInstance(); |
121
|
|
|
$Deal->setValues($voucher, [ |
122
|
|
|
'Id' => 'deal_ID', |
123
|
|
|
'ProgramId' => 'merchant_ID', |
124
|
|
|
'Code' => 'code', |
125
|
|
|
'LastChengeDate' => 'update_date', |
126
|
|
|
'StartDate' => 'start_date', |
127
|
|
|
'EndDate' => 'end_date', |
128
|
|
|
'Title' => 'name', |
129
|
|
|
'Description' => 'description', |
130
|
|
|
'IsExclusive' => 'is_exclusive', |
131
|
|
|
'MinimumOrderValue' => 'minimum_order_value', |
132
|
|
|
]); |
133
|
|
|
/* |
|
|
|
|
134
|
|
|
// Check voucher type (NOT USED HERE) |
135
|
|
|
$voucherType = $voucher['VoucherTypes']['VoucherType']; |
136
|
|
|
if (is_array($voucherType)) { |
137
|
|
|
foreach ($voucherType as $type) { |
138
|
|
|
switch ($type) { |
139
|
|
|
case 'AllProducts': |
140
|
|
|
break; |
141
|
|
|
case 'SpecificProducts': |
142
|
|
|
break; |
143
|
|
|
case 'MultiBuyDiscount': |
144
|
|
|
break; |
145
|
|
|
case 'Free Shipping': |
146
|
|
|
break; |
147
|
|
|
case 'Free Product': |
148
|
|
|
break; |
149
|
|
|
case 'Competition': |
150
|
|
|
break; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
*/ |
155
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER; |
156
|
|
|
|
157
|
|
|
// Decode tracking url from html snippet |
158
|
|
|
$snippet = $voucher['IntegrationCode']; |
159
|
|
|
if (!empty($snippet)) { |
160
|
|
|
$pos = strpos($snippet, 'http'); |
161
|
|
|
if ($pos !== false) { |
162
|
|
|
$posQuote = strpos($snippet,'"', $pos); |
163
|
|
|
if ($posQuote === false) { |
164
|
|
|
$posQuote = strpos($snippet,' ', $pos); |
165
|
|
|
} |
166
|
|
|
if ($posQuote !== false) { |
167
|
|
|
$Deal->default_track_uri = substr($snippet, $pos, $posQuote - $pos); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
$arrResult[] = $Deal; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
$result->deals[]=$arrResult; |
175
|
|
|
|
176
|
|
|
return $result; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param \DateTime $dateFrom |
181
|
|
|
* @param \DateTime $dateTo |
182
|
|
|
* @param int $merchantID |
|
|
|
|
183
|
|
|
* @return array of Transaction |
184
|
|
|
*/ |
185
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array |
186
|
|
|
{ |
187
|
|
|
$arrResult = array(); |
188
|
|
View Code Duplication |
if (count( $arrMerchantID ) < 1) { |
|
|
|
|
189
|
|
|
$merchants = $this->getMerchants(); |
190
|
|
|
foreach ($merchants as $merchant) { |
191
|
|
|
$arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name]; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
$transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom,$dateTo); |
195
|
|
|
//echo "<br>merchants id array<br>".print_r($arrMerchantID); |
|
|
|
|
196
|
|
|
//$counter=0; |
197
|
|
|
foreach($transactionList as $transaction) { |
198
|
|
|
$myTransaction = Transaction::createInstance(); |
199
|
|
|
try { |
200
|
|
|
$myTransaction->status = $transaction['status']; |
201
|
|
|
$myTransaction->amount = $transaction['amount']; |
202
|
|
|
$myTransaction->custom_ID = $transaction['custom_id']; |
203
|
|
|
$myTransaction->unique_ID = $transaction['unique_id']; |
204
|
|
|
$myTransaction->commission = $transaction['commission']; |
205
|
|
|
if (!empty($transaction['date'])) { |
206
|
|
|
$date = new \DateTime($transaction['date']); |
207
|
|
|
$myTransaction->date = $date; // $date->format('Y-m-d H:i:s'); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
$myTransaction->merchant_ID = $transaction['merchantId']; |
210
|
|
|
// Future use - Only few providers returns these dates values - <PN> - 2017-06-26 |
211
|
|
|
if (isset($transaction['click_date']) && !empty($transaction['click_date'])) { |
212
|
|
|
$myTransaction->click_date = new \DateTime($transaction['click_date']); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
if (isset($transaction['update_date']) && !empty($transaction['update_date'])) { |
215
|
|
|
$myTransaction->update_date = new \DateTime($transaction['update_date']); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
$arrResult[] = $myTransaction; |
218
|
|
|
} catch (\Exception $e) { |
219
|
|
|
//echo "stepE "; |
220
|
|
|
echo "<br><br>errore transazione Affilinet, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>"; |
221
|
|
|
var_dump($e->getTraceAsString()); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
return $arrResult; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param \DateTime $dateFrom |
229
|
|
|
* @param \DateTime $dateTo |
230
|
|
|
* @param int $merchantID |
231
|
|
|
* @return array of Stat |
232
|
|
|
*/ |
233
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array |
234
|
|
|
{ |
235
|
|
|
return array(); |
236
|
|
|
/* |
|
|
|
|
237
|
|
|
$this->_apiClient->setConnectId($this->_username); |
238
|
|
|
$this->_apiClient->setSecretKey($this->_password); |
239
|
|
|
$dateFromIsoEngFormat = $dateFrom->format('Y-m-d'); |
240
|
|
|
$dateToIsoEngFormat = $dateTo->format('Y-m-d'); |
241
|
|
|
$response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat); |
242
|
|
|
$arrResponse = json_decode($response, true); |
243
|
|
|
$reportItems = $arrResponse['reportItems']; |
244
|
|
|
$Stat = Stat::createInstance(); |
245
|
|
|
$Stat->reportItems = $reportItems; |
246
|
|
|
|
247
|
|
|
return array($Stat); |
248
|
|
|
*/ |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param array $params |
253
|
|
|
* |
254
|
|
|
* @return ProductsResultset |
255
|
|
|
*/ |
256
|
|
|
public function getProducts(array $params = []): ProductsResultset |
257
|
|
|
{ |
258
|
|
|
// TODO: Implement getProducts() method. |
259
|
|
|
throw new \Exception("Not implemented yet"); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Api call CommissionJunction |
264
|
|
|
*/ |
265
|
|
View Code Duplication |
private function _apiCall($url) |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$ch = curl_init(); |
268
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
269
|
|
|
curl_setopt($ch, CURLOPT_POST, FALSE); |
270
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
271
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
272
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
273
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $this->_passwordApi)); |
274
|
|
|
$curl_results = curl_exec($ch); |
275
|
|
|
curl_close($ch); |
276
|
|
|
return $curl_results; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function getTrackingParameter() |
280
|
|
|
{ |
281
|
|
|
return $this->_tracking_parameter; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.