Search code examples
python-3.xpaypalpaypal-rest-sdkpaypal-subscriptions

Problem with billing_agreement.cancel(). ('cancel() missing 1 required positional argument: 'attributes')


SDK/Library version: 1.13.1

Environment: Sandbox

PayPal-Debug-ID values: None

Language, the language version, and OS: Python, Ubuntu

Issue description: I'm getting the following error when I try to cancel a billing agreement.

The error:

TypeError: cancel() missing 1 required positional argument: 'attributes'

My code:

billing_agreement = BillingAgreement.find(billing_id)
    if billing_agreement.cancel():
        print(billing_agreement)
    else:
        flash('We are having some difficulties canceling your subscription, please try again later.', 'fails')
        return redirect(url_for('settings.settingspage'))

I'm getting the error because I need something in the attribute's value but I don't know what should I assign to the variable.

GitHub issue: https://github.com/paypal/PayPal-Python-SDK/issues/297


Solution

  • After some digging and looking at the documentation samples I found a sample about the cancel option and what I needed to assign to the attribute value was a cancel_note.

    The code:

    cancel_note = {"note": "Canceling the agreement"}
    user = Users.query.filter_by(id=ID).first()
    billing_id = Subscriptions.query.filter_by(email=user.email).filter_by(active=1).first().order_id
    billing_agreement = BillingAgreement.find(billing_id)
    if billing_agreement.cancel(cancel_note):
        flash('Subscription canceled with success.', 'success')
        return redirect(url_for('settings.settingspage'))
    else:
        flash('We are having some difficulties canceling your subscription, please try again later.', 'fails')
        return redirect(url_for('settings.settingspage'))
    

    The documentation sample