|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padosoft\AffiliateNetwork\Networks; |
|
4
|
|
|
|
|
5
|
|
|
use Padosoft\AffiliateNetwork\Transaction; |
|
6
|
|
|
use Padosoft\AffiliateNetwork\DealsResultset; |
|
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\AffiliateWindowEx; |
|
13
|
|
|
use Padosoft\AffiliateNetwork\ProductsResultset; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class AffiliateWindow |
|
17
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
|
18
|
|
|
*/ |
|
19
|
|
|
class AffiliateWindow 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 $_logged = false; |
|
29
|
|
|
protected $_tracking_parameter = 'clickref'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @method __construct |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(string $username, string $password) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->_network = new AffiliateWindowEx; |
|
37
|
|
|
$this->_username = $username; |
|
38
|
|
|
$this->_password = $password; |
|
39
|
|
|
$this->_apiClient = null; |
|
40
|
|
|
$this->login( $this->_username, $this->_password ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function login(string $username, string $password): bool |
|
44
|
|
|
{ |
|
45
|
|
|
$this->_logged = false; |
|
46
|
|
|
if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
$this->_username = $username; |
|
50
|
|
|
$this->_password = $password; |
|
51
|
|
|
$credentials = array(); |
|
52
|
|
|
$credentials["accountid"] = $this->_username; |
|
53
|
|
|
$credentials["apipassword"] = $this->_password; |
|
54
|
|
|
$this->_network->login( $credentials ); |
|
55
|
|
|
if ($this->_network->checkConnection()) { |
|
56
|
|
|
$this->_logged = true; |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->_logged; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function checkLogin() : bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->_logged; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array of Merchants |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getMerchants() : array |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$this->checkLogin()) { |
|
77
|
|
|
return array(); |
|
78
|
|
|
} |
|
79
|
|
|
$arrResult = array(); |
|
80
|
|
|
$merchantList = $this->_network->getMerchantList(); |
|
81
|
|
|
foreach($merchantList as $merchant) { |
|
82
|
|
|
$Merchant = Merchant::createInstance(); |
|
83
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
|
84
|
|
|
$Merchant->name = $merchant['name']; |
|
85
|
|
|
$Merchant->url = $merchant['url']; |
|
86
|
|
|
$Merchant->status = $merchant['status']; |
|
87
|
|
|
$arrResult[] = $Merchant; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $arrResult; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param int|null $merchantID |
|
95
|
|
|
* @param int $page |
|
96
|
|
|
* @param int $items_per_page |
|
97
|
|
|
* |
|
98
|
|
|
* @return DealsResultset |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset |
|
101
|
|
|
{ |
|
102
|
|
|
$result = DealsResultset::createInstance(); |
|
103
|
|
|
|
|
104
|
|
|
if (!isset($_ENV['AWIN_API_VOUCHER_KEY'])) { |
|
105
|
|
|
throw new \Exception("Awin api key not defined"); |
|
106
|
|
|
} |
|
107
|
|
|
$apiKey = $_ENV['AWIN_API_VOUCHER_KEY']; |
|
108
|
|
|
|
|
109
|
|
|
$arrResult = array(); |
|
110
|
|
|
$arrVouchers = $this->_network->getVouchers($apiKey); |
|
111
|
|
|
|
|
112
|
|
|
foreach($arrVouchers as $obj_voucher) { |
|
113
|
|
|
|
|
114
|
|
|
$voucher = str_getcsv($obj_voucher, ',', '"'); |
|
115
|
|
|
|
|
116
|
|
|
if (count($voucher) < 17) { |
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
$promotionId = $voucher[0]; |
|
120
|
|
|
if (!is_numeric($promotionId)) { |
|
121
|
|
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
$advertiser = $voucher[1]; |
|
|
|
|
|
|
124
|
|
|
$advertiserId = $voucher[2]; |
|
125
|
|
|
$type = $voucher[3]; |
|
|
|
|
|
|
126
|
|
|
$code = $voucher[4]; |
|
127
|
|
|
$description = $voucher[5]; |
|
128
|
|
|
$starts = $voucher[6]; |
|
129
|
|
|
$ends = $voucher[7]; |
|
130
|
|
|
$categories = $voucher[8]; |
|
|
|
|
|
|
131
|
|
|
$regions = $voucher[9]; |
|
|
|
|
|
|
132
|
|
|
$terms = $voucher[10]; |
|
|
|
|
|
|
133
|
|
|
$deeplink_tracking = $voucher[11]; |
|
134
|
|
|
$deeplink = $voucher[12]; |
|
|
|
|
|
|
135
|
|
|
$commission_group = $voucher[13]; |
|
|
|
|
|
|
136
|
|
|
$commission = $voucher[14]; |
|
|
|
|
|
|
137
|
|
|
$exclusive = $voucher[15]; |
|
138
|
|
|
$date_added = $voucher[16]; |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
if ($merchantID > 0) { |
|
141
|
|
|
if ($advertiserId != $merchantID) { |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$Deal = Deal::createInstance(); |
|
147
|
|
|
$Deal->deal_ID = $promotionId; |
|
|
|
|
|
|
148
|
|
|
$Deal->merchant_ID = $advertiserId; |
|
149
|
|
|
$Deal->code = $code; |
|
150
|
|
|
$Deal->description = $description; |
|
151
|
|
|
$Deal->start_date = $Deal->convertDate($starts); |
|
|
|
|
|
|
152
|
|
|
$Deal->end_date = $Deal->convertDate($ends); |
|
|
|
|
|
|
153
|
|
|
$Deal->default_track_uri = $deeplink_tracking; |
|
154
|
|
|
$Deal->is_exclusive = $exclusive; |
|
155
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER; |
|
156
|
|
|
$arrResult[] = $Deal; |
|
157
|
|
|
} |
|
158
|
|
|
$result->deals[]=$arrResult; |
|
159
|
|
|
|
|
160
|
|
|
return $result; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param \DateTime $dateFrom |
|
165
|
|
|
* @param \DateTime $dateTo |
|
166
|
|
|
* @param int $merchantID |
|
|
|
|
|
|
167
|
|
|
* @return array of Transaction |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
|
170
|
|
|
{/* |
|
171
|
|
|
if (!$this->checkLogin()) { |
|
172
|
|
|
return array(); |
|
173
|
|
|
}*/ |
|
174
|
|
|
//echo "go"; |
|
175
|
|
|
$arrResult = array(); |
|
176
|
|
|
/* |
|
177
|
|
|
if (count( $arrMerchantID ) < 1) { |
|
178
|
|
|
$merchants = $this->getMerchants(); |
|
179
|
|
|
foreach ($merchants as $merchant) { |
|
180
|
|
|
$arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name]; |
|
181
|
|
|
} |
|
182
|
|
|
}*/ |
|
183
|
|
|
|
|
184
|
|
|
try { |
|
185
|
|
|
// Added timezone parameter |
|
186
|
|
|
$transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC'); |
|
187
|
|
|
|
|
188
|
|
|
if (is_array($transactionList)) { |
|
189
|
|
|
//echo "stepC "; |
|
190
|
|
|
foreach ($transactionList as $transaction) { |
|
191
|
|
|
try { |
|
192
|
|
|
$myTransaction = Transaction::createInstance(); |
|
193
|
|
|
|
|
194
|
|
|
$myTransaction->merchant_ID = $transaction['merchantId']; |
|
195
|
|
|
$myTransaction->date = $transaction['date']; |
|
196
|
|
|
if (!empty($transaction['date'])) { |
|
197
|
|
|
$date = new \DateTime($transaction['date'], new \DateTimeZone('UTC')); |
|
198
|
|
|
$myTransaction->date = $date; |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
$myTransaction->unique_ID = $transaction['unique_id']; |
|
201
|
|
|
$myTransaction->custom_ID = $transaction['custom_id']; |
|
202
|
|
|
$myTransaction->status = $transaction['status']; |
|
203
|
|
|
$myTransaction->amount = $transaction['amount']; |
|
204
|
|
|
$myTransaction->commission = $transaction['commission']; |
|
205
|
|
|
$myTransaction->currency = $transaction['currency']; |
|
206
|
|
|
|
|
207
|
|
|
$arrResult[] = $myTransaction; |
|
208
|
|
|
} catch (\Exception $e) { |
|
209
|
|
|
//echo "stepE "; |
|
210
|
|
|
echo "<br><br>Transaction Error AffiliateWindow, id: ".$transaction['unique_id']." msg: ".$e->getMessage()."<br><br>"; |
|
211
|
|
|
var_dump($e->getTraceAsString()); |
|
|
|
|
|
|
212
|
|
|
//throw new \Exception($e); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
//echo "stepD "; |
|
217
|
|
|
} catch (\Exception $e) { |
|
218
|
|
|
$message = $e->getMessage(); |
|
219
|
|
|
if (strpos($message, '429') !== false) { |
|
220
|
|
|
echo "[AffiliateWindow][Error] 429 Too Many Requests" . PHP_EOL; |
|
221
|
|
|
throw new \Exception("Too many requests", 429 ); |
|
222
|
|
|
} |
|
223
|
|
|
else { |
|
224
|
|
|
echo "[AffiliateWindow][Error] " . $e->getMessage() . PHP_EOL; |
|
225
|
|
|
var_dump($e->getTraceAsString()); |
|
226
|
|
|
throw new \Exception($e); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
return $arrResult; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param \DateTime $dateFrom |
|
234
|
|
|
* @param \DateTime $dateTo |
|
235
|
|
|
* @param int $merchantID |
|
236
|
|
|
* @return array of Stat |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
|
239
|
|
|
{ |
|
240
|
|
|
return array(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param array $params |
|
246
|
|
|
* |
|
247
|
|
|
* @return ProductsResultset |
|
248
|
|
|
*/ |
|
249
|
|
|
public function getProducts(array $params = []): ProductsResultset |
|
250
|
|
|
{ |
|
251
|
|
|
// TODO: Implement getProducts() method. |
|
252
|
|
|
throw new \Exception("Not implemented yet"); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function getTrackingParameter(){ |
|
256
|
|
|
return $this->_tracking_parameter; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.