Search code examples
phpstripe-paymentscodeigniter-3

How to delete a Customer with Stripe API PHP


I have been integrating Stripe into my platform and have everything working. I can create Connected Accounts, Customers, Products, Prices create and update Subscriptions, it all works. The last thing I need to do is allow Customers the ability to delete their accounts. Here is my code:

public function remove_member()  //DELETES MEMBER ACCOUNT
        {  
            $this->api_error = '';
            $this->CI =& get_instance();
            $this->CI->load->config('stripe');
            require APPPATH .'third_party/stripe-php/init.php'; // Include the Stripe PHP bindings library
            \Stripe\Stripe::setApiKey($this->CI->config->item('stripe_api_key')); // Set API key
                        
            $customer = \Stripe\Customer::delete(
                'stripeCustomerID',
                []); 
        }

However it throws this error: "You must pass an array as the first argument to Stripe API method calls. "

But I believe I am following the Stripe Docs...here: https://stripe.com/docs/api/customers/delete?lang=php

Any suggestions?


Solution

  • For stripe-php versions prior to 7.33, you have to retrieve first and then call delete like this:

    $customer = \Stripe\Customer::retrieve('cus_xxx');
    $customer->delete();
    

    Since that makes an extra request, Stripe introduced a new client/service approved in 7.33.0 documented here which makes things easier as you can call delete directly as documented in the API reference here:

    $stripe = new \Stripe\StripeClient(
      'sk_test_...'
    );
    $stripe->customers->delete(
      'cus_...',
      []
    );