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

API: Payments #17

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
- name: Run book and change example
run: php ./examples/book-and-change.php

- name: Run book hold order example
run: php ./examples/book-hold-order.php

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

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

declare(strict_types=1);

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

use Duffel\Client;

echo "Duffel Flights API - book hold order 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("type" => "adult")
),
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"], true);

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"]),
"type" => "hold",
"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 hold order %s with booking reference %s\n", $order["id"], $order["booking_reference"]);
echo sprintf("This order must be cancelled or paid for by %s\n", $order["payment_status"]["payment_required_by"]);

$payment = $client->payments()->create($order["id"], array(
"amount" => $order["total_amount"],
"currency" => $order["total_currency"],
"type" => "balance",
));

echo sprintf("Paid for hold order %s with payment reference %s\n", $order["id"], $payment["id"]);

$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));
22 changes: 22 additions & 0 deletions src/Duffel/Api/Payments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Duffel\Api;

class Payments extends AbstractApi {
public function create(string $orderId, array $payment) {
$params = [
'order_id' => $orderId,
'payment' => [
'amount' => $payment['amount'],
'currency' => $payment['currency'],
'type' => $payment['type'],
],
];

return $this->post('/air/payments', \array_filter($params, function ($value) {
return null !== $value && (!\is_string($value) || '' !== $value);
}));
}
}
5 changes: 5 additions & 0 deletions src/Duffel/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Duffel\Api\OrderChangeRequests;
use Duffel\Api\OrderChanges;
use Duffel\Api\Orders;
use Duffel\Api\Payments;
use Duffel\Api\SeatMaps;
use Duffel\Exception\InvalidAccessTokenException;
use Duffel\HttpClient\Builder;
Expand Down Expand Up @@ -89,6 +90,10 @@ public function orders(): Orders {
return new Orders($this);
}

public function Payments(): Payments {
return new Payments($this);
}

public function seatMaps(): SeatMaps {
return new SeatMaps($this);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Duffel/Api/PaymentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Duffel\Tests\Api;

use Duffel\Api\Payments;
use Duffel\Client;
use Http\Client\Common\HttpMethodsClientInterface;
use PHPUnit\Framework\TestCase;

class PaymentsTest extends TestCase {
private $mock;
private $stub;

public function setUp(): void {
$this->mock = $this->createMock(HttpMethodsClientInterface::class);
$this->stub = $this->createStub(Client::class);
$this->stub->method('getHttpClient')
->willReturn($this->mock);
}
public function testCreateWithIdCallsGetWithExpectedUri(): void {
$this->mock->expects($this->once())
->method('post')
->with(
$this->equalTo('/air/payments'),
$this->equalTo(['Content-Type' => 'application/json']),
$this->equalTo('{"data":{"order_id":"some-order-id","payment":{"amount":"30.20","currency":"GBP","type":"balance"}}}'),
);

$actual = new Payments($this->stub);
$actual->create('some-order-id', [
'amount' => '30.20',
'currency' => 'GBP',
'type' => 'balance',
]);
}
}
5 changes: 5 additions & 0 deletions tests/Duffel/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Duffel\Api\OrderChangeRequests;
use Duffel\Api\OrderChanges;
use Duffel\Api\Orders;
use Duffel\Api\Payments;
use Duffel\Api\SeatMaps;
use Duffel\Client;
use Duffel\Exception\InvalidAccessTokenException;
Expand Down Expand Up @@ -83,6 +84,10 @@ public function testOrdersUsesApiClass(): void {
$this->assertInstanceOf(Orders::class, $this->subject->orders());
}

public function testPaymentsUsesApiClass(): void {
$this->assertInstanceOf(Payments::class, $this->subject->payments());
}

public function testSeatMapsUsesApiClass(): void {
$this->assertInstanceOf(SeatMaps::class, $this->subject->seatMaps());
}
Expand Down