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 – 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 .

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. to capture events,
update the Your App and send emails to the customer when specific events happen.

Also Read :   DataTables Pagination Server-side Processing using PHP and MySQL

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 (

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()
        Stripe.apiKey = "YOUR apiKey put Key";
        def json = request.JSON
        def custId =
        def yourPlanId =[0]
        long activeUntil =[0].period.end
        def product_space
        def get_subID =
        Customer cust = Customer.retrieve(custId);
        com.stripe.model.Subscription subscription = cust.getSubscriptions().retrieve(get_subID);
            case "Trial_Plan" : product_space = Plan.TRIAL.plan_job_spaces;
            case "Basic_Plan" : product_space = Plan.BASIC.plan_job_spaces;
            case "Unlimited_Plan" : product_space = Plan.UNLIMITED.plan_job_spaces;
        table sub = table.findByCustomer_id(custId)
            sub.active_until = activeUntil
            sub.product_space = Integer.parseInt(product_space)
            sub.subscription_id = get_subID
            sub.plan_id = yourPlanId
        render "DONE"
        catch(Exception e)
            render "Stripe API Unsuccessful"

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 :   PHP prepend leading zero prefixes after number

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 :   PHP and MySqli CRUD Operation Tutorial for Beginners – Step By Step

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

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.


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( readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article( Are Most Always Welcome.