Skip to content

Commit

Permalink
Add Refunds api
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Linn committed Jul 28, 2014
1 parent 8a770d0 commit 6daff63
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 5 deletions.
89 changes: 89 additions & 0 deletions src/Api/Refunds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* User: Joe Linn
* Date: 7/28/2014
* Time: 3:14 PM
*/

namespace Stripe\Api;


use Stripe\Request\Refunds\CreateRefundRequest;
use Stripe\Response\Refunds\ListRefundsResponse;
use Stripe\Response\Refunds\RefundResponse;

class Refunds extends AbstractApi
{
const REFUND_RESPONSE_CLASS = 'Stripe\Response\Refunds\RefundResponse';
const LIST_REFUNDS_RESPONSE_CLASS = 'Stripe\Response\Refunds\ListRefundsResponse';

/**
* Create a refund
* @param string $chargeId
* @param CreateRefundRequest $request
* @return RefundResponse
* @link https://stripe.com/docs/api#create_refund
*/
public function createRefund($chargeId, CreateRefundRequest $request = null)
{
return $this->client->post($this->buildUrl($chargeId), self::REFUND_RESPONSE_CLASS, $request);
}

/**
* Retrieve a refund
* @param string $chargeId
* @param string $refundId
* @return RefundResponse
* @link https://stripe.com/docs/api#retrieve_refund
*/
public function getRefund($chargeId, $refundId)
{
return $this->client->get($this->buildUrl($chargeId, $refundId), self::REFUND_RESPONSE_CLASS);
}

/**
* Update a refund
* @param string $chargeId
* @param string $refundId
* @param array $metadata
* @return RefundResponse
* @link https://stripe.com/docs/api#update_refund
*/
public function updateRefund($chargeId, $refundId, array $metadata)
{
return $this->client->post($this->buildUrl($chargeId, $refundId), self::REFUND_RESPONSE_CLASS, array('metadata' => $metadata));
}

/**
* List refunds associated with the given charge
* @param string $chargeId
* @param int $limit
* @param string $startingAfter
* @param string $endingBefore
* @return ListRefundsResponse
* @link https://stripe.com/docs/api#list_refunds
*/
public function listRefunds($chargeId, $limit = 10, $startingAfter = null, $endingBefore = null)
{
return $this->client->get($this->buildUrl($chargeId), self::LIST_REFUNDS_RESPONSE_CLASS, null, array(
"ending_before" => $endingBefore,
"starting_after" => $startingAfter,
"limit" => $limit
));
}

/**
* Build a URL for a refund request
* @param string $chargeId
* @param string $refundId
* @return string
*/
protected function buildUrl($chargeId, $refundId = null)
{
$url = "charges/" . $chargeId . "/refunds";
if (!is_null($refundId)) {
$url .= "/" . $refundId;
}
return $url;
}
}
4 changes: 3 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public function request($method, $url, $deserializeTo = null, $body = null, arra
$value = 'false';
}
}
$request->getQuery()->set($key, $value);
if (!is_null($value)) {
$request->getQuery()->set($key, $value);
}
}
try {
$response = $request->send();
Expand Down
57 changes: 57 additions & 0 deletions src/Request/Refunds/CreateRefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* User: Joe Linn
* Date: 7/28/2014
* Time: 3:13 PM
*/

namespace Stripe\Request\Refunds;


class CreateRefundRequest
{
/**
* @var int
*/
protected $amount;

/**
* @var bool
*/
protected $refundApplicationFee;

/**
* @var array
*/
protected $metadata;

/**
* @param int $amount
* @return $this
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}

/**
* @param array $metadata
* @return $this
*/
public function setMetadata($metadata)
{
$this->metadata = $metadata;
return $this;
}

/**
* @param boolean $refundApplicationFee
* @return $this
*/
public function setRefundApplicationFee($refundApplicationFee)
{
$this->refundApplicationFee = $refundApplicationFee;
return $this;
}
}
1 change: 0 additions & 1 deletion src/Response/ApplicationFees/ApplicationFeeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


use JMS\Serializer\Annotation\Type;
use Stripe\Response\Refunds\RefundResponse;

class ApplicationFeeResponse
{
Expand Down
3 changes: 2 additions & 1 deletion src/Response/Refunds/ListRefundsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use JMS\Serializer\Annotation\Type;
use Stripe\Response\AbstractListResponse;

class ListRefundsResponse extends AbstractListResponse{
class ListRefundsResponse extends AbstractListResponse
{
/**
* @Type("Stripe\Response\Refunds\RefundResponse")
* @var RefundResponse[]
Expand Down
72 changes: 72 additions & 0 deletions src/Response/Refunds/RefundResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

class RefundResponse
{
/**
* @Type("string")
* @var string
*/
protected $id;

/**
* @Type("string")
* @var string
Expand Down Expand Up @@ -41,6 +47,36 @@ class RefundResponse
*/
protected $balanceTransaction;

/**
* @Type("string")
* @var string
*/
protected $charge;

/**
* @Type("array")
* @var array
*/
protected $metadata;

/**
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* @param string $id
* @return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}

/**
* @return int
*/
Expand Down Expand Up @@ -130,4 +166,40 @@ public function setBalanceTransaction($balanceTransaction)
$this->balanceTransaction = $balanceTransaction;
return $this;
}

/**
* @return string
*/
public function getCharge()
{
return $this->charge;
}

/**
* @param string $charge
* @return $this
*/
public function setCharge($charge)
{
$this->charge = $charge;
return $this;
}

/**
* @return array
*/
public function getMetadata()
{
return $this->metadata;
}

/**
* @param array $metadata
* @return $this
*/
public function setMetadata($metadata)
{
$this->metadata = $metadata;
return $this;
}
}
14 changes: 12 additions & 2 deletions src/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Stripe\Api\Invoices;
use Stripe\Api\Plans;
use Stripe\Api\Recipients;
use Stripe\Api\Refunds;
use Stripe\Api\Subscriptions;
use Stripe\Api\Tokens;

Expand All @@ -42,6 +43,7 @@
* @property Api\Invoices $invoices
* @property Api\Plans $plans
* @property Api\Recipients $recipients
* @property Api\Refunds $refunds
* @property Api\Subscriptions $subscriptions
* @property Api\Tokens $tokens
*/
Expand Down Expand Up @@ -76,8 +78,8 @@ public function __get($name)
{
$allowed = array(
'accounts', 'applicationFees', 'balance', 'cards', 'charges', 'coupons', 'customers', 'discounts',
'disputes', 'events', 'invoiceItems', 'invoices', 'plans', 'recipients', 'subscriptions', 'tokens',
'transfers'
'disputes', 'events', 'invoiceItems', 'invoices', 'plans', 'recipients', 'refunds', 'subscriptions',
'tokens', 'transfers'
);

if (in_array($name, $allowed)) {
Expand Down Expand Up @@ -204,6 +206,14 @@ public function recipients()
return $this->getApi('Recipients');
}

/**
* @return Refunds
*/
public function refunds()
{
return $this->getApi('Refunds');
}

/**
* @return Subscriptions
*/
Expand Down
Loading

0 comments on commit 6daff63

Please sign in to comment.