cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to vary qty of a subscription with REST API

Options
synagen
Contributor
Posted on

I have a monthly subscription plan set up in my merchant account that is quantity based (per registered user of the web site). A customer subscribes to the web site and selects a plan for monthly billing. The number of registered users of the client can vary, and so I need the web site to automatically vary the quantity of the customer's subscription whenever the number of users of that subscription changes. I have created a test REST API interface that attempts to change the quantity of the individual subscription, knowing the customer's subscription ID. The code runs without error and returns Status 204, but the customer's subscription on my Paypal does not change the qty, description or total monthly charge.

<?php
namespace PayPal\Api;
require '/opt/bitnami/php/bin/vendor/autoload.php';
use PayPalCheckoutSdk\Orders\OrdersPatchRequest;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
include('globals.php'); // defines the following fields:
/*
GLOBAL $paypal_URL;
GLOBAL $paypal_account;
GLOBAL $paypal_client_id;
GLOBAL $paypal_secret;
GLOBAL $APIusername;
GLOBAL $APIpassword;
GLOBAL $APIURL;
GLOBAL $paypal_merchantID;
GLOBAL $paypal;
GLOBAL $PaypalAccountID;
*/

$merchant_id=$paypal_merchantID;
$subscrID ='I-YT07M8NCEMRN'; // hard-coded subscription ID for testing

ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

$environment=new ProductionEnvironment($paypal_client_id, $paypal_secret);
$client = new PayPalHttpClient($environment);
$request = new OrdersCreateRequest();
$request->prefer("return=representation");

// hard coded change Qty to 3

$request->body =[
"intent" => "CAPTURE",
"purchase_units" => [[
"reference_id" => $subscrID,
"amount" => [
"value" => "1.23",
"currency_code" => "USD"
]
]],
"redirect_urls" => [
"cancel_url" => "https://calltaker.co/cancel",
"return_url" => "https://calltaker.co/return"
]
];

$createdOrder = $client->execute($request);
$request2 = new OrdersPatchRequest($createdOrder->result->id);
$request2->body = [
[
"op" => "add",
"path" => "/purchase_units/@reference_id=='$subscrID'/description",
"value" => "3 user license"
],
[
"op" => "replace",
"path" => "/purchase_units/@reference_id=='$subscrID'/amount",
"resource"=>[
"quantity"=>"3"
],
"value" => [
"currency_code" => "USD",
"value" => "1.56",
"quantity" => "3"
]
]
];
$response2 = $client->execute($request2);
?>

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.