简体   繁体   中英

Paypal Rest api: How to delete a billing plan?

The paypal developer documentation explains the steps to create and activate a Billing plan .

Is there a way to delete a billing plan?

An alternative way of deleting a BillingPlan (as per the original question) is to submit a patch request. Unfortunately this isn't too clear from looking at the API docs: https://developer.paypal.com/docs/api/payments.billing-plans/#plan_update

You want to patch the state of the BillingPlan into DELETED :

[
    {
        "path": "/",
        "value": {
            "state": "DELETED"
        },
        "op": "replace"
    }
]

Once patched, the deleted plan no longer shows up when you list all available plans via /v1/payments/billing-plans

There is a way to DELETE a Billing Plan.

If you see the samples in the REST-PHP-SDK , there is a file named DeletePlan.php which has the code to delete the billing plan.

It goes something like this:

$createdPlan = require 'CreatePlan.php';

use PayPal\Api\Plan;

try {
    $result = $createdPlan->delete($apiContext);
} catch (Exception $ex) {
    ResultPrinter::printError("Deleted a Plan", "Plan", $createdPlan->getId(), null, $ex);
    exit(1);
}

ResultPrinter::printResult("Deleted a Plan", "Plan", $createdPlan->getId(), null, null);

return $createdPlan;

This worked for me. Hope this helps.

Thanks @smiling_warrior.

For the python API https://github.com/paypal/PayPal-Python-SDK/blob/master/samples/subscription/billing_agreements/replace.py

I used:

a=paypalrestsdk.BillingPlan.find("P-98072754CC611563JLOGIIYA")
a.replace([{"op": "replace","path": "/","value": {"state":"DELETED"}}])

Or delete all:

allplans = paypalrestsdk.BillingPlan.all()
for plan in allplans.plans:
    a=paypalrestsdk.BillingPlan.find(plan.id)
    a.replace([{"op": "replace","path": "/","value": {"state":"DELETED"}}])

As a complementary information about @maxxon15 answer, here's the actual code that does the DELETE on the PHP SDK :

public function delete($apiContext = null, $restCall = null)
{
    ArgumentValidator::validate($this->getId(), "Id");
    $patchRequest = new PatchRequest();
    $patch = new Patch();
    $value = new PayPalModel('{
        "state":"DELETED"
    }');
    $patch->setOp('replace')
        ->setPath('/')
        ->setValue($value);
    $patchRequest->addPatch($patch);
    return $this->update($patchRequest, $apiContext, $restCall);
}

So in other terms, it simply does an update ( PATCH ) to the billing endpoint, as stated by @smiling-warrior

[
    {
        "path": "/",
        "value": {
            "state": "DELETED"
        },
        "op": "replace"
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM