Jump to content

API command to cancel PayPal Subscription?


Kian

Recommended Posts

I am in the process of finishing a script that automatically update domain pricing for new purchases and/or recurring amounts of existing ones based on current Registrars' price list.

Simply put, today .it TLD costs me on Internetbs 6 EUR. Tomorrow the costs is updated to 6.5 EUR. I recalculate pricing accordingly (eg. my cost + 100% markup). So far so good but I have a problem.

The domain in question happens to have an active PayPal Subscription that automatically sends me 12 EUR (the old price 6 EUR + 100% markup). Now that my new price is 13 EUR (6.5 EUR + 100% markup), next year I will have a problem. PayPal sends me 12 EUR, the proforma remains Unpaid and the customer is required to send me a payment of 1 EUR.

Similarly if my cost drops to 5.5 EUR, the new recurring amount will be 11 EUR hence the automatic payment from PayPal will cause an overpayment.

Here is the question. All I need to do is to cancel PayPal subscription (if present) whenever recurring amount changes. I can do that from WHMCS interface by clicking "Cancel Subscription" button but I don't think it is possible to "click" it via API.

immagine.png.8a7c4873162186d52756aaf6de0d1cff.png

Am I correct?

I don't like the idea of sending "Cancel Subscription" API request myself directly to PayPal server.

Thanks.

Link to comment
Share on other sites

Hello @Kian

I think you may create your custom API to cancel  PayPal subscription

at first you will create php file in this folder "includes/api"

let's name it "cancelpaypalsubscription.php"

I have made it for you

<?php
/*
 * @ Cancel Paypal Subscription
 * @ 2023/01/10
 * @ By Aladdin Jiroun
 */

$whmcs = WHMCS\Application::getInstance();

use \WHMCS\Module\GatewaySetting;

$subscriptionID = $whmcs->get_req_var("subscriptionID");

$apiusername = GatewaySetting::where('gateway', '=', 'paypal')->where('setting', '=', 'apiusername')->first()->value;
$apipassword = GatewaySetting::where('gateway', '=', 'paypal')->where('setting', '=', 'apipassword')->first()->value;
$apisignature = GatewaySetting::where('gateway', '=', 'paypal')->where('setting', '=', 'apisignature')->first()->value;

$apiresults = array();
$url = "https://api-3t.paypal.com/nvp";

$postFields = array();
$postFields["BUTTONSOURCE"] = "WHMCS_WPP_DP";
$postFields["USER"] = $apiusername;
$postFields["PWD"] = $apipassword;
$postFields["SIGNATURE"] = $apisignature;
$postFields["VERSION"] = "3.0";
$postFields["METHOD"] = "ManageRecurringPaymentsProfileStatus";
$postFields["PROFILEID"] = $subscriptionID;
$postFields["ACTION"] = "Cancel";
$postFields["NOTE"] = "Automatic Subscription Cancellation";
$result = curlCall($url, $postFields);
parse_str($result, $resultsArray);
$resultsArray["PROFILEID"] = $subscriptionID;

if (strtoupper($resultsArray["ACK"]) == "SUCCESS" && $resultsArray["PROFILEID"]) $apiresults = array("status" => "success", "rawdata" => $resultsArray);
else $apiresults = array("status" => "error", "rawdata" => $resultsArray);


then you can use this api inside whmcs like this

$command = 'cancelpaypalsubscription';
$postData = array(
    'subscriptionID' => 'XXXX',
);
$results = localAPI($command, $postData);


 

Link to comment
Share on other sites

13 hours ago, DennisHermannsen said:

I don't think there's a way to cancel the subscription from the WHMCS API, unfortunately.

The subscription can be cancelled using the PayPal API but I think it would be best to update the subscription to match the new recurring price instead: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_patch

 Thanks 😌

I just finished coding it. I used Cancel command instead of Patch since what I need is cancelling subscription permanently.

https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel

13 hours ago, AladdinJ said:

Hello @Kian

I think you may create your custom API to cancel  PayPal subscription

at first you will create php file in this folder "includes/api"

let's name it "cancelpaypalsubscription.php"

I have made it for you


<?php
/*
 * @ Cancel Paypal Subscription
 * @ 2023/01/10
 * @ By Aladdin Jiroun
 */

$whmcs = WHMCS\Application::getInstance();

use \WHMCS\Module\GatewaySetting;

$subscriptionID = $whmcs->get_req_var("subscriptionID");

$apiusername = GatewaySetting::where('gateway', '=', 'paypal')->where('setting', '=', 'apiusername')->first()->value;
$apipassword = GatewaySetting::where('gateway', '=', 'paypal')->where('setting', '=', 'apipassword')->first()->value;
$apisignature = GatewaySetting::where('gateway', '=', 'paypal')->where('setting', '=', 'apisignature')->first()->value;

$apiresults = array();
$url = "https://api-3t.paypal.com/nvp";

$postFields = array();
$postFields["BUTTONSOURCE"] = "WHMCS_WPP_DP";
$postFields["USER"] = $apiusername;
$postFields["PWD"] = $apipassword;
$postFields["SIGNATURE"] = $apisignature;
$postFields["VERSION"] = "3.0";
$postFields["METHOD"] = "ManageRecurringPaymentsProfileStatus";
$postFields["PROFILEID"] = $subscriptionID;
$postFields["ACTION"] = "Cancel";
$postFields["NOTE"] = "Automatic Subscription Cancellation";
$result = curlCall($url, $postFields);
parse_str($result, $resultsArray);
$resultsArray["PROFILEID"] = $subscriptionID;

if (strtoupper($resultsArray["ACK"]) == "SUCCESS" && $resultsArray["PROFILEID"]) $apiresults = array("status" => "success", "rawdata" => $resultsArray);
else $apiresults = array("status" => "error", "rawdata" => $resultsArray);


then you can use this api inside whmcs like this


$command = 'cancelpaypalsubscription';
$postData = array(
    'subscriptionID' => 'XXXX',
);
$results = localAPI($command, $postData);


 

Interesting. I didn't know PayPal still allowed NVP endpoints. Cool 👍 Thanks for the code btw. It will surely help many people.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated