<?php

declare (strict_types=1);
namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\Processor;

use WC_Order;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\PaymentHandlerInterface;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\PaymentResult;
/**
 * Processes Google Pay payments via WooCommerce Payments (Stripe)
 * by mapping the instrument to the expected $_POST format
 * and delegating to the official Store API payment executor.
 */
final class GooglePayPaymentProcessor implements PaymentHandlerInterface
{
    private StoreApiPaymentExecutor $executor;
    public function __construct(StoreApiPaymentExecutor $executor)
    {
        $this->executor = $executor;
    }
    public function supports(string $handler_id): bool
    {
        return 0 === \strpos($handler_id, 'gpay_wcpay_');
    }
    public function process(WC_Order $order, array $instrument): PaymentResult
    {
        $credential = $instrument['credential'] ?? [];
        $pm_id = $credential['token'] ?? null;
        if (!$pm_id || !\is_string($pm_id) || 0 !== \strpos($pm_id, 'pm_')) {
            return PaymentResult::failed('Invalid or missing Stripe PaymentMethod ID in payment instrument.');
        }
        // The payment method context that WCPay expects in $_POST.
        // For Google Pay (express checkout), we need to set the express payment method types.
        // We also set express_payment_type to bypass WooPay platform checks.
        $payment_data = ['payment_method' => 'woocommerce_payments', 'wcpay-express-payment-method-types' => \wp_json_encode(['card']), 'express_payment_type' => 'google_pay', 'wcpay-payment-method' => $pm_id];
        // Delegate to the Store API Executor which handles the actual WordPress hook
        return $this->executor->execute($order, 'woocommerce_payments', $payment_data);
    }
}
