GetGatewayDataFromRequest.php
46 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Actions; |
| 4 | |
| 5 | /** |
| 6 | * @since 2.22.0 |
| 7 | */ |
| 8 | class GetGatewayDataFromRequest |
| 9 | { |
| 10 | /** |
| 11 | * This filter logic will support the request coming in as application/json or formData. |
| 12 | * In order for the $gatewayData to be automatically accessible the data will need to come in |
| 13 | * through a specific key called `gatewayData`. |
| 14 | * |
| 15 | * @since 3.0.0 Updated logic to support all native content types. |
| 16 | * @since 2.22.0 |
| 17 | */ |
| 18 | public function __invoke(): array |
| 19 | { |
| 20 | $gatewayData = []; |
| 21 | |
| 22 | if (isset($_REQUEST['gatewayData'])) { |
| 23 | $gatewayData = give_clean($_REQUEST['gatewayData']); |
| 24 | } else if ($this->requestIsJson()) { |
| 25 | $requestData = file_get_contents('php://input'); |
| 26 | $requestData = json_decode($requestData, true); |
| 27 | |
| 28 | if (array_key_exists('gatewayData', $requestData)) { |
| 29 | $gatewayData = give_clean($requestData['gatewayData']); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return $gatewayData; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * This checks the server content type for 'application/json' to determine if it is a json request. |
| 38 | * |
| 39 | * @since 3.0.0 |
| 40 | */ |
| 41 | protected function requestIsJson(): bool |
| 42 | { |
| 43 | return isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], 'application/json'); |
| 44 | } |
| 45 | } |
| 46 |