<?php

declare(strict_types=1);

/*
 * PayPalRESTAPIsLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

namespace PayPalRESTAPIsLib\Tests\E2E;

use Exception;
use PayPalRESTAPIsLib\Controllers\OrdersController;
use PayPalRESTAPIsLib\Controllers\PaymentsController;
use PayPalRESTAPIsLib\Models\Builders\AmountBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountWithBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\ItemBuilder;
use PayPalRESTAPIsLib\Models\Builders\MoneyBuilder;
use PayPalRESTAPIsLib\Models\Builders\OrderRequestBuilder;
use PayPalRESTAPIsLib\Models\Builders\PaymentSourceBuilder;
use PayPalRESTAPIsLib\Models\Builders\PayPalWalletBuilder;
use PayPalRESTAPIsLib\Models\Builders\PayPalWalletExperienceContextBuilder;
use PayPalRESTAPIsLib\Models\Builders\PurchaseUnitRequestBuilder;
use PayPalRESTAPIsLib\Models\CheckoutPaymentIntent;
use PayPalRESTAPIsLib\Models\Order;
use PayPalRESTAPIsLib\Models\OrderAuthorizeResponse;
use PayPalRESTAPIsLib\Models\PayPalExperienceLandingPage;
use PayPalRESTAPIsLib\Tests\Controllers\BaseTestController;
use PayPalRESTAPIsLib\Tests\WebDriverUtilities;

class VoidAuthorizationFlowTest extends BaseTestController
{
    /**
     * @var OrdersController OrdersController instance
     */
    protected static $ordersController;

    /**
     * @var PaymentsController PaymentsController instance
     */
    protected static $paymentsController;

    /**
     * Setup test class
     */
    public static function setUpBeforeClass(): void
    {
        $client = parent::getClient();
        self::$ordersController = $client->getOrdersController();
        self::$paymentsController = $client->getPaymentsController();
    }

    public function testCreateOrder(): Order
    {
        $request = [
            'body' => OrderRequestBuilder::init(
                CheckoutPaymentIntent::AUTHORIZE,
                [
                    PurchaseUnitRequestBuilder::init(
                        AmountWithBreakdownBuilder::init(
                            'USD',
                            '25.00'
                        )
                            ->breakdown(
                                AmountBreakdownBuilder::init()
                                    ->itemTotal(
                                        MoneyBuilder::init(
                                            'USD',
                                            '25.00'
                                        )->build()
                                    )
                                    ->shipping(
                                        MoneyBuilder::init(
                                            'USD',
                                            '0'
                                        )->build()
                                    )
                                    ->taxTotal(
                                        MoneyBuilder::init(
                                            'USD',
                                            '0'
                                        )->build()
                                    )
                                    ->build()
                            )->build()
                    )
                        ->description('Clothing Shop')
                        ->items([
                            ItemBuilder::init(
                                'Levis 501',
                                MoneyBuilder::init(
                                    'USD',
                                    '25.00'
                                )->build(),
                                '1'
                            )
                                ->tax(
                                    MoneyBuilder::init(
                                        'USD',
                                        '0.00'
                                    )->build()
                                )
                                ->sku('5158936')
                                ->build()
                        ])
                        ->build()
                ]
            )
                ->paymentSource(
                    PaymentSourceBuilder::init()
                        ->paypal(
                            PayPalWalletBuilder::init()
                                ->experienceContext(
                                    PayPalWalletExperienceContextBuilder::init()
                                        ->locale('en-US')
                                        ->returnUrl('https://example.com/returnUrl')
                                        ->cancelUrl('https://example.com/cancelUrl')
                                        ->landingPage(PayPalExperienceLandingPage::LOGIN)
                                        ->build()
                                )
                                ->build()
                        )
                        ->build()
                )
                ->build(),
            'payPalRequestId' => strval(rand(1, 99999999999))
        ];

        $response = self::$ordersController->ordersCreate($request);

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(200, $response->getStatusCode());

        return $response->getResult();
    }

    /**
     * @depends testCreateOrder
     * @throws Exception
     */
    public function testCompletePayment(Order $orderDetail): void
    {
        $links = $orderDetail->getLinks();
        $url = null;
        if (isset($links)) {
            $url = $links[1]->getHref();
        }
        $this->assertNotNull($url);
        WebDriverUtilities::completePayment($url ?? '');
    }

    /**
     * @depends testCreateOrder
     * @depends testCompletePayment
     */
    public function testAuthorizeOrder(Order $orderDetail): OrderAuthorizeResponse
    {
        $request = [
            'id' => $orderDetail->getId(),
            'payPalRequestId' => strval(rand(1, 99999999999)),
            'prefer' => 'return=representation'
        ];

        $response = self::$ordersController->ordersAuthorize($request);

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(201, $response->getStatusCode());

        return $response->getResult();
    }

    /**
     * @depends testAuthorizeOrder
     */
    public function testCaptureAuthorization(OrderAuthorizeResponse $authorizeResponse): void
    {
        $purchaseUnits = $authorizeResponse->getPurchaseUnits();
        $authorizationId = null;
        if (isset($purchaseUnits)) {
            $authorizations = $purchaseUnits[0]->getPayments()->getAuthorizations();
            if (isset($authorizations)) {
                $authorizationId = $authorizations[0]->getId();
            }
        }
        $this->assertNotNull($authorizationId);
        $request = [
            'authorizationId' => $authorizationId,
            'prefer' => 'return=representation'
        ];

        $response = self::$paymentsController->authorizationsCapture($request);

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(201, $response->getStatusCode());
    }
}
