GetGatewayDataFromRequest.php
45 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 2.22.0 |
| 16 | */ |
| 17 | public function __invoke(): array |
| 18 | { |
| 19 | $gatewayData = []; |
| 20 | |
| 21 | if (!isset($_SERVER['CONTENT_TYPE'])) { |
| 22 | return $gatewayData; |
| 23 | } |
| 24 | |
| 25 | $contentType = $_SERVER['CONTENT_TYPE']; |
| 26 | |
| 27 | // this content type is typically used throughout legacy with jQuery and wp-ajax |
| 28 | if (($contentType === "application/x-www-form-urlencoded") && isset($_REQUEST['gatewayData'])) { |
| 29 | $gatewayData = give_clean($_REQUEST['gatewayData']); |
| 30 | } |
| 31 | |
| 32 | // this content type is typically used with the fetch api and our custom routes |
| 33 | if ($contentType === "application/json") { |
| 34 | $requestData = file_get_contents('php://input'); |
| 35 | $requestData = json_decode($requestData, true); |
| 36 | |
| 37 | if (array_key_exists('gatewayData', $requestData)) { |
| 38 | $gatewayData = give_clean($requestData['gatewayData']); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return $gatewayData; |
| 43 | } |
| 44 | } |
| 45 |