Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
8ctopus committed May 24, 2024
1 parent d487cd1 commit 1f4a10e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
10 changes: 4 additions & 6 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Plans.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 2 additions & 6 deletions src/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions src/RestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> $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
*
Expand Down
8 changes: 2 additions & 6 deletions src/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 1f4a10e

Please sign in to comment.