Search for Resources and Solutions

delete_subscription - delete payments subscriptions

A professional payments processing system requires a lot of time to be created and is important to understand how it will work. When i've added the first payment system, Paypal in Midrub, i've added only the verification of the valid payments to verify if the transactions are correct with expected amount and currency. Also, my old system have verified if the PayPal transaction already exists in the database. A year after i've added recurring payments and my payments system has became much more complex. One reason is to add hooks and verify when user deletes the subscribtions directly from PayPal. Another big problem was in to delete the old subscribtions if user wants to change the plan.

Now, the new payments system is much more complex, because it have to generate even invoices every month if the subscribtion is renewed monthly. But about this i will write in the next article. 

The method delete_subscription in the payments gateway class will delete the subscribtions. How you can use it? For example when user decides to change his plan, we have to delete all user's subscribtions. We don't know which gateway the user has used to subscribe to a plan. But we have the table subscribtions which has the subscription's details:

  • net_id - contains the subscribtion's id.
  • gateway - contains the payments gateway.

In the code above you can see an example how to call the method delete_subscription in all gateways:

                        // We need to delete the previous subscription
                        $subscriptions = $this->CI->base_model->get_data_where('subscriptions', '*', array(
                            'user_id' => $this->CI->user_id,

                        // Verify if old subscribtions exists
                        if ( $subscriptions ) {

                            // List all subscriptions
                            foreach ( $subscriptions as $subscription ) {
                                // Create an array
                                $array = array(

                                // Implode the array above
                                $cl = implode('\\', $array);

                                // Delete subscribtion
                                (new $cl())->delete_subscription($subscription);


                            // Delete the subscription from the database
                            $this->CI->base_model->delete('subscriptions', array(
                                'user_id' => $this->CI->user_id,
                            ) );


Now you have to add the required code inside the method delete_subscription which will delete the subscription.

Was this article helpful?