Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\OutgoingApi;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Validator;
- use App\Lead;
- use App\Source;
- use App\Country;
- use App\OrderLead;
- use Illuminate\Support\Facades\Log;
- class MaxiPartnersHummerApiController extends Controller {
- public function send($order, $lead, $apiUser = null, $type = 0) {
- $apiOptions = $order->getOptionsVals();
- $country_iso = "";
- $country = Country::where('id', '=',$lead->country_id)->first();
- if(!is_null($country)){
- $country_iso = $country->code;
- }
- $language = @$apiOptions['defaultLanguage'];
- if(!is_null($lead->language)){
- $language = $lead->language;
- }
- $ipAddress = $lead->ip_address;
- if($ipAddress == "" || $ipAddress == "::1" || is_null($ipAddress)){
- $ipAddress = $lead->getIpByCountry();
- }
- $url = "http://api-proxy.crhummer.mine.nu/Api";
- $apiUserName = $apiOptions['username'];
- $apiPassword = $apiOptions['password'];
- $campaignId = $apiOptions['campaign_id'];
- $curr = 'USD';
- //Get CountryCode--------------------------------------------------
- $post_country = array(
- 'api_username' => $apiOptions['username'],
- 'api_password' => $apiOptions['password'],
- 'jsonResponse' => 'true',
- 'MODULE' => 'Country',
- 'COMMAND' => 'view',
- 'FILTER[iso]' => $country_iso,
- );
- $result_country = $this->curl_post($url, $post_country);
- $objCountry = json_decode($result_country);
- if(!is_null($objCountry)){
- $country_id = $objCountry->status->Country->data_0->id;
- }else $country_id = 0;
- //-----------------------------------------------------------------
- $userPassword = 'none123456';
- $userFirstName = $lead->first_name;
- $userLastName = $lead->last_name;
- $email = $lead->email;
- $phone = $lead->phone_prefix.$lead->phone;
- $fields = array(
- 'MODULE' => 'Customer',
- 'COMMAND' => 'add',
- 'api_username' => $apiUserName, //The API Username
- 'api_password' => $apiPassword, //The API Password
- 'campaignId' => $campaignId,
- 'subCampaign' => '',
- 'FirstName' => urlencode($userFirstName),
- 'LastName' => urlencode($userLastName),
- //'gender' => 'male',
- 'email' => urlencode($email),
- 'Phone' => urlencode($phone),
- 'Country' => $country_id,
- 'password' => urlencode($userPassword),
- 'currency' => $curr,
- //'birthday' => $birthday,
- //'a_aid' => '1934',
- );
- $headers = array(
- 'Content-Type' => 'application/x-www-form-urlencoded',
- );
- $fields_string = '';
- $result = "";
- $apiMessage = "";
- $apiStatus = "";
- //url-ify the data for the POST
- foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
- rtrim($fields_string, '&');
- try {
- $ch = curl_init();
- if (FALSE === $ch)
- throw new \Exception('failed to initialize');
- //set the url, number of POST vars, POST data
- curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_POST, count($fields));
- curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
- //execute post
- $result = curl_exec($ch);
- if (FALSE === $result)
- throw new \Exception(curl_error($ch), curl_errno($ch));
- curl_close($ch);
- //$apiMessage = $result;
- if(strpos(@$result, 'failed') < 0){
- $orderLead->api_answer = $result;
- $apiStatus = 1;
- }else{
- $orderLead->api_answer = $result;
- $apiStatus = 0;
- }
- //close connection
- } catch(\Exception $e) {
- // trigger_error(sprintf(
- // 'Curl failed with error #%d: %s',
- // $e->getCode(), $e->getMessage()),
- // E_USER_ERROR);
- $apiMessage = sprintf(
- 'Curl failed with error #%d: %s',
- $e->getCode(), $e->getMessage());
- }
- $userId = 0;
- if (!is_null($apiUser)) {
- $userId = $apiUser->id;
- }
- //TODO: save this lead in order_leads
- $orderLead = new OrderLead;
- $orderLead->order_id = $order->id;
- $orderLead->lead_id = $lead->id;
- $orderLead->api_user_id = $userId;
- $orderLead->api_message = $apiMessage;
- $orderLead->api_status = $apiStatus;
- $orderLead->send_id = 0;
- $orderLead->api_answer = $result;
- $orderLead->save();
- }
- public function autoLogin($config, $email, $password, $api_key=null, $client_id=null) {
- $url = $config['autoLoginUrl'];
- $url = str_replace([
- '{EMAIL}',
- '{PASSWORD}',
- '{KEY}'
- ], [
- $email,
- $password,
- $api_key
- ], $url);
- return $url;
- }
- function curl_post($url, array $post = NULL, array $options = array()){
- $defaults = array(
- CURLOPT_URL => $url,
- CURLOPT_FOLLOWLOCATION => 1,
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => http_build_query($post),
- CURLOPT_RETURNTRANSFER => 1,
- );
- $ch = curl_init();
- curl_setopt_array($ch, ($options + $defaults));
- if( ! $result = curl_exec($ch)){
- trigger_error(curl_error($ch));
- }
- curl_close($ch);
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement