Skip to content

Commit

Permalink
Add support for updating a refund
Browse files Browse the repository at this point in the history
  • Loading branch information
kjc-stripe committed Jun 17, 2014
1 parent dc9a5c7 commit 9fec0f7
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
require(dirname(__FILE__) . '/Stripe/Event.php');
require(dirname(__FILE__) . '/Stripe/Transfer.php');
require(dirname(__FILE__) . '/Stripe/Recipient.php');
require(dirname(__FILE__) . '/Stripe/Refund.php');
require(dirname(__FILE__) . '/Stripe/ApplicationFee.php');
36 changes: 36 additions & 0 deletions lib/Stripe/Refund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

class Stripe_Refund extends Stripe_ApiResource
{
/**
* @return string The API URL for this Stripe refund.
*/
public function instanceUrl()
{
$id = $this['id'];
$charge = $this['charge'];
if (!$id) {
throw new Stripe_InvalidRequestError(
"Could not determine which URL to request: " .
"class instance has invalid ID: $id",
null
);
}
$id = Stripe_ApiRequestor::utf8($id);
$charge = Stripe_ApiRequestor::utf8($charge);

$base = self::classUrl('Stripe_Charge');
$chargeExtn = urlencode($charge);
$extn = urlencode($id);
return "$base/$chargeExtn/refunds/$extn";
}

/**
* @return Stripe_Refund The saved refund.
*/
public function save()
{
$class = get_class();
return self::_scopedSave($class);
}
}
1 change: 1 addition & 0 deletions lib/Stripe/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function convertToStripeObject($resp, $apiKey)
'transfer' => 'Stripe_Transfer',
'plan' => 'Stripe_Plan',
'recipient' => 'Stripe_Recipient',
'refund' => 'Stripe_Refund',
'subscription' => 'Stripe_Subscription'
);
if (self::isList($resp)) {
Expand Down
1 change: 1 addition & 0 deletions test/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ function exception_error_handler($errno, $errstr, $errfile, $errline)
require_once(dirname(__FILE__) . '/Stripe/Token.php');
require_once(dirname(__FILE__) . '/Stripe/TransferTest.php');
require_once(dirname(__FILE__) . '/Stripe/RecipientTest.php');
require_once(dirname(__FILE__) . '/Stripe/RefundTest.php');
require_once(dirname(__FILE__) . '/Stripe/ApplicationFeeTest.php');
require_once(dirname(__FILE__) . '/Stripe/UtilTest.php');
36 changes: 36 additions & 0 deletions test/Stripe/RefundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

class Stripe_RefundTest extends StripeTestCase
{

public function testCreate()
{
$charge = self::createTestCharge();
$ref = $charge->refunds->create(array('amount' => 100));
$this->assertEqual(100, $ref->amount);
$this->assertEqual($charge->id, $ref->charge);
}

public function testUpdateAndRetrieve()
{
$charge = self::createTestCharge();
$ref = $charge->refunds->create(array('amount' => 100));
$ref->metadata["key"] = "value";
$ref->save();
$ref = $charge->refunds->retrieve($ref->id);
$this->assertEqual("value", $ref->metadata["key"], "value");
}

public function testList()
{
$charge = self::createTestCharge();
$refA = $charge->refunds->create(array('amount' => 50));
$refB = $charge->refunds->create(array('amount' => 50));

$all = $charge->refunds->all();
$this->assertEqual(false, $all->has_more);
$this->assertEqual(2, count($all->data));
$this->assertEqual($refA->id, $all->data[0]->id);
$this->assertEqual($refB->id, $all->data[1]->id);
}
}
22 changes: 22 additions & 0 deletions test/Stripe/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
abstract class StripeTestCase extends UnitTestCase
{

/**
* Create a valid test charge.
*/
protected static function createTestCharge(array $attributes = array())
{
authorizeFromEnv();

return Stripe_Charge::create(
$attributes + array(
"amount" => 2000,
"currency" => "usd",
"description" => "Charge for [email protected]",
'card' => array(
'number' => '4242424242424242',
'exp_month' => 5,
'exp_year' => date('Y') + 3,
),
)
);
}


/**
* Create a valid test customer.
*/
Expand Down

0 comments on commit 9fec0f7

Please sign in to comment.