Skip to content

Commit

Permalink
Add support for CreditNote
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Mar 30, 2019
1 parent 19456bc commit 7f61106
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ php:

env:
global:
- STRIPE_MOCK_VERSION=0.49.0
- STRIPE_MOCK_VERSION=0.51.0
matrix:
- AUTOLOAD=1
- AUTOLOAD=0
Expand Down
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
require(dirname(__FILE__) . '/lib/Charge.php');
require(dirname(__FILE__) . '/lib/Checkout/Session.php');
require(dirname(__FILE__) . '/lib/Collection.php');
require(dirname(__FILE__) . '/lib/CreditNote.php');
require(dirname(__FILE__) . '/lib/CountrySpec.php');
require(dirname(__FILE__) . '/lib/Coupon.php');
require(dirname(__FILE__) . '/lib/Customer.php');
Expand Down
74 changes: 74 additions & 0 deletions lib/CreditNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Stripe;

/**
* Class CreditNote
*
* @property string $id
* @property string $object
* @property int $amount
* @property int $created
* @property string $currency
* @property string $customer
* @property string $invoice
* @property bool $livemode
* @property string $memo
* @property StripeObject $metadata
* @property int $next_payment_attempt
* @property string $number
* @property string $pdf
* @property string $reason
* @property string $refund
* @property string $status
* @property string $type
*
* @package Stripe
*/
class CreditNote extends ApiResource
{

const OBJECT_NAME = "credit_note";

use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Retrieve;
use ApiOperations\Update;

/**
* Possible string representations of the credit note reason.
* @link https://stripe.com/docs/api/credit_notes/object#credit_note_object-reason
*/
const REASON_DUPLICATE = 'duplicate';
const REASON_FRAUDULENT = 'fraudulent';
const REASON_ORDER_CHANGE = 'order_change';
const REASON_PRODUCT_UNSATISFACTORY = 'product_unsatisfactory';

/**
* Possible string representations of the credit note status.
* @link https://stripe.com/docs/api/credit_notes/object#credit_note_object-status
*/
const STATUS_ISSUED = 'issued';
const STATUS_VOID = 'void';

/**
* Possible string representations of the credit note type.
* @link https://stripe.com/docs/api/credit_notes/object#credit_note_object-status
*/
const TYPE_POST_PAYMENT = 'post_payment';
const TYPE_PRE_PAYMENT = 'pre_payment';

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return CreditNote The voided credit note.
*/
public function voidCreditNote($params = null, $opts = null)
{
$url = $this->instanceUrl() . '/void';
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
return $this;
}
}
3 changes: 3 additions & 0 deletions lib/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Event extends ApiResource
const COUPON_CREATED = 'coupon.created';
const COUPON_DELETED = 'coupon.deleted';
const COUPON_UPDATED = 'coupon.updated';
const CREDIT_NOTE_CREATED = 'credit_note.created';
const CREDIT_NOTE_UPDATED = 'credit_note.updated';
const CREDIT_NOTE_VOIDED = 'credit_note.voided';
const CUSTOMER_CREATED = 'customer.created';
const CUSTOMER_DELETED = 'customer.deleted';
const CUSTOMER_UPDATED = 'customer.updated';
Expand Down
1 change: 1 addition & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public static function convertToStripeObject($resp, $opts)
\Stripe\Card::OBJECT_NAME => 'Stripe\\Card',
\Stripe\Charge::OBJECT_NAME => 'Stripe\\Charge',
\Stripe\Checkout\Session::OBJECT_NAME => 'Stripe\\Checkout\\Session',
\Stripe\CreditNote::OBJECT_NAME => 'Stripe\\CreditNote',
\Stripe\CountrySpec::OBJECT_NAME => 'Stripe\\CountrySpec',
\Stripe\Coupon::OBJECT_NAME => 'Stripe\\Coupon',
\Stripe\Customer::OBJECT_NAME => 'Stripe\\Customer',
Expand Down
79 changes: 79 additions & 0 deletions tests/Stripe/CreditNoteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Stripe;

class CreditNoteTest extends TestCase
{
const TEST_RESOURCE_ID = 'cn_123';

public function testIsListable()
{
$this->expectsRequest(
'get',
'/v1/credit_notes'
);
$resources = CreditNote::all();
$this->assertTrue(is_array($resources->data));
$this->assertInstanceOf("Stripe\\CreditNote", $resources->data[0]);
}

public function testIsRetrievable()
{
$this->expectsRequest(
'get',
'/v1/credit_notes/' . self::TEST_RESOURCE_ID
);
$resource = CreditNote::retrieve(self::TEST_RESOURCE_ID);
$this->assertInstanceOf("Stripe\\CreditNote", $resource);
}

public function testIsCreatable()
{
$this->expectsRequest(
'post',
'/v1/credit_notes'
);
$resource = CreditNote::create([
"amount" => 100,
"invoice" => "in_132",
"reason" => "duplicate",
]);
$this->assertInstanceOf("Stripe\\CreditNote", $resource);
}

public function testIsSaveable()
{
$resource = CreditNote::retrieve(self::TEST_RESOURCE_ID);
$resource->metadata["key"] = "value";
$this->expectsRequest(
'post',
'/v1/credit_notes/' . $resource->id
);
$resource->save();
$this->assertInstanceOf("Stripe\\CreditNote", $resource);
}

public function testIsUpdatable()
{
$this->expectsRequest(
'post',
'/v1/credit_notes/' . self::TEST_RESOURCE_ID
);
$resource = CreditNote::update(self::TEST_RESOURCE_ID, [
"metadata" => ["key" => "value"],
]);
$this->assertInstanceOf("Stripe\\CreditNote", $resource);
}

public function testCanVoidCreditNote()
{
$creditNote = CreditNote::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'post',
'/v1/credit_notes/' . $creditNote->id . '/void'
);
$resource = $creditNote->voidCreditNote();
$this->assertInstanceOf("Stripe\\CreditNote", $resource);
$this->assertSame($resource, $creditNote);
}
}
2 changes: 1 addition & 1 deletion tests/Stripe/PlanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testIsCreatable()
'amount' => 100,
'interval' => 'month',
'currency' => 'usd',
'name' => self::TEST_RESOURCE_ID,
'nickname' => self::TEST_RESOURCE_ID,
'id' => self::TEST_RESOURCE_ID
]);
$this->assertInstanceOf("Stripe\\Plan", $resource);
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once(__DIR__ . '/StripeMock.php');

define("MOCK_MINIMUM_VERSION", "0.49.0");
define("MOCK_MINIMUM_VERSION", "0.51.0");

if (\Stripe\StripeMock::start()) {
register_shutdown_function('\Stripe\StripeMock::stop');
Expand Down

0 comments on commit 7f61106

Please sign in to comment.