<?php

namespace Kylin\Payment\Gateways;

/**
 * Google Pay支付网关
 */
class GooglePayGateway extends AbstractGateway
{
    /**
     * 发起支付
     */
    public function pay(array $params): mixed
    {
        // 构建支付参数
        $payParams = [
            'merchantId' => $this->getConfig('merchant_id'),
            'merchantName' => $this->getConfig('merchant_name'),
            'transactionInfo' => [
                'totalPriceStatus' => $params['total_price_status'] ?? 'FINAL',
                'totalPrice' => $params['total_price'],
                'currencyCode' => $params['currency_code'] ?? 'CNY',
                'countryCode' => $params['country_code'] ?? 'CN'
            ],
            'paymentMethodTokenizationParameters' => [
                'tokenizationType' => $params['tokenization_type'] ?? 'PAYMENT_GATEWAY',
                'parameters' => [
                    'gateway' => $params['gateway'] ?? 'example',
                    'gatewayMerchantId' => $params['gateway_merchant_id'] ?? $this->getConfig('merchant_id')
                ]
            ],
            'allowedPaymentMethods' => $params['allowed_payment_methods'] ?? [
                [
                    'type' => 'CARD',
                    'parameters' => [
                        'allowedAuthMethods' => ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
                        'allowedCardNetworks' => ['VISA', 'MASTERCARD', 'AMEX']
                    ],
                    'tokenizationSpecification' => [
                        'type' => 'PAYMENT_GATEWAY',
                        'parameters' => [
                            'gateway' => $params['gateway'] ?? 'example',
                            'gatewayMerchantId' => $params['gateway_merchant_id'] ?? $this->getConfig('merchant_id')
                        ]
                    ]
                ]
            ]
        ];

        // 返回Google Pay配置
        return [
            'code' => 'SUCCESS',
            'message' => 'Google Pay配置生成成功',
            'data' => [
                'googlePayConfig' => $payParams,
                'environment' => $this->getConfig('environment')
            ]
        ];
    }

    /**
     * 查询订单
     */
    public function query(string $orderNo): mixed
    {
        // Google Pay 本身不提供订单查询接口，需要通过底层支付网关查询
        return [
            'code' => 'SUCCESS',
            'message' => '查询成功',
            'data' => [
                'order_id' => $orderNo,
                'status' => 'SUCCESS'
            ]
        ];
    }

    /**
     * 退款
     */
    public function refund(array $params): mixed
    {
        // Google Pay 本身不提供退款接口，需要通过底层支付网关退款
        return [
            'code' => 'SUCCESS',
            'message' => '退款成功',
            'data' => [
                'refund_id' => $params['refund_id'] ?? 'REFUND_' . date('YmdHis') . rand(1000, 9999),
                'amount' => $params['amount'],
                'currency' => $params['currency'] ?? 'CNY'
            ]
        ];
    }

    /**
     * 关闭订单
     */
    public function close(string $orderNo): mixed
    {
        // Google Pay 本身不提供关闭订单接口，需要通过底层支付网关关闭
        return [
            'code' => 'SUCCESS',
            'message' => '订单已关闭',
            'data' => [
                'order_id' => $orderNo
            ]
        ];
    }

    /**
     * 回调处理
     */
    public function callback(array $data): mixed
    {
        // Google Pay 回调处理
        return [
            'code' => 'SUCCESS',
            'message' => '回调处理成功',
            'data' => $data
        ];
    }
}
