|
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
|
|
|
|
|
13
|
|
|
// require "../vendor/fubralimited/php-oara/Oara/Network/Publisher/Zanox/Zapi/ApiClient.php"; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Zanox |
|
17
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
|
18
|
|
|
*/ |
|
19
|
|
|
class Zanox extends AbstractNetwork implements NetworkInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var object |
|
23
|
|
|
*/ |
|
24
|
|
|
private $_network = null; |
|
25
|
|
|
protected $_apiClient = null; |
|
26
|
|
|
private $_username = ''; |
|
27
|
|
|
private $_password = ''; |
|
28
|
|
|
private $_logged = false; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @method __construct |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(string $username, string $password) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->_network = new ZanoxEx; |
|
37
|
|
|
$this->_username = $username; |
|
38
|
|
|
$this->_password = $password; |
|
39
|
|
|
$this->login( $this->_username, $this->_password ); |
|
40
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function login(string $username, string $password): bool |
|
44
|
|
|
{ |
|
45
|
|
|
$this->_logged = false; |
|
46
|
|
|
if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
|
47
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
$this->_username = $username; |
|
51
|
|
|
$this->_password = $password; |
|
52
|
|
|
$credentials = array(); |
|
53
|
|
|
$credentials["connectid"] = $this->_username; |
|
54
|
|
|
$credentials["secretkey"] = $this->_password; |
|
55
|
|
|
$this->_network->login( $credentials ); |
|
56
|
|
|
$this->_apiClient = $this->_network->getApiClient(); |
|
57
|
|
|
if ($this->_network->checkConnection()) { |
|
58
|
|
|
$this->_logged = true; |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->_logged; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function checkLogin(): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->_logged; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array of Merchants |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function getMerchants(): array |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
if (!$this->checkLogin()) { |
|
79
|
|
|
return array(); |
|
80
|
|
|
} |
|
81
|
|
|
$arrResult = array(); |
|
82
|
|
|
$merchantList = $this->_network->getMerchantList(); |
|
83
|
|
|
foreach ($merchantList as $merchant) { |
|
84
|
|
|
$Merchant = Merchant::createInstance(); |
|
85
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
|
86
|
|
|
$Merchant->name = $merchant['name']; |
|
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=NULL,int $page=0,int $items_per_page=10 ): DealsResultset |
|
101
|
|
|
{ |
|
102
|
|
|
if (!isIntegerPositive($items_per_page)){ |
|
103
|
|
|
$items_per_page=10; |
|
104
|
|
|
} |
|
105
|
|
|
$result=DealsResultset::createInstance(); |
|
106
|
|
|
if (!$this->checkLogin()) { |
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
/*$this->_apiClient->setConnectId( $this->_username ); |
|
|
|
|
|
|
110
|
|
|
$this->_apiClient->setSecretKey( $this->_password );*/ |
|
111
|
|
|
$adSpaces=$this->_apiClient->getAdspaces(0,100); |
|
112
|
|
|
if (!is_object($adSpaces)){ |
|
113
|
|
|
$adSpaces=json_encode($adSpaces); |
|
114
|
|
|
} |
|
115
|
|
|
if ($adSpaces->items<1){ |
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$adSpaceId=$adSpaces->adspaceItems->adspaceItem[0]->id; |
|
120
|
|
|
if (!isIntegerPositive($merchantID)){ |
|
121
|
|
|
$merchantID=NULL; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$Response = $this->_apiClient->searchIncentives($merchantID,$adSpaceId,'coupons',NULL,$page,$items_per_page); |
|
125
|
|
|
|
|
126
|
|
|
if (!is_object($Response)){ |
|
127
|
|
|
$Response=json_decode($Response); |
|
128
|
|
|
} |
|
129
|
|
|
$result->page=$Response->page; |
|
130
|
|
|
$result->items=$Response->items; |
|
131
|
|
|
$result->total=$Response->total; |
|
132
|
|
|
($Response->total>0)?$result->num_pages=(int)ceil($Response->total/$items_per_page):$result->num_pages=0; |
|
|
|
|
|
|
133
|
|
|
$arrAdmediumItems = $Response->incentiveItems->incentiveItem; |
|
134
|
|
|
|
|
135
|
|
|
foreach ($arrAdmediumItems as $admediumItems) { |
|
136
|
|
|
$Deal = Deal::createInstance(); |
|
137
|
|
|
$Deal->id = (int)$admediumItems->id; |
|
|
|
|
|
|
138
|
|
|
$Deal->created_at =$admediumItems->createDate; |
|
|
|
|
|
|
139
|
|
|
$Deal->startDate = $admediumItems->startDate; |
|
140
|
|
|
isset($admediumItems->endDate)?$Deal->endDate = $admediumItems->endDate:$Deal->endDate = ''; |
|
|
|
|
|
|
141
|
|
|
$Deal->name = $admediumItems->name; |
|
142
|
|
|
$Deal->code = $admediumItems->couponCode; |
|
143
|
|
|
$Deal->description = $admediumItems->info4customer; |
|
144
|
|
|
$Deal->note = $admediumItems->info4publisher.' '.$admediumItems->restrictions; |
|
|
|
|
|
|
145
|
|
|
$Deal->is_percent = 0; |
|
|
|
|
|
|
146
|
|
|
$Deal->value=0; |
|
|
|
|
|
|
147
|
|
|
$Deal->currency=''; |
|
|
|
|
|
|
148
|
|
|
//dd($admediumItems->percentage); |
|
|
|
|
|
|
149
|
|
|
if (isset($admediumItems->percentage) && isIntegerPositive($admediumItems->percentage)){ |
|
150
|
|
|
$Deal->is_percent = 1; |
|
|
|
|
|
|
151
|
|
|
$Deal->value=$admediumItems->percentage; |
|
152
|
|
|
}elseif (isset($admediumItems->total)){ |
|
153
|
|
|
|
|
154
|
|
|
$Deal->value=$admediumItems->total; |
|
155
|
|
|
$Deal->currency=$admediumItems->currency; |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
//$Deal->deal_type = $admediumItems['admediumType']; |
|
|
|
|
|
|
158
|
|
|
$Deal->merchant_ID = (int)$admediumItems->program->id; |
|
159
|
|
|
|
|
160
|
|
|
$Deal->merchant_name = $admediumItems->program->_; |
|
161
|
|
|
$Deal->ppv = $admediumItems->admedia->admediumItem[0]->trackingLinks->trackingLink[0]->ppv; |
|
162
|
|
|
$Deal->ppc = $admediumItems->admedia->admediumItem[0]->trackingLinks->trackingLink[0]->ppc; |
|
163
|
|
|
$result->deals[]=$Deal; |
|
164
|
|
|
/*if ($merchantID > 0) { |
|
|
|
|
|
|
165
|
|
|
if ($merchantID == $admediumItems['program']['@id']) { |
|
166
|
|
|
$arrResult[] = $Deal; |
|
167
|
|
|
} |
|
168
|
|
|
} else { |
|
169
|
|
|
$arrResult[] = $Deal; |
|
170
|
|
|
}*/ |
|
171
|
|
|
} |
|
172
|
|
|
//dd($result); |
|
173
|
|
|
return $result; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param \DateTime $dateFrom |
|
179
|
|
|
* @param \DateTime $dateTo |
|
180
|
|
|
* @param int $merchantID |
|
|
|
|
|
|
181
|
|
|
* |
|
182
|
|
|
* @return array of Transaction |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array |
|
185
|
|
|
{ |
|
186
|
|
|
if (!$this->checkLogin()) { |
|
187
|
|
|
return array(); |
|
188
|
|
|
} |
|
189
|
|
|
$arrResult = array(); |
|
190
|
|
|
if (count( $arrMerchantID ) < 1) { |
|
191
|
|
|
$merchants = $this->getMerchants(); |
|
192
|
|
|
foreach ($merchants as $merchant) { |
|
193
|
|
|
$arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name]; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
$transcationList = $this->_network->getTransactionList( $arrMerchantID, $dateTo, $dateFrom ); |
|
197
|
|
|
foreach ($transcationList as $transaction) { |
|
198
|
|
|
$Transaction = Transaction::createInstance(); |
|
199
|
|
|
array_key_exists_safe( $transaction, |
|
200
|
|
|
'currency' ) ? $Transaction->currency = $transaction['currency'] : $Transaction->currency = ''; |
|
201
|
|
|
array_key_exists_safe( $transaction, |
|
202
|
|
|
'status' ) ? $Transaction->status = $transaction['status'] : $Transaction->status = ''; |
|
203
|
|
|
array_key_exists_safe( $transaction, |
|
204
|
|
|
'amount' ) ? $Transaction->amount = $transaction['amount'] : $Transaction->amount = ''; |
|
|
|
|
|
|
205
|
|
|
array_key_exists_safe( $transaction, |
|
206
|
|
|
'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
|
207
|
|
|
array_key_exists_safe( $transaction, |
|
208
|
|
|
'title' ) ? $Transaction->title = $transaction['title'] : $Transaction->title = ''; |
|
209
|
|
|
array_key_exists_safe( $transaction, |
|
210
|
|
|
'unique_id' ) ? $Transaction->unique_ID = $transaction['unique_id'] : $Transaction->unique_ID = ''; |
|
211
|
|
|
array_key_exists_safe( $transaction, |
|
212
|
|
|
'commission' ) ? $Transaction->commission = $transaction['commission'] : $Transaction->commission = ''; |
|
|
|
|
|
|
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
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $arrResult; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param \DateTime $dateFrom |
|
227
|
|
|
* @param \DateTime $dateTo |
|
228
|
|
|
* @param int $merchantID |
|
229
|
|
|
* |
|
230
|
|
|
* @return array of Stat |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array |
|
233
|
|
|
{ |
|
234
|
|
|
return array(); |
|
235
|
|
|
/* |
|
|
|
|
|
|
236
|
|
|
$this->_apiClient->setConnectId($this->_username); |
|
237
|
|
|
$this->_apiClient->setSecretKey($this->_password); |
|
238
|
|
|
$dateFromIsoEngFormat = $dateFrom->format('Y-m-d'); |
|
239
|
|
|
$dateToIsoEngFormat = $dateTo->format('Y-m-d'); |
|
240
|
|
|
$response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat); |
|
241
|
|
|
$arrResponse = json_decode($response, true); |
|
242
|
|
|
$reportItems = $arrResponse['reportItems']; |
|
243
|
|
|
$Stat = Stat::createInstance(); |
|
244
|
|
|
$Stat->reportItems = $reportItems; |
|
245
|
|
|
|
|
246
|
|
|
return array($Stat); |
|
247
|
|
|
*/ |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
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.