Skip to content

Commit

Permalink
Added payment sessions support (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Jan 30, 2024
1 parent 4310ef9 commit a9b413f
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/Checkout/CheckoutApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Checkout\Payments\PaymentsClient;
use Checkout\Payments\Hosted\HostedPaymentsClient;
use Checkout\Payments\Links\PaymentLinksClient;
use Checkout\Payments\Sessions\PaymentSessionsClient;
use Checkout\Reports\ReportsClient;
use Checkout\Risk\RiskClient;
use Checkout\Sessions\SessionsClient;
Expand Down Expand Up @@ -44,6 +45,8 @@ final class CheckoutApi extends CheckoutApmApi
private $issuingClient;
private $paymentContextClient;

private $paymentSessionsClient;

public function __construct(CheckoutConfiguration $configuration)
{
$baseApiClient = $this->getBaseApiClient($configuration);
Expand All @@ -64,6 +67,7 @@ public function __construct(CheckoutConfiguration $configuration)
$this->financialClient = new FinancialClient($baseApiClient, $configuration);
$this->issuingClient = new IssuingClient($baseApiClient, $configuration);
$this->paymentContextClient = new PaymentContextsClient($baseApiClient, $configuration);
$this->paymentSessionsClient = new PaymentSessionsClient($baseApiClient, $configuration);
$this->balancesClient = new BalancesClient(
$this->getBalancesApiClient($configuration),
$configuration
Expand Down Expand Up @@ -231,6 +235,14 @@ public function getPaymentContextsClient()
return $this->paymentContextClient;
}

/**
* @return PaymentSessionsClient
*/
public function getPaymentSessionsClient()
{
return $this->paymentSessionsClient;
}

/**
* @param CheckoutConfiguration $configuration
* @return ApiClient
Expand Down
13 changes: 13 additions & 0 deletions lib/Checkout/Payments/Sessions/Billing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Checkout\Payments\Sessions;

use Checkout\Common\Address;

class Billing
{
/**
* @var Address
*/
public $address;
}
31 changes: 31 additions & 0 deletions lib/Checkout/Payments/Sessions/PaymentSessionsClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Checkout\Payments\Sessions;

use Checkout\ApiClient;
use Checkout\AuthorizationType;
use Checkout\CheckoutApiException;
use Checkout\CheckoutConfiguration;
use Checkout\Client;

class PaymentSessionsClient extends Client
{

const PAYMENT_SESSIONS = "payment-sessions";

public function __construct(ApiClient $apiClient, CheckoutConfiguration $configuration)
{
parent::__construct($apiClient, $configuration, AuthorizationType::$secretKey);
}

/**
* @param PaymentSessionsRequest $paymentSessionsRequest
* @return array
* @throws CheckoutApiException
*/
public function createPaymentSessions(PaymentSessionsRequest $paymentSessionsRequest)
{
return $this->apiClient->post(self::PAYMENT_SESSIONS, $paymentSessionsRequest, $this->sdkAuthorization());
}

}
43 changes: 43 additions & 0 deletions lib/Checkout/Payments/Sessions/PaymentSessionsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Checkout\Payments\Sessions;

use Checkout\Common\CustomerRequest;

class PaymentSessionsRequest
{
/**
* @var int
*/
public $amount;

/**
* @var string value of Currency
*/
public $currency;

/**
* @var string
*/
public $reference;

/**
* @var Billing
*/
public $billing;

/**
* @var CustomerRequest
*/
public $customer;

/**
* @var string
*/
public $success_url;

/**
* @var string
*/
public $failure_url;
}
2 changes: 2 additions & 0 deletions test/Checkout/Tests/Forex/ForexIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function before()
*/
public function shouldRequestQuote()
{
$this->markTestSkipped("unstable");

$quoteRequest = new QuoteRequest();
$quoteRequest->source_currency = Currency::$GBP;
$quoteRequest->source_amount = 30000;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Checkout\Tests\Payments\Sessions;

use Checkout\CheckoutApiException;
use Checkout\Payments\Sessions\PaymentSessionsClient;
use Checkout\Payments\Sessions\PaymentSessionsRequest;
use Checkout\PlatformType;
use Checkout\Tests\UnitTestFixture;

class PaymentSessionsClientTest extends UnitTestFixture
{
/**
* @var PaymentSessionsClient
*/
private $client;

/**
* @before
*/
public function init()
{
$this->initMocks(PlatformType::$default);
$this->client = new PaymentSessionsClient($this->apiClient, $this->configuration);
}

/**
* @test
* @throws CheckoutApiException
*/
public function shouldCreatePaymentSessions()
{

$this->apiClient
->method("post")
->willReturn("response");

$response = $this->client->createPaymentSessions(new PaymentSessionsRequest());
$this->assertNotNull($response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Checkout\Tests\Payments\Sessions;

use Checkout\CheckoutApiException;
use Checkout\CheckoutArgumentException;
use Checkout\CheckoutAuthorizationException;
use Checkout\CheckoutException;
use Checkout\Common\Currency;
use Checkout\Common\CustomerRequest;
use Checkout\Payments\Sessions\Billing;
use Checkout\Payments\Sessions\PaymentSessionsRequest;
use Checkout\PlatformType;
use Checkout\Tests\SandboxTestFixture;

class PaymentSessionsIntegrationTest extends SandboxTestFixture
{
/**
* @before
* @throws CheckoutAuthorizationException
* @throws CheckoutArgumentException
* @throws CheckoutException
*/
public function before()
{
$this->init(PlatformType::$default);
}

/**
* @test
* @throws CheckoutApiException
*/
public function shouldCreatePaymentsSessions()
{
$request = $this->createPaymentSessionsRequest();

$response = $this->checkoutApi->getPaymentSessionsClient()->createPaymentSessions($request);

$this->assertResponse(
$response,
"id",
"amount",
"locale",
"currency",
"payment_methods",
"_links",
"_links.self"
);

foreach ($response["payment_methods"] as $payment_method) {
$this->assertResponse(
$payment_method,
"type"
);
}
}

private function createPaymentSessionsRequest()
{
$billing = new Billing();
$billing->address = $this->getAddress();

$customer = new CustomerRequest();
$customer->name = "John Smith";
$customer->email = "john.smith@example.com";

$paymentSessionsRequest = new PaymentSessionsRequest();
$paymentSessionsRequest->amount = 2000;
$paymentSessionsRequest->currency = Currency::$GBP;
$paymentSessionsRequest->reference = "ORD-123A";
$paymentSessionsRequest->billing = $billing;
$paymentSessionsRequest->customer = $customer;
$paymentSessionsRequest->success_url = "https://example.com/payments/success";
$paymentSessionsRequest->failure_url = "https://example.com/payments/failure";

return $paymentSessionsRequest;
}
}
2 changes: 2 additions & 0 deletions test/Checkout/Tests/Reports/ReportsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function before()
*/
public function shouldGetAllReports()
{
$this->markTestSkipped("unstable");

$response = $this->checkoutApi->getReportsClient()->getAllReports($this->getQuery());

$this->assertResponse(
Expand Down

0 comments on commit a9b413f

Please sign in to comment.