Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.07 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\OutgoingApi;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use Validator;
  8. use App\Lead;
  9. use App\Source;
  10. use App\Country;
  11. use App\OrderLead;
  12. use Illuminate\Support\Facades\Log;
  13.  
  14. class MaxiPartnersHummerApiController extends Controller {
  15.  
  16.  
  17.     public function send($order, $lead, $apiUser = null, $type = 0) {
  18.  
  19.         $apiOptions = $order->getOptionsVals();
  20.  
  21.         $country_iso = "";
  22.  
  23.         $country = Country::where('id', '=',$lead->country_id)->first();
  24.         if(!is_null($country)){
  25.             $country_iso = $country->code;
  26.         }
  27.  
  28.         $language =  @$apiOptions['defaultLanguage'];
  29.         if(!is_null($lead->language)){
  30.             $language = $lead->language;
  31.         }
  32.  
  33.         $ipAddress = $lead->ip_address;
  34.  
  35.         if($ipAddress == "" || $ipAddress == "::1" || is_null($ipAddress)){
  36.             $ipAddress = $lead->getIpByCountry();
  37.         }
  38.  
  39.         $url = "http://api-proxy.crhummer.mine.nu/Api";
  40.  
  41.         $apiUserName = $apiOptions['username'];
  42.         $apiPassword = $apiOptions['password'];
  43.         $campaignId =  $apiOptions['campaign_id'];
  44.         $curr = 'USD';
  45.  
  46.         //Get CountryCode--------------------------------------------------
  47.         $post_country = array(
  48.             'api_username' => $apiOptions['username'],
  49.             'api_password' => $apiOptions['password'],
  50.             'jsonResponse' => 'true',
  51.             'MODULE' => 'Country',
  52.             'COMMAND' => 'view',
  53.             'FILTER[iso]' => $country_iso,
  54.         );
  55.  
  56.         $result_country = $this->curl_post($url, $post_country);
  57.         $objCountry = json_decode($result_country);
  58.         if(!is_null($objCountry)){
  59.             $country_id = $objCountry->status->Country->data_0->id;
  60.         }else $country_id = 0;
  61.  
  62.  
  63.  
  64.  
  65.         //-----------------------------------------------------------------
  66.  
  67.         $userPassword = 'none123456';
  68.         $userFirstName = $lead->first_name;
  69.         $userLastName =  $lead->last_name;
  70.         $email = $lead->email;
  71.         $phone = $lead->phone_prefix.$lead->phone;
  72.  
  73.         $fields = array(
  74.             'MODULE' => 'Customer',
  75.             'COMMAND' => 'add',
  76.             'api_username' => $apiUserName, //The API Username
  77.             'api_password' => $apiPassword, //The API Password
  78.             'campaignId' => $campaignId,
  79.             'subCampaign' => '',
  80.             'FirstName' => urlencode($userFirstName),
  81.             'LastName' => urlencode($userLastName),
  82.             //'gender' => 'male',
  83.             'email' => urlencode($email),
  84.             'Phone' => urlencode($phone),
  85.             'Country' => $country_id,
  86.             'password' => urlencode($userPassword),
  87.             'currency' => $curr,
  88.             //'birthday' => $birthday,
  89.             //'a_aid' => '1934',
  90.         );
  91.  
  92.         $headers = array(
  93.             'Content-Type' => 'application/x-www-form-urlencoded',
  94.         );
  95.  
  96.         $fields_string = '';
  97.  
  98.         $result = "";
  99.         $apiMessage = "";
  100.         $apiStatus = "";
  101.         //url-ify the data for the POST
  102.         foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  103.  
  104.         rtrim($fields_string, '&');
  105.  
  106.         try {
  107.             $ch = curl_init();
  108.  
  109.             if (FALSE === $ch)
  110.                 throw new \Exception('failed to initialize');
  111.  
  112.             //set the url, number of POST vars, POST data
  113.             curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
  114.             curl_setopt($ch,CURLOPT_URL, $url);
  115.             curl_setopt($ch,CURLOPT_POST, count($fields));
  116.             curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  117.             curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  118.  
  119.             //execute post
  120.             $result = curl_exec($ch);
  121.             if (FALSE === $result)
  122.                 throw new \Exception(curl_error($ch), curl_errno($ch));
  123.  
  124.  
  125.             curl_close($ch);
  126.  
  127.             //$apiMessage = $result;
  128.             if(strpos(@$result, 'failed') < 0){
  129.                 $orderLead->api_answer = $result;
  130.                 $apiStatus = 1;
  131.             }else{
  132.                 $orderLead->api_answer = $result;
  133.                 $apiStatus = 0;
  134.             }
  135.  
  136.             //close connection
  137.         } catch(\Exception $e) {
  138.  
  139.             // trigger_error(sprintf(
  140.             //     'Curl failed with error #%d: %s',
  141.             //     $e->getCode(), $e->getMessage()),
  142.             //     E_USER_ERROR);
  143.  
  144.             $apiMessage = sprintf(
  145.                 'Curl failed with error #%d: %s',
  146.                 $e->getCode(), $e->getMessage());
  147.  
  148.         }
  149.  
  150.         $userId = 0;
  151.         if (!is_null($apiUser)) {
  152.             $userId = $apiUser->id;
  153.         }
  154.  
  155.         //TODO: save this lead in order_leads
  156.         $orderLead = new OrderLead;
  157.         $orderLead->order_id = $order->id;
  158.         $orderLead->lead_id = $lead->id;
  159.         $orderLead->api_user_id = $userId;
  160.         $orderLead->api_message = $apiMessage;
  161.         $orderLead->api_status = $apiStatus;
  162.  
  163.         $orderLead->send_id = 0;
  164.         $orderLead->api_answer = $result;
  165.  
  166.         $orderLead->save();
  167.  
  168.     }
  169.  
  170.     public function autoLogin($config, $email, $password, $api_key=null, $client_id=null) {
  171.         $url = $config['autoLoginUrl'];
  172.  
  173.         $url = str_replace([
  174.                     '{EMAIL}',
  175.                     '{PASSWORD}',
  176.                     '{KEY}'
  177.                 ], [
  178.                     $email,
  179.                     $password,
  180.                     $api_key
  181.                 ], $url);
  182.  
  183.  
  184.         return $url;
  185.     }
  186.  
  187.     function curl_post($url, array $post = NULL, array $options = array()){
  188.         $defaults = array(
  189.             CURLOPT_URL => $url,
  190.             CURLOPT_FOLLOWLOCATION => 1,
  191.             CURLOPT_POST => 1,
  192.             CURLOPT_POSTFIELDS => http_build_query($post),
  193.             CURLOPT_RETURNTRANSFER => 1,
  194.         );
  195.         $ch = curl_init();
  196.         curl_setopt_array($ch, ($options + $defaults));
  197.         if( ! $result = curl_exec($ch)){
  198.             trigger_error(curl_error($ch));
  199.         }
  200.         curl_close($ch);
  201.         return $result;
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement