diff --git a/VERSION b/VERSION index bc80560..dc1e644 100755 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.5.0 +1.6.0 diff --git a/lib/Balance.php b/lib/Balance.php new file mode 100644 index 0000000..079a0af --- /dev/null +++ b/lib/Balance.php @@ -0,0 +1,28 @@ += v7.1.0) private static $types = array( - 'application_url' => 'Payjp\\ApplicationUrl', - 'card' => 'Payjp\\Card', - 'charge' => 'Payjp\\Charge', - 'customer' => 'Payjp\\Customer', - 'event' => 'Payjp\\Event', - 'list' => 'Payjp\\Collection', - 'plan' => 'Payjp\\Plan', - 'statement' => 'Payjp\\Statement', - 'statement_url' => 'Payjp\\StatementUrl', - 'subscription' => 'Payjp\\Subscription', - 'tenant' => 'Payjp\\Tenant', - 'token' => 'Payjp\\Token', - 'tenant_transfer' => 'Payjp\\TenantTransfer', - 'transfer' => 'Payjp\\Transfer', + 'application_url' => \Payjp\ApplicationUrl::class, + 'balance' => \Payjp\Balance::class, + 'card' => \Payjp\Card::class, + 'charge' => \Payjp\Charge::class, + 'customer' => \Payjp\Customer::class, + 'event' => \Payjp\Event::class, + 'list' => \Payjp\Collection::class, + 'plan' => \Payjp\Plan::class, + 'statement' => \Payjp\Statement::class, + 'statement_url' => \Payjp\StatementUrl::class, + 'subscription' => \Payjp\Subscription::class, + 'tenant' => \Payjp\Tenant::class, + 'token' => \Payjp\Token::class, + 'tenant_transfer' => \Payjp\TenantTransfer::class, + 'term' => \Payjp\Term::class, + 'transfer' => \Payjp\Transfer::class, ); /** diff --git a/tests/AccountTest.php b/tests/AccountTest.php index 0a55a9d..596b268 100644 --- a/tests/AccountTest.php +++ b/tests/AccountTest.php @@ -6,92 +6,89 @@ class AccountTest extends TestCase { private function managedAccountResponse($id) { - return array( - 'accounts_enabled' => array('merchant', 'customer'), + return [ + 'accounts_enabled' => [ + 'merchant', + 'customer', + ], 'created' => 1432965397, - 'customer' => array( - 'cards' => array( - 'count' => 1, - 'data' => array( - array( - 'address_city' => '\u8d64\u5742', - 'address_line1' => '7-4', - 'address_line2' => '203', - 'address_state' => '\u6e2f\u533a', - 'address_zip' => '1070050', - 'address_zip_check' => 'passed', - 'brand' => 'Visa', - 'country' => self::COUNTRY, - 'created' => 1432965397, - 'cvc_check' => 'passed', - 'exp_month' => 12, - 'exp_year' => 2016, - 'fingerprint' => 'e1d8225886e3a7211127df751c86787f', - 'id' => 'car_99abf74cb5527ff68233a8b836dd', - 'last4' => '4242', - 'livemode' => true, - 'name' => 'Test Hodler', - 'object' => 'card' - ) - ), - 'object' => 'list', - 'url' => '/v1/accounts/cards' - ), - 'created' => 1432965397, - 'default_card' => null, - 'description' => 'user customer', - 'email' => null, - 'id' => 'acct_cus_38153121efdb7964dd1e147', - 'object' => 'customer' - ), - 'email' => 'firstuser@mail.com', - 'id' => $id, - 'merchant' => array( - 'bank_enabled' => false, - 'brands_accepted' => array( - 'Visa', - 'American Express', - 'MasterCard', - 'JCB', - 'Diners Club' - ), - 'business_type' => 'personal', - 'charge_type' => null, - 'contact_phone' => null, - 'country' => 'JP', - 'created' => 1432965397, - 'currencies_supported' => array( - self::CURRENCY - ), - 'default_currency' => self::CURRENCY, - 'details_submitted' => false, - 'id' => 'acct_mch_002418151ef82e49f6edee1', - 'livemode_activated_at' => 1432965401, - 'livemode_enabled' => true, - 'object' => 'merchant', - 'product_detail' => null, - 'product_name' => null, - 'product_type' => null, - 'site_published' => false, - 'url' => null - ), - 'object' => 'account' - - ); + 'customer' => [ + 'cards' => [ + 'count' => 1, + 'data' => [ + [ + 'address_city' => '\u8d64\u5742', + 'address_line1' => '7-4', + 'address_line2' => '203', + 'address_state' => '\u6e2f\u533a', + 'address_zip' => '1070050', + 'address_zip_check' => 'passed', + 'brand' => 'Visa', + 'country' => self::COUNTRY, + 'created' => 1432965397, + 'cvc_check' => 'passed', + 'exp_month' => 12, + 'exp_year' => 2016, + 'fingerprint' => 'e1d8225886e3a7211127df751c86787f', + 'id' => 'car_99abf74cb5527ff68233a8b836dd', + 'last4' => '4242', + 'livemode' => true, + 'name' => 'Test Hodler', + 'object' => 'card' + ] + ], + 'object' => 'list', + 'url' => '/v1/accounts/cards' + ], + 'created' => 1432965397, + 'default_card' => null, + 'description' => 'user customer', + 'email' => null, + 'id' => 'acct_cus_38153121efdb7964dd1e147', + 'object' => 'customer' + ], + 'email' => 'firstuser@mail.com', + 'id' => $id, + 'merchant' => [ + 'bank_enabled' => false, + 'brands_accepted' => [ + 'Visa', + 'American Express', + 'MasterCard', + 'JCB', + 'Diners Club' + ], + 'business_type' => 'personal', + 'charge_type' => null, + 'contact_phone' => null, + 'country' => 'JP', + 'created' => 1432965397, + 'currencies_supported' => [ + self::CURRENCY + ], + 'default_currency' => self::CURRENCY, + 'details_submitted' => false, + 'id' => 'acct_mch_002418151ef82e49f6edee1', + 'livemode_activated_at' => 1432965401, + 'livemode_enabled' => true, + 'object' => 'merchant', + 'product_detail' => null, + 'product_name' => null, + 'product_type' => null, + 'site_published' => false, + 'url' => null + ], + 'object' => 'account' + ]; } public function testBasicRetrieve() { - $this->mockRequest('GET', '/v1/accounts', array(), $this->managedAccountResponse('acct_ABC')); + $this->mockRequest('GET', '/v1/accounts', [], $this->managedAccountResponse('acct_ABC')); $account = Account::retrieve(); - + $this->assertSame('acct_ABC', $account->id); $this->assertSame('acct_cus_38153121efdb7964dd1e147', $account->customer->id); $this->assertSame('acct_mch_002418151ef82e49f6edee1', $account->merchant->id); } - - public function testRetrieve() - { - $account = Account::retrieve(); - } } diff --git a/tests/BalanceTest.php b/tests/BalanceTest.php new file mode 100644 index 0000000..b82054a --- /dev/null +++ b/tests/BalanceTest.php @@ -0,0 +1,176 @@ + $id, + 'livemode' => false, + 'object' => 'term', + 'charge_count' => 158, + 'refund_count' => 25, + 'dispute_count' => 2, + 'end_at' => 1439650800, + 'start_at' => 1438354800, + ]; + } + + private function balanceResource($id) + { + return [ + 'created' => 1438354800, + 'id' => $id, + 'livemode' => false, + 'net' => 1000, + 'object' => 'balance', + 'state' => 'collecting', + 'statements' => [ + 'count' => 2, + 'data' => [ + [ + 'balance_id' => $id, + 'term' => $this->termResource('tm_sample1'), + 'created' => 1695892351, + 'id' => 'st_sample1', + 'items' => [ + [ + 'amount' => 25, + 'name' => 'チャージバックによる手数料返還', + 'subject' => 'chargeback_fee_offset', + 'tax_rate' => '0.00' + ], [ + 'amount' => -775, + 'name' => 'チャージバック', + 'subject' => 'chargeback', + 'tax_rate' => '0.00' + ], [ + 'amount' => 36, + 'name' => '返金による手数料返還', + 'subject' => 'refund_fee_offset', + 'tax_rate' => '0.00' + ], [ + 'amount' => -1800, + 'name' => '返金', + 'subject' => 'gross_refund', + 'tax_rate' => '0.00' + ], [ + 'amount' => -75, + 'name' => '決済手数料', + 'subject' => 'fee', + 'tax_rate' => '0.00' + ], [ + 'amount' => 3125, + 'name' => '売上', + 'subject' => 'gross_sales', + 'tax_rate' => '0.00' + ] + ], + 'net' => 536, + 'object' => 'statement', + 'livemode' => true, + 'title' => null, + 'tenant_id' => null, + 'type' => 'sales', + 'updated' => 1695892351 + ], [ + 'balance_id' => $id, + 'term' => null, + 'created' => 1695892350, + 'id' => 'st_sample2', + 'items' => [ + [ + 'amount' => -10000, + 'name' => 'プロプラン利用料', + 'subject' => 'proplan', + 'tax_rate' => '10.00' + ] + ], + 'net' => -10000, + 'object' => 'statement', + 'livemode' => true, + 'title' => 'プロプラン月額料金', + 'tenant_id' => null, + 'type' => 'service_fee', + 'updated' => 1695892350 + ], + ], + 'has_more' => false, + 'object' => 'list', + 'url' => '/v1/statements' + ], + 'closed' => false, + 'due_date' => null, + 'bank_info' => [ + 'bank_code' => '0000', + 'bank_branch_code' => '123', + 'bank_account_type' => '普通', + 'bank_account_number' => '1234567', + 'bank_account_holder_name' => 'ペイ タロウ', + 'bank_account_status' => 'pending' + ] + ]; + } + + private function balancesResource($ids = []) + { + return [ + 'count' => count($ids), + 'data' => array_map(function ($id) { + return $this->balanceResource($id); + }, $ids), + 'has_more' => false, + 'object' => 'list', + 'url' => '/v1/balances', + ]; + } + + public function testRetrieve() + { + $expectedBalanceId = 'ba_sample1'; + $expectedBalanceResource = $this->balanceResource($expectedBalanceId); + $this->mockRequest('GET', "/v1/balances/$expectedBalanceId", [], $expectedBalanceResource); + $balance = Balance::retrieve($expectedBalanceId); + $this->assertSame($expectedBalanceId, $balance->id); + $this->assertSame($expectedBalanceResource['created'], $balance->created); + $this->assertSame($expectedBalanceResource['livemode'], $balance->livemode); + $this->assertSame($expectedBalanceResource['net'], $balance->net); + $this->assertSame($expectedBalanceResource['object'], $balance->object); + $this->assertSame($expectedBalanceResource['state'], $balance->state); + $this->assertSame($expectedBalanceResource['closed'], $balance->closed); + $this->assertSame($expectedBalanceResource['due_date'], $balance->due_date); + + $this->assertInstanceOf(PayjpObject::class, $balance->bank_info); + $this->assertSame($expectedBalanceResource['bank_info']['bank_code'], $balance->bank_info->bank_code); + $this->assertSame($expectedBalanceResource['bank_info']['bank_branch_code'], $balance->bank_info->bank_branch_code); + $this->assertSame($expectedBalanceResource['bank_info']['bank_account_type'], $balance->bank_info->bank_account_type); + $this->assertSame($expectedBalanceResource['bank_info']['bank_account_number'], $balance->bank_info->bank_account_number); + $this->assertSame($expectedBalanceResource['bank_info']['bank_account_holder_name'], $balance->bank_info->bank_account_holder_name); + $this->assertSame($expectedBalanceResource['bank_info']['bank_account_status'], $balance->bank_info->bank_account_status); + + $this->assertInstanceOf(Collection::class, $balance->statements); + $this->assertSame($expectedBalanceResource['statements']['count'], $balance->statements->count); + $this->assertSame($expectedBalanceResource['statements']['has_more'], $balance->statements->has_more); + $this->assertSame($expectedBalanceResource['statements']['object'], $balance->statements->object); + $this->assertSame($expectedBalanceResource['statements']['url'], $balance->statements->url); + + foreach ($balance->statements->data as $statement) { + $this->assertInstanceOf(Statement::class, $statement); + } + } + + public function testAll() + { + $expectedBalanceIds = ['ba_sample1', 'ba_sample2']; + $this->mockRequest('GET', '/v1/balances', [], $this->balancesResource($expectedBalanceIds)); + $balances = Balance::all(); + $this->assertSame(count($expectedBalanceIds), $balances['count']); + $this->assertCount(count($expectedBalanceIds), $balances['data']); + foreach ($balances['data'] as $index => $balance) { + $this->assertInstanceOf(Balance::class, $balance); + $this->assertSame($expectedBalanceIds[$index], $balance->id); + } + } +} diff --git a/tests/CardErrorTest.php b/tests/CardErrorTest.php index cbcdce2..7ad174e 100644 --- a/tests/CardErrorTest.php +++ b/tests/CardErrorTest.php @@ -10,23 +10,15 @@ public function testExpiredCard() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 6, - "exp_year" => date('Y') + 3, - "cvc" => "314" + 'number' => '4242424242424242', + 'exp_month' => 6, + 'exp_year' => date('Y') - 1, + 'cvc' => '314' ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); - - $charge = array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ); - try { - Charge::create($charge); + Token::create($params, ['payjp_direct_token_generate' => 'true']); } catch (Error\Card $e) { $this->assertSame(402, $e->getHttpStatus()); } diff --git a/tests/ChargeTest.php b/tests/ChargeTest.php index 1c83312..84f6a40 100644 --- a/tests/ChargeTest.php +++ b/tests/ChargeTest.php @@ -18,21 +18,20 @@ public function testCreate() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => "05", - 'exp_year' => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => '05', + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $c = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + $c = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id, + ]); $this->assertTrue($c->paid); $this->assertFalse($c->refunded); } @@ -44,24 +43,22 @@ public function testIdempotentCreate() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 5, - 'exp_year' => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $c = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ), - array( - 'idempotency_key' => self::generateRandomString(), - ) - ); + $c = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ], [ + 'idempotency_key' => self::generateRandomString(), + ]); $this->assertTrue($c->paid); $this->assertFalse($c->refunded); @@ -74,21 +71,20 @@ public function testRetrieve() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 5, - "exp_year" => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $c = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + $c = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ]); $d = Charge::retrieve($c->id); $this->assertSame($d->id, $c->id); } @@ -98,50 +94,40 @@ public function testAll() { self::authorizeFromEnv(); - $charges = Charge::all( - array( - 'limit' => 3, - 'offset' => 10 - ) - ); + $charges = Charge::all([ + 'limit' => 3, + 'offset' => 10 + ]); $planID = 'gold-' . self::randomString(); self::retrieveOrCreatePlan($planID); $customer = self::createTestCustomer(); - $charge = Charge::create( - array( - 'amount' => 1000, - 'currency' => self::CURRENCY, - 'customer' => $customer->id - ) - ); + $charge = Charge::create([ + 'amount' => 1000, + 'currency' => self::CURRENCY, + 'customer' => $customer->id + ]); - $charges_2 = Charge::all( - array( - 'customer' => $customer->id - ) - ); + $charges_2 = Charge::all([ + 'customer' => $customer->id + ]); $this->assertSame(1, count($charges_2['data'])); $this->assertSame($charge->id, $charges_2['data'][0]->id); - $charge_2 = Charge::create( - array( - 'amount' => 1500, - 'currency' => self::CURRENCY, - 'customer' => $customer->id - ) - ); - - $charges_3 = Charge::all( - array( - 'limit' => 2, - 'offset' => 0, - 'customer' => $customer->id - ) - ); + $charge_2 = Charge::create([ + 'amount' => 1500, + 'currency' => self::CURRENCY, + 'customer' => $customer->id + ]); + + $charges_3 = Charge::all([ + 'limit' => 2, + 'offset' => 0, + 'customer' => $customer->id + ]); $this->assertSame(2, count($charges_3['data'])); } @@ -153,21 +139,20 @@ public function testUpdateDescription() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 5, - "exp_year" => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $charge = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + $charge = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ]); $charge->description = 'foo bar'; $charge->save(); @@ -184,22 +169,21 @@ public function testCaptureAll() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 5, - "exp_year" => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $charge = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id, - 'capture' => false - ) - ); + $charge = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id, + 'capture' => false + ]); $this->assertFalse($charge->captured); @@ -215,22 +199,21 @@ public function testCapturePart() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 5, - "exp_year" => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $charge = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id, - 'capture' => false - ) - ); + $charge = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id, + 'capture' => false + ]); $this->assertFalse($charge->captured); @@ -246,30 +229,27 @@ public function testRefund() $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 5, - "exp_year" => date('Y') + 1 + 'number' => '4242424242424242', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $charge = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id, - ) - ); + $charge = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id, + ]); $this->assertTrue($charge->captured); - $redundChargePart = $charge->refund( - array( - 'amount' => 50, - 'refund_reason' => 'foo bar 1' - ) - ); + $redundChargePart = $charge->refund([ + 'amount' => 50, + 'refund_reason' => 'foo bar 1' + ]); $this->assertTrue($redundChargePart->refunded); $this->assertSame('foo bar 1', $redundChargePart->refund_reason); @@ -278,11 +258,9 @@ public function testRefund() $this->assertSame('foo bar 1', $charge->refund_reason); $this->assertSame(50, $charge->amount_refunded); - $refundChargeAll = $charge->refund( - array( - 'refund_reason' => 'foo bar 2' - ) - ); + $refundChargeAll = $charge->refund([ + 'refund_reason' => 'foo bar 2' + ]); $this->assertTrue($refundChargeAll->refunded); $this->assertSame('foo bar 2', $refundChargeAll->refund_reason); @@ -294,27 +272,26 @@ public function testRefund() public function testInvalidCard() { - $this->expectException("\Payjp\Error\Card"); + $this->expectException('\Payjp\Error\Card'); self::authorizeFromEnv(); $params = [ 'card' => [ - "number" => "4242424242424241", - "exp_month" => 5, - "exp_year" => date('Y') + 1 + 'number' => '4242424242424241', + 'exp_month' => 5, + 'exp_year' => date('Y') + 1, + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ]); } @@ -324,23 +301,22 @@ public function testInvalidAddressZipTest() $params = [ 'card' => [ - "number" => "4000000000000070", - "exp_month" => '05', - 'exp_year' => (date('Y') + 1) + 'number' => '4000000000000070', + 'exp_month' => '05', + 'exp_year' => (date('Y') + 1), + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $ch = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + $ch = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ]); - $this->assertSame("failed", $ch->card->address_zip_check); + $this->assertSame('failed', $ch->card->address_zip_check); } public function testInvalidCvcTest() @@ -349,23 +325,22 @@ public function testInvalidCvcTest() $params = [ 'card' => [ - "number" => "4000000000000100", - "exp_month" => '05', - 'exp_year' => (date('Y') + 1) + 'number' => '4000000000000100', + 'exp_month' => '05', + 'exp_year' => (date('Y') + 1), + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $ch = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + $ch = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ]); - $this->assertSame("failed", $ch->card->cvc_check); + $this->assertSame('failed', $ch->card->cvc_check); } public function testUnavailableCvcTest() @@ -374,22 +349,21 @@ public function testUnavailableCvcTest() $params = [ 'card' => [ - "number" => "4000000000000150", - "exp_month" => '05', - 'exp_year' => (date('Y') + 1) + 'number' => '4000000000000150', + 'exp_month' => '05', + 'exp_year' => (date('Y') + 1), + 'cvc' => '123', ] ]; - $card = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + $card = Token::create($params, ['payjp_direct_token_generate' => 'true']); - $ch = Charge::create( - array( - 'amount' => 100, - 'currency' => self::CURRENCY, - 'card' => $card->id - ) - ); + $ch = Charge::create([ + 'amount' => 100, + 'currency' => self::CURRENCY, + 'card' => $card->id + ]); - $this->assertSame("unavailable", $ch->card->cvc_check); + $this->assertSame('unavailable', $ch->card->cvc_check); } } diff --git a/tests/CustomerTest.php b/tests/CustomerTest.php index 58fb867..dcc9fde 100644 --- a/tests/CustomerTest.php +++ b/tests/CustomerTest.php @@ -7,57 +7,55 @@ class CustomerTest extends TestCase // POST/customers public function testCreate() { - $attribute = array( - 'email' => 'gdb@pay.jp', - 'description' => 'foo bar' - ); - + $attribute = [ + 'email' => 'gdb@pay.jp', + 'description' => 'foo bar' + ]; + $customer = self::createTestCustomer($attribute); $this->assertSame($attribute['email'], $customer->email); $this->assertSame($attribute['description'], $customer->description); } - + // GET/customers/:id public function testRetrieve() { $customer = self::createTestCustomer(); $retrieve_customer = Customer::retrieve($customer->id); - $this->assertSame($customer->id, $retrieve_customer->id); } - + // GET/customers/ public function testAll() { - $customers = Customer::all( - array( - 'limit' => 3, - 'offset' => 10 - ) - ); + $customers = Customer::all([ + 'limit' => 3, + 'offset' => 10 + ]); + $this->assertCount(3, $customers['data']); } - + // DELETE/customers/:id public function testDeletion() { $customer = self::createTestCustomer(); $id = $customer->id; - + $delete_customer = $customer->delete(); $this->assertSame($id, $customer->id); $this->assertFalse($customer->livemode); $this->assertTrue($customer->deleted); - + $this->assertNull($customer['active_card']); - + $this->assertSame($id, $delete_customer->id); $this->assertFalse($delete_customer->livemode); $this->assertTrue($delete_customer->deleted); - + $this->assertNull($delete_customer['active_card']); } - + //POST /customers/:id public function testSave() { @@ -84,7 +82,7 @@ public function testSave() public function testBogusAttribute() { - $this->expectException("\Payjp\Error\InvalidRequest"); + $this->expectException('\Payjp\Error\InvalidRequest'); $customer = self::createTestCustomer(); $customer->bogus = 'bogus'; $customer->save(); @@ -92,199 +90,189 @@ public function testBogusAttribute() public function testUpdateDescriptionEmpty() { - $this->expectException("\InvalidArgumentException"); + $this->expectException('\InvalidArgumentException'); $customer = self::createTestCustomer(); $customer->description = ''; - + $customer->save(); - + $updatedCustomer = Customer::retrieve($customer->id); $this->assertSame('123', $updatedCustomer->description); } - + public function testUpdateDescriptionNull() { - $customer = self::createTestCustomer(array('description' => 'foo bar')); + $customer = self::createTestCustomer(['description' => 'foo bar']); $customer->description = null; - + $customer->save(); - + $updatedCustomer = Customer::retrieve($customer->id); $this->assertSame('', $updatedCustomer->description); $this->assertSame('', $customer->description); } - + //POST /customers/:id/cards //GET /customers/:id/cards public function testCustomerAddCard() { $customer = $this->createTestCustomer(); - + $defaultCard = $customer->cards->data[0]; $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 12, - "exp_year" => date('Y') + 3, - "cvc" => "314" + 'number' => '4242424242424242', + 'exp_month' => 12, + 'exp_year' => date('Y') + 3, + 'cvc' => '314' ] ]; - $token = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); - + $token = Token::create($params, ['payjp_direct_token_generate' => 'true']); + $params2 = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 7, - "exp_year" => date('Y') + 3, - "cvc" => "314" + 'number' => '4242424242424242', + 'exp_month' => 7, + 'exp_year' => date('Y') + 3, + 'cvc' => '314' ] ]; - $card = Token::create($params2, $options = ['payjp_direct_token_generate' => 'true']); - - $createdCard = $customer->cards->create(array("card" => $token->id)); - $createdCard_2 = $customer->cards->create(array("card" => $card->id)); - + $card = Token::create($params2, ['payjp_direct_token_generate' => 'true']); + + $createdCard = $customer->cards->create(['card' => $token->id]); + $createdCard_2 = $customer->cards->create(['card' => $card->id]); + $updatedCustomer = Customer::retrieve($customer->id); $cardList = $updatedCustomer->cards->all(); - $this->assertSame(count($cardList["data"]), 3); - - $this->assertSame($createdCard_2->id, $cardList["data"][0]->id); - $this->assertSame($createdCard->id, $cardList["data"][1]->id); - $this->assertSame($defaultCard->id, $cardList["data"][2]->id); - - $card = $customer->cards->retrieve($cardList["data"][1]->id); - $this->assertSame($card->id, $cardList["data"][1]->id); - + $this->assertSame(count($cardList['data']), 3); + + $this->assertSame($createdCard_2->id, $cardList['data'][0]->id); + $this->assertSame($createdCard->id, $cardList['data'][1]->id); + $this->assertSame($defaultCard->id, $cardList['data'][2]->id); + + $card = $customer->cards->retrieve($cardList['data'][1]->id); + $this->assertSame($card->id, $cardList['data'][1]->id); + $updatedCustomer = Customer::retrieve($customer->id); - $cardList = $updatedCustomer->cards->all( - array( - 'limit' => 1, - 'offset' => 1 - ) - ); - $this->assertSame(count($cardList["data"]), 1); + $cardList = $updatedCustomer->cards->all([ + 'limit' => 1, + 'offset' => 1 + ]); + $this->assertSame(count($cardList['data']), 1); } - + //POST /customers/:id/cards/:card_id public function testCustomerUpdateCard() { $customer = $this->createTestCustomer(); - + $cards = $customer->cards->all(); - $this->assertSame(count($cards["data"]), 1); - + $this->assertSame(count($cards['data']), 1); + $card = $cards['data'][0]; - $card->name = "Littleorc"; + $card->name = 'Littleorc'; $card->save(); - + $updatedCustomer = Customer::retrieve($customer->id); $cardList = $updatedCustomer->cards->all(); - $this->assertSame($cardList["data"][0]->name, "Littleorc"); + $this->assertSame($cardList['data'][0]->name, 'Littleorc'); } - + public function testCustomerDeleteCard() { $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 7, - "exp_year" => date('Y') + 3, - "cvc" => "314" + 'number' => '4242424242424242', + 'exp_month' => 7, + 'exp_year' => date('Y') + 3, + 'cvc' => '314' ] ]; $token = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); - + $customer = $this->createTestCustomer(); - $createdCard = $customer->cards->create(array("card" => $token->id)); - + $createdCard = $customer->cards->create(['card' => $token->id]); + $updatedCustomer = Customer::retrieve($customer->id); $cardList = $updatedCustomer->cards->all(); - $this->assertSame(count($cardList["data"]), 2); - + $this->assertSame(count($cardList['data']), 2); + $deleteStatus = $updatedCustomer->cards->retrieve($createdCard->id)->delete(); $this->assertTrue($deleteStatus->deleted); - + $postDeleteCustomer = Customer::retrieve($customer->id); $postDeleteCards = $postDeleteCustomer->cards->all(); - $this->assertSame(count($postDeleteCards["data"]), 1); - + $this->assertSame(count($postDeleteCards['data']), 1); + $cardList = $updatedCustomer->cards->all(); - $this->assertSame(count($cardList["data"]), 1); + $this->assertSame(count($cardList['data']), 1); } - + public function testCustomerSubscriptionAllRetrieve() { $planID = 'gold-' . self::randomString(); self::retrieveOrCreatePlan($planID); - + $customer = self::createTestCustomer(); - - $subscription = Subscription::create( - array( - 'customer' => $customer->id, - 'plan' => $planID - ) - ); - + + $subscription = Subscription::create([ + 'customer' => $customer->id, + 'plan' => $planID + ]); + $planID_2 = 'gold-2-' . self::randomString(); self::retrieveOrCreatePlan($planID_2); - $subscription_2 = Subscription::create( - array( - 'customer' => $customer->id, - 'plan' => $planID_2 - ) - ); - + $subscription_2 = Subscription::create([ + 'customer' => $customer->id, + 'plan' => $planID_2 + ]); + $customerRetrive = Customer::retrieve($customer->id); $subscriptions = $customerRetrive->subscriptions->all(); $this->assertSame($subscription_2->id, $subscriptions['data'][0]->id); $this->assertSame($subscription->id, $subscriptions['data'][1]->id); - + $this->assertSame(2, count($subscriptions['data'])); $this->assertSame($customer->id, $subscriptions['data'][0]->customer); $this->assertSame($planID_2, $subscriptions['data'][0]->plan->id); - + $subscriptionRetrieve = $customerRetrive->subscriptions->retrieve($subscription->id); $this->assertSame($subscription->id, $subscriptionRetrieve->id); $this->assertSame($planID, $subscriptionRetrieve->plan->id); } - + public function testCustomerChargeAll() { $planID = 'gold-' . self::randomString(); self::retrieveOrCreatePlan($planID); - + $customer = self::createTestCustomer(); - - $charge = Charge::create( - array( - 'amount' => 1000, - 'currency' => self::CURRENCY, - 'customer' => $customer->id - ) - ); - + + $charge = Charge::create([ + 'amount' => 1000, + 'currency' => self::CURRENCY, + 'customer' => $customer->id + ]); + $charges = $customer->charges(); - + $this->assertSame(1, count($charges['data'])); $this->assertSame($charge->id, $charges['data'][0]->id); - $charge_2 = Charge::create( - array( - 'amount' => 1500, - 'currency' => self::CURRENCY, - 'customer' => $customer->id - ) - ); - + $charge_2 = Charge::create([ + 'amount' => 1500, + 'currency' => self::CURRENCY, + 'customer' => $customer->id + ]); + $charges_2 = $customer->charges(); - + $this->assertSame(2, count($charges_2['data'])); $this->assertSame($charge_2->id, $charges_2['data'][0]->id); $this->assertSame($charge->id, $charges_2['data'][1]->id); diff --git a/tests/PlanTest.php b/tests/PlanTest.php index af2b5f8..23e4946 100644 --- a/tests/PlanTest.php +++ b/tests/PlanTest.php @@ -7,82 +7,75 @@ class PlanTest extends TestCase public function testCreateRetrieveAll() { self::authorizeFromEnv(); - $planID = 'gold-' . self::randomString(); - $p = Plan::create(array( + $planId = 'gold-' . self::randomString(); + $p = Plan::create([ 'amount' => 2000, 'interval' => 'month', 'currency' => self::CURRENCY, 'name' => 'Plan', - 'id' => $planID - )); - - $plan_retrieve = Plan::retrieve($planID); - $this->assertSame($planID, $plan_retrieve->id); - - $planID_2 = 'foobar-2-' . self::randomString(); - $p_2 = Plan::create( - array( - 'amount' => 3000, - 'interval' => 'month', - 'currency' => self::CURRENCY, - 'name' => 'Plan_2', - 'id' => $planID_2 - ) - ); + 'id' => $planId + ]); - $plans = Plan::all( - array( - 'limit' => 2, - 'offset' => 0 - ) - ); + $plan_retrieve = Plan::retrieve($planId); + $this->assertSame($planId, $plan_retrieve->id); + + $planId_2 = 'foobar-2-' . self::randomString(); + $p_2 = Plan::create([ + 'amount' => 3000, + 'interval' => 'month', + 'currency' => self::CURRENCY, + 'name' => 'Plan_2', + 'id' => $planId_2 + ]); + + $plans = Plan::all([ + 'limit' => 2, + 'offset' => 0 + ]); $this->assertSame(2, count($plans['data'])); } public function testDeletion() { self::authorizeFromEnv(); - $plan_ID = 'gold-' . self::randomString(); - $p = Plan::create(array( + $planId = 'gold-' . self::randomString(); + $p = Plan::create([ 'amount' => 2000, 'interval' => 'month', 'currency' => self::CURRENCY, 'name' => 'Plan', - 'id' => $plan_ID - )); + 'id' => $planId + ]); $p->delete(); $this->assertTrue($p->deleted); - $this->assertSame($plan_ID, $p->id); + $this->assertSame($planId, $p->id); } public function testFalseyId() { try { - $retrievedPlan = Plan::retrieve('0'); + Plan::retrieve('0'); } catch (Error\InvalidRequest $e) { - // Can either succeed or 404, all other errors are bad - if ($e->httpStatus !== 404) { - $this->fail(); - } + $this->assertSame(404, $e->httpStatus); } } public function testSave() { self::authorizeFromEnv(); - $planID = 'gold-' . self::randomString(); - $p = Plan::create(array( + $planId = 'gold-' . self::randomString(); + $p = Plan::create([ 'amount' => 2000, 'interval' => 'month', 'currency' => self::CURRENCY, 'name' => 'Plan', - 'id' => $planID - )); + 'id' => $planId + ]); $p->name = 'A new plan name'; $p->save(); $this->assertSame($p->name, 'A new plan name'); - $payjpPlan = Plan::retrieve($planID); + $payjpPlan = Plan::retrieve($planId); $this->assertSame($p->name, $payjpPlan->name); } } diff --git a/tests/StatementTest.php b/tests/StatementTest.php index ed12334..bbff422 100644 --- a/tests/StatementTest.php +++ b/tests/StatementTest.php @@ -4,9 +4,25 @@ class StatementTest extends TestCase { + private function termResource($id) + { + return [ + 'created' => 1438354800, + 'id' => $id, + 'livemode' => false, + 'object' => 'term', + 'charge_count' => 158, + 'refund_count' => 25, + 'dispute_count' => 2, + 'end_at' => 1439650800, + 'start_at' => 1438354800, + ]; + } + private function managedStatementResource($id) { - return array( + return [ + 'balance_id' => 'ba_sample', 'created' => time(), 'id' => $id, 'items' => [ @@ -35,14 +51,20 @@ private function managedStatementResource($id) 'tax_rate' => '0.10' ] ], + 'livemode' => false, + 'net' => 211248433, 'object' => 'statement', - 'title' => null - ); + 'tenant_id' => 'ten_sample', + 'type' => 'sales', + 'term' => $this->termResource('tm_sample'), + 'title' => null, + 'updated' => 1695892351, + ]; } private function managedStatementResources($ids) { - return array( + return [ 'count' => count($ids), 'data' => array_map(function ($id) { return $this->managedStatementResource($id); @@ -50,7 +72,7 @@ private function managedStatementResources($ids) 'has_more' => false, 'object' => 'list', 'url' => '/v1/statements' - ); + ]; } private function managedDownloadUrlResource() @@ -65,9 +87,26 @@ private function managedDownloadUrlResource() public function testRetrieve() { $expectedStatementId = 'st_0d08780a33ab77f1c911a1b7286bd'; - $this->mockRequest('GET', '/v1/statements/' . $expectedStatementId, array(), $this->managedStatementResource($expectedStatementId)); + $expectedStatementResource = $this->managedStatementResource($expectedStatementId); + $this->mockRequest('GET', '/v1/statements/' . $expectedStatementId, [], $expectedStatementResource); $statement = Statement::retrieve($expectedStatementId); + $this->assertSame($expectedStatementResource['balance_id'], $statement->balance_id); + $this->assertSame($expectedStatementResource['created'], $statement->created); $this->assertSame($expectedStatementId, $statement->id); + $this->assertSame($expectedStatementResource['livemode'], $statement->livemode); + $this->assertSame($expectedStatementResource['net'], $statement->net); + $this->assertSame($expectedStatementResource['object'], $statement->object); + $this->assertSame($expectedStatementResource['tenant_id'], $statement->tenant_id); + $this->assertSame($expectedStatementResource['title'], $statement->title); + $this->assertSame($expectedStatementResource['type'], $statement->type); + $this->assertSame($expectedStatementResource['updated'], $statement->updated); + $this->assertInstanceOf(Term::class, $statement->term); + foreach ($statement->items as $item) { + $this->assertArrayHasKey('amount', $item); + $this->assertArrayHasKey('name', $item); + $this->assertArrayHasKey('subject', $item); + $this->assertArrayHasKey('tax_rate', $item); + } } public function testAll() @@ -75,8 +114,8 @@ public function testAll() $expectedStatementIds = array('st_6b7d642291873e7b97e9175d7d6b8', 'st_0d08780a33ab77f1c911a1b7286bd'); $this->mockRequest('GET', '/v1/statements', array(), $this->managedStatementResources($expectedStatementIds)); $statements = Statement::all(); - $this->assertSame(2, $statements['count']); - $this->assertCount(2, $statements['data']); + $this->assertSame(count($expectedStatementIds), $statements['count']); + $this->assertCount(count($expectedStatementIds), $statements['data']); $this->assertSame($expectedStatementIds[0], $statements['data'][0]->id); $this->assertSame($expectedStatementIds[1], $statements['data'][1]->id); } diff --git a/tests/TermTest.php b/tests/TermTest.php new file mode 100644 index 0000000..4415362 --- /dev/null +++ b/tests/TermTest.php @@ -0,0 +1,62 @@ + $id, + 'livemode' => false, + 'object' => 'term', + 'charge_count' => 158, + 'refund_count' => 25, + 'dispute_count' => 2, + 'end_at' => 1439650800, + 'start_at' => 1438354800, + ]; + } + + private function termResources($ids = []) + { + return [ + 'count' => count($ids), + 'data' => array_map(function ($id) { + return $this->termResource($id); + }, $ids), + 'has_more' => false, + 'object' => 'list', + 'url' => '/v1/terms', + ]; + } + + public function testRetrieve() + { + $expectedTermId = 'tm_sample1'; + $expectedTermResource = $this->termResource($expectedTermId); + $this->mockRequest('GET', "/v1/terms/$expectedTermId", [], $expectedTermResource); + $term = Term::retrieve($expectedTermId); + $this->assertSame($expectedTermId, $term->id); + $this->assertSame($expectedTermResource['livemode'], $term->livemode); + $this->assertSame($expectedTermResource['object'], $term->object); + $this->assertSame($expectedTermResource['charge_count'], $term->charge_count); + $this->assertSame($expectedTermResource['refund_count'], $term->refund_count); + $this->assertSame($expectedTermResource['dispute_count'], $term->dispute_count); + $this->assertSame($expectedTermResource['end_at'], $term->end_at); + $this->assertSame($expectedTermResource['start_at'], $term->start_at); + } + + public function testAll() + { + $expectedTermIds = ['tm_sample1', 'tm_sample2']; + $this->mockRequest('GET', '/v1/terms', [], $this->termResources($expectedTermIds)); + $terms = Term::all(); + $this->assertSame(count($expectedTermIds), $terms['count']); + $this->assertCount(count($expectedTermIds), $terms['data']); + foreach ($terms['data'] as $index => $term) { + $this->assertInstanceOf(Term::class, $term); + $this->assertSame($expectedTermIds[$index], $term->id); + } + } +} diff --git a/tests/TokenTest.php b/tests/TokenTest.php index 144ebcd..cbf7fef 100644 --- a/tests/TokenTest.php +++ b/tests/TokenTest.php @@ -10,40 +10,41 @@ public function testUrls() $token = new Token('abcd/efgh'); $this->assertSame($token->instanceUrl(), '/v1/tokens/abcd%2Fefgh'); } - + public function testCreate() { self::authorizeFromEnv(); - + $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 6, - "exp_year" => date('Y') + 3, - "cvc" => "314" + 'number' => '4242424242424242', + 'exp_month' => 6, + 'exp_year' => date('Y') + 3, + 'cvc' => '314' ] ]; - - $token = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); + + $token = Token::create($params, ['payjp_direct_token_generate' => 'true']); + $this->assertNotNull($token); } - + public function testRetrieve() { self::authorizeFromEnv(); $params = [ 'card' => [ - "number" => "4242424242424242", - "exp_month" => 6, - "exp_year" => date('Y') + 3, - "cvc" => "314" + 'number' => '4242424242424242', + 'exp_month' => 6, + 'exp_year' => date('Y') + 3, + 'cvc' => '314' ] ]; - - $token = Token::create($params, $options = ['payjp_direct_token_generate' => 'true']); - + + $token = Token::create($params, ['payjp_direct_token_generate' => 'true']); + $token_retrieve = Token::retrieve($token->id); - + $this->assertSame($token->id, $token_retrieve->id); } } diff --git a/tests/TransferTest.php b/tests/TransferTest.php index 92158b2..cfda56f 100644 --- a/tests/TransferTest.php +++ b/tests/TransferTest.php @@ -7,39 +7,34 @@ class TransferTest extends TestCase public function testAllRetrieve() { self::authorizeFromEnv(); - - $transfers = Transfer::all( - array( - 'limit' => 3, - 'offset' => 0 - ) - ); + + $transfers = Transfer::all([ + 'limit' => 3, + 'offset' => 0 + ]); if (count($transfers['data'])) { $transfer = Transfer::retrieve($transfers['data'][0]->id); $this->assertSame($transfers['data'][0]->id, $transfer->id); } } - + public function testAllCharge() { self::authorizeFromEnv(); - - $transfers = Transfer::all( - array( - 'limit' => 3, - 'offset' => 0 - ) - ); - + + $transfers = Transfer::all([ + 'limit' => 3, + 'offset' => 0 + ]); + if (count($transfers['data'])) { $transfer = Transfer::retrieve($transfers['data'][0]->id); - $charges = $transfer->charges->all( - array( - 'limit' => 3, - 'offset' => 0 - ) - ); + $charges = $transfer->charges->all([ + 'limit' => 3, + 'offset' => 0 + ]); + $this->assertTrue(count($charges['data']) > 0); } } }