forked from duffelhq/duffel-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-and-book-return.php
105 lines (80 loc) · 2.85 KB
/
search-and-book-return.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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" => "[email protected]",
)
)
));
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));