mollie / mollie-api-python

Mollie API client for Python
http://www.mollie.com
BSD 2-Clause "Simplified" License
113 stars 55 forks source link

Creating a subscription for a customer #170

Closed Gilduuw closed 4 years ago

Gilduuw commented 4 years ago

Hi - more a question than an issue, I'm sorry for that.

I'm trying to create a subscription for a specific customer.

In the API docs (https://docs.mollie.com/reference/v2/subscriptions-api/create-subscription), there is no Python example available.

I experimented with the following:

mollie_client.customer_subscriptions.create ({
                'amount': {
                    'currency': 'EUR',
                    'value': '0.01'
                },
                'customerId': mollie_customer_id,
                'interval': '1 month',
                'description': 'Recurring Timeless subscription',
                'startDate': '2020-09-30',
                'webhookUrl': webhook_url,
            })

Error returned: Non-existent body parameter "customerId" for this API call. Did you mean: "times"?

I've tried other setups (first retrieving the Customer object): customer.subscriptions.create() customer.create_subscription()

But with similar results (non-existent API methods).

So what would be the correct way to approach this via the python client?

I could ofcourse create a custom POST request, but I prefer to use the built-in client when possible.

Thanks!

whyscream commented 4 years ago

I'm sorry that this is not documented better. We only have a unit test describing the correct pattern at: https://github.com/mollie/mollie-api-python/blob/master/tests/test_customer_subscriptions.py#L102

Basically, the correct pattern is add the customer contextually:

payload = {
    'amount': {
        'currency': 'EUR',
        'value': '0.01'
    },
    'interval': '1 month',
    'description': 'Recurring Timeless subscription',
    'startDate': '2020-09-30',
    'webhookUrl': webhook_url,
}
mollie_client.customer_subscriptions.with_parent_id(mollie_customer_id).create(payload)
# or 
mollie_customer = client.customers.get(mollie_customer_id)
mollie_client.customer_subscriptions.on(mollie_customer).create(payload)

@Gilduuw does this solve your problem?

Gilduuw commented 4 years ago

That solved it, thanks :) Will check out unit tests first in the future!