stripe-paymentsomnipay

Omnipay Stripe add listPlan limit parameter


The Stripe 'list all plans' api call by default only returns a maximum of 10 plans; There is an optional limit parameter which I wish to set to the value 100 to raise the default limit - Stripe api ref.

I'm using Omnipay-Stripe and I can return 10 plans with the following code:

$response = $this->gateway->listPlans($params)->send();
if ($response->isSuccessful()) {
    $data = $response->getData();
    return $data['data'];
}

Following this SO answer I'm trying to set the limit parameter to 100 with the code below:

$request = $this->gateway->listPlans();
$data = $request->getData();
$data['limit'] = 100;    
$response = $request->sendData($data);
if ($response->isSuccessful()) {
    $data = $response->getData();
    return $data['data'];
}

But this still will not return any more than 10 plans. I have successfully tested stripe-php to prove the limit parameter does work, but no luck yet with omnipay-stripe. Any ideas please?

I've tried adding the following function to omnipay\stripe\src\Message\ListPlansRequest.php

public function setLimit($limit)
{
    return $this->setParameter('limit', $limit);
}

And changed my code to:

$request = $this->gateway->listPlans();
$request->setLimit('100');
$data = $request->getData();
$response = $request->sendData($data);

But still only 10 plans returned and the limit parameter is not logged as received in the Stripe request GET logs.


Solution

  • The Stripe listPlans function is a GET request to https://api.stripe.com/v1/plans so the limit parameter needs to be in a query string like https://api.stripe.com/v1/plans?limit=3

    I was hoping that Omnipay gateway logic, knowing that ListPlansRequest HttpMethod is 'GET', would add any parameters set to a query string in the CURLOPT_URL. So I tried the following code in my project:

    $request = $this->gateway->listPlans(array('limit' => 100))->send();
    

    and

    $request = $this->gateway->listPlans()->initialize(array('setLimit' => 100))->send();
    

    But neither applied the limit parameter.

    So I added the following class methods to ListPlansRequest:

    public function getLimit()
    {
        return $this->getParameter('limit');
    }
    
    public function setLimit($limit)
    {
        return $this->setParameter('limit', $limit);
    }
    

    And extended method get EndPoint:

    public function getEndpoint()
    {
        $query = '';
    
        $limit = (int)$this->getLimit();
    
        if($limit > 0) $query = '?' . http_build_query(array('limit' => $limit));
    
        return $this->endpoint.'/plans' . $query;
    }
    

    And the following code in my project now returns all 21 subscription plans:

    $request = $this->gateway->listPlans(array('limit' => 100))->send();