Stripe API Webhook upgrade and downgrade Subscriptions

Stripe API Webhook upgrade and downgrade Subscriptions

In this Post We Will Explain About is With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Proration Upgrade and Downgrade Subscriptions using StripeExample

In this post we will show you Best way to implement Stripe subscription upgrading and downgrading, hear for How to downgrade/upgrade plan in Stripe payments using API in PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Webhook – Upgrading and Downgrading Plans – Stripe

In previous Post on stripe create Accounts, we wrote about the simple step by step and customised implementation as well as also subscribing a user select with a plan. In this POST, we will talk about the your Site will need to interact with Stripe Webhooks – here stripe api webhook of the stripe which is generally hit a simple URL given in the offical documents https://dashboard.stripe.com/account/webhooks .

There are lots of the events which cab be attached to lots of the URLs and on the clicked of the event the URL will be called.
https://stripe.com/docs/subscriptions/webhooks to capture events,
update the Your App and send emails to the customer when specific events happen.

Also Read :   VueJs Autocomplete using Laravel Example

The events are like:

  • simple customer.created
  • customer.deleted
  • payment.created
  • payment.failed
  • payment.paid

We can also set the settings envirnment of the URL set. as well as It can be test or live mode to settings environment and is based on the keys simple private and public mode like as a (test/live) we are using. For example:

We Are using the event invoice. with payment_succeeded and using the simple URL (http://live24u.com/payment).

Below source code is the action which is called simple with the step by step trigger of the simple URL.

<span style="font-size:16px;">def payment()
    {
        try{
        Stripe.apiKey = "YOUR apiKey put Key";
        def json = request.JSON
        def custId = json.data.object.customer
        def yourPlanId =  json.data.object.lines.data[0].plan.id
        long activeUntil = json.data.object.lines.data[0].period.end
        def product_space
        def get_subID = json.data.object.subscription
        Customer cust = Customer.retrieve(custId);
        com.stripe.model.Subscription subscription = cust.getSubscriptions().retrieve(get_subID);
        switch(yourPlanId){
            case "Trial_Plan" : product_space = Plan.TRIAL.plan_job_spaces;
                break;
            case "Basic_Plan" : product_space = Plan.BASIC.plan_job_spaces;
                break;
            case "Unlimited_Plan" : product_space = Plan.UNLIMITED.plan_job_spaces;
                break;
        }
        table sub = table.findByCustomer_id(custId)
        if(sub){
            sub.active_until = activeUntil
            sub.product_space = Integer.parseInt(product_space)
            sub.subscription_id = get_subID
            sub.plan_id = yourPlanId
            sub.save(flush:true)
        }
        render "DONE"
        }
        catch(Exception e)
        {
            render "Stripe API Unsuccessful"
        }
    }
</span>

How to downgrade-upgrade plan in Stripe payments using API in PHP

In the above simple stripe API Source code,We are getting the simple some Information Like as a customer id,subscription id, end of period as well as other information from the subscription object to save all the information it in the table on server as well as database so as to inform the all customer.

Also Read :   Dynamically Add Remove multiple input fields in PHP MYSQL with Jquery Ajax

Upgrade/Downgrade subscription

We can do the your current pans upgrade plan as well as downgrade plan from the active or existing customer simple subscription.
For an Examples like as a,

  • we have 3 plans Like as (trial plan, gold plan, platinum plan) attached all the to my simple stripe account on stripe and selected each plan renewed some days means after 30 days.
  • So Example First of all,
  • A create new customer is subscribed to the simple gold plan than simple that would be of like as a $20 then after gere 5 days like that select plan..
  • here we want to stripe api upgrade plan to platinum plan so that would be of like as a $80.
  • and Then we will some charge him for Like as a 5 days according simple example to select the gold plan and remaining simple as a 25 days according to the got new platinum plan for stripe api.

This type of stripe api charge simple policy is called prorate.

To upgrade the subscription,

  • we first of all need to get all the customer for which we need to some change the subscription.
  • And Then get all the existing in panel subscription object.
  • And Then we can simple trigger the call a update subsctiption API call.
  • Below is the source code for Stripe api using upgrade and downgrade plans.
Also Read :   Simple Ajax Request Example with JQuery and PHP

<span style="font-size:16px;">try{
    Stripe.apiKey = "YOUR Stripe Key";
    def accesstoken = params.stripeToken;
    Customer cust = Customer.retrieve(s.customer_id);
         
    //Created By Live24u.com : update subscription for simple customer
    com.stripe.model.Subscription subscription = cust.getSubscriptions().retrieve(s.subscription_id);
    log.debug("old plan: " + subscription.plan.name)
     
    //Created By Live24u.com : attach card to the simple customer retrieved from Stripe
    Map<String, Object> updateParamsCard = new HashMap<String, Object>();
    updateParamsCard.put("card", accesstoken);
    cust.update(updateParamsCard);
     
    //Created By Live24u.com : update subscription for simple customer.
    Map<String, Object> updateParams = new HashMap<String, Object>();
    updateParams.put("plan", session.getAttribute("plan_id").toString());
    //Created By Live24u.com : update subscription to simple new plan
    def changeupgradessubresults = subscription.update(updateParams);
     
    log.debug("changeupgradessubresults " + changeupgradessubresults)
    log.debug("changeupgradessubresults.id " + changeupgradessubresults.id)
    log.debug("changeupgradessubresults.plan.name " + changeupgradessubresults.plan.name)
     
    //Created By Live24u.com : update subscription
    updateSubscription(s.customer_id, changeupgradessubresults.id, changeupgradessubresults.plan.name, changeupgradessubresults.currentPeriodEnd, product_space)
}
catch(com.stripe.exception.CardException e)
{
    flash.message = "Stripe API card Error occured"
    e.printStackTrace()
}
catch(Exception e){
  flash.message = "Stripe API Error occuered"
  e.printStackTrace()
}
</span>

By IN this way, we could use simple prorate functionality of using stripe webhook API as it is a default calculation functionality but we could create it simple false by setting the config “prorate: false” some set property while your updating the old subscription.

Example

I hope you have Got What is Stripe event call using webhook and upgrade and downgrade of subscriptionAnd how it works.I would Like to have FeadBack From My Blog(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.