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\TradeDoublerEx; |
13
|
|
|
use Padosoft\AffiliateNetwork\ProductsResultset; |
14
|
|
|
|
15
|
|
|
if (!defined('COOKIES_BASE_DIR')){ |
16
|
|
|
define('COOKIES_BASE_DIR',public_path('upload/report')); |
17
|
|
|
} |
18
|
|
|
/** |
19
|
|
|
* Class TradeDoubler |
20
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
21
|
|
|
*/ |
22
|
|
|
class TradeDoublerWhitelabel extends AbstractNetwork implements NetworkInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
private $_network = null; |
28
|
|
|
private $_apiClient = null; |
29
|
|
|
private $_username = ''; |
30
|
|
|
private $_password = ''; |
31
|
|
|
private $_idSite = ''; |
32
|
|
|
private $_logged = false; |
33
|
|
|
protected $_tracking_parameter = 'epi'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* TradeDoubler constructor. |
37
|
|
|
* @param string $username |
38
|
|
|
* @param string $password |
39
|
|
|
* @param string $idSite |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $username, string $password, string $idSite = '') |
42
|
|
|
{ |
43
|
|
|
$this->_network = new TradeDoublerEx; |
44
|
|
|
$this->_username = $username; |
45
|
|
|
$this->_password = $password; |
46
|
|
|
$this->_idSite = $idSite; |
47
|
|
|
$this->_apiClient = null; |
48
|
|
|
$this->login( $this->_username, $this->_password ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function login(string $username, string $password, string $idSite = ''): bool |
52
|
|
|
{ |
53
|
|
|
$this->_logged = false; |
54
|
|
|
if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
55
|
|
|
|
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
$this->_username = $username; |
59
|
|
|
$this->_password = $password; |
60
|
|
|
$this->_idSite = $idSite; |
61
|
|
|
$credentials = array(); |
62
|
|
|
$credentials["user"] = $this->_username; |
63
|
|
|
$credentials["password"] = $this->_password; |
64
|
|
|
$credentials["idSite"] = $this->_idSite; |
65
|
|
|
$this->_network->login( $credentials ); |
66
|
|
|
|
67
|
|
|
if ($this->_network->checkConnection()) { |
68
|
|
|
$this->_logged = true; |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->_logged; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function checkLogin() : bool |
79
|
|
|
{ |
80
|
|
|
return $this->_logged; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array of Merchants |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function getMerchants() : array |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if (!$this->checkLogin()) { |
89
|
|
|
return array(); |
90
|
|
|
} |
91
|
|
|
$arrResult = array(); |
92
|
|
|
$merchantList = $this->_network->getMerchantList(); |
93
|
|
|
foreach($merchantList as $merchant) { |
94
|
|
|
$Merchant = Merchant::createInstance(); |
95
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
96
|
|
|
$Merchant->name = $merchant['name']; |
97
|
|
|
$arrResult[] = $Merchant; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $arrResult; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int|null $merchantID |
105
|
|
|
* @param int $page |
106
|
|
|
* @param int $items_per_page |
107
|
|
|
* |
108
|
|
|
* @return DealsResultset |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if (!isIntegerPositive($items_per_page)){ |
113
|
|
|
$items_per_page=10; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
$result=DealsResultset::createInstance(); |
116
|
|
|
if (!$this->checkLogin()) { |
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
$arrResult = array(); |
120
|
|
|
$jsonVouchers = file_get_contents("https://api.tradedoubler.com/1.0/vouchers.json;dateOutputFormat=iso8601?token=".$_ENV['TRADEDOUBLER_TOKEN']); |
121
|
|
|
$arrVouchers = json_decode($jsonVouchers, true); |
122
|
|
|
|
123
|
|
|
foreach($arrVouchers as $voucher) { |
124
|
|
|
$Deal = Deal::createInstance(); |
125
|
|
|
$Deal->setValues($voucher, [ |
126
|
|
|
'id' => 'deal_ID' , |
127
|
|
|
'programId' => 'merchant_ID' , |
128
|
|
|
'code' => 'code' , |
129
|
|
|
'updateDate' => 'update_date' , |
130
|
|
|
'publishStartDate' => 'publish_start_date' , |
131
|
|
|
'publishEndDate' => 'publish_end_date' , |
132
|
|
|
'startDate' => 'start_date' , |
133
|
|
|
'endDate' => 'end_date' , |
134
|
|
|
'title' => 'name' , |
135
|
|
|
'shortDescription' => 'short_description' , |
136
|
|
|
'description' => 'description' , |
137
|
|
|
'voucherTypeId' => 'deal_type' , |
138
|
|
|
'defaultTrackUri' => 'default_track_uri' , |
139
|
|
|
'landingUrl' => 'landing_url' , |
140
|
|
|
'discountAmount' => 'discount_amount' , |
141
|
|
|
'isPercentage' => 'is_percentage' , |
142
|
|
|
'publisherInformation' => 'information' , |
143
|
|
|
'languageId' => 'language' , |
144
|
|
|
'exclusive' => 'is_exclusive' , |
145
|
|
|
'siteSpecific' => 'is_site_specific' , |
146
|
|
|
'currencyId' => 'currency_initial' , |
147
|
|
|
'logoPath' => 'logo_path' , |
148
|
|
|
]); |
149
|
|
|
switch ($voucher['voucherTypeId']) { |
150
|
|
|
case 1: |
151
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER; |
152
|
|
|
break; |
153
|
|
|
case 2: |
154
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT; |
155
|
|
|
break; |
156
|
|
|
case 3: |
157
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_ARTICLE; |
158
|
|
|
break; |
159
|
|
|
case 4: |
160
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_SHIPPING; |
161
|
|
|
break; |
162
|
|
|
case 5: |
163
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_LOTTERY; |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if($merchantID > 0) { |
168
|
|
|
if($voucher['programId'] == $merchantID) { |
169
|
|
|
$arrResult[] = $Deal; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
else { |
173
|
|
|
$arrResult[] = $Deal; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
$result->deals[]=$arrResult; |
177
|
|
|
return $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param \DateTime $dateFrom |
182
|
|
|
* @param \DateTime $dateTo |
183
|
|
|
* @param int $merchantID |
|
|
|
|
184
|
|
|
* @return array of Transaction |
185
|
|
|
*/ |
186
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
187
|
|
|
{ |
188
|
|
|
if (!$this->checkLogin()) { |
189
|
|
|
return array(); |
190
|
|
|
} |
191
|
|
|
$arrResult = array(); |
192
|
|
View Code Duplication |
if (count( $arrMerchantID ) < 1) { |
|
|
|
|
193
|
|
|
$merchants = $this->getMerchants(); |
194
|
|
|
foreach ($merchants as $merchant) { |
195
|
|
|
$arrMerchantID[$merchant->merchant_ID] = ['cid' => $merchant->merchant_ID, 'name' => $merchant->name]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
$transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo); |
199
|
|
View Code Duplication |
foreach($transactionList as $transaction) { |
|
|
|
|
200
|
|
|
$Transaction = Transaction::createInstance(); |
201
|
|
|
$Transaction->merchant_ID = $transaction['merchantId']; |
202
|
|
|
$date = new \DateTime($transaction['date']); |
203
|
|
|
$Transaction->date = $date; // $date->format('Y-m-d H:i:s'); |
|
|
|
|
204
|
|
|
$Transaction->unique_ID = $transaction['unique_id']; |
205
|
|
|
$Transaction->transaction_ID = $transaction['unique_id'] . '-' . $transaction['event_id']; |
206
|
|
|
array_key_exists_safe( $transaction, |
207
|
|
|
'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
208
|
|
|
$Transaction->status = $transaction['status']; |
209
|
|
|
$Transaction->amount = $transaction['amount']; |
210
|
|
|
$Transaction->commission = $transaction['commission']; |
211
|
|
|
$Transaction->currency = $transaction['currency']; |
212
|
|
|
$arrResult[] = $Transaction; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $arrResult; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param \DateTime $dateFrom |
220
|
|
|
* @param \DateTime $dateTo |
221
|
|
|
* @param int $merchantID |
222
|
|
|
* @return array of Stat |
223
|
|
|
*/ |
224
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
225
|
|
|
{ |
226
|
|
|
return array(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param array $params |
232
|
|
|
* |
233
|
|
|
* @return ProductsResultset |
234
|
|
|
*/ |
235
|
|
|
public function getProducts(array $params = []): ProductsResultset |
236
|
|
|
{ |
237
|
|
|
// TODO: Implement getProducts() method. |
238
|
|
|
throw new \Exception("Not implemented yet"); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function getTrackingParameter(){ |
242
|
|
|
return $this->_tracking_parameter; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
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.