CGSmith-LLC / shipwise-api

Shipwise API
Other
0 stars 0 forks source link

Stripe Billing and Invoicing [FEATURE] #174

Open cgsmith opened 1 year ago

cgsmith commented 1 year ago

Is your feature request related to a problem? Please describe. Billing right now is partly done through Stripe and manually done through Quickbooks. I want to use Stripe for everything billing out of Shipwise. The current integration is fairly broken and just adds credit cards. It also handles the invoicing but doesn't enroll them in the subscription.

Describe the solution you'd like No migrations are necessary as I will be informing the customers of the new subscriptions and terms. They will then choose a plan and be subscribed.

I created a subscription for testing in Stripe under the test mode. Test subscription

  1. A user will need a subscription to access Shipwise after registration. Related to this #78
  2. If a subscription is cancelled. Shipwise will need to have an endpoint to listen for this and then stop any active integrations
  3. Create usage records on a scheduled basis reference Subscription lifecycle

Additional context Useful documentation:

Stripe Subscription Documentation Stripe Customer Portals

Loom explanation for @bohdan-vorona Video

bohdan-vorona commented 1 year ago

@cgsmith Pushed some latest changes - https://github.com/CGSmith-LLC/shipwise-api/compare/master...feature/stripe-billing-and-invoicing Docs are updated. // FYI

I'm going to finish the webhooks implementation and the {payment/subscription management} via the customer portal.


I added logic for working with Stripe webhooks, but, since we already have similar logic for Shopify, it would be great to combine some parts. I've created https://github.com/CGSmith-LLC/shipwise-api/issues/177 for this. Once, these two branches are merged, I will implement this.

bohdan-vorona commented 1 year ago

Working on:

bohdan-vorona commented 1 year ago

@cgsmith

Working on:

curr-subs

cgsmith commented 1 year ago

Thanks @bohdan-vorona let me review what you currently have. I think you might be adding more of a direct integration instead of leveraging their customer portal.

No problem if you did!

bohdan-vorona commented 1 year ago

Check - Stripe (webhooks + Dashboard):

Check - website:

bohdan-vorona commented 1 year ago

@cgsmith In general, I've finished the main part of Stripe integration. Implemented the following webhooks: _customer.deleted, checkout.session.completed, customer.subscription.created, customer.subscription.trial_willend, customer.subscription.deleted, customer.subscription.updated.

I'm working on usage-based measurement.

Some moments I need help with:

  1. For the moment, to get the current customer I use this mock code:
$customer = Customer::findOne(1); // I find the first customer
$subscriptionService = new SubscriptionService($customer);

How shall I determine the current customer? As I see, in the User model we have such code:

    public function getCustomerId()
    {
        return $this->customer_id;
    }

But also, as I see, one user can have several customers:

    public function getCustomerIds()
    {
        return ArrayHelper::getColumn(
            $this->hasMany('frontend\models\UserCustomer', ['user_id' => 'id'])
                ->select('customer_id')
                ->asArray()
                ->all(),
            'customer_id');
    }

So, shall I use the field customer_id from the user table or ...?

  1. There was old billing logic I used:
        if (!Yii::$app->session->has($module->switchIdentitySessionKey) &&
            !Yii::$app->user->identity->isAdmin &&
            Yii::$app->user->identity->isDirectCustomer()) {
                // code...
        }

I left all these checks for my logic too. Is it correct? It means that I will check the current active subscription only if it's not an admin and is a direct customer and it's not switching between users.

  1. Please have a look at the screenshots below. Let me know if you want something to change or add.

I can propose not showing our header and footer when the user sees the pricing widget. Just add a neat button Back + Logo or something like that.


Menu:

stripe-menu

Subscription page:

stripe-subscribe

Active trial:

stripe-trial

Active subscription:

stripe-active

Thx for subscribing:

stripe-thx

Redirect if no active subscription:

stripe-redirect

bohdan-vorona commented 1 year ago

Stripe qty usage:

stripe-usage