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\ProductsResultset; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LinkShare |
16
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
17
|
|
|
*/ |
18
|
|
|
class LinkShare extends AbstractNetwork implements NetworkInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var object |
22
|
|
|
*/ |
23
|
|
|
private $_network = null; |
24
|
|
|
private $_username = ''; |
25
|
|
|
private $_password = ''; |
26
|
|
|
private $_apiClient = null; |
27
|
|
|
protected $_tracking_parameter = ''; |
28
|
|
|
private $_idSite = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @method __construct |
32
|
|
|
*/ |
33
|
|
|
public function __construct(string $username, string $password, string $idSite = '') |
34
|
|
|
{ |
35
|
|
|
$this->_network = new \Oara\Network\Publisher\LinkShare; |
36
|
|
|
$this->_username = $username; |
37
|
|
|
$this->_password = $password; |
38
|
|
|
$this->_idSite = $idSite; |
39
|
|
|
|
40
|
|
|
$this->login( $this->_username, $this->_password, $idSite ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function login(string $username, string $password,string $idSite=''): bool{ |
|
|
|
|
44
|
|
|
$this->_logged = false; |
|
|
|
|
45
|
|
|
if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
$this->_username = $username; |
50
|
|
|
$this->_password = $password; |
51
|
|
|
$credentials = array(); |
52
|
|
|
$credentials["user"] = $this->_username; |
53
|
|
|
$credentials["password"] = $this->_password; |
54
|
|
|
$credentials["idSite"] = $idSite; |
55
|
|
|
$this->_network->login($credentials); |
56
|
|
|
if ($this->_network->checkConnection()) { |
57
|
|
|
$this->_logged = true; |
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
|
|
View Code Duplication |
public function getMerchants() : array |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$arrResult = array(); |
77
|
|
|
$merchantList = $this->_network->getMerchantList(); |
78
|
|
|
foreach($merchantList as $merchant) { |
79
|
|
|
$Merchant = Merchant::createInstance(); |
80
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
81
|
|
|
$Merchant->name = $merchant['name']; |
82
|
|
|
$Merchant->status = $merchant['status']; |
83
|
|
|
$Merchant->termination_date = $merchant['termination_date']; |
84
|
|
|
$Merchant->url = $merchant['url']; |
85
|
|
|
$arrResult[] = $Merchant; |
86
|
|
|
} |
87
|
|
|
return $arrResult; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $merchantID |
92
|
|
|
* @return array of Deal |
93
|
|
|
*/ |
94
|
|
|
public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset |
95
|
|
|
{ |
96
|
|
|
$arrResult = array(); |
97
|
|
|
|
98
|
|
|
$apiKey = $_ENV['LINKSHARE_TOKEN']; |
99
|
|
|
$network = $_ENV['LINKSHARE_NETWORK']; |
100
|
|
|
$result = DealsResultset::createInstance(); |
101
|
|
|
|
102
|
|
|
$arrVouchers = $this->_network->getVouchers($apiKey, $network); |
103
|
|
|
|
104
|
|
|
foreach($arrVouchers as $voucher) { |
105
|
|
|
if (!empty($voucher['tracking']) && !empty($voucher['advertiser_id'])) { |
106
|
|
|
$Deal = Deal::createInstance(); |
107
|
|
|
$Deal->deal_ID = md5($voucher['tracking']); // Use link to generate a unique deal ID |
|
|
|
|
108
|
|
|
$Deal->merchant_ID = $voucher['advertiser_id']; |
109
|
|
|
$Deal->code = $voucher['code']; |
110
|
|
|
$Deal->description = $voucher['description'] . ' ' . $voucher['restriction']; |
111
|
|
|
$Deal->start_date = $Deal->convertDate($voucher['start_date']); |
|
|
|
|
112
|
|
|
$Deal->start_date->setTime(0, 0, 0); |
113
|
|
|
$Deal->end_date = $Deal->convertDate($voucher['end_date']); |
|
|
|
|
114
|
|
|
$Deal->end_date->setTime(23, 59, 59); |
115
|
|
|
$Deal->default_track_uri = $voucher['tracking']; |
116
|
|
|
$Deal->is_exclusive = false; |
117
|
|
|
$Deal->deal_type = $voucher['type']; |
118
|
|
|
$arrResult[] = $Deal; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$result->deals[]=$arrResult; |
123
|
|
|
|
124
|
|
|
return $result; |
125
|
|
|
; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param \DateTime $dateFrom |
130
|
|
|
* @param \DateTime $dateTo |
131
|
|
|
* @param int $merchantID |
|
|
|
|
132
|
|
|
* @return array of Transaction |
133
|
|
|
*/ |
134
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
135
|
|
|
{ |
136
|
|
|
|
137
|
|
|
$arrResult = array(); |
138
|
|
|
$this->_network->_nid = $_ENV['LINKSHARE_NETWORK']; |
139
|
|
|
$transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo); |
140
|
|
|
foreach($transactionList as $transaction) { |
141
|
|
|
$Transaction = Transaction::createInstance(); |
142
|
|
|
if (isset($transaction['currency']) && !empty($transaction['currency'])) { |
143
|
|
|
$Transaction->currency = $transaction['currency']; |
144
|
|
|
} else { |
145
|
|
|
$Transaction->currency = "EUR"; |
146
|
|
|
} |
147
|
|
|
$Transaction->status = $transaction['status']; |
148
|
|
|
$Transaction->amount = $transaction['amount']; |
149
|
|
|
array_key_exists_safe( $transaction,'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
150
|
|
|
$Transaction->title = ''; |
151
|
|
|
$Transaction->unique_ID = $transaction['unique_id']; |
152
|
|
|
$Transaction->commission = $transaction['commission']; |
153
|
|
|
$date = new \DateTime($transaction['date']); |
154
|
|
|
$Transaction->date = $date; // $date->format('Y-m-d H:i:s'); |
|
|
|
|
155
|
|
|
// Future use - Only few providers returns these dates values - <PN> - 2017-06-29 |
156
|
|
|
if (isset($transaction['click_date']) && !empty($transaction['click_date'])) { |
157
|
|
|
$Transaction->click_date = new \DateTime($transaction['click_date']); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
if (isset($transaction['update_date']) && !empty($transaction['update_date'])) { |
160
|
|
|
$Transaction->update_date = new \DateTime($transaction['update_date']); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
$Transaction->merchant_ID = $transaction['merchantId']; |
163
|
|
|
$Transaction->campaign_name = $transaction['merchantName']; |
164
|
|
|
$Transaction->IP = $transaction['IP']; |
165
|
|
|
$Transaction->approved = false; |
166
|
|
|
if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){ |
167
|
|
|
$Transaction->approved = true; |
168
|
|
|
} |
169
|
|
|
$arrResult[] = $Transaction; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $arrResult; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param \DateTime $dateFrom |
177
|
|
|
* @param \DateTime $dateTo |
178
|
|
|
* @param int $merchantID |
179
|
|
|
* @return array of Stat |
180
|
|
|
*/ |
181
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
182
|
|
|
{ |
183
|
|
|
// TODO |
184
|
|
|
return array(); |
185
|
|
|
/* |
186
|
|
|
$this->_apiClient->setConnectId($this->_username); |
187
|
|
|
$this->_apiClient->setSecretKey($this->_password); |
188
|
|
|
$dateFromIsoEngFormat = $dateFrom->format('Y-m-d'); |
189
|
|
|
$dateToIsoEngFormat = $dateTo->format('Y-m-d'); |
190
|
|
|
$response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat); |
191
|
|
|
$arrResponse = json_decode($response, true); |
192
|
|
|
$reportItems = $arrResponse['reportItems']; |
193
|
|
|
$Stat = Stat::createInstance(); |
194
|
|
|
$Stat->reportItems = $reportItems; |
195
|
|
|
|
196
|
|
|
return array($Stat); |
197
|
|
|
*/ |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $params |
203
|
|
|
* |
204
|
|
|
* @return ProductsResultset |
205
|
|
|
*/ |
206
|
|
|
public function getProducts(array $params = []): ProductsResultset |
207
|
|
|
{ |
208
|
|
|
// TODO: Implement getProducts() method. |
209
|
|
|
throw new \Exception("Not implemented yet"); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getTrackingParameter(){ |
213
|
|
|
return $this->_tracking_parameter; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
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.