4
votes

I am trying to integrate the Paypal recurring payments for my mobile app. So far I managed to implement Paypal payments on various PHP apps using using https://github.com/paypal/PayPal-PHP-SDK, but this is the first time I am implementing recurring payments

I am trying to build the payment for the billing plan using the following code:

        $paymentDefinition = new PaymentDefinition();
        $paymentDefinition->setName('Mobile App subscription')
            ->setType('REGULAR')
            ->setFrequency('Month')
            ->setFrequencyInterval("1")
            ->setCycles("1")
            ->setAmount(
                new Currency(
                    array(
                        'value' => 50, 
                        'currency' => 'USD'
                    )
                )
            );

From the Paypal documentation, I understood that "setCycles" should be set to 0 for unlimited subscriptions. Setting it to 0 using the PHP SDK returns a 400 error.

Everything looks fine and I am receiving the first payment, but I am not sure that setting the Cycle to "1" will do the job I am looking for.

4

4 Answers

5
votes

I had the same problem, my solution was to just remove the setCylcles altogther, see here an example of how the plan and payment definition classes should look:

$plan = new Plan();
    $plan->setName('Some example name')
        ->setDescription('A short description of the plan')
        ->setType('INFINITE');

$paymentDefinition = new PaymentDefinition();
    $paymentDefinition->setName('Regular Payments')
        ->setType('REGULAR')
        ->setFrequency('Month')
        ->setFrequencyInterval("1")
        ->setAmount(new Currency(array('value' => 100, 'currency' => 'USD')));enter code here
4
votes

Just found the solution:

https://developer.paypal.com/docs/api/#paymentdefinition-object

Number of cycles in this payment definition. For INFINITE type plans, cycles should be set to 0 for a REGULAR type payment_definition. Required.

so, basically you must set the type of the plan to "INFINITE" in order to set cycles to 0.

Thank you.

0
votes
0
votes

I'm having a similar issue, following https://developer.paypal.com/docs/subscriptions/integrate/#3-create-a-plan

I copied the CURL example they provide, added my token and all that. I ran it the first time with the JSON /billing_cycles/1/total_cycles set to 12. This seemed to work. But then I realized I couldn't figure out how to specify the plan as infinite. So I tried again removing the total_cycles, or setting it to a value of 0, in both cases I get the following error:

{
    "name": "INVALID_REQUEST",
    "message": "Request is not well-formed, syntactically incorrect, or violates schema.",
    "debug_id": "4ae9c3405d9b",
    "details": [{
        "field": "/billing_cycles/1/pricing_scheme",
        "location": "body",
        "issue": "MALFORMED_REQUEST_JSON",
        "description": "The request JSON is not well formed."
    }],
    "links": []
}

The docs at that URL only say the following about finite vs infinite plans:

You can create either a finite plan or an infinite plan:

Finite Plan - A plan with a fixed number of cycles. Once a buyer subscribes, a finite plan expires after the total number of cycles defined for are completed. For example, a buyer can subscribe to an online tutorial service for 3 cycles.

Infinite Plan - A plan without a fixed number of cycles. Once a buyer subscribes, an infinite plan remains active until cancelation. For example, a buyer can subscribe to a video or music streaming service with no specified end date.

So it would seem that not specifying a number of cycles probably should be the right way to go but clearly this isn't working. I wish their docs were clearer.