Skip to content

Commit

Permalink
Fix function name & improve addon support
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Huez authored and Manuel Huez committed Mar 19, 2017
1 parent d515858 commit d81be3c
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 41 additions & 9 deletions src/Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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];
}
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/AuthorizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,23 +559,22 @@ 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);

$request = new Request($this->client);
$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);
Expand Down
50 changes: 41 additions & 9 deletions src/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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];
}
Expand Down Expand Up @@ -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(

Expand Down
42 changes: 38 additions & 4 deletions src/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
22 changes: 10 additions & 12 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Loading

0 comments on commit d81be3c

Please sign in to comment.