diff --git a/README.md b/README.md index 888a439..6245746 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The package's installation is done using composer. Simply add these lines to you ```json { "require": { - "ProcessOut/ProcessOut-php": "^6.0.1" + "ProcessOut/ProcessOut-php": "^6.1.0" } } ``` diff --git a/spec.php b/spec.php index aa80617..b85acf3 100644 --- a/spec.php +++ b/spec.php @@ -36,11 +36,12 @@ assert(!empty($customer->getId()), 'The created customer ID should not be empty'); $subscription = $client->newSubscription(array( + 'customer_id' => $customer->getId(), 'amount' => '9.99', 'currency' => 'USD', 'interval' => '1d', 'name' => 'great subscription' -))->create($customer->getId()); +))->create(); assert(!empty($subscription->getId()), 'The created subscription ID should not be empty'); // Expand a customers' project and fetch gateways diff --git a/src/Addon.php b/src/Addon.php index a52fa58..2e71b04 100755 --- a/src/Addon.php +++ b/src/Addon.php @@ -497,18 +497,52 @@ public function fillWithData($data) /** - * Apply a new addon to the given subscription ID. + * Get the addons applied to the subscription. * @param string $subscriptionId * @param array $options - * @return $this + * @return array */ - public function apply($subscriptionId, $options = array()) + public function fetchSubscriptionAddons($subscriptionId, $options = array()) { $this->fillWithData($options); $request = new Request($this->client); $path = "/subscriptions/" . urlencode($subscriptionId) . "/addons"; + $data = array( + + ); + + $response = $request->get($path, $data, $options); + $returnValues = array(); + + + // Handling for field addons + $a = array(); + $body = $response->getBody(); + foreach($body['addons'] as $v) + { + $tmp = new Addon($this->client); + $tmp->fillWithData($v); + $a[] = $tmp; + } + $returnValues['Addons'] = $a; + + return array_values($returnValues)[0]; + } + + /** + * Create a new addon to the given subscription ID. + * @param array $options + * @return $this + */ + public function create($options = array()) + { + $this->fillWithData($options); + + $request = new Request($this->client); + $path = "/subscriptions/" . urlencode($this->getSubscriptionId()) . "/addons"; + $data = array( "plan_id" => $this->getPlanId(), "type" => $this->getType(), @@ -528,7 +562,7 @@ public function apply($subscriptionId, $options = array()) // Handling for field addon $body = $response->getBody(); $body = $body['addon']; - $returnValues['apply'] = $this->fillWithData($body); + $returnValues['create'] = $this->fillWithData($body); return array_values($returnValues)[0]; } @@ -601,18 +635,16 @@ public function save($options = array()) } /** - * Remove an addon applied to a subscription. - * @param string $subscriptionId - * @param string $addonId + * Delete an addon applied to a subscription. * @param array $options * @return bool */ - public function remove($subscriptionId, $addonId, $options = array()) + public function delete($options = array()) { $this->fillWithData($options); $request = new Request($this->client); - $path = "/subscriptions/" . urlencode($subscriptionId) . "/addons/" . urlencode($addonId) . ""; + $path = "/subscriptions/" . urlencode($this->getSubscriptionId()) . "/addons/" . urlencode($this->getId()) . ""; $data = array( "prorate" => (!empty($options["prorate"])) ? $options["prorate"] : null, diff --git a/src/AuthorizationRequest.php b/src/AuthorizationRequest.php index d2a1b1c..fd2d837 100755 --- a/src/AuthorizationRequest.php +++ b/src/AuthorizationRequest.php @@ -559,11 +559,10 @@ public function fetchCustomer($options = array()) /** * Create a new authorization request for the given customer ID. - * @param string $customerId * @param array $options * @return $this */ - public function create($customerId, $options = array()) + public function create($options = array()) { $this->fillWithData($options); @@ -571,11 +570,11 @@ public function create($customerId, $options = array()) $path = "/authorization-requests"; $data = array( + "customer_id" => $this->getCustomerId(), "name" => $this->getName(), "currency" => $this->getCurrency(), "return_url" => $this->getReturnUrl(), - "cancel_url" => $this->getCancelUrl(), - "customer_id" => $customerId + "cancel_url" => $this->getCancelUrl() ); $response = $request->post($path, $data, $options); diff --git a/src/Discount.php b/src/Discount.php index 882d982..62beb3f 100755 --- a/src/Discount.php +++ b/src/Discount.php @@ -497,18 +497,52 @@ public function fillWithData($data) /** - * Apply a new discount to the given subscription ID. + * Get the discounts applied to the subscription. * @param string $subscriptionId * @param array $options - * @return $this + * @return array */ - public function apply($subscriptionId, $options = array()) + public function fetchSubscriptionDiscounts($subscriptionId, $options = array()) { $this->fillWithData($options); $request = new Request($this->client); $path = "/subscriptions/" . urlencode($subscriptionId) . "/discounts"; + $data = array( + + ); + + $response = $request->get($path, $data, $options); + $returnValues = array(); + + + // Handling for field discounts + $a = array(); + $body = $response->getBody(); + foreach($body['discounts'] as $v) + { + $tmp = new Discount($this->client); + $tmp->fillWithData($v); + $a[] = $tmp; + } + $returnValues['Discounts'] = $a; + + return array_values($returnValues)[0]; + } + + /** + * Create a new discount for the given subscription ID. + * @param array $options + * @return $this + */ + public function create($options = array()) + { + $this->fillWithData($options); + + $request = new Request($this->client); + $path = "/subscriptions/" . urlencode($this->getSubscriptionId()) . "/discounts"; + $data = array( "coupon_id" => $this->getCouponId(), "name" => $this->getName(), @@ -524,7 +558,7 @@ public function apply($subscriptionId, $options = array()) // Handling for field discount $body = $response->getBody(); $body = $body['discount']; - $returnValues['apply'] = $this->fillWithData($body); + $returnValues['create'] = $this->fillWithData($body); return array_values($returnValues)[0]; } @@ -560,18 +594,16 @@ public function find($subscriptionId, $discountId, $options = array()) } /** - * Remove a discount applied to a subscription. - * @param string $subscriptionId - * @param string $discountId + * Delete a discount applied to a subscription. * @param array $options * @return bool */ - public function remove($subscriptionId, $discountId, $options = array()) + public function delete($options = array()) { $this->fillWithData($options); $request = new Request($this->client); - $path = "/subscriptions/" . urlencode($subscriptionId) . "/discounts/" . urlencode($discountId) . ""; + $path = "/subscriptions/" . urlencode($this->getSubscriptionId()) . "/discounts/" . urlencode($this->getId()) . ""; $data = array( diff --git a/src/Refund.php b/src/Refund.php index c73ec5a..ea992d4 100755 --- a/src/Refund.php +++ b/src/Refund.php @@ -358,6 +358,41 @@ public function fillWithData($data) } + /** + * Get the transaction's refunds. + * @param string $transactionId + * @param array $options + * @return array + */ + public function fetchTransactionRefunds($transactionId, $options = array()) + { + $this->fillWithData($options); + + $request = new Request($this->client); + $path = "/transactions/" . urlencode($transactionId) . "/refunds"; + + $data = array( + + ); + + $response = $request->get($path, $data, $options); + $returnValues = array(); + + + // Handling for field refunds + $a = array(); + $body = $response->getBody(); + foreach($body['refunds'] as $v) + { + $tmp = new Refund($this->client); + $tmp->fillWithData($v); + $a[] = $tmp; + } + $returnValues['Refunds'] = $a; + + return array_values($returnValues)[0]; + } + /** * Find a transaction's refund by its ID. * @param string $transactionId @@ -389,17 +424,16 @@ public function find($transactionId, $refundId, $options = array()) } /** - * Apply a refund to a transaction. - * @param string $transactionId + * Create a refund for a transaction. * @param array $options * @return bool */ - public function apply($transactionId, $options = array()) + public function create($options = array()) { $this->fillWithData($options); $request = new Request($this->client); - $path = "/transactions/" . urlencode($transactionId) . "/refunds"; + $path = "/transactions/" . urlencode($this->getTransactionId()) . "/refunds"; $data = array( "amount" => $this->getAmount(), diff --git a/src/Subscription.php b/src/Subscription.php index 2bc3990..e5a3a93 100755 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -1256,12 +1256,12 @@ public function findAddon($addonId, $options = array()) } /** - * Remove an addon applied to a subscription. + * Delete an addon applied to a subscription. * @param string $addonId * @param array $options * @return bool */ - public function removeAddon($addonId, $options = array()) + public function deleteAddon($addonId, $options = array()) { $this->fillWithData($options); @@ -1378,12 +1378,12 @@ public function findDiscount($discountId, $options = array()) } /** - * Remove a discount applied to a subscription. + * Delete a discount applied to a subscription. * @param string $discountId * @param array $options * @return bool */ - public function removeDiscount($discountId, $options = array()) + public function deleteDiscount($discountId, $options = array()) { $this->fillWithData($options); @@ -1472,11 +1472,10 @@ public function all($options = array()) /** * Create a new subscription for the given customer. - * @param string $customerId * @param array $options * @return $this */ - public function create($customerId, $options = array()) + public function create($options = array()) { $this->fillWithData($options); @@ -1492,11 +1491,11 @@ public function create($customerId, $options = array()) "metadata" => $this->getMetadata(), "interval" => $this->getInterval(), "trial_end_at" => $this->getTrialEndAt(), + "customer_id" => $this->getCustomerId(), "return_url" => $this->getReturnUrl(), "cancel_url" => $this->getCancelUrl(), "source" => (!empty($options["source"])) ? $options["source"] : null, - "coupon_id" => (!empty($options["coupon_id"])) ? $options["coupon_id"] : null, - "customer_id" => $customerId + "coupon_id" => (!empty($options["coupon_id"])) ? $options["coupon_id"] : null ); $response = $request->post($path, $data, $options); @@ -1580,11 +1579,10 @@ public function save($options = array()) /** * Cancel a subscription. The reason may be provided as well. - * @param string $cancellationReason * @param array $options * @return $this */ - public function cancel($cancellationReason, $options = array()) + public function cancel($options = array()) { $this->fillWithData($options); @@ -1593,8 +1591,8 @@ public function cancel($cancellationReason, $options = array()) $data = array( "cancel_at" => $this->getCancelAt(), - "cancel_at_end" => (!empty($options["cancel_at_end"])) ? $options["cancel_at_end"] : null, - "cancellation_reason" => $cancellationReason + "cancellation_reason" => $this->getCancellationReason(), + "cancel_at_end" => (!empty($options["cancel_at_end"])) ? $options["cancel_at_end"] : null ); $response = $request->delete($path, $data, $options); diff --git a/src/Token.php b/src/Token.php index deebc21..25e9901 100755 --- a/src/Token.php +++ b/src/Token.php @@ -435,18 +435,17 @@ public function fillWithData($data) /** - * Find a customer's token by its ID. + * Get the customer's tokens. * @param string $customerId - * @param string $tokenId * @param array $options - * @return $this + * @return array */ - public function find($customerId, $tokenId, $options = array()) + public function fetchCustomerTokens($customerId, $options = array()) { $this->fillWithData($options); $request = new Request($this->client); - $path = "/customers/" . urlencode($customerId) . "/tokens/" . urlencode($tokenId) . ""; + $path = "/customers/" . urlencode($customerId) . "/tokens"; $data = array( @@ -456,67 +455,68 @@ public function find($customerId, $tokenId, $options = array()) $returnValues = array(); - // Handling for field token + // Handling for field tokens + $a = array(); $body = $response->getBody(); - $body = $body['token']; - $returnValues['find'] = $this->fillWithData($body); + foreach($body['tokens'] as $v) + { + $tmp = new Token($this->client); + $tmp->fillWithData($v); + $a[] = $tmp; + } + $returnValues['Tokens'] = $a; return array_values($returnValues)[0]; } /** - * Create a new token for the given customer ID. + * Find a customer's token by its ID. * @param string $customerId - * @param string $source + * @param string $tokenId * @param array $options * @return $this */ - public function create($customerId, $source, $options = array()) + public function find($customerId, $tokenId, $options = array()) { $this->fillWithData($options); $request = new Request($this->client); - $path = "/customers/" . urlencode($customerId) . "/tokens"; + $path = "/customers/" . urlencode($customerId) . "/tokens/" . urlencode($tokenId) . ""; $data = array( - "metadata" => $this->getMetadata(), - "settings" => (!empty($options["settings"])) ? $options["settings"] : null, - "target" => (!empty($options["target"])) ? $options["target"] : null, - "source" => $source + ); - $response = $request->post($path, $data, $options); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field token $body = $response->getBody(); $body = $body['token']; - $returnValues['create'] = $this->fillWithData($body); + $returnValues['find'] = $this->fillWithData($body); return array_values($returnValues)[0]; } /** - * Create a new token for the given customer ID from an authorization request - * @param string $customerId - * @param string $source - * @param string $target + * Create a new token for the given customer ID. * @param array $options * @return $this */ - public function createFromRequest($customerId, $source, $target, $options = array()) + public function create($options = array()) { $this->fillWithData($options); $request = new Request($this->client); - $path = "/customers/" . urlencode($customerId) . "/tokens"; + $path = "/customers/" . urlencode($this->getCustomerId()) . "/tokens"; $data = array( "metadata" => $this->getMetadata(), + "source" => (!empty($options["source"])) ? $options["source"] : null, "settings" => (!empty($options["settings"])) ? $options["settings"] : null, - "source" => $source, - "target" => $target + "target" => (!empty($options["target"])) ? $options["target"] : null, + "set_default" => (!empty($options["set_default"])) ? $options["set_default"] : null ); $response = $request->post($path, $data, $options); @@ -526,7 +526,7 @@ public function createFromRequest($customerId, $source, $target, $options = arra // Handling for field token $body = $response->getBody(); $body = $body['token']; - $returnValues['createFromRequest'] = $this->fillWithData($body); + $returnValues['create'] = $this->fillWithData($body); return array_values($returnValues)[0]; }