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

Commit

Permalink
Merge pull request #29 from duffelhq/jc/docs-add-return-example
Browse files Browse the repository at this point in the history
docs: Add return example
  • Loading branch information
jesse-c authored Jun 22, 2023
2 parents 8971168 + d540fa9 commit e3bf163
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 5 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ jobs:
- name: Run exploring data example
run: php ./examples/exploring-data.php

- name: Run search and book example
run: php ./examples/search-and-book.php
- name: Run search and book example (one way)
run: php ./examples/search-and-book-one-way.php

- name: Run search and book example (return)
run: php ./examples/search-and-book-return.php

- name: Run book and change example
run: php ./examples/book-and-change.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Duffel\Client;

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

$client = new Client();
Expand All @@ -15,14 +15,14 @@
$departureDate = (new DateTime)->add(new DateInterval("P10D"))->format('Y-m-d');

$offerRequest = $client->offerRequests()->create(
"economy",
"economy",
array(
array("type" => "adult")
),
array(
array(
"origin" => "LHR",
"destination" => "STN",
"destination" => "STN",
"departure_date" => $departureDate
)
)
Expand Down
105 changes: 105 additions & 0 deletions examples/search-and-book-return.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

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

use Duffel\Client;

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

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

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

$offerRequest = $client->offerRequests()->create(
"economy",
array(
array("type" => "adult")
),
array(
array(
"origin" => "LHR",
"destination" => "STN",
"departure_date" => $departureDate_1
),
array(
"origin" => "STN",
"destination" => "LHR",
"departure_date" => $departureDate_2
)
)
);

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"], true);

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

$availableService = array_shift($pricedOffer["available_services"]);

echo sprintf("Adding an extra bag with service %s costing %s (%s)\n", $availableService["id"],
$availableService["total_amount"],
$availableService["total_currency"],
);

$totalAmount = round(($pricedOffer["total_amount"] + $availableService["total_amount"]), 2);

echo sprintf("Total amount is %s\n", $totalAmount);

$order = $client->orders()->create(array(
"selected_offers" => array($pricedOffer["id"]),
"services" => array(
array(
"id" => $availableService["id"],
"quantity" => 1,
)
),
"payments" => array(
array(
"type" => "balance",
"amount" => $totalAmount,
"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" => "z.e.l@zeppelin.aero",
)
)
));

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

$orderCancellation = $client->orderCancellations()->create($order["id"]);

echo sprintf("Requested refund quote for order %s – %s (%s) is available\n", $order["id"], $orderCancellation["refund_amount"], $orderCancellation["refund_currency"]);

$client->orderCancellations()->confirm($orderCancellation["id"]);

echo sprintf("Confirmed refund quote for order %s\n", $order["id"]);

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

0 comments on commit e3bf163

Please sign in to comment.