diff --git a/composer.json b/composer.json index 6a1e168..610a37d 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { - "name": "Crosspay", + "name": "gurimmer/Crosspay", + "description": "Crosspay is an online payment API wrapper.", "type": "library", "require": { "php": "^7.0", @@ -16,7 +17,7 @@ "authors": [ { "name": "gorou178", - "email": "s.anai@innovator.jp.net" + "email": "s.reianai@gmail.com" } ], "autoload": { diff --git a/phpunit.xml b/phpunit.xml index d6ac1b4..ab83507 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - src + src diff --git a/src/crosspay/ChargeInterface.php b/src/crosspay/ChargeInterface.php index d6a8cb7..51202cf 100644 --- a/src/crosspay/ChargeInterface.php +++ b/src/crosspay/ChargeInterface.php @@ -3,6 +3,7 @@ namespace Crosspay; use Crosspay\response\Charge; +use Crosspay\response\Collection; use Crosspay\response\Refund; interface ChargeInterface @@ -15,7 +16,7 @@ public function save(Charge $target, $params = null, $options = null) : Charge; public function capture(Charge $target, $params = null, $options = null) : Charge; - public function all($params = null, $options = null) : array; + public function all($params = null, $options = null) : Collection; public function refund(Charge $target, $params = null, $options = null) : Refund; } diff --git a/src/crosspay/CustomerInterface.php b/src/crosspay/CustomerInterface.php index d5a2256..44768ce 100644 --- a/src/crosspay/CustomerInterface.php +++ b/src/crosspay/CustomerInterface.php @@ -2,6 +2,7 @@ namespace Crosspay; +use Crosspay\response\Collection; use Crosspay\response\Customer; interface CustomerInterface @@ -14,5 +15,5 @@ public function delete(Customer $target, $params = null, $options = null) : Cust public function retrieve($id, $options = null) : Customer; - public function all($params = null, $options = null) : array; + public function all($params = null, $options = null) : Collection; } diff --git a/src/crosspay/EventInterface.php b/src/crosspay/EventInterface.php index 42ceedd..115422d 100644 --- a/src/crosspay/EventInterface.php +++ b/src/crosspay/EventInterface.php @@ -2,10 +2,11 @@ namespace Crosspay; +use Crosspay\response\Collection; use Crosspay\response\Event; interface EventInterface { public function retrieve($id, $options = null) : Event; - public function all($params = null, $options = null) : array; + public function all($params = null, $options = null) : Collection; } diff --git a/src/crosspay/SubscriptionInterface.php b/src/crosspay/SubscriptionInterface.php index 46ca56f..3ce5f1c 100644 --- a/src/crosspay/SubscriptionInterface.php +++ b/src/crosspay/SubscriptionInterface.php @@ -2,6 +2,7 @@ namespace Crosspay; +use Crosspay\response\Collection; use Crosspay\response\Subscription; interface SubscriptionInterface @@ -14,5 +15,5 @@ public function retrieve($id, $params = null, $options = null) : Subscription; public function cancel(Subscription $target, $params = null, $options = null) : Subscription; - public function all($params = null, $options = null) : array; + public function all($params = null, $options = null) : Collection; } diff --git a/src/crosspay/adapter/NullCharge.php b/src/crosspay/adapter/NullCharge.php index 4391fab..2966fdf 100644 --- a/src/crosspay/adapter/NullCharge.php +++ b/src/crosspay/adapter/NullCharge.php @@ -3,6 +3,7 @@ namespace Crosspay\Adapter; use Crosspay\response\Charge; +use Crosspay\response\Collection; use Crosspay\response\Refund; class NullCharge extends AbstractCharge @@ -28,7 +29,7 @@ public function capture(Charge $target, $params = null, $options = null): Charge return null; } - public function all($params = null, $options = null): array + public function all($params = null, $options = null): Collection { return null; } diff --git a/src/crosspay/adapter/NullCustomer.php b/src/crosspay/adapter/NullCustomer.php index f58db14..9c310b6 100644 --- a/src/crosspay/adapter/NullCustomer.php +++ b/src/crosspay/adapter/NullCustomer.php @@ -2,6 +2,7 @@ namespace Crosspay\Adapter; +use Crosspay\response\Collection; use Crosspay\response\Customer; class NullCustomer extends AbstractCustomer @@ -27,7 +28,7 @@ public function retrieve($id, $options = null): Customer return null; } - public function all($params = null, $options = null): array + public function all($params = null, $options = null): Collection { return null; } diff --git a/src/crosspay/adapter/NullEvent.php b/src/crosspay/adapter/NullEvent.php index 27f87d5..2ea687b 100644 --- a/src/crosspay/adapter/NullEvent.php +++ b/src/crosspay/adapter/NullEvent.php @@ -2,6 +2,7 @@ namespace Crosspay\Adapter; +use Crosspay\response\Collection; use Crosspay\response\Event; class NullEvent extends AbstractEvent @@ -11,7 +12,7 @@ public function retrieve($id, $params = null, $options = null) : Event return null; } - public function all($params = null, $options = null) : array + public function all($params = null, $options = null) : Collection { return null; } diff --git a/src/crosspay/adapter/NullSubscription.php b/src/crosspay/adapter/NullSubscription.php index 924e012..ac65c86 100644 --- a/src/crosspay/adapter/NullSubscription.php +++ b/src/crosspay/adapter/NullSubscription.php @@ -2,6 +2,7 @@ namespace Crosspay\Adapter; +use Crosspay\response\Collection; use Crosspay\response\Subscription; class NullSubscription extends AbstractSubscription @@ -27,7 +28,7 @@ public function cancel(Subscription $target, $params = null, $options = null) : return null; } - public function all($params = null, $options = null) : array + public function all($params = null, $options = null) : Collection { return null; } diff --git a/src/crosspay/payjp/PayjpCharge.php b/src/crosspay/payjp/PayjpCharge.php index 868dd3b..7df4a53 100644 --- a/src/crosspay/payjp/PayjpCharge.php +++ b/src/crosspay/payjp/PayjpCharge.php @@ -5,6 +5,7 @@ use Crosspay\Adapter\AbstractCharge; use Crosspay\exception\CrosspayException; use Crosspay\response\Charge; +use Crosspay\response\Collection; use Crosspay\response\Refund; use Exception; @@ -68,19 +69,14 @@ public function capture(Charge $target, $params = null, $options = null): Charge } } - public function all($params = null, $options = null): array + public function all($params = null, $options = null): Collection { try { $charges = \Payjp\Charge::all($params, $options); + return new PayjpCollectionResponse($charges); } catch (Exception $e) { throw new CrosspayException('charge all exception from payjp', 0, $e); } - - $results = []; - foreach ($charges as $charge) { - $results[] = new PayjpChargeResponse($charge); - } - return $results; } public function refund(Charge $target, $params = null, $options = null): Refund diff --git a/src/crosspay/payjp/PayjpCollectionResponse.php b/src/crosspay/payjp/PayjpCollectionResponse.php new file mode 100644 index 0000000..8dc64b8 --- /dev/null +++ b/src/crosspay/payjp/PayjpCollectionResponse.php @@ -0,0 +1,59 @@ +url(); + $responseClass = ''; + if (strpos($url, 'charges') !== false) { + $responseClass = 'PayjpChargeResponse'; + } + if (strpos($url, 'subscriptions') !== false) { + $responseClass = 'PayjpSubscriptionResponse'; + } + if (strpos($url, 'customers') !== false) { + $responseClass = 'PayjpCustomerResponse'; + } + if (strpos($url, 'events') !== false) { + $responseClass = 'PayjpEventResponse'; + } + + $results = []; + foreach ($this->response->data as $data) { + $results[] = new $responseClass($data); + } + return $results; + } + + public function __construct(string $response = '') + { + parent::__construct($response); + + } + + public function count(): int + { + return $this->response->count; + } + + public function url(): string + { + return $this->response->url; + } + + public function hasMore(): bool + { + return $this->response->has_more; + } + + public function data(): array + { + return $this->convertData(); + } +} \ No newline at end of file diff --git a/src/crosspay/payjp/PayjpCustomer.php b/src/crosspay/payjp/PayjpCustomer.php index 2e2250e..cecf77a 100644 --- a/src/crosspay/payjp/PayjpCustomer.php +++ b/src/crosspay/payjp/PayjpCustomer.php @@ -4,6 +4,7 @@ use Crosspay\Adapter\AbstractCustomer; use Crosspay\exception\CrosspayException; +use Crosspay\response\Collection; use Crosspay\response\Customer; use Exception; @@ -67,18 +68,13 @@ public function retrieve($id, $options = null): Customer } } - public function all($params = null, $options = null): array + public function all($params = null, $options = null): Collection { try { $customers = \Payjp\Customer::all($params, $options); + return new PayjpCollectionResponse($customers); } catch (Exception $e) { throw new CrosspayException('customer all exception from payjp', 0, $e); } - - $results = []; - foreach ($customers as $customer) { - $results[] = new PayjpCustomerResponse($customer); - } - return $results; } } diff --git a/src/crosspay/payjp/PayjpEvent.php b/src/crosspay/payjp/PayjpEvent.php index 8e447ec..3ae5d0a 100644 --- a/src/crosspay/payjp/PayjpEvent.php +++ b/src/crosspay/payjp/PayjpEvent.php @@ -4,6 +4,7 @@ use Crosspay\Adapter\AbstractEvent; use Crosspay\exception\CrosspayException; +use Crosspay\response\Collection; use Crosspay\response\Event; use Exception; @@ -20,18 +21,13 @@ public function retrieve($id, $options = null): Event } } - public function all($params = null, $options = null): array + public function all($params = null, $options = null): Collection { try { $events = \Payjp\Event::all($params, $options); + return new PayjpCollectionResponse($events); } catch (Exception $e) { throw new CrosspayException('event all exception from payjp', 0, $e); } - - $results = []; - foreach ($events as $event) { - $results[] = new PayjpEventResponse($event); - } - return $results; } } diff --git a/src/crosspay/payjp/PayjpSubscription.php b/src/crosspay/payjp/PayjpSubscription.php index b27c664..4e15c5d 100644 --- a/src/crosspay/payjp/PayjpSubscription.php +++ b/src/crosspay/payjp/PayjpSubscription.php @@ -4,6 +4,7 @@ use Crosspay\Adapter\AbstractSubscription; use Crosspay\exception\CrosspayException; +use Crosspay\response\Collection; use Crosspay\response\Subscription; use Exception; @@ -67,18 +68,13 @@ public function cancel(Subscription $target, $params = null, $options = null): S } } - public function all($params = null, $options = null): array + public function all($params = null, $options = null): Collection { try { $subscriptions = \Payjp\Subscription::all($params, $options); + return new PayjpCollectionResponse($subscriptions); } catch (Exception $e) { throw new CrosspayException('subscription all exception from payjp', 0, $e); } - - $results = []; - foreach ($subscriptions as $subscription) { - $results[] = new PayjpSubscriptionResponse($subscription); - } - return $results; } } diff --git a/src/crosspay/response/Collection.php b/src/crosspay/response/Collection.php new file mode 100644 index 0000000..5cf9525 --- /dev/null +++ b/src/crosspay/response/Collection.php @@ -0,0 +1,12 @@ +url(); + $responseClass = ''; + if (strpos($url, 'charges') !== false) { + $responseClass = 'StripeChargeResponse'; + } + if (strpos($url, 'subscriptions') !== false) { + $responseClass = 'StripeSubscriptionResponse'; + } + if (strpos($url, 'customers') !== false) { + $responseClass = 'StripeCustomerResponse'; + } + if (strpos($url, 'events') !== false) { + $responseClass = 'StripeEventResponse'; + } + + $results = []; + foreach ($this->response->data as $data) { + $results[] = new $responseClass($data); + } + return $results; + } + + public function count(): int + { + return count($this->data()); + } + + public function url(): string + { + return $this->response->url; + } + + public function hasMore(): bool + { + return $this->response->has_more; + } + + public function data(): array + { + return $this->convertData(); + } +} diff --git a/src/crosspay/stripe/StripeCustomer.php b/src/crosspay/stripe/StripeCustomer.php index 974a9b8..92580b6 100644 --- a/src/crosspay/stripe/StripeCustomer.php +++ b/src/crosspay/stripe/StripeCustomer.php @@ -4,6 +4,7 @@ use Crosspay\Adapter\AbstractCustomer; use Crosspay\exception\CrosspayException; +use Crosspay\response\Collection; use Crosspay\response\Customer; use Exception; @@ -67,18 +68,13 @@ public function retrieve($id, $options = null) : Customer } } - public function all($params = null, $options = null) : array + public function all($params = null, $options = null) : Collection { try { $customers = \Stripe\Customer::all($params, $options); + return new StripeCollectionResponse($customers); } catch (Exception $e) { throw new CrosspayException('customer all exception from stripe', 0, $e); } - - $results = []; - foreach ($customers as $customer) { - $results[] = new StripeCustomerResponse($customer); - } - return $results; } } diff --git a/src/crosspay/stripe/StripeEvent.php b/src/crosspay/stripe/StripeEvent.php index f646b64..8c8d133 100644 --- a/src/crosspay/stripe/StripeEvent.php +++ b/src/crosspay/stripe/StripeEvent.php @@ -4,6 +4,7 @@ use Crosspay\Adapter\AbstractEvent; use Crosspay\exception\CrosspayException; +use Crosspay\response\Collection; use Crosspay\response\Event; use Exception; @@ -20,11 +21,11 @@ public function retrieve($id, $options = null) : Event } } - public function all($params = null, $options = null) : array + public function all($params = null, $options = null) : Collection { try { - $event = \Stripe\Event::all($params, $options); - return new StripeEventResponse($event); + $events = \Stripe\Event::all($params, $options); + return new StripeCollectionResponse($events); } catch (Exception $e) { throw new CrosspayException('event all exception from stripe', 0, $e); } diff --git a/src/crosspay/stripe/StripeSubscription.php b/src/crosspay/stripe/StripeSubscription.php index 741ec70..37a64c8 100644 --- a/src/crosspay/stripe/StripeSubscription.php +++ b/src/crosspay/stripe/StripeSubscription.php @@ -4,6 +4,7 @@ use Crosspay\Adapter\AbstractSubscription; use Crosspay\exception\CrosspayException; +use Crosspay\response\Collection; use Crosspay\response\Subscription; use Exception; @@ -67,11 +68,11 @@ public function cancel(Subscription $target, $params = null, $options = null) : } } - public function all($params = null, $options = null) : array + public function all($params = null, $options = null) : Collection { try { - $subscription = \Stripe\Subscription::all($params, $options); - return new StripeSubscriptionResponse($subscription); + $subscriptions = \Stripe\Subscription::all($params, $options); + return new StripeCollectionResponse($subscriptions); } catch (Exception $e) { throw new CrosspayException('subscription all exception from stripe', 0, $e); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..15f224b --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,4 @@ +crossPay = new CrossPay([ + 'provider' => 'payjp', + 'api_key' => getenv('PAYJP_KEY'), + 'api_secret' => getenv('PAYJP_SECRET'), + ]); + $this->testCardToken = Token::create([ + 'card' => [ + 'number' => $this->normalCardNumber(CardBrand::Visa()), + 'cvc' => '123', + 'exp_month' => '1', + 'exp_year' => '2020' + ] + ]); + } + + protected function tearDown() + { + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } + } + + public function testCreate() + { + $email = 'test@example.com'; + $description = 'crosspay test description'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + 'description' => $description + ]); + + $this->assertNotNull($this->customer); + $this->assertNotNull($this->customer->id()); + $this->assertNotNull($this->customer->created()); + $this->assertNotNull($this->customer->cards()); + + + $this->assertNotNull($this->customer->toArray()); + $this->assertJson($this->customer->toJson()); + + $this->assertEquals($this->customer->email(), $email); + $this->assertEquals($this->customer->description(), $description); + $this->assertEquals($this->customer->defaultCard()->brand(), CardBrand::Visa); + $this->assertEquals($this->customer->defaultCard()->last4(), '4242'); + } + + public function testSave() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + ]); + + $newEmail = 'update@example.com'; + $updateCustomer = $this->crossPay->customer()->save($this->customer, [ + 'email' => $newEmail + ]); + + $this->assertNotNull($updateCustomer); + $this->assertEquals($updateCustomer->email(), $newEmail); + } + + public function testDelete() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + ]); + + $deleteCustomer = $this->crossPay->customer()->delete($this->customer); + $this->assertNotNull($deleteCustomer); + $this->assertEquals($deleteCustomer->id(), $this->customer->id()); + } + + public function testRetrieve() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + ]); + + $customer = $this->crossPay->customer()->retrieve($this->customer->id()); + $this->assertNotNull($customer); + $this->assertEquals($customer->id(), $this->customer->id()); + } + + public function testAll() + { + $description = 'crosspay test description'; + $customer1 = $this->crossPay->customer()->create([ + 'email' => 'test1@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + $customer2 = $this->crossPay->customer()->create([ + 'email' => 'test2@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + $customer3 = $this->crossPay->customer()->create([ + 'email' => 'test3@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + + $listLimit = 2; + $collection = $this->crossPay->customer()->all([ + 'limit' => $listLimit + ]); + $this->assertNotNull($collection); + $this->assertEquals($collection->count(), $listLimit); + $this->assertNotNull($collection->url()); + $this->assertTrue($collection->hasMore()); + + $this->assertEquals($collection->data()[0]->id(), $customer1->id()); + $this->assertEquals($collection->data()[1]->id(), $customer2->id()); + } + +} diff --git a/tests/payjp/PayjpCustomerTest.php b/tests/payjp/PayjpCustomerTest.php index fafd0cc..32b13ae 100644 --- a/tests/payjp/PayjpCustomerTest.php +++ b/tests/payjp/PayjpCustomerTest.php @@ -3,6 +3,7 @@ namespace Crosspay\Test\Payjp; use Crosspay\CrossPay; +use Crosspay\response\Customer; use Crosspay\Test\CardBrand; use Crosspay\Test\TestCard; use Crosspay\Test\TestCase; @@ -15,12 +16,19 @@ class PayjpCustomerTest extends TestCase /** @var CrossPay */ protected $crossPay; protected $testCardToken; + /** @var Customer */ + protected $customer; public static function setUpBeforeClass() { self::authorizeFromEnv(); } + public static function tearDownAfterClass() + { + + } + protected function setUp() { $this->crossPay = new CrossPay([ @@ -40,48 +48,109 @@ protected function setUp() protected function tearDown() { - + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } } public function testCreate() { $email = 'test@example.com'; - $customer = $this->crossPay->customer()->create([ + $description = 'crosspay test description'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + 'description' => $description + ]); + + $this->assertNotNull($this->customer); + $this->assertNotNull($this->customer->id()); + $this->assertNotNull($this->customer->created()); + $this->assertNotNull($this->customer->cards()); + + + $this->assertNotNull($this->customer->toArray()); + $this->assertJson($this->customer->toJson()); + + $this->assertEquals($this->customer->email(), $email); + $this->assertEquals($this->customer->description(), $description); + $this->assertEquals($this->customer->defaultCard()->brand(), CardBrand::Visa); + $this->assertEquals($this->customer->defaultCard()->last4(), '4242'); + } + + public function testAll() + { + $description = 'crosspay test description'; + $customer1 = $this->crossPay->customer()->create([ + 'email' => 'test1@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + $customer2 = $this->crossPay->customer()->create([ + 'email' => 'test2@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + $customer3 = $this->crossPay->customer()->create([ + 'email' => 'test3@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + + $listLimit = 2; + $collection = $this->crossPay->customer()->all([ + 'limit' => $listLimit + ]); + $this->assertNotNull($collection); + $this->assertEquals($collection->count(), $listLimit); + $this->assertNotNull($collection->url()); + $this->assertTrue($collection->hasMore()); + + $this->assertEquals($collection->data()[0]->id(), $customer1->id()); + $this->assertEquals($collection->data()[1]->id(), $customer2->id()); + } + + public function testRetrieve() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ 'email' => $email, 'card' => $this->testCardToken, ]); + $customer = $this->crossPay->customer()->retrieve($this->customer->id()); $this->assertNotNull($customer); - $this->assertNotNull($customer->id()); - $this->assertNotNull($customer->created()); - - $this->assertEquals($customer->email(), $email); - $this->assertEquals($customer->defaultCard()->brand(), CardBrand::Visa); - $this->assertEquals($customer->defaultCard()->last4(), '4242'); - - $amount = 1000; - $currency = 'jpy'; - $chargeDescription = 'test charge'; - $charge = $this->crossPay->charge()->create([ - 'customer' => $customer->id(), - 'amount' => $amount, - 'currency' => $currency, - 'description' => $chargeDescription + $this->assertEquals($customer->id(), $this->customer->id()); + } + + public function testSave() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, ]); - $this->assertNotNull($charge); - $this->assertNotNull($charge->id()); - $this->assertNotNull($charge->created()); + $newEmail = 'update@example.com'; + $updateCustomer = $this->crossPay->customer()->save($this->customer, [ + 'email' => $newEmail + ]); + + $this->assertNotNull($updateCustomer); + $this->assertEquals($updateCustomer->email(), $newEmail); + } - $this->assertEquals($charge->currency(), $currency); - $this->assertEquals($charge->amount(), $amount); - $this->assertEquals($charge->customerId(), $customer->id()); - $this->assertEquals($charge->description(), $chargeDescription); - $this->assertEquals($charge->card()->brand(), CardBrand::Visa); - $this->assertEquals($charge->card()->last4(), '4242'); + public function testDelete() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + ]); - $this->assertTrue($charge->captured()); - $this->assertFalse($charge->refunded()); + $deleteCustomer = $this->crossPay->customer()->delete($this->customer); + $this->assertNotNull($deleteCustomer); + $this->assertEquals($deleteCustomer->id(), $this->customer->id()); } } diff --git a/tests/payjp/PayjpEventTest.php b/tests/payjp/PayjpEventTest.php new file mode 100644 index 0000000..6e5d10c --- /dev/null +++ b/tests/payjp/PayjpEventTest.php @@ -0,0 +1,59 @@ +crossPay = new CrossPay([ + 'provider' => 'payjp', + 'api_key' => getenv('PAYJP_KEY'), + 'api_secret' => getenv('PAYJP_SECRET'), + ]); + $this->testCardToken = Token::create([ + 'card' => [ + 'number' => $this->normalCardNumber(CardBrand::Visa()), + 'cvc' => '123', + 'exp_month' => '1', + 'exp_year' => '2020' + ] + ]); + } + + public function testRetrieve() + { + + } + + public function testAll() + { + + } + +} diff --git a/tests/payjp/PayjpSubscriptionTest.php b/tests/payjp/PayjpSubscriptionTest.php new file mode 100644 index 0000000..c18dbbd --- /dev/null +++ b/tests/payjp/PayjpSubscriptionTest.php @@ -0,0 +1,81 @@ +crossPay = new CrossPay([ + 'provider' => 'payjp', + 'api_key' => getenv('PAYJP_KEY'), + 'api_secret' => getenv('PAYJP_SECRET'), + ]); + $this->testCardToken = Token::create([ + 'card' => [ + 'number' => $this->normalCardNumber(CardBrand::Visa()), + 'cvc' => '123', + 'exp_month' => '1', + 'exp_year' => '2020' + ] + ]); + } + + protected function tearDown() + { + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } + } + + public function testCreate() + { + + } + + public function testSave() + { + + } + + public function testRetrieve() + { + + } + + public function testCancel() + { + + } + + public function testAll() + { + + } + +} diff --git a/tests/stripe/StripeChargeTest.php b/tests/stripe/StripeChargeTest.php new file mode 100644 index 0000000..10ec618 --- /dev/null +++ b/tests/stripe/StripeChargeTest.php @@ -0,0 +1,74 @@ +crossPay = new CrossPay([ + 'provider' => 'stripe', + 'api_key' => getenv('STRIPE_KEY'), + 'api_secret' => getenv('STRIPE_SECRET'), + 'api_version' => getenv('STRIPE_API_VERSION') + ]); + $this->testCardToken = $this->normalCardStripeToken(CardBrand::Visa()); + } + + protected function tearDown() + { + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } + } + + public function testCreate() + { + + } + + public function testRetrieve() + { + + } + + public function testSave() + { + + } + + public function testCapture() + { + + } + + public function testAll() + { + + } + + public function testRefund() + { + + } + +} diff --git a/tests/stripe/StripeCustomerTest.php b/tests/stripe/StripeCustomerTest.php index 959282f..7ed034d 100644 --- a/tests/stripe/StripeCustomerTest.php +++ b/tests/stripe/StripeCustomerTest.php @@ -3,6 +3,7 @@ namespace Crosspay\Test\Stripe; use Crosspay\CrossPay; +use Crosspay\response\Customer; use Crosspay\Test\CardBrand; use Crosspay\Test\TestCard; use Crosspay\Test\TestCase; @@ -13,6 +14,9 @@ class StripeCustomerTest extends TestCase /** @var CrossPay */ protected $crossPay; + protected $testCardToken; + /** @var Customer */ + protected $customer; public static function setUpBeforeClass() { @@ -27,52 +31,113 @@ protected function setUp() 'api_secret' => getenv('STRIPE_SECRET'), 'api_version' => getenv('STRIPE_API_VERSION') ]); + $this->testCardToken = $this->normalCardStripeToken(CardBrand::Visa()); } protected function tearDown() { - + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } } public function testCreate() { $email = 'test@example.com'; - $customer = $this->crossPay->customer()->create([ - 'email' => 'test@example.com', - 'source' => $this->normalCardStripeToken(CardBrand::Visa()), + $description = 'crosspay test description'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + 'description' => $description ]); - $this->assertNotNull($customer); - $this->assertNotNull($customer->id()); - $this->assertNotNull($customer->created()); - - $this->assertEquals($customer->email(), $email); - $this->assertEquals($customer->defaultCard()->brand(), CardBrand::Visa); - $this->assertEquals($customer->defaultCard()->last4(), '4242'); - - $amount = 1000; - $currency = 'jpy'; - $chargeDescription = 'test charge'; - $charge = $this->crossPay->charge()->create([ - 'customer' => $customer->id(), - 'amount' => $amount, - 'currency' => $currency, - 'description' => $chargeDescription + $this->assertNotNull($this->customer); + $this->assertNotNull($this->customer->id()); + $this->assertNotNull($this->customer->created()); + $this->assertNotNull($this->customer->cards()); + + $this->assertNotNull($this->customer->toArray()); + $this->assertJson($this->customer->toJson()); + + $this->assertEquals($this->customer->email(), $email); + $this->assertEquals($this->customer->description(), $description); + $this->assertEquals($this->customer->defaultCard()->brand(), CardBrand::Visa); + $this->assertEquals($this->customer->defaultCard()->last4(), '4242'); + } + + public function testSave() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + ]); + + $newEmail = 'update@example.com'; + $updateCustomer = $this->crossPay->customer()->save($this->customer, [ + 'email' => $newEmail + ]); + + $this->assertNotNull($updateCustomer); + $this->assertEquals($updateCustomer->email(), $newEmail); + } + + public function testDelete() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, ]); - $this->assertNotNull($charge); - $this->assertNotNull($charge->id()); - $this->assertNotNull($charge->created()); + $deleteCustomer = $this->crossPay->customer()->delete($this->customer); + $this->assertNotNull($deleteCustomer); + $this->assertEquals($deleteCustomer->id(), $this->customer->id()); + } + + public function testRetrieve() + { + $email = 'test@example.com'; + $this->customer = $this->crossPay->customer()->create([ + 'email' => $email, + 'card' => $this->testCardToken, + ]); - $this->assertEquals($charge->currency(), $currency); - $this->assertEquals($charge->amount(), $amount); - $this->assertEquals($charge->customerId(), $customer->id()); - $this->assertEquals($charge->description(), $chargeDescription); - $this->assertEquals($charge->card()->brand(), CardBrand::Visa); - $this->assertEquals($charge->card()->last4(), '4242'); + $customer = $this->crossPay->customer()->retrieve($this->customer->id()); + $this->assertNotNull($customer); + $this->assertEquals($customer->id(), $this->customer->id()); + } + + public function testAll() + { + $description = 'crosspay test description'; + $customer1 = $this->crossPay->customer()->create([ + 'email' => 'test1@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + $customer2 = $this->crossPay->customer()->create([ + 'email' => 'test2@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + $customer3 = $this->crossPay->customer()->create([ + 'email' => 'test3@example.com', + 'card' => $this->testCardToken, + 'description' => $description + ]); + + $listLimit = 2; + $collection = $this->crossPay->customer()->all([ + 'limit' => $listLimit + ]); + $this->assertNotNull($collection); + $this->assertEquals($collection->count(), $listLimit); + $this->assertNotNull($collection->url()); + $this->assertTrue($collection->hasMore()); - $this->assertTrue($charge->captured()); - $this->assertFalse($charge->refunded()); + $this->assertEquals($collection->data()[0]->id(), $customer1->id()); + $this->assertEquals($collection->data()[1]->id(), $customer2->id()); } } diff --git a/tests/stripe/StripeEventTest.php b/tests/stripe/StripeEventTest.php new file mode 100644 index 0000000..3395d79 --- /dev/null +++ b/tests/stripe/StripeEventTest.php @@ -0,0 +1,53 @@ +crossPay = new CrossPay([ + 'provider' => 'stripe', + 'api_key' => getenv('STRIPE_KEY'), + 'api_secret' => getenv('STRIPE_SECRET'), + 'api_version' => getenv('STRIPE_API_VERSION') + ]); + $this->testCardToken = $this->normalCardStripeToken(CardBrand::Visa()); + } + + protected function tearDown() + { + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } + } + + public function testRetrieve() + { + + } + + public function testAll() + { + + } +} diff --git a/tests/stripe/StripeSubscriptionTest.php b/tests/stripe/StripeSubscriptionTest.php new file mode 100644 index 0000000..4450ba4 --- /dev/null +++ b/tests/stripe/StripeSubscriptionTest.php @@ -0,0 +1,69 @@ +crossPay = new CrossPay([ + 'provider' => 'stripe', + 'api_key' => getenv('STRIPE_KEY'), + 'api_secret' => getenv('STRIPE_SECRET'), + 'api_version' => getenv('STRIPE_API_VERSION') + ]); + $this->testCardToken = $this->normalCardStripeToken(CardBrand::Visa()); + } + + protected function tearDown() + { + if ($this->customer) { + $this->crossPay->customer()->delete($this->customer); + } + } + + public function testCreate() + { + + } + + public function testSave() + { + + } + + public function testRetrieve() + { + + } + + public function testCancel() + { + + } + + public function testAll() + { + + } + +}