Skip to content

Commit

Permalink
Feature: add check for application/json in headers accept (#6857)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jonwaldstein and Jon Waldstein authored Jul 31, 2023
1 parent a0d278f commit 627e92c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Framework/PaymentGateways/Traits/HandleHttpResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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' => [
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 627e92c

Please sign in to comment.