From 7f61106f9ec22049ffa7bd8df0dbac2c46508c78 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Fri, 29 Mar 2019 18:48:31 -0700 Subject: [PATCH] Add support for CreditNote --- .travis.yml | 2 +- init.php | 1 + lib/CreditNote.php | 74 ++++++++++++++++++++++++++++++ lib/Event.php | 3 ++ lib/Util/Util.php | 1 + tests/Stripe/CreditNoteTest.php | 79 +++++++++++++++++++++++++++++++++ tests/Stripe/PlanTest.php | 2 +- tests/bootstrap.php | 2 +- 8 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 lib/CreditNote.php create mode 100644 tests/Stripe/CreditNoteTest.php diff --git a/.travis.yml b/.travis.yml index 9e7a080556..ce9532a9d3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ php: env: global: - - STRIPE_MOCK_VERSION=0.49.0 + - STRIPE_MOCK_VERSION=0.51.0 matrix: - AUTOLOAD=1 - AUTOLOAD=0 diff --git a/init.php b/init.php index 0ed7ab45d2..31ab0254c7 100644 --- a/init.php +++ b/init.php @@ -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'); diff --git a/lib/CreditNote.php b/lib/CreditNote.php new file mode 100644 index 0000000000..413dad9cc8 --- /dev/null +++ b/lib/CreditNote.php @@ -0,0 +1,74 @@ +instanceUrl() . '/void'; + list($response, $opts) = $this->_request('post', $url, $params, $opts); + $this->refreshFrom($response, $opts); + return $this; + } +} diff --git a/lib/Event.php b/lib/Event.php index 319cc9db0c..73a427ef3f 100644 --- a/lib/Event.php +++ b/lib/Event.php @@ -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'; diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 03b80ee304..c0604ab399 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -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', diff --git a/tests/Stripe/CreditNoteTest.php b/tests/Stripe/CreditNoteTest.php new file mode 100644 index 0000000000..c256bb91c7 --- /dev/null +++ b/tests/Stripe/CreditNoteTest.php @@ -0,0 +1,79 @@ +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); + } +} diff --git a/tests/Stripe/PlanTest.php b/tests/Stripe/PlanTest.php index 8d71745ed5..9c33b41745 100644 --- a/tests/Stripe/PlanTest.php +++ b/tests/Stripe/PlanTest.php @@ -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); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index afa7e079bb..fe4ffa9f2d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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');