FAQ Q137: How to set up a custom pricing scheme?

Section: Setting up your pricing and invoicing
Note: this is an advanced topic not directly supported by planyo's interface and requiring programming skills.

Planyo's Pricing Manager supports a vast range of pricing models that include seasonal pricing and discounts depending on a number of conditions (see this question for more info).

If this doesn't fit your pricing model, you can specify a completely custom pricing scheme in form of an external script located on your web server that will return the price based on the input parameters passed to your script by planyo, such as rental start/end time, number of persons, number of resources rented and resource ID.

To use a custom pricing scheme, instead of specifying price in resource settings (you can also do this in the Default price rule in Pricing Manager), specify the URL of your script (e.g. http://yoursite.com/scriptname.php). Please note that further Pricing Manager rules (after Default price) will not be applied when custom script is used. The following parameters are sent (POST method):


There are three possibilities with respect to what the script returns:

Dependencies of the price on your reservation form fields


Making calls to your pricing script can be resource-intensive, especially if your calculations need to access data from external sources. This is why Planyo needs to understand exactly which fields modify the price. As explained in the paragraph above describing dependencies, if your price depends on values of any non-standard fields (personal fields except for user's email address or custom reservation form items) then you must indicate which fields your script uses. In such case your response must be a JSON array and not a single float (price).

Failing to print the price dependencies will simply mean that the customer won't immediately see the updated price if they change a field which should update the price. Note that the price calculated for the reservations will always be correct, regardless of whether you indicate the dependencies or not.

Sample script


Here's a sample PHP script that can calculate a different price for weekly and daily rentals as well as for different seasons:


< ?php
// date/time functions will work fine with timezone set to GMT
date_default_timezone_set (GMT);

// sample seasons and pricing here
define ('HIGH_SEASON_MONTH_START', 7); // first month of high season (without discount)
define ('HIGH_SEASON_MONTH_END', 9); // last month of high season (without discount)
define ('LOW_SEASON_DISCOUNT', 20); // discount in %
define ('WEEKLY_PRICE', 100); // price for a week in EUR
define ('DAILY_PRICE', 20); // price per day in EUR

function get_param ($name) {
        return $_REQUEST [$name];
}

// parameters passed by planyo
$units = get_param ('units'); // number of time units
$count = get_param ('count'); // number of resources
$start_date_stamp = get_param ('start'); // start date
$end_date_stamp = get_param ('end'); // end date
$persons = get_param ('persons'); // number of persons
$resource = get_param ('resource'); // resource id

// start date
$start_date = getdate ($start_date_stamp);
$month = $start_date ['mon'];

// season-based discount
if ($month < HIGH_SEASON_MONTH_START || $month > HIGH_SEASON_MONTH_END)
        $discount_factor = 100 - LOW_SEASON_DISCOUNT;
else
        $discount_factor = 100;

// use weekly or daily price
if ($units >= 7) {
        $weeks = ceil ($units / 7);
        $price = $weeks * WEEKLY_PRICE * $discount_factor / 100;
}
else {
        $price = $units * DAILY_PRICE * $discount_factor / 100;
}

echo $price;

? >


Testing the pricing script


You can test/debug your pricing script by going to http://www.planyo.com/reserve-test.php (you need to be logged in first). Here you can enter your custom pricing URL, choose the dates and under the price shown you can click on the link 'Price URL used'. This will give you the exact parameters passed by Planyo to your script for given input parameters (start/end time, number of persons etc.).

Pricing script with prefetching


Using the pricing script can have one negative effect: it can make the resource search slow -- more specifically, this happens when the search results display the price. In such case, the pricing script must be called as many times as the number of search results. As you can imagine, calling a script on your server 100 times (if you have 100 resources available) can take some time..

This problem can be solved by adding prefetching to your pricing script. In case of search (and other operations where the price must be known for many different data sets at once), planyo will also pass the additional data sets for which the price must be calculated. The additional data sets will include different start/end times, quantity and resource ID. These parameters are passed as: priceX-start, priceX-end, priceX-resource, priceX-count where X is the data set number starting at 2, e.g. price2-start, price2-end, price2-resource, price2-count then price3-start, price3-end, price3-resource, price3-count and so on.. All other parameters should be taken from the first (original) data set, only these 4 are passed for each data set.

If using prefetching you must return a JSON array with the additional price saved in the keys price2, price3, price4 etc. These prices can in turn be either an array with full information or a single price, e.g. both of the following results are valid:
{"price":15,"regular_price":15,"info_text":"3 days @ $5.00 = $15.00","can_reserve":true,"error_text":null,"deposit":1.53, "price2":20, "price3":25}
or
{"price":15,"regular_price":15,"info_text":"3 days @ $5.00 = $15.00","can_reserve":true,"error_text":null,"deposit":1.53, "price2": {"price":20,"regular_price":25}, "price3": {"price":25,"regular_price":35}}

For your convenience you can download a pricing script template and use it as the basis for your pricing script. Prefetching is built into this script so you don't have to worry about it and you'll only need to update a single function to return a price based on given parameters. See the comments inside the script.

Using a custom script only for availability


There is also a possibility of continuing to use the pricing manager while having planyo call an external script on your server only to find out in a resource can be reserved or not (on top of the other regular constraints). The unavailability script will be called using the same parameters as the pricing script (see above) and - similarly to the pricing script - should return a JSON array with the keys: can_reserve, info_text, error_text, prevent_admin_reservation (also see above for the details). The only required key is can_reserve although if reservation is not possible it's strongly recommended to return the reason in error_text. The other array keys used by the pricing script (e.g. price) will be ignored. Here's are two examples of unavailability script responses:
{"can_reserve":false,"error_text":"Reservation not possible because of national holiday"}
or
{"can_reserve":true}

For your convenience you can download an unavailability script template and use it as the basis for your unavailability script. Prefetching is built into this script so you don't have to worry about it and you'll only need to update a single function to return availability based on given parameters. See the comments inside the script.

Note that the unavailability script will also work with prefetching, see the details above.

In order to be able to define the unavailability script use this special link. You will see a new field with the URL of the script in resource settings / time-related settings.

Questions Fréquemment Posées (FAQ)