diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8925195 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +vendor +composer.lock \ No newline at end of file diff --git a/README.md b/README.md index bc69ab3..cf9f366 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,16 @@ $order->setPaymentInfo ($paymentInfo); $result = $api->createOrder ($order); +``` + +Get customer management portal link + +```php +// Create API instance +$api = new \API2Client\Api ('api2.templatemonster.com', 'myUserName', 'myUserToken'); +$subscriptionId = 'abc12345678'; +$link = $api->getCustomerManagementPortalLink($subscriptionId); + ``` Error Handling @@ -184,4 +194,4 @@ catch (\API2Client\Client\APIException $e) $e->getMessage (); } -``` +``` \ No newline at end of file diff --git a/src/API2Client/Api.php b/src/API2Client/Api.php index 9522403..7a3ee42 100644 --- a/src/API2Client/Api.php +++ b/src/API2Client/Api.php @@ -11,17 +11,22 @@ use API2Client\Client\APIClient; use API2Client\Client\Http\HttpClient; +use API2Client\Entities\Order\Links; +use API2Client\Entities\Order\Status; +use API2Client\Entities\OrderCreated; use API2Client\Entities\Subscription; +use API2Client\Setters\CustomerPortalFactory; use API2Client\Setters\OrderCreatedFactory; use API2Client\Setters\OrderCurrencyFactory; use API2Client\Setters\OrderItemFactory; use API2Client\Setters\OrderLinksFactory; -use API2Client\Setters\OrderStatusesFactory; use API2Client\Setters\OrderPaymentFactory; +use API2Client\Setters\OrderStatusesFactory; use API2Client\Setters\OrderStatusFactory; use API2Client\Setters\SubscriptionCreatedFactory; use API2Client\Setters\SubscriptionResultFactory; use API2Client\Setters\TemplateFactory; +use InvalidArgumentException; /** * Class Api @@ -39,34 +44,33 @@ class Api * @param $apiUser * @param $apiPassword */ - public function __construct ($apiHost, $apiUser, $apiPassword) + public function __construct($apiHost, $apiUser, $apiPassword) { $this->client = new APIClient (); $this->client - ->setApiHost ($apiHost) - ->setApiUser ($apiUser) - ->setApiPassword ($apiPassword); + ->setApiHost($apiHost) + ->setApiUser($apiUser) + ->setApiPassword($apiPassword); } /** * Return count of templates * - * @throws ApiException * @return integer + * @throws ApiException */ - public function getTemplatesCount () + public function getTemplatesCount() { $response = $this ->client - ->call ('templates.count', array (), HttpClient::REQUEST_GET); + ->call('templates.count', array(), HttpClient::REQUEST_GET); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } - return $response->getResult (); + return $response->getResult(); } /** @@ -81,41 +85,42 @@ public function getTemplatesCount () * @return mixed * @throws ApiException */ - public function getTemplatesList ($offset = 0, $limit = 20, $dateFrom = null, $dateTo = null, $properties = array (), $includedFree = 0 ) - { - $params = array ('start' => $offset, 'count' => $limit, 'includedFree' => $includedFree ); - - if ($properties) - { + public function getTemplatesList( + $offset = 0, + $limit = 20, + $dateFrom = null, + $dateTo = null, + $properties = array(), + $includedFree = 0 + ) { + $params = array('start' => $offset, 'count' => $limit, 'includedFree' => $includedFree); + + if ($properties) { $params['properties'] = join(',', $properties); } - if ($dateFrom !== null) - { + if ($dateFrom !== null) { $params['from'] = $dateFrom; } - if ($dateFrom !== null) - { + if ($dateFrom !== null) { $params['to'] = $dateTo; } $response = $this ->client - ->call ('templates.list', $params, HttpClient::REQUEST_GET); + ->call('templates.list', $params, HttpClient::REQUEST_GET); - if (!$response->isSuccess()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $templateFactory = new TemplateFactory (); - $result = array (); + $result = array(); - foreach ($response->getResult () as $templateData) - { - $result[] = $templateFactory->create ($templateData); + foreach ($response->getResult() as $templateData) { + $result[] = $templateFactory->create($templateData); } return $result; @@ -128,69 +133,66 @@ public function getTemplatesList ($offset = 0, $limit = 20, $dateFrom = null, $d * @return \API2Client\Entities\Template * @throws ApiException */ - public function getTemplate ($template_id) + public function getTemplate($template_id) { $response = $this ->client - ->call ('templates.item', array ('item_id' => $template_id), HttpClient::REQUEST_GET); + ->call('templates.item', array('item_id' => $template_id), HttpClient::REQUEST_GET); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $template_factory = new TemplateFactory (); return $template_factory - ->create ($response->getResult () [0]); + ->create($response->getResult() [0]); } /** * Create Order * * @param Entities\Order $order + * @return OrderCreated * @throws ApiException - * @return \API2Client\Entities\OrderCreated */ - public function createOrder (Entities\Order $order) + public function createOrder(Entities\Order $order) { $response = $this ->client - ->call ('orders.create', $order->toArray (), HttpClient::REQUEST_RAW); + ->call('orders.create', $order->toArray(), HttpClient::REQUEST_RAW); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $orderCreated = new OrderCreatedFactory (); return $orderCreated - ->create ($response->getResult ()); + ->create($response->getResult()); } /** * Get order Status by Order ID * * @param string $order_id + * @return Status * @throws ApiException - * @return \API2Client\Entities\Order\Status */ - public function getOrderStatus ($order_id) + public function getOrderStatus($order_id) { $response = $this ->client - ->call ('orders.status', array ('order_id' => $order_id), HttpClient::REQUEST_GET); + ->call('orders.status', array('order_id' => $order_id), HttpClient::REQUEST_GET); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $orderStatus = new OrderStatusFactory (); return $orderStatus - ->create ($response->getResult ()); + ->create($response->getResult()); } /** @@ -198,69 +200,70 @@ public function getOrderStatus ($order_id) * * @param string $order_id * @param string $template_id + * @return Links * @throws ApiException - * @return \API2Client\Entities\Order\Links */ - public function getOrderLinks ($order_id, $template_id) + public function getOrderLinks($order_id, $template_id) { $response = $this ->client - ->call ('orders.links', array ('order_id' => $order_id, 'template_id' => $template_id), HttpClient::REQUEST_GET); - - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + ->call( + 'orders.links', + array('order_id' => $order_id, 'template_id' => $template_id), + HttpClient::REQUEST_GET + ); + + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $factory = new OrderLinksFactory (); return $factory - ->create ($response->getResult ()); + ->create($response->getResult()); } /** * Get all Statuses * - * @throws ApiException * @return array [\API2Client\Entities\Order\Status] + * @throws ApiException */ - public function getOrder ($order_id) + public function getOrder($order_id) { $response = $this ->client - ->call ('orders.item', array ('order_id' => $order_id), HttpClient::REQUEST_GET); + ->call('orders.item', array('order_id' => $order_id), HttpClient::REQUEST_GET); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $factory = new OrderItemFactory (); return $factory - ->create ($response->getResult ()); + ->create($response->getResult()); } /** * Get all Statuses * - * @throws ApiException * @return array [\API2Client\Entities\Order\Status] + * @throws ApiException */ - public function getOrderStatuses () + public function getOrderStatuses() { $response = $this ->client - ->call ('orders.statuses', array (), HttpClient::REQUEST_GET); + ->call('orders.statuses', array(), HttpClient::REQUEST_GET); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $orderStatuses = new OrderStatusesFactory (); return $orderStatuses - ->create ($response->getResult ()); + ->create($response->getResult()); } /** @@ -269,23 +272,21 @@ public function getOrderStatuses () * @return array * @throws ApiException */ - public function getPaymentMethods () + public function getPaymentMethods() { $response = $this ->client - ->call ('orders.payments'); + ->call('orders.payments'); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $orderPayments = new OrderPaymentFactory (); - $result = array (); + $result = array(); - foreach ($response->getResult () as $paymentData) - { - $result[] = $orderPayments->create ($paymentData); + foreach ($response->getResult() as $paymentData) { + $result[] = $orderPayments->create($paymentData); } return $result; @@ -297,23 +298,21 @@ public function getPaymentMethods () * @return array * @throws ApiException */ - public function getCurrencies () + public function getCurrencies() { $response = $this ->client - ->call ('orders.currencies'); + ->call('orders.currencies'); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $currency = new OrderCurrencyFactory (); - $result = array (); + $result = array(); - foreach ($response->getResult () as $currencyData) - { - $result[] = $currency->create ($currencyData); + foreach ($response->getResult() as $currencyData) { + $result[] = $currency->create($currencyData); } return $result; @@ -324,21 +323,20 @@ public function getCurrencies () * @return Entities\SubscriptionResult * @throws ApiException */ - public function createPaymentSubscription (Subscription $subscription) + public function createPaymentSubscription(Subscription $subscription) { $response = $this ->client - ->call ('orders.subscribe', $subscription->toArray (), HttpClient::REQUEST_RAW); + ->call('orders.subscribe', $subscription->toArray(), HttpClient::REQUEST_RAW); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $subscription = new SubscriptionResultFactory(); return $subscription - ->create ($response->getResult ()); + ->create($response->getResult()); } /** @@ -346,21 +344,20 @@ public function createPaymentSubscription (Subscription $subscription) * @return Entities\SubscriptionCreated * @throws ApiException */ - public function cancelPaymentSubscription ($id) + public function cancelPaymentSubscription($id) { $response = $this ->client - ->call ('orders.unsubscribe', array ('id' => $id), HttpClient::REQUEST_RAW); + ->call('orders.unsubscribe', array('id' => $id), HttpClient::REQUEST_RAW); - if (!$response->isSuccess ()) - { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } $subscription = new SubscriptionResultFactory (); return $subscription - ->create ($response->getResult ()); + ->create($response->getResult()); } /** @@ -384,10 +381,38 @@ public function cancelOrder($order_id, $black_list = 0, $refund = 1, $comment = HttpClient::REQUEST_RAW ); - if (!$response->isSuccess ()) { - throw new ApiException ($response->getErrorMessage ()); + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); } return $order_id; } + + /** + * Get currencies list + * + * @return Entities\Order\CustomerPortal + * @throws ApiException + */ + public function getCustomerManagementPortalLink($subscriptionId) + { + if (empty($subscriptionId) || !is_string($subscriptionId)) { + throw new InvalidArgumentException('Bad Argument'); + } + + $response = $this + ->client + ->call( + 'orders.getCustomerManagementPortalLink', + array('subscription_id' => $subscriptionId), + HttpClient::REQUEST_RAW + ); + + if (!$response->isSuccess()) { + throw new ApiException ($response->getErrorMessage()); + } + + $factory = new CustomerPortalFactory(); + return $factory->create($response->getResult()); + } } diff --git a/src/API2Client/Entities/Order/CustomerPortal.php b/src/API2Client/Entities/Order/CustomerPortal.php new file mode 100644 index 0000000..1840ba2 --- /dev/null +++ b/src/API2Client/Entities/Order/CustomerPortal.php @@ -0,0 +1,29 @@ +link; + } + + /** + * @param string $link + */ + public function setLink($link) + { + $this->link = $link; + } +} \ No newline at end of file diff --git a/src/API2Client/Setters/CustomerPortalFactory.php b/src/API2Client/Setters/CustomerPortalFactory.php new file mode 100644 index 0000000..e57bf88 --- /dev/null +++ b/src/API2Client/Setters/CustomerPortalFactory.php @@ -0,0 +1,22 @@ +setLink($this->getValue('link', $data, null)); + + return $portal; + } +} \ No newline at end of file