|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padosoft\AffiliateNetwork\Networks; |
|
4
|
|
|
|
|
5
|
|
|
use Padosoft\AffiliateNetwork\Transaction; |
|
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
|
|
|
|
|
12
|
|
|
// require "../vendor/fubralimited/php-oara/Oara/Network/Publisher/CommissionJunction/Zapi/ApiClient.php"; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CommissionJunction |
|
16
|
|
|
* @package Padosoft\AffiliateNetwork\Networks |
|
17
|
|
|
*/ |
|
18
|
|
|
class CommissionJunction 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 $_passwordApi = ''; |
|
28
|
|
|
private $_website_id = ''; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @method __construct |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(string $username, string $password, string $passwordApi, string $website_id) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->_network = new \Oara\Network\Publisher\CommissionJunction; |
|
36
|
|
|
$this->_username = $username; |
|
37
|
|
|
$this->_password = $password; |
|
38
|
|
|
$this->_passwordApi = $passwordApi; |
|
39
|
|
|
$this->_website_id = $website_id; |
|
40
|
|
|
// $this->_apiClient = \ApiClient::factory(PROTOCOL_JSON); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
public function checkLogin() : bool |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$credentials = array(); |
|
49
|
|
|
$credentials["user"] = $this->_username; |
|
50
|
|
|
$credentials["password"] = $this->_password; |
|
51
|
|
|
$credentials["apipassword"] = $this->_passwordApi; |
|
52
|
|
|
|
|
53
|
|
|
$this->_network->login($credentials); |
|
54
|
|
|
if ($this->_network->checkConnection()) { |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return array of Merchants |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getMerchants() : array |
|
65
|
|
|
{ |
|
66
|
|
|
$arrResult = array(); |
|
67
|
|
|
$merchantList = $this->_network->getMerchantList(); |
|
68
|
|
|
foreach($merchantList as $merchant) { |
|
69
|
|
|
$Merchant = Merchant::createInstance(); |
|
70
|
|
|
$Merchant->merchant_ID = $merchant['cid']; |
|
71
|
|
|
$Merchant->name = $merchant['name']; |
|
72
|
|
|
$arrResult[] = $Merchant; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $arrResult; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param int $merchantID |
|
80
|
|
|
* @return array of Deal |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getDeals(int $merchantID = 0) : array |
|
83
|
|
|
{ |
|
84
|
|
|
$response = $this->_apiCall('https://link-search.api.cj.com/v2/link-search?website-id='.$this->_website_id.'&promotion-type=coupon&advertiser-ids=joined'); |
|
85
|
|
|
if (\preg_match("/error/", $response)) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
$arrResult = array(); |
|
89
|
|
|
$arrResponse = xml2array($response); |
|
90
|
|
|
if(!is_array($arrResponse) || count($arrResponse) <= 0) { |
|
91
|
|
|
return $arrResult; |
|
92
|
|
|
} |
|
93
|
|
|
$arrCoupon = $arrResponse['cj-api']['links']['link']; |
|
94
|
|
|
foreach($arrCoupon as $coupon) { |
|
95
|
|
|
$Deal = Deal::createInstance(); |
|
96
|
|
|
$Deal->merchant_ID = $coupon['advertiser-id']; |
|
97
|
|
|
$Deal->merchant_name = $coupon['advertiser-name']; |
|
98
|
|
|
$Deal->ppc = $coupon['click-commission']; |
|
99
|
|
|
$Deal->description = $coupon['description']; |
|
100
|
|
|
$startDate = new \DateTime($coupon['promotion-start-date']); |
|
101
|
|
|
$Deal->startDate = $startDate; |
|
102
|
|
|
$startDate = new \DateTime($coupon['promotion-end-date']); |
|
|
|
|
|
|
103
|
|
|
$Deal->endDate = $endDate; |
|
|
|
|
|
|
104
|
|
|
$Deal->code = $coupon['coupon-code']; |
|
105
|
|
|
if($merchantID > 0) { |
|
106
|
|
|
if($merchantID == $coupon['advertiser-id']) { |
|
107
|
|
|
$arrResult[] = $Deal; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
else { |
|
111
|
|
|
$arrResult[] = $Deal; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $arrResult; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param \DateTime $dateFrom |
|
120
|
|
|
* @param \DateTime $dateTo |
|
121
|
|
|
* @param int $merchantID |
|
|
|
|
|
|
122
|
|
|
* @return array of Transaction |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
|
125
|
|
|
{ |
|
126
|
|
|
$arrResult = array(); |
|
127
|
|
|
$transcationList = $this->_network->getTransactionList($arrMerchantID, $dateTo, $dateFrom); |
|
128
|
|
|
foreach($transcationList as $transaction) { |
|
129
|
|
|
$Transaction = Transaction::createInstance(); |
|
130
|
|
|
$Transaction->status = $transaction['status']; |
|
131
|
|
|
$Transaction->amount = $transaction['amount']; |
|
132
|
|
|
$Transaction->custom_ID = $transaction['custom_id']; |
|
133
|
|
|
$Transaction->unique_ID = $transaction['unique_id']; |
|
134
|
|
|
$Transaction->commission = $transaction['commission']; |
|
135
|
|
|
$date = new \DateTime($transaction['date']); |
|
136
|
|
|
$Transaction->date = $date; // $date->format('Y-m-d H:i:s'); |
|
|
|
|
|
|
137
|
|
|
$Transaction->merchant_ID = $transaction['merchantId']; |
|
138
|
|
|
$arrResult[] = $Transaction; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $arrResult; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param \DateTime $dateFrom |
|
146
|
|
|
* @param \DateTime $dateTo |
|
147
|
|
|
* @param int $merchantID |
|
148
|
|
|
* @return array of Stat |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
|
151
|
|
|
{ |
|
152
|
|
|
return array(); |
|
153
|
|
|
/* |
|
|
|
|
|
|
154
|
|
|
$this->_apiClient->setConnectId($this->_username); |
|
155
|
|
|
$this->_apiClient->setSecretKey($this->_password); |
|
156
|
|
|
$dateFromIsoEngFormat = $dateFrom->format('Y-m-d'); |
|
157
|
|
|
$dateToIsoEngFormat = $dateTo->format('Y-m-d'); |
|
158
|
|
|
$response = $this->_apiClient->getReportBasic($dateFromIsoEngFormat, $dateToIsoEngFormat); |
|
159
|
|
|
$arrResponse = json_decode($response, true); |
|
160
|
|
|
$reportItems = $arrResponse['reportItems']; |
|
161
|
|
|
$Stat = Stat::createInstance(); |
|
162
|
|
|
$Stat->reportItems = $reportItems; |
|
163
|
|
|
|
|
164
|
|
|
return array($Stat); |
|
165
|
|
|
*/ |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Api call CommissionJunction |
|
170
|
|
|
*/ |
|
171
|
|
|
private function _apiCall($url) |
|
172
|
|
|
{ |
|
173
|
|
|
$ch = curl_init(); |
|
174
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
175
|
|
|
curl_setopt($ch, CURLOPT_POST, FALSE); |
|
176
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
|
177
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: " . $this->_passwordApi)); |
|
178
|
|
|
$curl_results = curl_exec($ch); |
|
179
|
|
|
curl_close($ch); |
|
180
|
|
|
return $curl_results; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.