[#53] editBook fails with additionalFields

Migrated from Redmine #53 | Author: Steve Murphy
Status: Feedback | Priority: High, I’m very impatient | Created: 2017-10-06


I have been trying to use the same method for applying additionalFields using editBook as using book however I receive a failure saying it can’t find the additionalFields.

editBook works for services with no additionalFields, but returns error saying it wasn’t supplied for services that do have additional fields to edit

snippet used to book (works)

//javascript to build fields
var add_fields_ids = document.querySelectorAll(‘[id^=“add_field_id_”]’);
if (add_fields_ids.length > 0) {
var arr = Array.prototype.map.call( add_fields_ids, function( el, i ) {
return el.id;
});

		var add_field_data = {
			additional_field: []
		};

		for (i = 0; i < arr.length; i++) {
			var field_id = arr[i].split('_')[arr[i].split('_').length-1];

			var val = document.getElementById(arr[i]).textContent;
			if (val == '') {
				val = document.getElementById(arr[i]).value;
			}

			add_field_data.additional_field.push({
				field_id : field_id,
				field_val : val
			});
		}

	}

//server-side PHP

$additional_fields = $_POST[‘additional_fields’];

$additionalFields = array();
foreach ($additional_fields as $key => $value) {
     foreach ($value as $key => $value) {
      $additionalFields[$value["field_id"]] = $value["field_val"];
     }
} 

$book = $client->book($eventId, $unitId, $date, $time, $clientData, $additionalFields, $count);

snippet used to editBook (fails)

//javascript to build fields
var add_fields_ids = document.querySelectorAll(‘[id^=“add_field_id_”]’);
if (add_fields_ids.length > 0) {
var arr = Array.prototype.map.call( add_fields_ids, function( el, i ) {
return el.id;
});

		var add_field_data = {
			additional_field: []
		};

		for (i = 0; i < arr.length; i++) {
			var field_id = arr[i].split('_')[arr[i].split('_').length-1];

			var val = document.getElementById(arr[i]).textContent;
			if (val == '') {
				val = document.getElementById(arr[i]).value;
			}

			add_field_data.additional_field.push({
				field_id : field_id,
				field_val : val
			});

			// alert(val);
			// alert();
		}

	}

//server-side PHP

$additional_fields = $_POST[‘additional_fields’];

$additionalFields = array();
foreach ($additional_fields as $key => $value) {
     foreach ($value as $key => $value) {
      $additionalFields[$value["field_id"]] = $value["field_val"];
     }
} 

$editBook = $client->editBook($schedulerId, $eventId, $unitId, $clientId, $startDate, $startTime, $additionalFields);

Dmytro Bondarev wrote:

Please provide data that you send to server.

Steve Murphy wrote:

Thank you Dmitry

Here is the array I would be passing to simplybook in the function, with the additionalField IDs and their values

{“9f09f0cb8bc385f5442005ef7d7fdd94”:"Female ",“16ddfd657ecda3a614330ad14422a1c5”:“2007-01-23”,“507868ac69281d7d8159b3d8a9e2b988”:“2017-09-13”,“d8c640e1ae5319cd257166356673fe68”:“Tripped in the forest”,“1d0e70f3caf50d5199bf0162240d2a35”:"Friend ",“7374f2a0c2080aca9dcdaf782a05c6cb”:“Nothing”,“c847ff0b85ccbbb76725f89f151480ef”:“John Smith”,“2a4fffe5adb4d1a07135d0f03bfb839a”:“555-555-5555”,“a408305aba9faa709df064ee44050354”:“on”}

Same method I use to do the book()

Dmytro Bondarev wrote:

Hi! Please just provide full request.
It is required to check if you pass parameters in right way.

Steve Murphy wrote:

Ok, thanks Dmitry! My apologies. I have to run back to my computer quick, and will grab the entire function. I’ll leave the credentials out of the connection string for the token/client if that is ok. Back shortly with the entire request.

Steve Murphy wrote:

// This method works for editing events without additional fields just fine as well

$client = simplyBookAdminToken($user_identity);

  $schedulerId = $_POST['id']; //Booking ID from getBooking
  $eventId = $_POST['event_id'];
  $unitId = $_POST['unit_id'];
  $clientId = $_POST['client_id'];
  $startDate = $_POST['date'];
  $startTime = $_POST['time'];
  $additional_fields = $_POST['additional_fields'];
  
  $additionalFields = array();
  foreach ($additional_fields as $key => $value) {
     foreach ($value as $key => $value) {
        $additionalFields[$value["field_id"]] = $value["field_val"];
     }
  }
  
  if (checkAvailability($startDate, $startTime, $eventId, $unitId, $client) == true) {
    $editBook = $client->editBook($schedulerId, $eventId, $unitId, $clientId, $startDate, $startTime, $additionalFields);
    echo json_encode($editBook);
  } else {
    echo 'The selected time is not available';
  }

Redmine Admin wrote:

please send us raw request body

Steve Murphy wrote:

Do you want everything for the specific booking? Booking is, event is, etc.?

Thanks

Steve Murphy wrote:

Those were supposed to say booking id, event id - sorry, autoCorrect :slight_smile:

Redmine Admin wrote:

yes, full raw request with all fields for specific booking you need to edit but can’t

Steve Murphy wrote:

<?php require_once("JsonRpcClient.php"); $loginClient = new JsonRpcClient('http://user-api.simplybook.me' . '/login/'); $token = $loginClient->getUserToken('achinyx', 'admin', 'deleted-for-post'); $client = new JsonRpcClient('http://user-api.simplybook.me' . '/admin/', array( 'headers' => array( 'X-Company-Login: ' . 'achinyx', 'X-User-Token: ' . $token ) )); date_default_timezone_set("America/New_York"); $schedulerId = "91"; $eventId = "2"; $unitId = "7"; $clientId = "31"; $startDate = "2017-10-04"; $startTime = "11:00:00"; $additional_fields = array("9f09f0cb8bc385f5442005ef7d7fdd94"=>"Female ","16ddfd657ecda3a614330ad14422a1c5"=>"2003-12-23","507868ac69281d7d8159b3d8a9e2b988"=>"2017-09-13","d8c640e1ae5319cd257166356673fe68"=>"Tripped in the forest","1d0e70f3caf50d5199bf0162240d2a35"=>"Friend ","7374f2a0c2080aca9dcdaf782a05c6cb"=>"Something","c847ff0b85ccbbb76725f89f151480ef"=>"John Smith","2a4fffe5adb4d1a07135d0f03bfb839a"=>"555-555-5555","a408305aba9faa709df064ee44050354"=>"on"); $editBook = $client->editBook($schedulerId, $eventId, $unitId, $clientId, $startDate, $startTime, $additionalFields); echo json_encode($editBook); // the error returned is that it can't find the additionalFields when they are required, if I make them optional then it succeeds and overwrites them as blank values ?>

Steve Murphy wrote:

sorry, I had a typo in the last post - $additional_fields should be $additionalFIelds to see the correct error, forgot to update after getting everything hard-coded instead of passed as variables

Redmine Admin wrote:

we do not need any code. Just raw http request body please, no code

Steve Murphy wrote:

So sorry! Let me know if this works

action=editBook&id=91&client_id=31&unit_id=7&event_id=2&date=2017-10-04&time=11%3A00%3A00&additional_fields%5Badditional_field%5D%5B0%5D%5Bfield_id%5D=9f09f0cb8bc385f5442005ef7d7fdd94&additional_fields%5Badditional_field%5D%5B0%5D%5Bfield_val%5D=Female+&additional_fields%5Badditional_field%5D%5B1%5D%5Bfield_id%5D=16ddfd657ecda3a614330ad14422a1c5&additional_fields%5Badditional_field%5D%5B1%5D%5Bfield_val%5D=2016-10-03&additional_fields%5Badditional_field%5D%5B2%5D%5Bfield_id%5D=507868ac69281d7d8159b3d8a9e2b988&additional_fields%5Badditional_field%5D%5B2%5D%5Bfield_val%5D=2017-07-11&additional_fields%5Badditional_field%5D%5B3%5D%5Bfield_id%5D=d8c640e1ae5319cd257166356673fe68&additional_fields%5Badditional_field%5D%5B3%5D%5Bfield_val%5D=Injured&additional_fields%5Badditional_field%5D%5B4%5D%5Bfield_id%5D=1d0e70f3caf50d5199bf0162240d2a35&additional_fields%5Badditional_field%5D%5B4%5D%5Bfield_val%5D=Friend+&additional_fields%5Badditional_field%5D%5B5%5D%5Bfield_id%5D=7374f2a0c2080aca9dcdaf782a05c6cb&additional_fields%5Badditional_field%5D%5B5%5D%5Bfield_val%5D=None&additional_fields%5Badditional_field%5D%5B6%5D%5Bfield_id%5D=c847ff0b85ccbbb76725f89f151480ef&additional_fields%5Badditional_field%5D%5B6%5D%5Bfield_val%5D=John+Smith&additional_fields%5Badditional_field%5D%5B7%5D%5Bfield_id%5D=2a4fffe5adb4d1a07135d0f03bfb839a&additional_fields%5Badditional_field%5D%5B7%5D%5Bfield_val%5D=555-555-5555&additional_fields%5Badditional_field%5D%5B8%5D%5Bfield_id%5D=a408305aba9faa709df064ee44050354&additional_fields%5Badditional_field%5D%5B8%5D%5Bfield_val%5D=on

Redmine Admin wrote:

it is not a raw request body

Steve Murphy wrote:

My apologies, I am struggling to try and get what you are requesting. The request data I can trace from the network is below, however I thought an earlier request specified the fields. If this is not what you need, can you send me an example? My apologies again

:authority:ptondemand.com
:method:POST
:path:/wp-admin/admin-ajax.php
:scheme:https
accept:/
accept-encoding:gzip, deflate, br
accept-language:en-US,en;q=0.8
content-length:1571
content-type:application/x-www-form-urlencoded; charset=UTF-8
cookie:wordpress_4418c6eeaa82ef650019b93f2aa1bf9b=jedipt%7C1507728574%7CRE1iI33RkAA9nidoitoKz9SGOc3A2qMNBTtdoyevYrN%7C1d3f4e19eb64a3fb5cc72a88ce0da865d029006e63b505551c0f0d291955f189; wordpress_logged_in_4418c6eeaa82ef650019b93f2aa1bf9b=jedipt%7C1507728574%7CRE1iI33RkAA9nidoitoKz9SGOc3A2qMNBTtdoyevYrN%7C0cbce720fd05e01e0317ecdc771cc03e656d569fb6ad4e1379a818d6815487f2; PHPSESSID=c45e8e32fcd7df12663def7ceebdaa1e; _ga=GA1.2.294066125.1504746473; _gid=GA1.2.1271734037.1507724961; _gali=add_field_id_a408305aba9faa709df064ee44050354
origin:https://ptondemand.com
referer:https://ptondemand.com/dashboard/?ref=calendar&identity=8
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
x-requested-with:XMLHttpRequest

Dmytro Bondarev wrote:

We need raw request that you send to our server.
We do not need data that you send to your server.

Redmine Admin wrote:

it icannot be request to OUR API. Our API does not accept data in this format

Steve Murphy wrote:

I am clearly struggling very much to capture that data in the way you need - any suggestions? I am not able to see the request data anywhere, and am struggling to determine how to capture it.

My apologies at my ineptitude

Steve Murphy wrote:

As I am posting additional fields to your API identically to the book as editBook but editBook is not recognizing additionalFields, could it be a bug in API for editBook?