<?php

declare(strict_types=1);

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

namespace PaypalServerSDKLib\Tests\E2E;

use Exception;
use PaypalServerSDKLib\Controllers\OrdersController;
use PaypalServerSDKLib\Models\Builders\AddressBuilder;
use PaypalServerSDKLib\Models\Builders\AmountBreakdownBuilder;
use PaypalServerSDKLib\Models\Builders\AmountWithBreakdownBuilder;
use PaypalServerSDKLib\Models\Builders\CardAttributesBuilder;
use PaypalServerSDKLib\Models\Builders\CardExperienceContextBuilder;
use PaypalServerSDKLib\Models\Builders\CardRequestBuilder;
use PaypalServerSDKLib\Models\Builders\CardVerificationBuilder;
use PaypalServerSDKLib\Models\Builders\ItemBuilder;
use PaypalServerSDKLib\Models\Builders\MoneyBuilder;
use PaypalServerSDKLib\Models\Builders\OrderRequestBuilder;
use PaypalServerSDKLib\Models\Builders\PaymentSourceBuilder;
use PaypalServerSDKLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSDKLib\Models\CardVerificationMethod;
use PaypalServerSDKLib\Models\CheckoutPaymentIntent;
use PaypalServerSDKLib\Models\Order;
use PaypalServerSDKLib\Tests\Controllers\BaseTestController;
use PaypalServerSDKLib\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());
    }
}
