Retrieve a multiple stripe charges in php

Viewed 596

I just want to know if there is way to retrieve charges with multiples charges_id in stripe.

For example in the docs show how to get one charge. But we need get multiple charges. So, we don't want to made a multiple calls to the stripe method retrieve, this is to slow. We dont want to make this:

foreach ($result as $p_key => $payment) {
     $charge = $this->CI->stripe_lib->retrieve_charge('ch_......', 'secret_key');
     if (isset($charge['charge'])) {
         $amount_charged = (float)$charge['charge']->amount / 100;

         // echo "<pre>";
         // print_r($amount_charged );
         // echo "</pre>";

     }
}

this is in Codeigniter. And this is the function on the library:

public function retrieve_charge($charge_id, $secret_key) {

    $errors = array();
    try {

        \Stripe\Stripe::setApiKey($secret_key);
        $charge = \Stripe\Charge::retrieve($charge_id);
        return array('charge' => $charge);

    } catch(Stripe_CardError $e) {
        $errors = array('error' => false, 'message' => 'Card was declined.', 'e' => $e);
    } catch (Stripe_InvalidRequestError $e) {
        $errors = array('error' => false, 'message' => 'Invalid parameters were supplied to Stripe\'s API', 'e' => $e);
    } catch (Stripe_AuthenticationError $e) {
        $errors = array('error' => false, 'message' => 'Authentication with Stripe\'s API failed!', 'e' => $e);
    } catch (Stripe_ApiConnectionError $e) {
        $errors = array('error' => false, 'message' => 'Network communication with Stripe failed', 'e' => $e);
    } catch (Stripe_Error $e) {
        $errors = array('error' => false, 'message' => 'Stripe error. Something wrong just happened!', 'e' => $e);
    } catch (Exception $e) {
        if (isset($e->jsonBody['error']['type']) && $e->jsonBody['error']['type'] == 'idempotency_error') {
            $errors = array('error' => false, 'message' => $e->getMessage(), 'e' => $e, 'type' => 'idempotency_error');
        } else {
            $errors = array('error' => false, 'message' => 'An error has occurred getting customer info.', 'e' => $e);
        }
    }

    return $errors;
}

With this code: \Stripe\Charge::all(["limit" => 3]); returns all charge but in the docs I didn't see if this method returns me also a multiple charges id.

I appreciate all your help.

Thanks and I'm sorry for my english.

1 Answers

Thanks for your question. It seems you have already identified the right method to retrieve multiple charges using the PHP library!

You are correct in that \Stripe\Charge::all(["limit" => 3]) call [0] will return you multiple charges, up to the limit specified in the arguments [1].

In the response to the above request, you will receive an array of charge objects [2], each having an id field [3] that would be the charge ID.

Hope that helps! Please let me know if you have any questions.

Cheers,

Heath

[0] https://stripe.com/docs/api/charges/list?lang=php

[1] https://stripe.com/docs/api/charges/list?lang=php#list_charges-limit

[2] https://stripe.com/docs/api/charges/object?lang=php

[3] https://stripe.com/docs/api/charges/object?lang=php#charge_object-id

Related