/Oara/Network/WebGains.php

http://github.com/fubralimited/php-oara · PHP · 325 lines · 224 code · 32 blank · 69 comment · 35 complexity · 3cd7f309760ad323c1df37087f6c276d MD5 · raw file

  1. <?php
  2. /**
  3. * Api Class
  4. *
  5. * @author Carlos Morillo Merino
  6. * @category Oara_Network_Wg
  7. * @copyright Fubra Limited
  8. * @version Release: 01.00
  9. *
  10. */
  11. class Oara_Network_WebGains extends Oara_Network{
  12. /**
  13. * Soap client.
  14. */
  15. private $_soapClient = null;
  16. /**
  17. * Web client.
  18. */
  19. private $_webClient = null;
  20. /**
  21. * Export Merchant Parameters
  22. * @var array
  23. */
  24. private $_exportMerchantParameters = null;
  25. /**
  26. * Export Transaction Parameters
  27. * @var array
  28. */
  29. private $_exportTransactionParameters = null;
  30. /**
  31. * Export Overview Parameters
  32. * @var array
  33. */
  34. private $_exportOverviewParameters = null;
  35. /**
  36. * Converter configuration for the merchants.
  37. * @var array
  38. */
  39. private $_merchantConverterConfiguration = Array ('programID'=>'cid',
  40. 'programName'=>'name',
  41. 'programURL'=>'url',
  42. 'programDescription'=>'description'
  43. );
  44. /**
  45. * Converter configuration for the transactions.
  46. * @var array
  47. */
  48. private $_transactionConverterConfiguration = Array ('status'=>'status',
  49. 'saleValue'=>'amount',
  50. 'commission'=>'commission',
  51. 'date'=>'date',
  52. 'merchantId'=>'merchantId',
  53. 'custom_id'=>'clickRef',
  54. );
  55. /**
  56. * Array with the id from the campaigns
  57. * @var array
  58. */
  59. private $_campaignMap = array();
  60. /**
  61. * Constructor.
  62. * @param $webgains
  63. * @return Oara_Network_Wg_Api
  64. */
  65. public function __construct($credentials)
  66. {
  67. $user = $credentials['user'];
  68. $password = $credentials['password'];
  69. $wsdlUrl = 'http://ws.webgains.com/aws.php';
  70. //Setting the client.
  71. $this->_soapClient = new Zend_Soap_Client($wsdlUrl, array('login' => $user,
  72. 'encoding' => 'UTF-8',
  73. 'password' => $password,
  74. 'compression'=> SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
  75. 'soap_version' => SOAP_1_1));
  76. $loginUrl = 'https://www.webgains.com/loginform.html?action=login';
  77. $valuesLogin = array(
  78. new Oara_Curl_Parameter('screenwidth', 1280),
  79. new Oara_Curl_Parameter('screenheight', 768),
  80. new Oara_Curl_Parameter('colourdepth', 32),
  81. new Oara_Curl_Parameter('user_type', 'affiliateuser'),
  82. new Oara_Curl_Parameter('username', $user),
  83. new Oara_Curl_Parameter('password', $password),
  84. new Oara_Curl_Parameter('submitbutton', 'PLS WAIT')
  85. );
  86. $this->_webClient = new Oara_Curl_Access($loginUrl, $valuesLogin, $credentials);
  87. $this->_exportMerchantParameters = array('username' => $user,
  88. 'password' => $password
  89. );
  90. $this->_exportTransactionParameters = array('username' => $user,
  91. 'password' => $password
  92. );
  93. $this->_exportOverviewParameters = array('username' => $user,
  94. 'password' => $password
  95. );
  96. }
  97. /**
  98. * Check the connection
  99. */
  100. public function checkConnection(){
  101. $connection = false;
  102. $urls = array();
  103. $urls[] = new Oara_Curl_Request('http://www.webgains.com/affiliates/index.html', array());
  104. $exportReport = $this->_webClient->get($urls);
  105. if (preg_match("/\/affiliates\/logout\.html/", $exportReport[0])){
  106. $connection = true;
  107. }
  108. return $connection;
  109. }
  110. /**
  111. * (non-PHPdoc)
  112. * @see library/Oara/Network/Oara_Network_Base#getMerchantList()
  113. */
  114. public function getMerchantList()
  115. {
  116. $this->_campaignMap = self::getCampaignMap();
  117. $merchantList = Array();
  118. foreach ($this->_campaignMap as $campaignKey => $campaignValue){
  119. $merchants = $this->_soapClient->getProgramsWithMembershipStatus($this->_exportMerchantParameters['username'], $this->_exportMerchantParameters['password'], $campaignKey);
  120. foreach ($merchants as $merchant){
  121. if ($merchant->programMembershipStatusName == 'Live'){
  122. $merchantList[$merchant->programID] = $merchant;
  123. }
  124. }
  125. }
  126. $merchantList = Oara_Utilities::soapConverter($merchantList, $this->_merchantConverterConfiguration);
  127. return $merchantList;
  128. }
  129. /**
  130. * (non-PHPdoc)
  131. * @see library/Oara/Network/Oara_Network_Base#getTransactionList($merchantId,$dStartDate,$dEndDate)
  132. */
  133. public function getTransactionList($merchantList = null, Zend_Date $dStartDate = null, Zend_Date $dEndDate = null, $merchantMap = null)
  134. {
  135. $totalTransactions = Array();
  136. $dStartDate = clone $dStartDate;
  137. $dStartDate->setHour("00");
  138. $dStartDate->setMinute("00");
  139. $dStartDate->setSecond("00");
  140. $dEndDate = clone $dEndDate;
  141. $dEndDate->setHour("23");
  142. $dEndDate->setMinute("59");
  143. $dEndDate->setSecond("59");
  144. foreach ($this->_campaignMap as $campaignKey => $campaignValue){
  145. $transactionList = $this->_soapClient->getDetailedEarnings($dStartDate->getIso(), $dEndDate->getIso(),
  146. $campaignKey, $this->_exportTransactionParameters['username'],
  147. $this->_exportTransactionParameters['password']);
  148. foreach ($transactionList as $transaction){
  149. if (in_array($transaction->programID, $merchantList)){
  150. $transaction->merchantId = $transaction->programID;
  151. if ($transaction->status == 'confirmed'){
  152. $transaction->status = Oara_Utilities::STATUS_CONFIRMED;
  153. } else if ($transaction->status == 'delayed') {
  154. $transaction->status = Oara_Utilities::STATUS_PENDING;
  155. } else if ($transaction->status == 'cancelled'){
  156. $transaction->status = Oara_Utilities::STATUS_DECLINED;
  157. } else{
  158. throw new Exception('Error in the transaction status');
  159. }
  160. $transactionDate = new Zend_Date($transaction->date,"yyyy-MM-ddTHH:mm:ss");
  161. $transaction->date = $transactionDate->toString("yyyy-MM-dd HH:mm:ss");
  162. $totalTransactions[] = $transaction;
  163. }
  164. }
  165. }
  166. $totalTransactions = Oara_Utilities::soapConverter($totalTransactions, $this->_transactionConverterConfiguration);
  167. return $totalTransactions;
  168. }
  169. /**
  170. * (non-PHPdoc)
  171. * @see library/Oara/Network/Oara_Network_Base#getOverviewList($merchantId,$dStartDate,$dEndDate)
  172. */
  173. public function getOverviewList($transactionList = array(), $merchantList = null, Zend_Date $dStartDate = null, Zend_Date $dEndDate = null, $merchantMap = null)
  174. {
  175. $totalOverviews = Array();
  176. $transactionArray = Oara_Utilities::transactionMapPerDay($transactionList);
  177. foreach ($transactionArray as $merchantId => $merchantTransaction){
  178. foreach ($merchantTransaction as $date => $transactionList){
  179. $overview = Array();
  180. $overview['merchantId'] = $merchantId;
  181. $overviewDate = new Zend_Date($date, "yyyy-MM-dd");
  182. $overview['date'] = $overviewDate->toString("yyyy-MM-dd HH:mm:ss");
  183. $overview['click_number'] = 0;
  184. $overview['impression_number'] = 0;
  185. $overview['transaction_number'] = 0;
  186. $overview['transaction_confirmed_value'] = 0;
  187. $overview['transaction_confirmed_commission']= 0;
  188. $overview['transaction_pending_value']= 0;
  189. $overview['transaction_pending_commission']= 0;
  190. $overview['transaction_declined_value']= 0;
  191. $overview['transaction_declined_commission']= 0;
  192. $overview['transaction_paid_value']= 0;
  193. $overview['transaction_paid_commission']= 0;
  194. foreach ($transactionList as $transaction){
  195. $overview['transaction_number'] ++;
  196. if ($transaction['status'] == Oara_Utilities::STATUS_CONFIRMED){
  197. $overview['transaction_confirmed_value'] += $transaction['amount'];
  198. $overview['transaction_confirmed_commission'] += $transaction['commission'];
  199. } else if ($transaction['status'] == Oara_Utilities::STATUS_PENDING){
  200. $overview['transaction_pending_value'] += $transaction['amount'];
  201. $overview['transaction_pending_commission'] += $transaction['commission'];
  202. } else if ($transaction['status'] == Oara_Utilities::STATUS_DECLINED){
  203. $overview['transaction_declined_value'] += $transaction['amount'];
  204. $overview['transaction_declined_commission'] += $transaction['commission'];
  205. } else if ($transaction['status'] == Oara_Utilities::STATUS_PAID){
  206. $overview['transaction_paid_value'] += $transaction['amount'];
  207. $overview['transaction_paid_commission'] += $transaction['commission'];
  208. }
  209. }
  210. $totalOverviews[] = $overview;
  211. }
  212. }
  213. return $totalOverviews;
  214. }
  215. /**
  216. * Get the campaings identifiers and returns it in an array.
  217. * @return array
  218. */
  219. private function getCampaignMap(){
  220. $campaingMap = array();
  221. $urls = array();
  222. $urls[] = new Oara_Curl_Request('http://www.webgains.com/affiliates/report.html?f=0&action=sf', array());
  223. $exportReport = $this->_webClient->get($urls);
  224. $matches = array();
  225. if (preg_match("/<select name=\"campaignswitchid\" class=\"formelement\" style=\"width:134px\">([^\t]*)<\/select>/",
  226. $exportReport[0], $matches)){
  227. if (preg_match_all("/<option value=\"(.*)\" .*>(.*)<\/option>/", $matches[1], $matches)){
  228. $campaingNumber = count($matches[1]);
  229. $i = 0;
  230. while ($i < $campaingNumber){
  231. $campaingMap[$matches[1][$i]] = $matches[2][$i];
  232. $i++;
  233. }
  234. } else{
  235. throw new Exception('No campaigns found');
  236. }
  237. } else {
  238. throw new Exception ("No campaigns found");
  239. }
  240. return $campaingMap;
  241. }
  242. /**
  243. * (non-PHPdoc)
  244. * @see Oara/Network/Oara_Network_Base#getPaymentHistory()
  245. */
  246. public function getPaymentHistory(){
  247. $paymentHistory = array();
  248. $urls = array();
  249. $urls[] = new Oara_Curl_Request('https://www.webgains.com/affiliates/payment.html', array());
  250. $exportReport = $this->_webClient->get($urls);
  251. /*** load the html into the object ***/
  252. $doc = new DOMDocument();
  253. libxml_use_internal_errors(true);
  254. $doc->validateOnParse = true;
  255. $doc->loadHTML($exportReport[0]);
  256. $tableList = $doc->getElementsByTagName('table');
  257. $i = 0;
  258. $enc = false;
  259. while ($i < $tableList->length && !$enc) {
  260. $registerTable = $tableList->item($i);
  261. if ($registerTable->getAttribute('class') == 'withgrid'){
  262. $enc = true;
  263. }
  264. $i++;
  265. }
  266. if (!$enc){
  267. throw new Exception ('Fail getting the payment History');
  268. }
  269. $registerLines = $registerTable->childNodes;
  270. for ($i = 2;$i < $registerLines->length ;$i++) {
  271. $obj = array();
  272. $linkList = $registerLines->item($i)->getElementsByTagName('a');
  273. $url = $linkList->item(1)->attributes->getNamedItem("href")->nodeValue;
  274. $parseUrl = parse_url(trim($url));
  275. $parameters = explode('&', $parseUrl['query']);
  276. foreach($parameters as $parameter){
  277. $parameterValue = explode('=', $parameter);
  278. if ($parameterValue[0] == 'payment'){
  279. $obj['pid'] = $parameterValue[1];
  280. }
  281. }
  282. $registerLine = $registerLines->item($i)->childNodes;
  283. $date = new Zend_Date($registerLine->item(0)->nodeValue, "dd/MM/yy");
  284. $obj['date'] = $date->toString("yyyy-MM-dd HH:mm:ss");
  285. $value = $registerLine->item(2)->nodeValue;
  286. preg_match( '/[0-9]+(,[0-9]{3})*(\.[0-9]{2})?$/', $value, $matches);
  287. $obj['value'] = Oara_Utilities::parseDouble($matches[0]);
  288. $obj['method'] = $registerLine->item(6)->nodeValue;
  289. $paymentHistory[] = $obj;
  290. }
  291. return $paymentHistory;
  292. }
  293. }