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