Nedelin Yordanov
Nedelin Yordanov

Reputation: 149

Paypal Rest API activate billing plan

I am trying to activate a billing plan that is previously created and I get the malformed json error.

Activate endpoint:

"/v1/payments/billing-plans/{plan-id}"

Request body:

[

   {

      "path": "",

      "value": {

         "state": "ACTIVE"

      },

      "op": "replace"

   }

]

In PHP:

$payLoad = json_encode(array(array(
    'path' => "",
    'value' => array(
        'state' => 'ACTIVE'
    ),
    'op' => 'replace'

)));

And the response I get:

{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request"....}

Edit: I have also tried path to have a value of "/"

"path": "\/",

but to no avail.

Upvotes: 0

Views: 1687

Answers (3)

Kirsy92
Kirsy92

Reputation: 51

I know it's kind of late but this worked on postman with PATCH request (instead of POST)

[
    {
        "path": "/",
        "value": {
            "state": "ACTIVE"
        },
       "op": "replace"
    }
]

Upvotes: 2

Chris
Chris

Reputation: 77

Sorry if this answer is a little late but I had the same issue recently and thought I'd share the solution for anyone else who may run into it.

If you plan on using the PHP SDK for recurring payment (plan) PATCH calls the source needs altering slightly.

It's caused by line 56 in PPHttpConnection.php, the switch statement only attaches post fields if the method is POST. However, PATCH requests also send a payload and therefore also require post fields to be added, without them the API returns a malformed JSON error as it's not receiving any content.

switch($this->httpConfig->getMethod()) {
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;                     
}

Change the above to:

 switch($this->httpConfig->getMethod()) {
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break; 
        case 'PATCH':
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;           
}

The PATCH call should then work perfectly!

Upvotes: 1

Eshan
Eshan

Reputation: 3677

You can try to use this code , it works for me :

<?php
    $header = array();
    $header[] = 'Content-type: application/json';
    $header[] = 'Authorization: Bearer XXXXX-XXXXXXXXXXXXXXXXXXXXXXXXX';
    $url = 'https://api.sandbox.paypal.com/v1/payments/billing-plans/P-34L0290663823456FEE7TINY';
    $data ='[
        {
            "op": "replace",
            "path": "/",
            "value": {
                "state": "ACTIVE"
            }
        }
    ]';

    //open connection
    $ch = curl_init();

    //set connection properties
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    //curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);


    //execute post
    $result = curl_exec($ch);

    $err = curl_errno($ch);
    $errmsg = curl_error($ch) ;
    $info = curl_getinfo($ch);
    curl_close($ch);
    if( $err )
    {
    echo 'error';
    }
    if( $errmsg ){
    echo '<h3>Error</h3>'.$errmsg;
    }
    else
    echo $result;


    ?>

Upvotes: 0

Related Questions