From 627e92c9a85c344ec4fdcee8207328dbe2944b7f Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Mon, 31 Jul 2023 15:42:42 -0400 Subject: [PATCH] Feature: add check for application/json in headers accept (#6857) * feature: add check for multipart/form-data * refactor: cleanup logic * docs: add since tag and descriptions * refactor: check headers for accept bbefore content-type * docs: update since tag --------- Co-authored-by: Jon Waldstein --- .../Traits/HandleHttpResponses.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Framework/PaymentGateways/Traits/HandleHttpResponses.php b/src/Framework/PaymentGateways/Traits/HandleHttpResponses.php index 7d061aa416..36d0846a97 100644 --- a/src/Framework/PaymentGateways/Traits/HandleHttpResponses.php +++ b/src/Framework/PaymentGateways/Traits/HandleHttpResponses.php @@ -11,6 +11,7 @@ trait HandleHttpResponses /** * Handle Response * + * @unreleased added check for responding with json * @since 2.27.0 add support for json content-type * @since 2.18.0 * @@ -19,7 +20,7 @@ trait HandleHttpResponses public function handleResponse($type) { if ($type instanceof RedirectResponse) { - if (isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], "application/json")) { + if ($this->wantsJson()) { wp_send_json([ 'type' => 'redirect', 'data' => [ @@ -60,4 +61,20 @@ public function handleExceptionResponse(\Exception $exception, string $message) give_set_error('PaymentGatewayException', $message); give_send_back_to_checkout(); } + + /** + * This checks the server headers for 'application/json' to determine if it should respond with json. + * + * @unreleased + * + * @return bool + */ + protected function wantsJson(): bool + { + if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json')) { + return true; + } + + return isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], 'application/json'); + } }