Skip to content
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

[1.6] KTC-131 Subscriptions with Stripe #1973

Merged
merged 71 commits into from
Sep 23, 2024
Merged

[1.6] KTC-131 Subscriptions with Stripe #1973

merged 71 commits into from
Sep 23, 2024

Conversation

Keysie27
Copy link
Collaborator

@Keysie27 Keysie27 commented Sep 12, 2024

General:

Management of plans, prices, subscriptions, and items using Stripe. The GraphQL mutations allow creating, updating, and deleting these resources both in Stripe and the database.

Tasks:

  • Mutations for creating, updating, and deleting entities.
  • Interaction with Stripe.
  • Unit tests.
  • Optimize the flow by delegating to Stripe using webhooks with Laravel Cashier.
  • Database restructuring: create a subscriptions database, a prices table, and a stripe_customers table.
  • Manage notifications.
  • Handle errors and exceptions (observers).
  • Define additional queries and mutations for the frontend.
  • Refactor to optimize the mutations.

Requirements:

  • Select Plan: Handled by the createSubscription mutation, where companies can select a plan and its associated items (prices).
  • Change Plan: Handled by the updateSubscription mutation, allowing users to switch plans and update subscription items.
  • Cancel Subscription: Handled by the deleteSubscription mutation, which cancels the subscription in Stripe and updates the database.
  • Expiration and Free Trial to Paid Plan: Managed by Stripe's billing cycles. Free trial periods are set using the trial_days parameter in the createSubscription mutation. Stripe automatically transitions the subscription to a paid plan after the trial ends. (Pending, for using webhooks)

Considerations:

  • Stripe subscriptions can have multiple plans, and a plan can have multiple prices.
  • Stripe customers must be created before making a subscription.
  • Each SubscriptionItem in Stripe can only have one price_id.
  • Payment intervals, currency, and amounts are managed in prices in Stripe.
  • A subscription cannot have prices with different payment intervals in Stripe.
  • The frontend can use Stripe.js or Stripe Elements to capture payment details and generate the payment_method_id. This delegates payment handling to Stripe, removing the need for a local payment_methods_id table, and instead using a payment_methods table with the stripe_id.
  • Stripe does not allow updating certain attributes like currency and interval for existing prices via the API; these can only be updated through the Stripe dashboard.

@Keysie27 Keysie27 marked this pull request as draft September 12, 2024 22:14
@kaioken kaioken changed the title KTC-131 Subscriptions with Stripe [1.5] KTC-131 Subscriptions with Stripe Sep 12, 2024
@kaioken kaioken changed the title [1.5] KTC-131 Subscriptions with Stripe [1.6] KTC-131 Subscriptions with Stripe Sep 18, 2024
app/GraphQL/Subscription/Mutations/Plans/PlanMutation.php Outdated Show resolved Hide resolved
app/GraphQL/Subscription/Mutations/Plans/PlanMutation.php Outdated Show resolved Hide resolved
database/seeders/AppSettingsSeeder.php Outdated Show resolved Hide resolved
graphql/schemas/Subscription/companySubscription.graphql Outdated Show resolved Hide resolved
src/Baka/Traits/KanvasModelTrait.php Outdated Show resolved Hide resolved
src/Domains/Subscription/Models/BaseModel.php Outdated Show resolved Hide resolved
public function create(array $req): PriceModel
{
$app = app(Apps::class);
$stripeProduct = StripeProduct::create([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if Stripe has validation for not create duplicate records?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each product or price has a different ID in Stripe even if it has the same name and detail. That's why en is linked by the Stripe price_id.

return new Plan();
}

public static function getByIdWithApp(int $id, ?AppInterface $app = null): Model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this functionality in the base model Check this file

public static function getByIdWithApp(int $id, AppInterface $app): Price
{
try {
return self::getModel()::notDeleted()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do it different
$price = Kanvas\Subscription\Prices\Models\Price::getById($id, $app);
$plan = $price->plan;

In Laravel , when you use eloquent queries, all relationships are retrieved in the result, in this case plan, and you can access to it with maggic attribute plan

Also our models has default relationships

  • user
  • company
  • app

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using companies subscriptions to apps, you can have prices/plans without a relationship with a company and user, because plans/prices belong to apps, companies can subscribe to apps, so you can have plans/prices that companies are not using so not relationship

$prices = [
[
'apps_plans_id' => 1,
'stripe_id' => 'price_1Q11XeBwyV21ueMMd6yZ4Tl5',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This id, I believe that is move to env, @kaioken can be add this id to the test env

Copy link
Contributor

@FredPeal FredPeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaioken @Keysie27 What do you think about create a workflow diagram about Subscription, for doc the integration with front, back and stripe

Copy link
Contributor

@FredPeal FredPeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, good job 😊😁

@kaioken kaioken marked this pull request as ready for review September 23, 2024 13:09
@kaioken kaioken merged commit e2b3eab into development Sep 23, 2024
6 checks passed
@kaioken kaioken deleted the KTC-131 branch September 23, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants