1
|
|
|
<?php |
2
|
|
|
namespace Padosoft\AffiliateNetwork\Networks; |
3
|
|
|
|
4
|
|
|
use Padosoft\AffiliateNetwork\Transaction; |
5
|
|
|
use Padosoft\AffiliateNetwork\DealsResultset; |
6
|
|
|
use Padosoft\AffiliateNetwork\Merchant; |
7
|
|
|
use Padosoft\AffiliateNetwork\Stat; |
8
|
|
|
use Padosoft\AffiliateNetwork\Deal; |
9
|
|
|
use Padosoft\AffiliateNetwork\AbstractNetwork; |
10
|
|
|
use Padosoft\AffiliateNetwork\NetworkInterface; |
11
|
|
|
use Padosoft\AffiliateNetwork\ProductsResultset; |
12
|
|
|
use \Oara\Network\Publisher\ImpactRadius as OaraImpactRadius; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ImpactRadius |
16
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
17
|
|
|
*/ |
18
|
|
|
class ImpactRadius extends AbstractNetwork implements NetworkInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var object |
22
|
|
|
*/ |
23
|
|
|
private $_network = null; |
24
|
|
|
private $_apiClient = null; |
25
|
|
|
private $_username = ''; |
26
|
|
|
private $_password = ''; |
27
|
|
|
private $_logged = false; |
28
|
|
|
protected $_tracking_parameter = 'subId1'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @method __construct |
32
|
|
|
*/ |
33
|
|
|
public function __construct(string $username, string $password) |
34
|
|
|
{ |
35
|
|
|
$this->_network = new OaraImpactRadius; |
36
|
|
|
|
37
|
|
|
$this->_username = $username; |
38
|
|
|
$this->_password = $password; |
39
|
|
|
$this->_apiClient = null; |
40
|
|
|
$this->login( $this->_username, $this->_password ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function login(string $username, string $password): bool |
44
|
|
|
{ |
45
|
|
|
if ($this->_username == $username && $this->_password == $password && $this->_network->checkConnection()) { |
46
|
|
|
$this->_logged = true; |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
$this->_logged = false; |
50
|
|
|
if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
$this->_username = $username; |
54
|
|
|
$this->_password = $password; |
55
|
|
|
$credentials = array(); |
56
|
|
|
// Old |
57
|
|
|
// $credentials["user"] = $this->_username; |
58
|
|
|
// $credentials["password"] = $this->_password; |
59
|
|
|
// New - need sid/token pair to access API directly ... |
60
|
|
|
// ... Login simulation using username/password no longer works due to a new browser js challenge script - 2019-03-01 <PN> |
61
|
|
|
$credentials["api-sid"] = $this->_username; |
62
|
|
|
$credentials["api-token"] = $this->_password; |
63
|
|
|
|
64
|
|
|
$this->_network->login( $credentials ); |
65
|
|
|
if ($this->_network->checkConnection()) { |
66
|
|
|
$this->_logged = true; |
67
|
|
|
} |
68
|
|
|
return $this->_logged; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function checkLogin() : bool |
75
|
|
|
{ |
76
|
|
|
return $this->_logged; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array of Merchants |
81
|
|
|
*/ |
82
|
|
|
public function getMerchants() : array |
83
|
|
|
{ |
84
|
|
|
if (!$this->checkLogin()) { |
85
|
|
|
return array(); |
86
|
|
|
} |
87
|
|
|
$arrResult = array(); |
88
|
|
|
$merchantList = $this->_network->getMerchantList(); |
89
|
|
|
foreach($merchantList as $merchant) { |
90
|
|
|
$Merchant = Merchant::createInstance(); |
91
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
92
|
|
|
$Merchant->name = $merchant['name']; |
93
|
|
|
$Merchant->url = $merchant['url']; |
94
|
|
|
$Merchant->status = $merchant['status']; |
95
|
|
|
$arrResult[] = $Merchant; |
96
|
|
|
} |
97
|
|
|
return $arrResult; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int|null $merchantID |
102
|
|
|
* @param int $page |
103
|
|
|
* @param int $items_per_page |
104
|
|
|
* |
105
|
|
|
* @return DealsResultset |
106
|
|
|
*/ |
107
|
|
|
public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset |
108
|
|
|
{ |
109
|
|
|
$result = DealsResultset::createInstance(); |
110
|
|
|
$arrResult = array(); |
111
|
|
|
|
112
|
|
|
$dealsList = $this->_network->getDeals(); |
113
|
|
|
foreach($dealsList as $dealItem) { |
114
|
|
|
|
115
|
|
|
$Deal = Deal::createInstance(); |
116
|
|
|
$Deal->deal_ID = $dealItem['id']; |
117
|
|
|
$Deal->merchant_ID = $dealItem['campaign_id']; |
118
|
|
|
$Deal->code = $dealItem['promo_code']; |
119
|
|
|
$Deal->description = $dealItem['description']; |
120
|
|
|
$Deal->start_date = $Deal->convertDate($dealItem['start_date']); |
|
|
|
|
121
|
|
|
$Deal->end_date = $Deal->convertDate($dealItem['end_date']); |
|
|
|
|
122
|
|
|
$Deal->default_track_uri = $dealItem['url']; |
123
|
|
|
switch ($dealItem['type']) { |
124
|
|
|
case 'GENERAL_SALE': |
125
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_VOUCHER; |
126
|
|
|
break; |
127
|
|
|
case 'FREE_SHIPPING': |
128
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_SHIPPING; |
129
|
|
|
break; |
130
|
|
|
case 'GIFT_WITH_PURCHASE': |
131
|
|
|
case 'BOGO': // Buy One Get One |
132
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_FREE_ARTICLE; |
133
|
|
|
break; |
134
|
|
|
case 'REBATE': |
135
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT; |
136
|
|
|
break; |
137
|
|
|
default: |
138
|
|
|
echo "Impact Radius - Deal id " . $dealItem['id'] . " Program " . $dealItem['campaign_name'] . " - Unexpected Deal type:" . $dealItem['type'] ."<br>"; |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
if ($dealItem['discount_type'] == 'PERCENT' || $dealItem['discount_type'] == 'PERCENT_MAXIMUM') { |
142
|
|
|
$Deal->is_percentage = true; |
143
|
|
|
$Deal->discount_amount = $dealItem['discount_percent']; |
144
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT; |
145
|
|
|
} elseif ($dealItem['discount_type'] == 'PERCENT_RANGE') { |
146
|
|
|
$Deal->is_percentage = true; |
147
|
|
|
$Deal->discount_amount = $dealItem['discount_percent_range_max']; |
148
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT; |
149
|
|
|
} elseif ($dealItem['discount_type'] == 'FIXED') { |
150
|
|
|
$Deal->is_percentage = false; |
151
|
|
|
$Deal->discount_amount = $dealItem['discount_amount']; |
152
|
|
|
$Deal->deal_type = \Oara\Utilities::OFFER_TYPE_DISCOUNT; |
153
|
|
|
} |
154
|
|
|
else { |
155
|
|
|
if ($dealItem['status'] != 'EXPIRED') { |
156
|
|
|
echo "Impact Radius - Deal id " . $dealItem['id'] . " Program " . $dealItem['campaign_name'] . " - Unhandled Discount type:" . $dealItem['discount_type'] . PHP_EOL; |
157
|
|
|
$Deal->is_percentage = false; |
158
|
|
|
$Deal->amount = $dealItem['discount_amount']; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
$Deal->minimum_order_value = $dealItem['minimum_purchase']; |
162
|
|
|
|
163
|
|
|
$arrResult[] = $Deal; |
164
|
|
|
} |
165
|
|
|
$result->deals[]=$arrResult; |
166
|
|
|
return $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param \DateTime $dateFrom |
171
|
|
|
* @param \DateTime $dateTo |
172
|
|
|
* @param int $merchantID |
|
|
|
|
173
|
|
|
* @return array of Transaction |
174
|
|
|
*/ |
175
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
176
|
|
|
{ |
177
|
|
|
$arrResult = array(); |
178
|
|
|
|
179
|
|
|
try { |
180
|
|
|
// Added timezone parameter |
181
|
|
|
$transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC'); |
182
|
|
|
|
183
|
|
|
if (is_array($transactionList)) { |
184
|
|
|
foreach ($transactionList as $transaction) { |
185
|
|
|
try { |
186
|
|
|
$myTransaction = Transaction::createInstance(); |
187
|
|
|
$myTransaction->unique_ID = $transaction['unique_id']; |
188
|
|
|
$myTransaction->merchant_ID = $transaction['merchant_id']; |
189
|
|
|
$date = new \DateTime($transaction['date']); |
190
|
|
|
$myTransaction->date = $date; |
|
|
|
|
191
|
|
|
$myTransaction->date_click = $transaction['date_click']; |
|
|
|
|
192
|
|
|
if (isset($transaction['custom_id'])) { |
193
|
|
|
$myTransaction->custom_ID = $transaction['custom_id']; |
194
|
|
|
} |
195
|
|
|
else { |
196
|
|
|
$myTransaction->custom_ID = null; |
197
|
|
|
} |
198
|
|
|
$myTransaction->amount = $transaction['amount']; |
199
|
|
|
$myTransaction->commission = $transaction['commission']; |
200
|
|
|
$myTransaction->currency = $transaction['currency']; |
201
|
|
|
$myTransaction->status = $transaction['status']; |
202
|
|
|
$arrResult[] = $myTransaction; |
203
|
|
|
} catch (\Exception $e) { |
204
|
|
|
echo "<br><br>Transaction Error Impact Radius, id: ".$myTransaction->unique_ID." msg: ".$e->getMessage()."<br><br>"; |
205
|
|
|
var_dump($e->getTraceAsString()); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} catch (\Exception $e) { |
210
|
|
|
echo "<br><br>Generic Error Impact Radius: ".$e->getMessage()."<br><br>"; |
211
|
|
|
var_dump($e->getTraceAsString()); |
212
|
|
|
throw new \Exception($e); |
213
|
|
|
} |
214
|
|
|
return $arrResult; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param \DateTime $dateFrom |
219
|
|
|
* @param \DateTime $dateTo |
220
|
|
|
* @param int $merchantID |
221
|
|
|
* @return array of Stat |
222
|
|
|
*/ |
223
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
224
|
|
|
{ |
225
|
|
|
// TODO |
226
|
|
|
throw new \Exception("Not implemented yet"); |
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
|
|
|
/** |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getTrackingParameter(){ |
245
|
|
|
return $this->_tracking_parameter; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.