diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..b810a08 --- /dev/null +++ b/.php_cs @@ -0,0 +1,23 @@ +exclude('vendor') + ->in([__DIR__]); + +$config = Symfony\CS\Config::create() + ->fixers([ + '-phpdoc_params', + '-phpdoc_short_description', + '-phpdoc_inline_tag', + '-pre_increment', + '-heredoc_to_nowdoc', + '-spaces_cast', + '-include', + '-phpdoc_no_package', + 'concat_with_spaces', + 'ordered_use', + 'short_array_syntax', + ]) + ->finder($finder); + +return $config; diff --git a/.travis.yml b/.travis.yml index 589d768..8f9bace 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ sudo: false cache: directories: - $HOME/.composer/cache + - vendor install: - travis_retry composer self-update && composer --version @@ -20,4 +21,5 @@ install: - travis_retry composer install --prefer-dist --no-interaction script: - - phpunit --verbose $PHPUNIT_FLAGS \ No newline at end of file + - vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff + - phpunit --verbose $PHPUNIT_FLAGS diff --git a/Billable.php b/Billable.php index e4ce9a6..15353cb 100644 --- a/Billable.php +++ b/Billable.php @@ -19,6 +19,7 @@ /** * Class Billable + * * @package yii2mod\cashier */ trait Billable @@ -35,6 +36,7 @@ trait Billable * * @param int $amount * @param array $options + * * @return \Stripe\Charge * * @throws \Stripe\Error\Card @@ -63,11 +65,13 @@ public function charge($amount, array $options = []) * * @param $charge * @param array $options + * * @return StripeRefund */ public function refund($charge, array $options = []) { $options['charge'] = $charge; + return StripeRefund::create($options, ['api_key' => $this->getStripeKey()]); } @@ -87,6 +91,7 @@ public function hasCardOnFile() * @param string $description * @param int $amount * @param array $options + * * @return bool * * @throws \Stripe\Error\Card @@ -116,6 +121,7 @@ public function invoiceFor($description, $amount, array $options = []) * * @param string $subscription * @param string $plan + * * @return SubscriptionBuilder */ public function newSubscription($subscription, $plan) @@ -128,6 +134,7 @@ public function newSubscription($subscription, $plan) * * @param string $subscription * @param string|null $plan + * * @return bool */ public function onTrial($subscription = 'default', $plan = null) @@ -159,6 +166,7 @@ public function onGenericTrial() * * @param string $subscription * @param string|null $plan + * * @return bool */ public function subscribed($subscription = 'default', $plan = null) @@ -170,6 +178,7 @@ public function subscribed($subscription = 'default', $plan = null) if (is_null($plan)) { return $subscription->valid(); } + return $subscription->valid() && $subscription->stripePlan === $plan; } @@ -178,6 +187,7 @@ public function subscribed($subscription = 'default', $plan = null) * Get a subscription instance by name. * * @param string $subscription + * * @return SubscriptionModel|null */ public function subscription($subscription = 'default') @@ -225,7 +235,6 @@ public function upcomingInvoice() return new Invoice($this, $stripeInvoice); } catch (InvalidRequest $e) { - // } } @@ -233,6 +242,7 @@ public function upcomingInvoice() * Find an invoice by ID. * * @param string $id + * * @return Invoice|null */ public function findInvoice($id) @@ -240,7 +250,6 @@ public function findInvoice($id) try { return new Invoice($this, StripeInvoice::retrieve($id, $this->getStripeKey())); } catch (Exception $e) { - } } @@ -248,7 +257,9 @@ public function findInvoice($id) * Find an invoice or throw a 404 error. * * @param string $id + * * @return Invoice + * * @throws NotFoundHttpException */ public function findInvoiceOrFail($id) @@ -267,6 +278,7 @@ public function findInvoiceOrFail($id) * * @param string $id * @param array $data + * * @return Response */ public function downloadInvoice($id, array $data) @@ -279,6 +291,7 @@ public function downloadInvoice($id, array $data) * * @param bool $includePending * @param array $parameters + * * @return array */ public function invoices($includePending = false, $parameters = []) @@ -307,6 +320,7 @@ public function invoices($includePending = false, $parameters = []) * Get an array of the entity's invoices. * * @param array $parameters + * * @return array */ public function invoicesIncludingPending(array $parameters = []) @@ -318,7 +332,6 @@ public function invoicesIncludingPending(array $parameters = []) * Update customer's credit card. * * @param string $token - * @return void */ public function updateCard($token) { @@ -367,6 +380,7 @@ public function updateCardFromStripe() $this->cardLastFour = null; $this->update(false); } + return $this; } @@ -374,6 +388,7 @@ public function updateCardFromStripe() * Fills the user's properties with the source from Stripe. * * @param \Stripe\Card|null $card + * * @return $this */ protected function fillCardDetails($card) @@ -382,6 +397,7 @@ protected function fillCardDetails($card) $this->cardBrand = $card->brand; $this->cardLastFour = $card->last4; } + return $this; } @@ -389,7 +405,6 @@ protected function fillCardDetails($card) * Apply a coupon to the billable entity. * * @param string $coupon - * @return void */ public function applyCoupon($coupon) { @@ -405,6 +420,7 @@ public function applyCoupon($coupon) * * @param array|string $plans * @param string $subscription + * * @return bool */ public function subscribedToPlan($plans, $subscription = 'default') @@ -428,6 +444,7 @@ public function subscribedToPlan($plans, $subscription = 'default') * Determine if the entity is on the given plan. * * @param string $plan + * * @return bool */ public function onPlan($plan) @@ -452,6 +469,7 @@ public function hasStripeId() * * @param string $token * @param array $options + * * @return Customer */ public function createAsStripeCustomer($token, array $options = []) @@ -522,7 +540,6 @@ public static function getStripeKey() * Set the Stripe API key. * * @param string $key - * @return void */ public static function setStripeKey($key) { diff --git a/Cashier.php b/Cashier.php index 9d7832c..805a411 100644 --- a/Cashier.php +++ b/Cashier.php @@ -7,6 +7,7 @@ /** * Class Cashier + * * @package yii2mod\cashier */ class Cashier @@ -17,14 +18,14 @@ class Cashier * @var string */ protected static $currency = 'usd'; - + /** * The current currency symbol. * * @var string */ protected static $currencySymbol = '$'; - + /** * The custom currency formatter. * @@ -37,7 +38,6 @@ class Cashier * * @param string $currency * @param string|null $symbol - * @return void */ public static function useCurrency($currency, $symbol = null) { @@ -49,7 +49,9 @@ public static function useCurrency($currency, $symbol = null) * Guess the currency symbol for the given currency. * * @param string $currency + * * @return string + * * @throws Exception */ protected static function guessCurrencySymbol($currency) @@ -64,7 +66,7 @@ protected static function guessCurrencySymbol($currency) case 'gbp': return '£'; default: - throw new Exception("Unable to guess symbol for currency. Please explicitly specify it."); + throw new Exception('Unable to guess symbol for currency. Please explicitly specify it.'); } } @@ -82,7 +84,6 @@ public static function usesCurrency() * Set the currency symbol to be used when formatting currency. * * @param string $symbol - * @return void */ public static function useCurrencySymbol($symbol) { @@ -103,7 +104,6 @@ public static function usesCurrencySymbol() * Set the custom currency formatter. * * @param callable $callback - * @return void */ public static function formatCurrencyUsing(callable $callback) { @@ -114,6 +114,7 @@ public static function formatCurrencyUsing(callable $callback) * Format the given amount into a displayable currency. * * @param int $amount + * * @return string */ public static function formatAmount($amount) @@ -125,7 +126,7 @@ public static function formatAmount($amount) if (StringHelper::startsWith($amount, '-')) { return '-' . static::usesCurrencySymbol() . ltrim($amount, '-'); } + return static::usesCurrencySymbol() . $amount; } - -} \ No newline at end of file +} diff --git a/Invoice.php b/Invoice.php index 1dac231..20b49e2 100644 --- a/Invoice.php +++ b/Invoice.php @@ -2,14 +2,15 @@ namespace yii2mod\cashier; -use DOMPDF; use Carbon\Carbon; +use DOMPDF; use Stripe\Invoice as StripeInvoice; use Yii; use yii\helpers\ArrayHelper; /** * Class Invoice + * * @package yii2mod\cashier */ class Invoice @@ -28,7 +29,6 @@ class Invoice */ protected $invoice; - /** * Create a new invoice instance. * @@ -45,6 +45,7 @@ public function __construct($user, StripeInvoice $invoice) * Get a Carbon date for the invoice. * * @param \DateTimeZone|string $timezone + * * @return \Carbon\Carbon */ public function date($timezone = null) @@ -201,6 +202,7 @@ public function subscriptions() * Get all of the invoie items by a given type. * * @param string $type + * * @return array */ public function invoiceItemsByType($type) @@ -222,6 +224,7 @@ public function invoiceItemsByType($type) * Format the given amount into a string based on the user's preferences. * * @param int $amount + * * @return string */ protected function formatAmount($amount) @@ -233,6 +236,7 @@ protected function formatAmount($amount) * Return invoice html * * @param array $data + * * @return string */ public function renderInvoiceHtml(array $data) @@ -248,6 +252,7 @@ public function renderInvoiceHtml(array $data) * Capture the invoice as a PDF and return the raw bytes. * * @param array $data + * * @return string */ public function pdf(array $data) @@ -260,7 +265,7 @@ public function pdf(array $data) require_once $configPath; } - $dompdf = new DOMPDF; + $dompdf = new DOMPDF(); $dompdf->load_html($this->renderInvoiceHtml($data)); @@ -273,7 +278,9 @@ public function pdf(array $data) * Create an invoice download response. * * @param array $data + * * @return \yii\web\Response + * * @throws \yii\web\HttpException */ public function download(array $data) @@ -308,6 +315,7 @@ public function asStripeInvoice() * Dynamically get values from the Stripe invoice. * * @param string $key + * * @return mixed */ public function __get($key) diff --git a/InvoiceItem.php b/InvoiceItem.php index 15f29f5..72626e8 100644 --- a/InvoiceItem.php +++ b/InvoiceItem.php @@ -1,10 +1,12 @@ quantity = $quantity; + return $this; } @@ -98,11 +101,13 @@ public function quantity($quantity) * Specify the ending date of the trial. * * @param int $trialDays + * * @return $this */ public function trialDays($trialDays) { $this->trialDays = $trialDays; + return $this; } @@ -114,6 +119,7 @@ public function trialDays($trialDays) public function skipTrial() { $this->skipTrial = true; + return $this; } @@ -121,11 +127,13 @@ public function skipTrial() * The coupon to apply to a new subscription. * * @param string $coupon + * * @return $this */ public function withCoupon($coupon) { $this->coupon = $coupon; + return $this; } @@ -133,11 +141,13 @@ public function withCoupon($coupon) * The metadata to apply to a new subscription. * * @param array $metadata + * * @return $this */ public function withMetadata($metadata) { $this->metadata = $metadata; + return $this; } @@ -145,6 +155,7 @@ public function withMetadata($metadata) * Add a new Stripe subscription to the user. * * @param array $options + * * @return SubscriptionModel */ public function add(array $options = []) @@ -157,7 +168,9 @@ public function add(array $options = []) * * @param string|null $token * @param array $options + * * @return SubscriptionModel + * * @throws Exception */ public function create($token = null, array $options = []) @@ -190,6 +203,7 @@ public function create($token = null, array $options = []) * * @param string|null $token * @param array $options + * * @return \Stripe\Customer */ protected function getStripeCustomer($token = null, array $options = []) @@ -204,6 +218,7 @@ protected function getStripeCustomer($token = null, array $options = []) $this->user->updateCard($token); } } + return $customer; } diff --git a/composer.json b/composer.json index 879f8a2..1cd5d20 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,11 @@ "name": "yii2mod/yii2-cashier", "description": "Yii2 Cashier provides an interface to Stripe's subscription billing services.", "type": "yii2-extension", - "keywords": ["yii2", "stripe", "billing"], + "keywords": [ + "yii2", + "stripe", + "billing" + ], "license": "MIT", "authors": [ { @@ -17,6 +21,9 @@ "nesbot/carbon": "~1.0", "dompdf/dompdf": "^0.6.1" }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~1.7" + }, "autoload": { "psr-4": { "yii2mod\\cashier\\": "" diff --git a/controllers/WebhookController.php b/controllers/WebhookController.php index 097a786..d9291a7 100644 --- a/controllers/WebhookController.php +++ b/controllers/WebhookController.php @@ -2,23 +2,24 @@ namespace yii2mod\cashier\controllers; -use yii2mod\cashier\models\SubscriptionModel; use Exception; +use Stripe\Event as StripeEvent; use Yii; use yii\filters\VerbFilter; use yii\helpers\Inflector; use yii\web\Controller; -use Stripe\Event as StripeEvent; use yii\web\Response; +use yii2mod\cashier\models\SubscriptionModel; /** * Class WebhookController + * * @package yii2mod\cashier\controllers */ class WebhookController extends Controller { /** - * @var bool whether to enable CSRF validation for the actions in this controller. + * @var bool whether to enable CSRF validation for the actions in this controller */ public $enableCsrfValidation = false; @@ -32,8 +33,8 @@ public function behaviors() 'class' => VerbFilter::className(), 'actions' => [ 'handle-webhook' => ['post'], - ] - ] + ], + ], ]; } @@ -60,6 +61,7 @@ public function actionHandleWebhook() * Handle a cancelled customer from a Stripe subscription. * * @param array $payload + * * @return Response */ protected function handleCustomerSubscriptionDeleted(array $payload) @@ -77,7 +79,7 @@ protected function handleCustomerSubscriptionDeleted(array $payload) return new Response([ 'statusCode' => 200, - 'statusText' => 'Webhook Handled' + 'statusText' => 'Webhook Handled', ]); } @@ -85,6 +87,7 @@ protected function handleCustomerSubscriptionDeleted(array $payload) * Get the billable entity instance by Stripe ID. * * @param string $stripeId + * * @return null|static */ protected function getUserByStripeId($stripeId) @@ -98,6 +101,7 @@ protected function getUserByStripeId($stripeId) * Verify with Stripe that the event is genuine. * * @param string $id + * * @return bool */ protected function eventExistsOnStripe($id) @@ -117,7 +121,7 @@ protected function eventExistsOnStripe($id) public function missingMethod() { return new Response([ - 'statusCode' => 200 + 'statusCode' => 200, ]); } -} \ No newline at end of file +} diff --git a/migrations/m160511_085953_init.php b/migrations/m160511_085953_init.php index 3f5c614..7520bb2 100644 --- a/migrations/m160511_085953_init.php +++ b/migrations/m160511_085953_init.php @@ -25,7 +25,7 @@ public function up() 'trialEndAt' => $this->timestamp()->null(), 'endAt' => $this->timestamp()->null(), 'createdAt' => $this->timestamp()->null(), - 'updatedAt' => $this->timestamp()->null() + 'updatedAt' => $this->timestamp()->null(), ], $tableOptions); $this->addColumn('user', 'stripeId', $this->string()); diff --git a/models/SubscriptionModel.php b/models/SubscriptionModel.php index 32a7c7d..837deb5 100644 --- a/models/SubscriptionModel.php +++ b/models/SubscriptionModel.php @@ -13,17 +13,16 @@ /** * This is the model class for table "Subscription". * - * @property integer $id - * @property integer $userId + * @property int $id + * @property int $userId * @property string $name * @property string $stripeId * @property string $stripePlan - * @property integer $quantity + * @property int $quantity * @property Carbon $trialEndAt * @property Carbon $endAt - * @property integer $createdAt - * @property integer $updatedAt - * + * @property int $createdAt + * @property int $updatedAt * @property \yii\db\ActiveRecord $user */ class SubscriptionModel extends ActiveRecord @@ -96,13 +95,13 @@ public function behaviors() $currentDateExpression = Yii::$app->db->getDriverName() === 'sqlite' ? "DATETIME('now')" : 'NOW()'; return new Expression($currentDateExpression); - } + }, ], 'carbon' => [ 'class' => CarbonBehavior::className(), 'attributes' => [ 'trialEndAt', - 'endAt' + 'endAt', ], ], ]; @@ -110,6 +109,7 @@ public function behaviors() /** * User relation + * * @return \yii\db\ActiveQuery */ public function getUser() @@ -161,7 +161,6 @@ public function onTrial() } } - /** * Determine if the subscription is within its grace period after cancellation. * @@ -180,11 +179,13 @@ public function onGracePeriod() * Increment the quantity of the subscription. * * @param int $count + * * @return $this */ public function incrementQuantity($count = 1) { $this->updateQuantity($this->quantity + $count); + return $this; } @@ -192,12 +193,14 @@ public function incrementQuantity($count = 1) * Increment the quantity of the subscription, and invoice immediately. * * @param int $count + * * @return $this */ public function incrementAndInvoice($count = 1) { $this->incrementQuantity($count); $this->user->invoice(); + return $this; } @@ -205,11 +208,13 @@ public function incrementAndInvoice($count = 1) * Decrement the quantity of the subscription. * * @param int $count + * * @return $this */ public function decrementQuantity($count = 1) { $this->updateQuantity(max(1, $this->quantity - $count)); + return $this; } @@ -217,6 +222,7 @@ public function decrementQuantity($count = 1) * Update the quantity of the subscription. * * @param int $quantity + * * @return $this */ public function updateQuantity($quantity) @@ -248,6 +254,7 @@ public function noProrate() * Change the billing cycle anchor on a plan change. * * @param int|string $date + * * @return $this */ public function anchorBillingCycleOn($date = 'now') @@ -265,6 +272,7 @@ public function anchorBillingCycleOn($date = 'now') * Swap the subscription to a new Stripe plan. * * @param string $plan + * * @return $this */ public function swap($plan) @@ -351,8 +359,6 @@ public function cancelNow() /** * Mark the subscription as cancelled. - * - * @return void */ public function markAsCancelled() { @@ -397,7 +403,6 @@ public function resume() return $this; } - /** * Get the subscription as a Stripe subscription object. * @@ -407,5 +412,4 @@ public function asStripeSubscription() { return $this->user->asStripeCustomer()->subscriptions->retrieve($this->stripeId); } - -} \ No newline at end of file +} diff --git a/tests/CashierTest.php b/tests/CashierTest.php index 3c49e37..413fc59 100644 --- a/tests/CashierTest.php +++ b/tests/CashierTest.php @@ -118,7 +118,7 @@ public function testCreatingSubscriptionWithCoupons() public function testGenericTrials() { - $user = new User; + $user = new User(); $this->assertFalse($user->onGenericTrial()); $user->trialEndAt = Carbon::tomorrow(); $this->assertTrue($user->onGenericTrial()); @@ -226,4 +226,4 @@ public function testRefunds() // Refund Tests $this->assertEquals(1000, $refund->amount); } -} \ No newline at end of file +} diff --git a/tests/TestCase.php b/tests/TestCase.php index e194d77..ec9da3a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,8 +2,8 @@ namespace yii2mod\cashier\tests; -use yii\helpers\ArrayHelper; use Yii; +use yii\helpers\ArrayHelper; /** * This is the base class for all yii framework unit tests. @@ -26,6 +26,7 @@ protected function tearDown() /** * Populates Yii::$app with a new application * The application will be destroyed on tearDown() automatically. + * * @param array $config The application configuration, if needed * @param string $appClass name of the application class to create */ @@ -53,9 +54,9 @@ protected function mockApplication($config = [], $appClass = '\yii\web\Applicati ], 'params' => [ 'stripe' => [ - 'apiKey' => getenv('STRIPE_SECRET') - ] - ] + 'apiKey' => getenv('STRIPE_SECRET'), + ], + ], ], $config)); } @@ -109,7 +110,7 @@ protected function setupTestDbData() $db->createCommand()->insert('user', [ 'username' => 'John Doe', - 'email' => 'johndoe@domain.com' + 'email' => 'johndoe@domain.com', ])->execute(); } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index effa70f..10cfa10 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -10,4 +10,4 @@ $_SERVER['SCRIPT_FILENAME'] = __FILE__; require_once(__DIR__ . '/../vendor/autoload.php'); -require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); \ No newline at end of file +require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); diff --git a/tests/data/CashierTestControllerStub.php b/tests/data/CashierTestControllerStub.php index 7bc548c..b07f59d 100644 --- a/tests/data/CashierTestControllerStub.php +++ b/tests/data/CashierTestControllerStub.php @@ -6,6 +6,7 @@ /** * Class CashierTestControllerStub + * * @package yii2mod\cashier\tests\data */ class CashierTestControllerStub extends WebhookController @@ -14,4 +15,4 @@ protected function eventExistsOnStripe($id) { return true; } -} \ No newline at end of file +} diff --git a/tests/data/User.php b/tests/data/User.php index 47f94d4..f2445db 100644 --- a/tests/data/User.php +++ b/tests/data/User.php @@ -7,7 +7,7 @@ use yii2mod\cashier\Billable; /** - * @property integer $id + * @property int $id * @property string $username * @property string $email */ @@ -72,4 +72,4 @@ public function validateAuthKey($authKey) { // TODO: Implement validateAuthKey() method. } -} \ No newline at end of file +} diff --git a/views/invoice.php b/views/invoice.php index 83fc0b2..5006c33 100644 --- a/views/invoice.php +++ b/views/invoice.php @@ -1,8 +1,8 @@