From 1994aa9ada258b7be91b09477be3d78c1e5cf55b Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Wed, 27 Mar 2019 11:01:06 +0100 Subject: [PATCH] FEAT #9107 Webservices end --- src/app/action/controllers/ShippingTrait.php | 21 ++++++-------------- src/app/shipping/models/ShippingModel.php | 2 +- src/core/models/CurlModel.php | 12 +++++++---- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/app/action/controllers/ShippingTrait.php b/src/app/action/controllers/ShippingTrait.php index 62aea920752..abf8ffff3a4 100644 --- a/src/app/action/controllers/ShippingTrait.php +++ b/src/app/action/controllers/ShippingTrait.php @@ -71,7 +71,7 @@ trait ShippingTrait if (empty($contact[0])) { return ['errors' => ['Contact does not exist for attachment']]; } - if (!empty($contact['address_country']) && strtoupper(trim($contact['address_country'])) != 'FRANCE') { + if (!empty($contact[0]['address_country']) && strtoupper(trim($contact[0]['address_country'])) != 'FRANCE') { return ['errors' => ['Contact country is not France']]; } $afnorAddress = ContactController::getContactAfnor($contact[0]); @@ -119,20 +119,11 @@ trait ShippingTrait $errors[] = "Maileva sending creation failed for attachment {$attachmentId}"; continue; } - - $sendings = CurlModel::execSimple([ - 'url' => $mailevaConfig['uri'] . '/mail/v1/sendings', - 'bearerAuth' => ['token' => $token], - 'method' => 'GET' - ]); - if ($sendings['code'] != 200) { - $errors[] = "Maileva get sendings failed for attachment {$attachmentId}"; - continue; - } - - foreach ($sendings['response']['sendings'] as $sending) { - if ($sending['name'] == $sendingName) { - $sendingId = $sending['id']; + foreach ($createSending['headers'] as $header) { + if (strpos($header, 'Location:') !== false) { + $sendingId = strrchr($header, '/'); + $sendingId = substr($sendingId, 1); + break; } } if (empty($sendingId)) { diff --git a/src/app/shipping/models/ShippingModel.php b/src/app/shipping/models/ShippingModel.php index 6df206c3035..26a8ec2ceee 100644 --- a/src/app/shipping/models/ShippingModel.php +++ b/src/app/shipping/models/ShippingModel.php @@ -31,7 +31,7 @@ class ShippingModel 'columnsValues' => [ 'user_id' => $args['userId'], 'attachment_id' => $args['attachmentId'], - 'is_version' => $args['isVersion'], + 'is_version' => empty($args['isVersion']) ? 'false' : 'true', 'options' => $args['options'], 'fee' => $args['fee'], 'recipient_entity_id' => $args['recipientEntityId'], diff --git a/src/core/models/CurlModel.php b/src/core/models/CurlModel.php index 51a00284367..98630f7abef 100644 --- a/src/core/models/CurlModel.php +++ b/src/core/models/CurlModel.php @@ -261,7 +261,7 @@ class CurlModel ValidatorModel::stringType($args, ['url', 'method', 'user', 'password']); ValidatorModel::arrayType($args, ['headers', 'body', 'queryParams', 'basicAuth', 'bearerAuth']); - $opts = [CURLOPT_RETURNTRANSFER => true]; + $opts = [CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true]; //Headers if (!empty($args['headers'])) { @@ -312,11 +312,15 @@ class CurlModel $curl = curl_init(); curl_setopt_array($curl, $opts); $rawResponse = curl_exec($curl); - $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $errors = curl_error($curl); curl_close($curl); + $headers = substr($rawResponse, 0, $headerSize); + $headers = explode("\r\n", $headers); + $response = substr($rawResponse, $headerSize); + LogsController::add([ 'isTech' => true, 'moduleId' => 'curl', @@ -324,10 +328,10 @@ class CurlModel 'tableName' => 'curl', 'recordId' => 'execSimple', 'eventType' => "Url : {$args['url']} HttpCode : {$code} Errors : {$errors}", - 'eventId' => "Response : {$rawResponse}" + 'eventId' => "Response : {$response}" ]); - return ['code' => $code, 'response' => json_decode($rawResponse, true), 'errors' => $errors]; + return ['code' => $code, 'headers' => $headers, 'response' => json_decode($response, true), 'errors' => $errors]; } private static function createMultipartFormData(array $args) -- GitLab