-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stripe billing and invoicing #180
Open
bohdan-vorona
wants to merge
64
commits into
master
Choose a base branch
from
feature/stripe-billing-and-invoicing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
bd6b374
Removed old billing logic
bohdan-vorona 85b4c2e
Removed old Stripe component
bohdan-vorona e031cb7
Stripe integration initial docs, pricing widget
bohdan-vorona c42dcc5
Stripe webhooks, updated docs, updated SubscriptionService, added cus…
bohdan-vorona 43facce
Updated docs, updated webhooks, signature verification, added `checko…
bohdan-vorona 4340254
Subscription history, removed old stripe values, updated BaseCustomer…
bohdan-vorona 994d52d
Mock StripeCustomerSubscriptionDeletedJob, thank you page, invoice pa…
bohdan-vorona 6cd77c5
Upgraded Stripe jobs
bohdan-vorona ba98136
SubscriptionService upgrade
bohdan-vorona 7efd6c3
implemented StripeCustomerDeletedJob, implemented StripeCustomerSubsc…
bohdan-vorona 9316c84
Table and class rename, mock StripeCustomerSubscriptionTrialWillEndJo…
bohdan-vorona bf62e55
Subscribed page upgrade
bohdan-vorona dd8ff0d
Fields change
bohdan-vorona 0e321e2
Cron -> pastDueSubscriptions()
bohdan-vorona 6b96ca1
Stripe webhooks errors serialization update
bohdan-vorona 13783a9
StripeCustomerSubscriptionCreatedJob
bohdan-vorona 9ed299c
Stripe draft invoice fix
bohdan-vorona 1002fc5
Check user/customer subscription
bohdan-vorona a28dd6b
Removed unused webhooks
bohdan-vorona a9807a4
Updated docs
bohdan-vorona c7ac54a
Stripe qty usage
bohdan-vorona 3b6a46b
Docs updated.
bohdan-vorona 5e4bcce
Removed old billing logic
bohdan-vorona c8bce05
Removed old Stripe component
bohdan-vorona d28d89b
Stripe integration initial docs, pricing widget
bohdan-vorona a7b406f
Stripe webhooks, updated docs, updated SubscriptionService, added cus…
bohdan-vorona 7e7b30d
Updated docs, updated webhooks, signature verification, added `checko…
bohdan-vorona 3f00535
Subscription history, removed old stripe values, updated BaseCustomer…
bohdan-vorona a47cf90
Mock StripeCustomerSubscriptionDeletedJob, thank you page, invoice pa…
bohdan-vorona 4df98f6
Upgraded Stripe jobs
bohdan-vorona 9b1f653
SubscriptionService upgrade
bohdan-vorona 18dc1d5
implemented StripeCustomerDeletedJob, implemented StripeCustomerSubsc…
bohdan-vorona 5793b77
Table and class rename, mock StripeCustomerSubscriptionTrialWillEndJo…
bohdan-vorona b265afe
Subscribed page upgrade
bohdan-vorona 4988688
Fields change
bohdan-vorona 8103376
Cron -> pastDueSubscriptions()
bohdan-vorona 3159812
Stripe webhooks errors serialization update
bohdan-vorona 04964c2
StripeCustomerSubscriptionCreatedJob
bohdan-vorona 9a52c2b
Stripe draft invoice fix
bohdan-vorona a3c79da
Check user/customer subscription
bohdan-vorona 019b1e9
Removed unused webhooks
bohdan-vorona befb922
Updated docs
bohdan-vorona 4765fff
Stripe qty usage
bohdan-vorona 314b010
Docs updated.
bohdan-vorona f9da180
Merge branch 'feature/stripe-billing-and-invoicing' of https://github…
bohdan-vorona 7e796e8
composer update
bohdan-vorona d9125e1
Update common/models/Subscription.php
bohdan-vorona 49383d7
Update common/models/Subscription.php
bohdan-vorona 589e3ad
Update common/models/Subscription.php
bohdan-vorona 062f011
Update common/models/query/SubscriptionQuery.php
bohdan-vorona 78afb0f
Update common/models/query/SubscriptionQuery.php
bohdan-vorona aac0fbb
Update common/models/query/SubscriptionQuery.php
bohdan-vorona e0015c6
Update console/jobs/subscription/stripe/StripeCustomerSubscriptionCre…
bohdan-vorona 71dab96
Update common/models/query/SubscriptionQuery.php
bohdan-vorona 8d11ca3
Update console/jobs/subscription/stripe/StripeCheckoutSessionComplete…
bohdan-vorona d721bb0
Update console/jobs/subscription/stripe/StripeCheckoutSessionComplete…
bohdan-vorona 2c659b7
Update common/models/Subscription.php
bohdan-vorona 4da968e
Changes
bohdan-vorona 3a513fa
Merge branch 'feature/stripe-billing-and-invoicing' of https://github…
bohdan-vorona 3f21a1c
Stripe component + some changes after testing
bohdan-vorona 5c971b3
Stripe component small update
bohdan-vorona 8227e4d
Stripe component update
bohdan-vorona e696a17
Stripe component revert back latest changes
bohdan-vorona d693ac0
update to make file
cgsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Stripe webhooks, updated docs, updated SubscriptionService, added cus…
…tomer portal URL
- Loading branch information
commit a7b406fca3c6235cb6a28930c46771130b1c2d7c
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace common\models; | ||
|
||
use common\models\base\BaseSubscriptionWebhook; | ||
|
||
class SubscriptionWebhook extends BaseSubscriptionWebhook | ||
{ | ||
public const PAYMENT_METHOD_STRIPE = 'stripe'; | ||
|
||
public const STATUS_RECEIVED = 'received'; | ||
public const STATUS_PROCESSING = 'processing'; | ||
public const STATUS_SUCCESS = 'success'; | ||
public const STATUS_FAILED = 'failed'; | ||
|
||
############# | ||
# Statuses: # | ||
############# | ||
|
||
public static function getStatuses(): array | ||
{ | ||
return [ | ||
self::STATUS_RECEIVED => 'Received', | ||
self::STATUS_PROCESSING => 'Processing', | ||
self::STATUS_SUCCESS => 'Success', | ||
self::STATUS_FAILED => 'Failed', | ||
]; | ||
} | ||
|
||
public function isReceived(): bool | ||
{ | ||
return $this->status === self::STATUS_RECEIVED; | ||
} | ||
|
||
public function isProcessing(): bool | ||
{ | ||
return $this->status === self::STATUS_PROCESSING; | ||
} | ||
|
||
public function isSuccess(): bool | ||
{ | ||
return $this->status === self::STATUS_SUCCESS; | ||
} | ||
|
||
public function isFailed(): bool | ||
{ | ||
return $this->status === self::STATUS_FAILED; | ||
} | ||
|
||
public function setReceived(bool $withSave = true): void | ||
{ | ||
$this->status = self::STATUS_RECEIVED; | ||
|
||
if ($withSave) { | ||
$this->save(); | ||
} | ||
} | ||
|
||
public function setProcessing(bool $withSave = true): void | ||
{ | ||
$this->status = self::STATUS_PROCESSING; | ||
|
||
if ($withSave) { | ||
$this->save(); | ||
} | ||
} | ||
|
||
public function setSuccess(bool $withSave = true): void | ||
{ | ||
$this->status = self::STATUS_SUCCESS; | ||
|
||
if ($withSave) { | ||
$this->save(); | ||
} | ||
} | ||
|
||
public function setFailed(bool $withSave = true): void | ||
{ | ||
$this->status = self::STATUS_FAILED; | ||
|
||
if ($withSave) { | ||
$this->save(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace common\models\base; | ||
|
||
use Yii; | ||
use yii\db\ActiveRecord; | ||
|
||
/** | ||
* This is the model class for table "subscription_webhook". | ||
* | ||
* @property int $id | ||
* @property string $payment_method | ||
* @property string $status | ||
* @property string $event | ||
* @property string $payload | ||
* @property string $meta | ||
* @property string $created_date | ||
* @property string $updated_date | ||
*/ | ||
class BaseSubscriptionWebhook extends ActiveRecord | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function tableName(): string | ||
{ | ||
return 'subscription_webhook'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
[['payment_method', 'status', 'event', 'payload'], 'required'], | ||
[['payload', 'meta'], 'string'], | ||
[['created_date', 'updated_date'], 'safe'], | ||
[['payment_method', 'status', 'event'], 'string', 'max' => 64], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function attributeLabels(): array | ||
{ | ||
return [ | ||
'id' => 'ID', | ||
'payment_method' => 'Payment Method', | ||
'status' => 'Status', | ||
'event' => 'Event', | ||
'payload' => 'Payload', | ||
'meta' => 'Meta', | ||
'created_date' => 'Created Date', | ||
'updated_date' => 'Updated Date', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
console/migrations/m230401_140923_stripe_webhooks_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
use yii\db\Migration; | ||
|
||
/** | ||
* Class m230401_140923_stripe_webhooks_table | ||
*/ | ||
class m230401_140923_stripe_webhooks_table extends Migration | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function safeUp() | ||
{ | ||
$this->execute(" | ||
CREATE TABLE `subscription_webhook` ( | ||
`id` int NOT NULL AUTO_INCREMENT, | ||
`payment_method` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`status` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`event` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`payload` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, | ||
`meta` MEDIUMTEXT NULL DEFAULT NULL, | ||
`created_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
`updated_date` DATETIME NULL DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; | ||
"); | ||
|
||
$this->execute(" | ||
ALTER TABLE `subscription_webhook` | ||
CHANGE `updated_date` `updated_date` DATETIME on update CURRENT_TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP; | ||
"); | ||
|
||
$this->execute(" | ||
ALTER TABLE `subscription_webhook` ADD INDEX(`status`); | ||
"); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function safeDown() | ||
{ | ||
$this->dropTable("{{%subscription_webhook}}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace frontend\controllers; | ||
|
||
use Yii; | ||
use yii\web\{Response, BadRequestHttpException, ServerErrorHttpException}; | ||
use yii\helpers\Json; | ||
use common\models\SubscriptionWebhook; | ||
|
||
/** | ||
* Class SubscriptionWebhookController | ||
* @package frontend\controllers | ||
*/ | ||
class SubscriptionWebhookController extends Controller | ||
{ | ||
/** | ||
* @throws BadRequestHttpException | ||
*/ | ||
public function beforeAction($action): bool | ||
{ | ||
$this->enableCsrfValidation = false; | ||
Yii::$app->response->format = Response::FORMAT_JSON; | ||
return parent::beforeAction($action); | ||
} | ||
|
||
/** | ||
* @throws ServerErrorHttpException | ||
*/ | ||
public function actionStripe(): array | ||
{ | ||
//file_put_contents('stripe.txt', Yii::$app->request->rawBody); | ||
|
||
if (!Yii::$app->request->rawBody) { | ||
throw new ServerErrorHttpException('No request body.'); | ||
} | ||
|
||
$data = Json::decode(Yii::$app->request->rawBody); | ||
|
||
if (!isset($data['type'])) { | ||
throw new ServerErrorHttpException('Event type is not valid.'); | ||
} | ||
|
||
$subscriptionWebhook = $this->getNewSubscriptionWebhookObject(SubscriptionWebhook::PAYMENT_METHOD_STRIPE); | ||
$subscriptionWebhook->event = $data['type']; | ||
$subscriptionWebhook->payload = Yii::$app->request->rawBody; | ||
|
||
if (!$subscriptionWebhook->save()) { | ||
throw new ServerErrorHttpException('Event not saved.'); | ||
} | ||
|
||
return [ | ||
'status' => 200, | ||
'result' => 'success', | ||
]; | ||
} | ||
|
||
protected function getNewSubscriptionWebhookObject(string $paymentMethodName): SubscriptionWebhook | ||
{ | ||
$subscriptionWebhook = new SubscriptionWebhook(); | ||
$subscriptionWebhook->payment_method = $paymentMethodName; | ||
$subscriptionWebhook->status = SubscriptionWebhook::STATUS_RECEIVED; | ||
|
||
return $subscriptionWebhook; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
<?php | ||
use yii\web\View; | ||
use common\services\subscription\SubscriptionService; | ||
|
||
/* @var $this View */ | ||
/* @var $subscriptionService SubscriptionService */ | ||
?> | ||
|
||
<script async src="https://js.stripe.com/v3/pricing-table.js"></script> | ||
<stripe-pricing-table pricing-table-id="<?= Yii::$app->params['stripe']['pricing_table_id'] ?>" | ||
publishable-key="<?= Yii::$app->params['stripe']['publishable_key'] ?>"> | ||
|
||
<stripe-pricing-table | ||
pricing-table-id="<?= Yii::$app->params['stripe']['pricing_table_id'] ?>" | ||
samdark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
publishable-key="<?= Yii::$app->params['stripe']['publishable_key'] ?>" | ||
client-reference-id="<?= $subscriptionService->getCustomer()->id ?>" | ||
customer-email="<?= $subscriptionService->getCustomer()->email ?>" | ||
samdark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</stripe-pricing-table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
use yii\web\View; | ||
use common\services\subscription\SubscriptionService; | ||
|
||
/* @var $this View */ | ||
/* @var $subscriptionService SubscriptionService */ | ||
?> | ||
|
||
<h2>Your current subscription:</h2> | ||
|
||
<a href="<?= Yii::$app->params['stripe']['customer_portal_url'] ?>" target="_blank">Manage subscription</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,23 @@ | ||
<script async src="https://js.stripe.com/v3/pricing-table.js"></script> | ||
<stripe-pricing-table pricing-table-id="prctbl_1MmNdnAVtR6J0zbouXceXi2G" | ||
publishable-key="pk_test_UdowAZ9Ju8kv7WWAHoG4UzbT"> | ||
</stripe-pricing-table> | ||
<?php | ||
use yii\web\View; | ||
use common\services\subscription\SubscriptionService; | ||
|
||
/* @var $this View */ | ||
/* @var $subscriptionService SubscriptionService */ | ||
|
||
$title = 'Subscription'; | ||
$this->params['breadcrumbs'][] = $title; | ||
$this->title = $title . ' - ' . Yii::$app->name; | ||
?> | ||
|
||
<div> | ||
<?php if (!$subscriptionService->hasActiveSubscription()) { ?> | ||
<?= $this->render('_stripe-widget', [ | ||
'subscriptionService' => $subscriptionService, | ||
]) ?> | ||
<?php } else { ?> | ||
<?= $this->render('_subscribed', [ | ||
'subscriptionService' => $subscriptionService, | ||
]) ?> | ||
<?php } ?> | ||
</div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a great practice to configure service like this. Previous service definition was way better. Also, why re-creating it with
new
? It can be a dependency, can't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you like to move to a component only logic related to Stripe itself (like it was previously) and leave my
SubscriptionService
? Or, to moveSubscriptionService
completely (like a separate component - subscription)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stripe itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samdark Done.