skip to Main Content

Morning

Having a go at adding a custom PayPal Subscription to my website using PHP and the PayPal API. I have not really used PHP, or Curl before so its taking a while but I have managed to write a script to get an access token from the sandbox and link to an existing subscription plan which you you can subscribe to. The code below works…

curl_setopt($ch, CURLOPT_POSTFIELDS, "n  {n   "plan_id":$planID,n   "start_time":$startTime,n      "application_context": {n        "brand_name": "Sleep Happy Mattress",n        "locale": "en-US",n        "shipping_preference": "SET_PROVIDED_ADDRESS",n        "user_action": "SUBSCRIBE_NOW",n        "payment_method": {n          "payer_selected": "PAYPAL",n          "payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"n        },n        "return_url": "https://example.com/returnUrl",n        "cancel_url": "https://example.com/cancelUrl"n      }n    }");

However whenever I try to include the planID and start_time as php variables within curl_setopt($ch, CURLOPT_POSTFIELDS I get a "Request is not well-formed, syntactically incorrect, or violates schema." error.

$planID = 'P-25Y56437062492726MFWZ4GI';
$startTime = '2021-10-22T00:00:00Z';

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions');

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "n  {n   "plan_id": "P-25Y56437062492726MFWZ4GI",n   "start_time": "2021-10-22T00:00:00Z",n      "application_context": {n        "brand_name": "Sleep Happy Mattress",n        "locale": "en-UK",n        "shipping_preference": "SET_PROVIDED_ADDRESS",n        "user_action": "SUBSCRIBE_NOW",n        "payment_method": {n          "payer_selected": "PAYPAL",n          "payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"n        },n        "return_url": "https://example.com/returnUrl",n        "cancel_url": "https://example.com/cancelUrl"n      }n    }");
curl_setopt($ch, CURLOPT_POSTFIELDS, "n  {n   "plan_id":$planID,n   "start_time":$startTime,n      "application_context": {n        "brand_name": "Sleep Happy Mattress",n        "locale": "en-US",n        "shipping_preference": "SET_PROVIDED_ADDRESS",n        "user_action": "SUBSCRIBE_NOW",n        "payment_method": {n          "payer_selected": "PAYPAL",n          "payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"n        },n        "return_url": "https://example.com/returnUrl",n        "cancel_url": "https://example.com/cancelUrl"n      }n    }");

Is my concatenation wrong or do the variables need manipulating in someway first, or both??

Many thanks for the help

Chris

2

Answers


  1. Do not specify nor include a start_time, unless you want problems with amounts not showing in checkout.

    Your problem is you did not keep quotes around the variable values, which is necessary for JSON string syntax. Observe:

    "plan_id": "$planID",
    

    Using json_encode() to construct your string from an array object would be better.

    Login or Signup to reply.
  2. Create your JSON string like this:

    $data = [
        'plan_id' => $planID,
        'start_time' => $startTime,
        'application_context' => [
            'brand_name' => 'Sleep Happy Mattress',
            'locale' => 'en-US',
            'shipping_preference' => 'SET_PROVIDED_ADDRESS',
            'user_action' => 'SUBSCRIBE_NOW',
            'payment_method' => [
                'payer_selected' => 'PAYPAL',
                'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED'
            ],
            'return_url' => 'https://example.com/returnUrl',
            'cancel_url' => 'https://example.com/cancelUrl'
        ],    
    ];
    

    Then you can check that the nesting etc is correct (please note I don’t know if the above is correct, I just copied it from your question), and if you json_encode it you will create well-formed JSON.

    $jsonEncoded = json_encode($data);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search