<?php

/**
 * The Create Payment Token endpoint.
 *
 * @package WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint
 */
declare (strict_types=1);
namespace WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint;

use Exception;
use WC_Order;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Button\Exception\NonceValidationException;
/**
 * Class SubscriptionChangePaymentMethod
 */
class SubscriptionChangePaymentMethod implements EndpointInterface
{
    const ENDPOINT = 'ppc-subscription-change-payment-method';
    /**
     * The request data.
     *
     * @var RequestData
     */
    private $request_data;
    /**
     * SubscriptionChangePaymentMethod constructor.
     *
     * @param RequestData $request_data $request_data The request data.
     */
    public function __construct(RequestData $request_data)
    {
        $this->request_data = $request_data;
    }
    /**
     * Returns the nonce.
     *
     * @return string
     */
    public static function nonce(): string
    {
        return self::ENDPOINT;
    }
    /**
     * Handles the request.
     *
     * @throws Exception On Error.
     */
    public function handle_request(): void
    {
        try {
            if (!get_current_user_id()) {
                wp_send_json_error(array(), 403);
            }
            $data = $this->request_data->read_request($this->nonce());
            $subscription = wcs_get_subscription($data['subscription_id']);
            if (!$subscription instanceof WC_Order) {
                wp_send_json_error();
            }
            if ((int) $subscription->get_customer_id() !== (int) get_current_user_id()) {
                wp_send_json_error(array('message' => __('You do not have permission to modify this subscription.', 'woocommerce-paypal-payments')), 403);
            }
            $wc_payment_token = WC_Payment_Tokens::get($data['wc_payment_token_id']);
            if ($wc_payment_token && $wc_payment_token->get_user_id() !== (int) get_current_user_id()) {
                wp_send_json_error(array('message' => __('You do not have permission to use this payment token.', 'woocommerce-paypal-payments')), 403);
            }
            $subscription->set_payment_method($data['payment_method']);
            if ($wc_payment_token) {
                $subscription->add_payment_token($wc_payment_token);
            }
            $subscription->save();
            wp_send_json_success();
        } catch (NonceValidationException $error) {
            wp_send_json_error(array('message' => $error->getMessage()), 400);
        } catch (Exception $exception) {
            wp_send_json_error();
        }
    }
}
