phppaypalpaypal-sandboxpaypal-subscriptions

how to activate the paypal subscription API?


I am creating Paypal Subscription APIs, I created products, Plans and then I created Subscription, however, subscription status is in Pending_approval. I am trying to activate the this Subscription via following API

        $clientId = env('PAYPAL_SANDBOX_CLIENT_ID');
        $secretKey = env('PAYPAL_SANDBOX_CLIENT_SECRET');

        $subscriptionId = 'I-TY9BCUDPAXKU';

        $apiEndpoint = "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$subscriptionId}/activate";

        // Set up cURL options
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        curl_setopt($ch, CURLOPT_URL, $apiEndpoint);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
            'reason' => 'Reactivating the subscription',
        ]));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'X-PAYPAL-SECURITY-CONTEXT: {"consumer":{"accountNumber":1181198218909172527,"merchantId":"5KW8F2FXKX5HA"},"merchant":{"accountNumber":1659371090107732880,"merchantId":"2J6QB8YJQSJRJ"},"apiCaller":{"clientId":"AdtlNBDhgmQWi2xk6edqJVKklPFyDWxtyKuXuyVT-OgdnnKpAVsbKHgvqHHP","appId":"APP-6DV794347V142302B","payerId":"2J6QB8YJQSJRJ","accountNumber":"1659371090107732880"},"scopes":["https://api-m.paypal.com/v1/subscription/.*","https://uri.paypal.com/services/subscription","openid"]}',
            'Content-Type: application/json',
            'Accept: application/json',
            'Authorization: Basic ' . base64_encode("$clientId:$secretKey"),
        ]);

        $result = curl_exec($ch);

but i am keep getting this error

enter image description here

I just copy and paste the CURL script from paypal documentation to my project. Now the issue is how can i get the Accountnumber for mercahnt, consumer from the paypal documentation. I tried spent the whole day in this but cant get here. thanks


Solution

  • I was doing it wrong. No need to call an API to approve the Subscription, Just redirect to the URl from the response to approve payment from Client side and it works