diff --git a/src/API2Client/Api.php b/src/API2Client/Api.php index e03cf09..78b2b7e 100644 --- a/src/API2Client/Api.php +++ b/src/API2Client/Api.php @@ -11,6 +11,7 @@ use API2Client\Client\APIClient; use API2Client\Client\Http\HttpClient; +use API2Client\Entities\Subscription; use API2Client\Setters\OrderCreatedFactory; use API2Client\Setters\OrderCurrencyFactory; use API2Client\Setters\OrderItemFactory; @@ -18,6 +19,8 @@ use API2Client\Setters\OrderStatusesFactory; use API2Client\Setters\OrderPaymentFactory; use API2Client\Setters\OrderStatusFactory; +use API2Client\Setters\SubscriptionCreatedFactory; +use API2Client\Setters\SubscriptionResultFactory; use API2Client\Setters\TemplateFactory; /** @@ -314,4 +317,48 @@ public function getCurrencies () return $result; } + + /** + * @param Subscription $subscription + * @return Entities\SubscriptionResult + * @throws ApiException + */ + public function createPaymentSubscription (Subscription $subscription) + { + $response = $this + ->client + ->call ('orders.subscribe', $subscription->toArray (), HttpClient::REQUEST_RAW); + + if (!$response->isSuccess ()) + { + throw new ApiException ($response->getErrorMessage ()); + } + + $subscription = new SubscriptionResultFactory(); + + return $subscription + ->create ($response->getResult ()); + } + + /** + * @param $id + * @return Entities\SubscriptionCreated + * @throws ApiException + */ + public function cancelPaymentSubscription ($id) + { + $response = $this + ->client + ->call ('orders.unsubscribe', array ('id' => $id), HttpClient::REQUEST_RAW); + + if (!$response->isSuccess ()) + { + throw new ApiException ($response->getErrorMessage ()); + } + + $subscription = new SubscriptionResultFactory (); + + return $subscription + ->create ($response->getResult ()); + } } \ No newline at end of file diff --git a/src/API2Client/Client/APIClient.php b/src/API2Client/Client/APIClient.php index b13cba4..8546ecb 100644 --- a/src/API2Client/Client/APIClient.php +++ b/src/API2Client/Client/APIClient.php @@ -57,6 +57,7 @@ public function call ($apiMethod, $params = array (), $method = HttpClient::REQU { $resultContent = $this->httpClient->request ($this->getAuthorizedUrl ($apiMethod), $params, $method); + $response = new APIResponse (); $response->create ($resultContent); diff --git a/src/API2Client/Entities/Subscription.php b/src/API2Client/Entities/Subscription.php new file mode 100644 index 0000000..607f76d --- /dev/null +++ b/src/API2Client/Entities/Subscription.php @@ -0,0 +1,588 @@ +id; + } + + /** + * @param string $id + * @return $this + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + + /** + * @return int + */ + public function getPaymentSystemId() + { + return $this->payment_system_id; + } + + /** + * @param int $payment_system_id + * @return $this + */ + public function setPaymentSystemId($payment_system_id) + { + $this->payment_system_id = $payment_system_id; + return $this; + } + + /** + * @return int + */ + public function getProjectId() + { + return $this->project_id; + } + + /** + * @param int $project_id + * @return $this + */ + public function setProjectId($project_id) + { + $this->project_id = $project_id; + return $this; + } + + /** + * @return int + */ + public function getCurrencyId() + { + return $this->currency_id; + } + + /** + * @param int $currency_id + * @return $this + */ + public function setCurrencyId($currency_id) + { + $this->currency_id = $currency_id; + return $this; + } + + /** + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param string $title + * @return $this + */ + public function setTitle($title) + { + $this->title = $title; + return $this; + } + + /** + * @return int + */ + public function getStatus() + { + return $this->status; + } + + /** + * @param int $status + * @return $this + */ + public function setStatus($status) + { + $this->status = $status; + return $this; + } + + /** + * @return string + */ + public function getStatusName() + { + return $this->statusName; + } + + /** + * @param string $statusName + * @return $this + */ + public function setStatusName($statusName) + { + $this->statusName = $statusName; + return $this; + } + + /** + * @return mixed + */ + public function getStart() + { + return $this->start; + } + + /** + * @param mixed $start + * @return $this + */ + public function setStart($start) + { + $this->start = $start; + return $this; + } + + /** + * @return mixed + */ + public function getPeriod() + { + return $this->period; + } + + /** + * @param mixed $period + * @return $this + */ + public function setPeriod ($period) + { + $this->period = $period; + return $this; + } + + /** + * @return int + */ + public function getTerm() + { + return $this->term; + } + + /** + * @param int $term + * @return $this + */ + public function setTerm($term) + { + $this->term = $term; + return $this; + } + + /** + * @return int + */ + public function getAmount() + { + return $this->amount; + } + + /** + * @param int $amount + * @return $this + */ + public function setAmount($amount) + { + $this->amount = $amount; + return $this; + } + + /** + * @return float + */ + public function getInitAmount() + { + return $this->init_amount; + } + + /** + * @param float $init_amount + * @return $this + */ + public function setInitAmount($init_amount) + { + $this->init_amount = $init_amount; + return $this; + } + + /** + * @return string + */ + public function getCustomerPhone() + { + return $this->customer_phone; + } + + /** + * @param string $customer_phone + * @return $this + */ + public function setCustomerPhone($customer_phone) + { + $this->customer_phone = $customer_phone; + return $this; + } + + /** + * @return string + */ + public function getCustomerEmail() + { + return $this->customer_email; + } + + /** + * @param string $customer_email + * @return $this + */ + public function setCustomerEmail($customer_email) + { + $this->customer_email = $customer_email; + return $this; + } + + /** + * @return string + */ + public function getCustomerFullname() + { + return $this->customer_fullname; + } + + /** + * @param string $customer_fullname + * @return $this + */ + public function setCustomerFullname($customer_fullname) + { + $this->customer_fullname = $customer_fullname; + return $this; + } + + /** + * @return string + */ + public function getCustomerAddress() + { + return $this->customer_address; + } + + /** + * @param string $customer_address + * @return $this + */ + public function setCustomerAddress($customer_address) + { + $this->customer_address = $customer_address; + return $this; + } + + /** + * @return string + */ + public function getCustomerZip() + { + return $this->customer_zip; + } + + /** + * @param string $customer_zip + * @return $this + */ + public function setCustomerZip($customer_zip) + { + $this->customer_zip = $customer_zip; + return $this; + } + + /** + * @return string + */ + public function getCustomerCountryCode() + { + return $this->customer_country_code; + } + + /** + * @param string $customer_country_code + * @return $this + */ + public function setCustomerCountryCode($customer_country_code) + { + $this->customer_country_code = $customer_country_code; + return $this; + } + + /** + * @return string + */ + public function getCustomerStateCode() + { + return $this->customer_state_code; + } + + /** + * @param string $customer_state_code + * @return $this + */ + public function setCustomerStateCode($customer_state_code) + { + $this->customer_state_code = $customer_state_code; + return $this; + } + + /** + * @return string + */ + public function getCustomerCity() + { + return $this->customer_city; + } + + /** + * @param string $customer_city + * @return $this + */ + public function setCustomerCity($customer_city) + { + $this->customer_city = $customer_city; + return $this; + } + + /** + * @return string + */ + public function getCustomerUserAgent() + { + return $this->customer_user_agent; + } + + /** + * @param string $customer_user_agent + * @return $this + */ + public function setCustomerUserAgent($customer_user_agent) + { + $this->customer_user_agent = $customer_user_agent; + return $this; + } + + /** + * @return string + */ + public function getCustomerIpAddress() + { + return $this->customer_ip_address; + } + + /** + * @param string $customer_ip_address + * @return $this + */ + public function setCustomerIpAddress($customer_ip_address) + { + $this->customer_ip_address = $customer_ip_address; + return $this; + } + + /** + * @return string + */ + public function getCustomerLocalTime() + { + return $this->customer_local_time; + } + + /** + * @param string $customer_local_time + * @return $this + */ + public function setCustomerLocalTime($customer_local_time) + { + $this->customer_local_time = $customer_local_time; + return $this; + } + + + /** + * @return array + */ + public function toArray () + { + return array ( + 'id' => $this->getId(), + 'project_id' => $this->getProjectId(), + 'payment_system_id' => $this->getPaymentSystemId (), + 'currency_id' => $this->getCurrencyId(), + 'title' => $this->getTitle(), + 'period' => $this->getPeriod(), + 'start' => $this->getStart(), + 'term' => $this->getTerm(), + 'amount' => $this->getAmount(), + 'init_amount' => $this->getInitAmount(), + 'customer_email' => $this->getCustomerEmail(), + 'customer_fullname' => $this->getCustomerFullname(), + 'customer_address' => $this->getCustomerAddress(), + 'customer_country_code' => $this->getCustomerCountryCode(), + 'customer_state_code' => $this->getCustomerStateCode(), + 'customer_city' => $this->getCustomerCity(), + 'customer_zip' => $this->getCustomerZip(), + 'customer_phone' => $this->getCustomerPhone (), + 'customer_user_agent' => $this->getCustomerUserAgent(), + 'customer_ip_address' => $this->getCustomerIpAddress(), + 'customer_local_time' => $this->getCustomerLocalTime(), + ); + } +} \ No newline at end of file diff --git a/src/API2Client/Entities/SubscriptionResult.php b/src/API2Client/Entities/SubscriptionResult.php new file mode 100644 index 0000000..0454fd1 --- /dev/null +++ b/src/API2Client/Entities/SubscriptionResult.php @@ -0,0 +1,92 @@ +redirect_url; + } + + /** + * @param mixed $redirect_url + */ + public function setRedirectUrl($redirect_url) + { + $this->redirect_url = $redirect_url; + } + + /** + * @return Subscription + */ + public function getSubscription() + { + return $this->subscription; + } + + /** + * @param mixed $subscription + */ + public function setSubscription($subscription) + { + $this->subscription = $subscription; + } + + /** + * @return array + */ + public function getMessages() + { + return $this->messages; + } + + /** + * @param array $messages + */ + public function setMessages($messages) + { + $this->messages = $messages; + } + + /** + * @return boolean + */ + public function isSuccess () + { + return $this->status; + } + + /** + * @param boolean $status + * @return $this + */ + public function setStatus($status) + { + $this->status = $status; + return $this; + } +} \ No newline at end of file diff --git a/src/API2Client/Setters/SubscriptionResult.php b/src/API2Client/Setters/SubscriptionResult.php new file mode 100644 index 0000000..d5338e9 --- /dev/null +++ b/src/API2Client/Setters/SubscriptionResult.php @@ -0,0 +1,87 @@ +setRedirectUrl ($this->getValue ('redirect_url', $data, '')); + $created->setStatus ($this->getValue ('status', $data, false)); + + $dataSubscription = $this->getValue ('subscription', $data, array ()); + + if ($dataSubscription) + { + $created->setSubscription ($this->createSubscriptionObject ($dataSubscription)); + } + + $messages = $this->getValue ('messages', $data, array()); + + if ($messages) + { + $messages = $this->collectMessages ($messages); + $created->setMessages ($messages); + } + + + return $created; + } + + /** + * @param $messages + * @return array + */ + private function collectMessages ($messages) + { + return array_map (function ($message) { + return array_values ($message); + }, $messages); + } + + /** + * @param $dataSubscription + * @return Subscription + */ + private function createSubscriptionObject ($dataSubscription) + { + $subscription = new Subscription (); + + return $subscription + ->setId($this->getValue ('id', $dataSubscription, '')) + ->setProjectId ($this->getValue ('project_id', $dataSubscription, null)) + ->setCurrencyId ($this->getValue ('currency_id', $dataSubscription, null)) + ->setStatus ($this->getValue ('status', $dataSubscription, 0)) + ->setStatusName ($this->getValue ('status_name', $dataSubscription, '')) + ->setTitle ($this->getValue ('title', $dataSubscription, '')) + ->setStart ($this->getValue ('start', $dataSubscription, '')) + ->setPeriod ($this->getValue ('period', $dataSubscription, 0)) + ->setTerm ($this->getValue ('term', $dataSubscription, 0)) + ->setAmount ($this->getValue ('amount', $dataSubscription, 0)) + ->setInitAmount ($this->getValue ('init_amount', $dataSubscription, 0)) + ->setPaymentSystemId ($this->getValue ('payment_system_id', $dataSubscription, null)) + ->setCurrencyId ($this->getValue ('currency_id', $dataSubscription, null)) + ->setCustomerEmail ($this->getValue ('customer_email', $dataSubscription, '')) + ->setCustomerFullname ($this->getValue ('customer_fullname', $dataSubscription, '')) + ->setCustomerAddress ($this->getValue ('customer_address', $dataSubscription, '')) + ->setCustomerCountryCode ($this->getValue ('customer_country_code', $dataSubscription, '')) + ->setCustomerPhone ($this->getValue ('customer_phone', $dataSubscription, '')) + ->setCustomerStateCode ($this->getValue ('customer_state_code', $dataSubscription, '')) + ->setCustomerCity ($this->getValue ('customer_city', $dataSubscription, '')) + ->setCustomerZip ($this->getValue ('customer_zip', $dataSubscription, '')) + ->setCustomerUserAgent($this->getValue ('customer_user_agent', $dataSubscription, '')) + ->setCustomerIpAddress($this->getValue ('customer_ip_address', $dataSubscription, '')) + ->setCustomerLocalTime ($this->getValue ('customer_local_time', $dataSubscription, '')); + } +} \ No newline at end of file