<?php

namespace App\Domain\PaymentGateway\Services;

use YooKassa\Client;

class YookassaService
{
    public static function payments_link($amount, $order, $user)
    {

        $YOOKASSA_SHOP_ID = env('YOOKASSA_SHOP_ID');
        $YOOKASSA_KEY = env('YOOKASSA_KEY');

        $idempotenceKey = $order->number;
        $client = new Client();
        $client->setAuth($YOOKASSA_SHOP_ID, $YOOKASSA_KEY);
        $success_url = env('APP_URL') . '/payments/status/' . $order->number;
        $response = $client->createPayment(
            [
                'amount' => [
                    'value' => $amount,
                    'currency' => 'RUB',
                ],
                'capture' => true,
                'payment_method_data' => [
                    'type' => 'sbp',
                ],
                'confirmation' => [
                    'type' => 'redirect',
                    'return_url' => $success_url,
                ],
                'receipt' => [
                    'customer' => [
                        'full_name' => $user->name ?? $user->username,
                        'phone' => $user->phone,
                        "email" => $user->email
                    ],
                    'items' => [
                        [
                            'description' => 'оплата бонусов тизер',
                            'quantity' => 1.00,
                            'amount' => [
                                'value' => $amount,
                                'currency' => 'RUB',
                            ],
                            'vat_code' => 1,
                            'payment_mode' => 'full_payment'
                        ]
                    ]
                ],
                'description' => 'Заказ №' . $order->id,
            ],
            $idempotenceKey
        );
        $order->system_payment_id = $response->getId();
        $order->save();


        $confirmationUrl = $response->getConfirmation()->getConfirmationUrl();
        return $confirmationUrl;


    }
}
