Example code (PHP) to retrieve booking status and amount paid

Hey there,

Are there anybody to help me out?

I'm no developer though looking for some help in coding. Could you help me to come up with an example code to retrieve booking status and amount paid for that booking that I could literally just copy and paste into my website. I do have access to PHP template and I do know how to change the credentials for the API. But that's just about it of my knowledge in API's.

Thanks a lot

Comments

  • edited May 2016
    If you're using the library from our PHP-SDK (which I wrote a guide for setting up here if you want to familiarize yourself with what's happening), the process to request details of a booking is fairly simple.

    In this example, calling the booking/[booking_id] endpoint, I'm going to assume you are retrieving the booking ID from a $_GET variable, which would look like /my_page?booking_id=WXYZ-040516 in your URL -- if you're using WordPress templates, you can read up on custom query vars if your integration doesn't already capture a booking ID to use.  This also assumes your already configured CheckfrontAPI wrapper is referenced by a $Checkfront variable (which may not always be the case).

    $booking_id = strtoupper($_GET['booking_id']);
    
    
    $response = $Checkfront->get("booking/{$booking_id}");
    
    
    if (empty($response['booking'])) {
    
        echo "Booking not found!";
    
    } else {
    
        $status = $response['booking']['status_id'];
    
        $paid   = $response['booking']['amount_paid'];
    
        $total  = $response['booking']['total'];
    
        echo "{$booking_id} is in status '{$status}', and has paid {$paid} of {$total}";
    
    }
    As you can tell, this is a very basic and rough example, but it shows how easily you can pull the data you're looking for from the booking.

    This is far from the extent of the fields available to read from this endpoint however, so if you're interested in seeing an example of a booking, copy the ID of one of your active bookings, then navigate to Manage > Developer > Console in your account.  Enter booking/ followed by your booking ID, then hit enter; you should see a JSON object come back containing a structure including a "booking" section and some names matching the keys in the example above.  Comparing the results, you can use any that appear to be useful to your integration by simply copying the format used to return the existing example keys.

    If you're intending to use a custom receipt URL to redirect a customer to a page on your site that contains any private information obtained from their booking, I would recommend passing their {$BOOKING_TOKEN} in the redirect URL alongside the {$BOOKING_ID}, and using it as a basic security measure to ensure unauthorized users can't access a customer's private details by validating the user input token against the token you receive from a booking API response.

    A very basic example of this would be:

    $booking_token = $_GET['booking_token'];
    
    if ($booking_token != $response['booking']['token']) {
    
        exit("Booking token does not match!");
    
    }
Sign In or Register to comment.