diff --git a/src/Hooks.php b/src/Hooks.php index 9fb3c9a..4ddb430 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -77,9 +77,7 @@ public function create(string $url, array $eventTypes) : string $url = '/v1/notifications/webhooks'; - $body = json_encode($data, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $response = $this->sendRequest('POST', $url, [], $body, 201); + $response = $this->sendJsonRequest('POST', $url, [], $data, 201); $decoded = json_decode($response, true, 512, JSON_THROW_ON_ERROR); @@ -131,13 +129,13 @@ public function simulate(string $webhookId, string $eventType) : array break; } - $body = json_encode([ + $json = [ 'webhook_id' => $webhookId, 'event_type' => $eventType, 'resource_version' => $version, - ], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); + ]; - $response = $this->sendRequest('POST', $url, [], $body, 202); + $response = $this->sendJsonRequest('POST', $url, [], $json, 202); return json_decode($response, true); } diff --git a/src/Orders.php b/src/Orders.php index afb878c..df26681 100644 --- a/src/Orders.php +++ b/src/Orders.php @@ -80,9 +80,7 @@ public function create(Intent $intent, string $currency, float $amount, ?string ]; } - $body = json_encode($order, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $response = $this->sendRequest('POST', $url, [], $body, [200, 201]); + $response = $this->sendJsonRequest('POST', $url, [], $order, [200, 201]); return json_decode($response, true); } @@ -143,9 +141,7 @@ public function track(string $id, string $carrier, string $trackingNumber, strin // items ]; - $body = json_encode($order, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $response = $this->sendRequest('POST', $url, [], $body, 201); + $response = $this->sendJsonRequest('POST', $url, [], $order, 201); return json_decode($response, true); } diff --git a/src/Plans.php b/src/Plans.php index d780709..9157ba7 100644 --- a/src/Plans.php +++ b/src/Plans.php @@ -100,9 +100,7 @@ public function create(string $productId, string $name, string $description, Sta $object = (object) array_merge((array) $object1, $cycles->object(), (array) $payment->object(), (array) $object2, (array) $taxes->object()); - $body = json_encode($object, JSON_PRETTY_PRINT); - - $response = $this->sendRequest('POST', $url, [], $body, 201); + $response = $this->sendJsonRequest('POST', $url, [], (array) $object, 201); return json_decode($response, true, 512, JSON_THROW_ON_ERROR); } @@ -177,9 +175,7 @@ public function update(string $id, Operation $operation, string $attribute, bool ], ]; - $body = json_encode($update, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $this->sendRequest('PATCH', $url, [], $body, 204); + $this->sendJsonRequest('PATCH', $url, [], $update, 204); return $this; } diff --git a/src/Products.php b/src/Products.php index fa3a31e..89a385e 100644 --- a/src/Products.php +++ b/src/Products.php @@ -99,9 +99,7 @@ public function create(array $product) : self $url = '/v1/catalogs/products'; - $body = json_encode($product, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $this->sendRequest('POST', $url, [], $body, 201); + $this->sendJsonRequest('POST', $url, [], $product, 201); return $this; } @@ -126,11 +124,9 @@ public function update(string $id, string $operation, string $path, string $valu ], ]; - $body = json_encode($update, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - $url = "/v1/catalogs/products/{$id}"; - $this->sendRequest('PATCH', $url, [], $body, 204); + $this->sendJsonRequest('PATCH', $url, [], $update, 204); return $this; } diff --git a/src/RestBase.php b/src/RestBase.php index b08fbec..db7109e 100644 --- a/src/RestBase.php +++ b/src/RestBase.php @@ -50,6 +50,26 @@ protected function sendRequest(string $method, string $uri, array $headers, ?str return $this->handler->processResponse($response, $expectedStatus); } + /** + * Send json request + * + * @param string $method + * @param string $uri + * @param array $headers + * @param array $json + * @param array|int $expectedStatus + * + * @return string + * + * @throws PayPalException + */ + protected function sendJsonRequest(string $method, string $uri, array $headers, array $json, array|int $expectedStatus) : string + { + $body = json_encode($json, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); + + return $this->sendRequest($method, $uri, $headers, $body, $expectedStatus); + } + /** * Get headers * diff --git a/src/Subscriptions.php b/src/Subscriptions.php index b5e726f..11e001a 100644 --- a/src/Subscriptions.php +++ b/src/Subscriptions.php @@ -72,9 +72,7 @@ public function create(string $planId, string $successUrl, string $cancelUrl) : ], ]; - $body = json_encode($subscription, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $response = $this->sendRequest('POST', $url, [], $body, 201); + $response = $this->sendJsonRequest('POST', $url, [], $subscription, 201); return json_decode($response, true); } @@ -102,9 +100,7 @@ public function capture(string $id, string $currency, float $amount, string $not 'note' => $note, ]; - $body = json_encode($capture, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); - - $this->sendRequest('POST', $url, [], $body, 202); + $this->sendJsonRequest('POST', $url, [], $capture, 202); return $this; }