Hello i am trying to create a subscription using the code below but i am getting an error
Invalid path provided in patch request
It seems the script fails at activating the plan at
// Activate the plan $patch = new \PayPal\Api\Patch(); $patch->setOp('replace') ->setPath('/') ->setValue(new \stdClass(['state' => 'ACTIVE' ]));
Please if someone has experience with this advise what is breaking in my code, below is the full code for review, thanks in advance
require_once 'vendor/autoload.php';use PayPal\Rest\ApiContext;use PayPal\Auth\OAuthTokenCredential;use PayPal\Api\Agreement;use PayPal\Api\Plan;use PayPal\Api\Payer;use PayPal\Api\PayerInfo;use PayPal\Api\PaymentDefinition;use PayPal\Api\MerchantPreferences;use PayPal\Api\Currency;class PayPalRecurringPayments{private $apiContext;public function __construct(){// Replace with your PayPal client ID and secret$clientId = 'xxxxxxxxxxx';$clientSecret = 'xxxxxxxxxxx';$this->apiContext = new ApiContext(new OAuthTokenCredential($clientId, $clientSecret));// For sandbox testing$this->apiContext->setConfig(['mode' => 'sandbox', // Change to 'live' for production'log.LogEnabled' => true,'log.FileName' => 'PayPal.log','log.LogLevel' => 'DEBUG']);}public function createBillingPlan($planName, $description, $amount, $frequency = 'Month', $frequencyInterval = 1){// Create a new billing plan$plan = new Plan();$plan->setName($planName)->setDescription($description)->setType('INFINITE'); // or 'FIXED' for limited number of payments// Set payment definition$paymentDefinition = new PaymentDefinition();$paymentDefinition->setName('Regular Payments')->setType('REGULAR')->setFrequency($frequency)->setFrequencyInterval($frequencyInterval)->setCycles('0') // Set to 0 for infinite payments->setAmount(new Currency(['value' => $amount,'currency' => 'USD']));// Set merchant preferences$merchantPreferences = new MerchantPreferences();$merchantPreferences->setReturnUrl('https://example.com/success.php')->setCancelUrl('https://example.com/cancel.php')->setAutoBillAmount('yes')->setInitialFailAmountAction('CONTINUE')->setMaxFailAttempts('3');$plan->setPaymentDefinitions([$paymentDefinition]);$plan->setMerchantPreferences($merchantPreferences);try {// Create the plan$createdPlan = $plan->create($this->apiContext);// Activate the plan$patch = new \PayPal\Api\Patch();$patch->setOp('replace')->setPath('/')->setValue(new \stdClass(['state' => 'ACTIVE']));$patchRequest = new \PayPal\Api\PatchRequest();$patchRequest->addPatch($patch);$createdPlan->update($patchRequest, $this->apiContext);return $createdPlan;} catch (PayPal\Exception\PayPalConnectionException $ex) {echo $ex->getCode(); // Prints the Error Codeecho $ex->getData(); // Prints the detailed error message die($ex);} catch (Exception $ex) {die($ex);}}public function createBillingAgreement($planId, $customerName, $customerEmail){// Create new agreement$agreement = new Agreement();$agreement->setName('Subscription Agreement')->setDescription('Recurring payment agreement')->setStartDate(gmdate("Y-m-d\TH:i:s\Z", strtotime("+1 day")));// Set payer$payer = new Payer();$payer->setPaymentMethod('paypal');$payerInfo = new PayerInfo();$payerInfo->setEmail($customerEmail)->setFirstName($customerName);$payer->setPayerInfo($payerInfo);$agreement->setPayer($payer);// Set plan ID$plan = new Plan();$plan->setId($planId);$agreement->setPlan($plan);try {// Create agreement$agreement = $agreement->create($this->apiContext);// Extract approval URL$approvalUrl = $agreement->getApprovalLink();return $approvalUrl;} catch (PayPal\Exception\PayPalConnectionException $ex) {echo $ex->getCode(); // Prints the Error Codeecho $ex->getData(); // Prints the detailed error message die($ex);} catch (Exception $ex) {die($ex);}}public function executeBillingAgreement($token){try {$agreement = new Agreement();$executeAgreement = $agreement->execute($token, $this->apiContext);return $executeAgreement;} catch (PayPal\Exception\PayPalConnectionException $ex) {echo $ex->getCode(); // Prints the Error Codeecho $ex->getData(); // Prints the detailed error message die($ex);} catch (Exception $ex) {die($ex);}}}// Example usage:$paypal = new PayPalRecurringPayments();// Create a billing plan$plan = $paypal->createBillingPlan('Monthly Subscription','Monthly subscription service','1.99','Month',1);// Get plan ID from the created plan$planId = $plan->getId();try {// Create a billing agreement$approvalUrl = $paypal->createBillingAgreement($planId,'John Doe','customer@example.com');// Redirect user to $approvalUrl for payment approvalecho "Approval URL: " . $approvalUrl;} catch (PayPal\Exception\PayPalConnectionException $ex) {echo $ex->getCode(); // Prints the Error Codeecho $ex->getData(); // Prints the detailed error message die($ex);} catch (Exception $ex) {die($ex);}// After user approval, execute the agreement (in your return URL handler):// $token = $_GET['token'];// $result = $paypal->executeBillingAgreement($token);