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 WebGains |
16
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
17
|
|
|
*/ |
18
|
|
|
class WebGains extends AbstractNetwork implements NetworkInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var object |
22
|
|
|
*/ |
23
|
|
|
private $_network = null; |
24
|
|
|
private $_username = ''; |
25
|
|
|
private $_password = ''; |
26
|
|
|
private $_idSite = ''; |
27
|
|
|
private $_apiClient = null; |
28
|
|
|
protected $_tracking_parameter = 'clickref'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @method __construct |
32
|
|
|
*/ |
33
|
|
|
public function __construct(string $username, string $password,string $idSite='') |
34
|
|
|
{ |
35
|
|
|
$this->_network = new \Oara\Network\Publisher\WebGains; |
36
|
|
|
$this->_username = $username; |
37
|
|
|
$this->_password = $password; |
38
|
|
|
$this->_idSite = $idSite; |
39
|
|
|
$apiUrl = 'http://ws.webgains.com/aws.php'; |
40
|
|
|
$this->_apiClient = new \SoapClient($apiUrl, |
41
|
|
|
array('login' => $this->_username, |
42
|
|
|
'encoding' => 'UTF-8', |
43
|
|
|
'password' => $this->_password, |
44
|
|
|
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE, |
45
|
|
|
'soap_version' => SOAP_1_1) |
46
|
|
|
); |
47
|
|
|
$this->login( $this->_username, $this->_password, $this->_idSite); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function login(string $username, string $password, string $idSite = ''): bool{ |
|
|
|
|
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->_idSite = $idSite; |
59
|
|
|
$credentials = array(); |
60
|
|
|
$credentials["user"] = $this->_username; |
61
|
|
|
$credentials["password"] = $this->_password; |
62
|
|
|
$this->_network->login($credentials); |
63
|
|
|
if ($this->_network->checkConnection()) { |
64
|
|
|
$this->_logged = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->_logged; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function checkLogin() : bool |
74
|
|
|
{ |
75
|
|
|
return $this->_logged; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array of Merchants |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function getMerchants() : array |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$arrResult = array(); |
84
|
|
|
$merchantList = $this->_network->getMerchantList(); |
85
|
|
|
foreach($merchantList as $merchant) { |
86
|
|
|
$Merchant = Merchant::createInstance(); |
87
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
88
|
|
|
$Merchant->name = $merchant['name']; |
89
|
|
|
// Added more info - 2018-04-23 <PN> |
90
|
|
|
$Merchant->status = $merchant['status']; |
91
|
|
|
$Merchant->url = $merchant['url']; |
92
|
|
|
$arrResult[] = $Merchant; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $arrResult; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param int $merchantID to filter only one merchant |
100
|
|
|
* @return array of Deal |
101
|
|
|
*/ |
102
|
|
|
public function getDeals($merchantID = null, int $page = 0, int $items_per_page = 10): DealsResultset |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
$result = DealsResultset::createInstance(); |
106
|
|
|
$arrResult = array(); |
107
|
|
|
if (!empty($this->_idSite)) { |
108
|
|
|
// Account id is correct |
109
|
|
|
$arrVouchers = $this->_network->getVouchers($this->_idSite); |
110
|
|
|
|
111
|
|
|
foreach ($arrVouchers as $obj_voucher) { |
112
|
|
|
|
113
|
|
|
$voucher = str_getcsv($obj_voucher, ',', '"'); |
114
|
|
|
|
115
|
|
|
if (count($voucher) < 12) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
$voucher_id = $voucher[0]; |
119
|
|
|
$voucher_id = str_replace("\n", '', $voucher_id); |
120
|
|
|
$advertiserId = $voucher[1]; |
121
|
|
|
$code = $voucher[7]; |
122
|
|
|
if ($voucher_id == "Voucher ID" || !is_numeric($advertiserId) || $code == "Code") { |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
$starts = $voucher[4]; |
126
|
|
|
$ends = $voucher[5]; |
127
|
|
|
$deeplink_tracking = $voucher[6]; |
128
|
|
|
$description = $voucher[11]; |
129
|
|
|
$discount = abs((int)$voucher[8]); |
130
|
|
|
$is_percentage = (bool)(strpos($voucher[8], '%') !== false); |
131
|
|
|
|
132
|
|
|
if ($merchantID > 0) { |
133
|
|
|
if ($advertiserId != $merchantID) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$Deal = Deal::createInstance(); |
139
|
|
|
$Deal->deal_ID = $voucher_id; |
140
|
|
|
$Deal->merchant_ID = $advertiserId; |
|
|
|
|
141
|
|
|
$Deal->code = $code; |
142
|
|
|
$Deal->description = $description; |
143
|
|
|
$Deal->start_date = $Deal->convertDate($starts . ' 00:00:00'); |
|
|
|
|
144
|
|
|
$Deal->end_date = $Deal->convertDate($ends . ' 23:59:59'); |
|
|
|
|
145
|
|
|
$Deal->default_track_uri = $deeplink_tracking; |
146
|
|
|
$Deal->is_exclusive = false; |
147
|
|
|
$Deal->discount_amount = $discount; |
|
|
|
|
148
|
|
|
$Deal->is_percentage = $is_percentage; |
149
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER; |
150
|
|
|
$arrResult[] = $Deal; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
$result->deals[]=$arrResult; |
154
|
|
|
return $result; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param \DateTime $dateFrom |
159
|
|
|
* @param \DateTime $dateTo |
160
|
|
|
* @param array $arrMerchantID |
161
|
|
|
* @return array of Transaction |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
165
|
|
|
{ |
166
|
|
|
/* |
167
|
|
|
if (count( $arrMerchantID ) < 1) { |
168
|
|
|
$merchants = $this->getMerchants(); |
169
|
|
|
foreach ($merchants as $merchant) { |
170
|
|
|
$arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
*/ |
174
|
|
|
$arrResult = array(); |
175
|
|
|
$transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo); |
176
|
|
|
foreach($transactionList as $transaction) { |
177
|
|
|
$Transaction = Transaction::createInstance(); |
178
|
|
|
$Transaction->currency = $transaction['currency']; |
179
|
|
|
$Transaction->status = $transaction['status']; |
180
|
|
|
$Transaction->amount = $transaction['amount']; |
181
|
|
|
array_key_exists_safe( $transaction, |
182
|
|
|
'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
183
|
|
|
$Transaction->title = ''; |
184
|
|
|
$Transaction->unique_ID = $transaction['unique_id']; |
185
|
|
|
$Transaction->commission = $transaction['commission']; |
186
|
|
|
$date = new \DateTime($transaction['date']); |
187
|
|
|
$Transaction->date = $date; // $date->format('Y-m-d H:i:s'); |
|
|
|
|
188
|
|
|
$Transaction->merchant_ID = $transaction['merchantId']; |
189
|
|
|
$Transaction->approved = false; |
190
|
|
|
if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){ |
191
|
|
|
$Transaction->approved = true; |
192
|
|
|
} |
193
|
|
|
if ($transaction['paid'] == true) { |
194
|
|
|
$Transaction->paid = true; |
195
|
|
|
} |
196
|
|
|
$arrResult[] = $Transaction; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $arrResult; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param \DateTime $dateFrom |
204
|
|
|
* @param \DateTime $dateTo |
205
|
|
|
* @param int $merchantID |
206
|
|
|
* @return array of Stat |
207
|
|
|
*/ |
208
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
209
|
|
|
{ |
210
|
|
|
return array(); |
211
|
|
|
/* |
212
|
|
|
$this->_apiClient->setConnectId($this->_username); |
213
|
|
|
$this->_apiClient->setSecretKey($this->_password); |
214
|
|
|
$dateFromIsoEngFormat = $dateFrom->format('Y-m-d'); |
215
|
|
|
$dateToIsoEngFormat = $dateTo->format('Y-m-d'); |
216
|
|
|
$response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat); |
217
|
|
|
$arrResponse = json_decode($response, true); |
218
|
|
|
$reportItems = $arrResponse['reportItems']; |
219
|
|
|
$Stat = Stat::createInstance(); |
220
|
|
|
$Stat->reportItems = $reportItems; |
221
|
|
|
|
222
|
|
|
return array($Stat); |
223
|
|
|
*/ |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param array $params |
229
|
|
|
* |
230
|
|
|
* @return ProductsResultset |
231
|
|
|
*/ |
232
|
|
|
public function getProducts(array $params = []): ProductsResultset |
233
|
|
|
{ |
234
|
|
|
// TODO: Implement getProducts() method. |
235
|
|
|
throw new \Exception("Not implemented yet"); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function getTrackingParameter(){ |
239
|
|
|
return $this->_tracking_parameter; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
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.