<?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\Models\Builders\AddressBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\AmountWithBreakdownBuilder;
use PayPalRESTAPIsLib\Models\Builders\CardAttributesBuilder;
use PayPalRESTAPIsLib\Models\Builders\CardExperienceContextBuilder;
use PayPalRESTAPIsLib\Models\Builders\CardRequestBuilder;
use PayPalRESTAPIsLib\Models\Builders\CardVerificationBuilder;
use PayPalRESTAPIsLib\Models\Builders\ItemBuilder;
use PayPalRESTAPIsLib\Models\Builders\MoneyBuilder;
use PayPalRESTAPIsLib\Models\Builders\OrderRequestBuilder;
use PayPalRESTAPIsLib\Models\Builders\PaymentSourceBuilder;
use PayPalRESTAPIsLib\Models\Builders\PurchaseUnitRequestBuilder;
use PayPalRESTAPIsLib\Models\CardVerificationMethod;
use PayPalRESTAPIsLib\Models\CheckoutPaymentIntent;
use PayPalRESTAPIsLib\Models\Order;
use PayPalRESTAPIsLib\Tests\Controllers\BaseTestController;
use PayPalRESTAPIsLib\Tests\WebDriverUtilities;

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

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

    public function testCreateOrder(): Order
    {
        $request = [
            'body' => OrderRequestBuilder::init(
                CheckoutPaymentIntent::CAPTURE,
                [
                    PurchaseUnitRequestBuilder::init(
                        AmountWithBreakdownBuilder::init(
                            'USD',
                            '10.00'
                        )
                            ->breakdown(
                                AmountBreakdownBuilder::init()
                                    ->itemTotal(
                                        MoneyBuilder::init(
                                            'USD',
                                            '10.00'
                                        )->build()
                                    )->build()
                            )->build()
                    )
                        ->description('Camera Shop')
                        ->customId('testcustom_id')
                        ->invoiceId('invoice_number_' . rand(1, 99999999999))
                        ->items([
                            ItemBuilder::init(
                                'Levis 501 Selvedge STF',
                                MoneyBuilder::init(
                                    'USD',
                                    '5.00'
                                )->build(),
                                '1'
                            )
                                ->tax(MoneyBuilder::init(
                                    'USD',
                                    '0.00'
                                )->build())
                                ->sku('5158936')
                                ->build(),
                            ItemBuilder::init(
                                'T-Shirt',
                                MoneyBuilder::init(
                                    'USD',
                                    '5.00'
                                )->build(),
                                '1'
                            )
                                ->tax(MoneyBuilder::init(
                                    'USD',
                                    '0.00'
                                )->build())
                                ->sku('1457432')
                                ->build()
                        ])
                        ->build()
                ]
            )
                ->paymentSource(
                    PaymentSourceBuilder::init()
                        ->card(
                            CardRequestBuilder::init()
                                ->name('John Doe')
                                ->number('4868719995056080')
                                ->expiry('2027-02')
                                ->billingAddress(
                                    AddressBuilder::init(
                                        'US'
                                    )
                                        ->addressLine1('2211 N First Street')
                                        ->adminArea1('CA')
                                        ->adminArea2('San Jose')
                                        ->postalCode('95131')
                                        ->build()
                                )
                                ->attributes(
                                    CardAttributesBuilder::init()
                                        ->verification(
                                            CardVerificationBuilder::init()
                                                ->method(CardVerificationMethod::SCA_ALWAYS)
                                                ->build()
                                        )
                                        ->build()
                                )
                                ->experienceContext(CardExperienceContextBuilder::init()
                                    ->returnUrl('https://example.com/returnUrl')
                                    ->cancelUrl('https://example.com/cancelUrl')
                                    ->build())
                                ->build()
                        )
                        ->build()
                )
                ->build(),
            'payPalRequestId' => strval(rand(1, 99999999999)),
            'payPalPartnerAttributionId' => 'PayPal-Partner-Attribution-Id',
            'payPalClientMetadataId' => 'PayPal-Client-Metadata-Id',
            'prefer' => 'return=representation'
        ];

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

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

        return $response->getResult();
    }

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

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

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

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

    /**
     * @depends testCreateOrder
     * @depends testCaptureOrder
     */
    public function getOrderDetails(Order $orderDetail): void
    {
        $request = [
            'id' => $orderDetail->getId()
        ];

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

        $this->assertTrue($response->isSuccess());
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertNotNull($response->getResult()->getPaymentSource());
        $this->assertEquals('COMPLETED', $response->getResult()->getStatus());
    }
}
