Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Add Book And Change example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha Gerrand committed Apr 29, 2022
1 parent d81114b commit e3a5a64
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
- name: Run search and book example
run: php ./examples/search-and-book.php

- name: Run book and change example
run: php ./examples/book-and-change.php

- name: Run book with extra baggage example
run: php ./examples/book-with-extra-baggage.php

Expand Down
110 changes: 110 additions & 0 deletions examples/book-and-change.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use Duffel\Client;

echo "Duffel Flights API - search and book example\n";
$start = time();

$client = new Client;
$client->setAccessToken(getenv('DUFFEL_ACCESS_TOKEN'));

$departureDate = (new DateTime)->add(new DateInterval("P10D"))->format('Y-m-d');

$offerRequest = $client->offerRequests()->create(
"economy",
array(
array("age" => 25)
),
array(
array(
"origin" => "LHR",
"destination" => "STN",
"departure_date" => $departureDate
)
)
);

echo sprintf("Created offer request: %s\n", $offerRequest["id"]);

$offers = $client->offers()->all($offerRequest["id"]);

echo sprintf("Got %s offers\n", count($offers));

$selectedOffer = array_shift($offers);

echo sprintf("Selected offer %s to book\n", $selectedOffer["id"]);

$pricedOffer = $client->offers()->show($selectedOffer["id"]);

echo sprintf("The final price for offer %s is %s (%s)\n", $pricedOffer["id"],
$pricedOffer["total_amount"],
$pricedOffer["total_currency"]
);

$order = $client->orders()->create(array(
"selected_offers" => array($pricedOffer["id"]),
"payments" => array(
array(
"type" => "balance",
"amount" => $pricedOffer["total_amount"],
"currency" => $pricedOffer["total_currency"]
)
),
"passengers" => array(
array(
"id" => $pricedOffer["passengers"][0]["id"],
"title" => "mr",
"gender" => "m",
"given_name" => "Zepp",
"family_name" => "Lin",
"born_on" => "1990-01-26",
"phone_number" => "+441234567890",
"email" => "[email protected]",
)
)
));

echo sprintf("Created order %s with booking reference %s\n", $order["id"], $order["booking_reference"]);

$orderChangeRequest = $client->orderChangeRequests()->create($order["id"],
array(
"add" => array(
array(
"cabin_class" => "economy",
"departure_date" => (new DateTime)->add(new DateInterval("P10D"))->format('Y-m-d'),
"origin" => "LHR",
"destination" => "STN",
)
),
"remove" => array(
array(
"slice_id" => $order["slices"][0]["id"],
)
),
)
);

$orderChangeOffers = $client->orderChangeOffers()->all($orderChangeRequest["id"]);

echo sprintf("Got %d options for changing the order, picking the first option\n", count($orderChangeOffers));

$orderChange = $client->orderChanges()->create($orderChangeOffers[0]["id"]);

echo sprintf("Created order change %s, confirming...\n", $orderChange["id"]);

$client->orderChanges()->confirm($orderChange["id"],
array(
"type" => "balance",
"amount" => $orderChange["change_total_amount"],
"currency" => $orderChange["change_total_currency"],
),
);

echo sprintf("Processed change to order %s, costing %s (%s)\n", $order["id"], $orderChange["change_total_amount"], $orderChange["change_total_currency"]);

$finish = time();
echo sprintf("Finished in %d seconds.\n", ($finish - $start));

0 comments on commit e3a5a64

Please sign in to comment.