clear-skies-stripe


Nameclear-skies-stripe JSON
Version 0.9.21 PyPI version JSON
download
home_pagehttps://github.com/cmancone/clearskies-stripe
Summaryclearskies bindings for working with Stripe
upload_time2024-07-12 22:44:35
maintainerNone
docs_urlNone
authorConor Mancone
requires_python<4.0,>=3.10
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # clearskies-stripe

clearskies bindings for working with [Stripe](https://stripe.com/).  Mostly this means handlers and actions to control the flow of data to and from stripe.

# Installation

To install:

```
pip install clear-skies-stripe
```

# Usage

## Authentication

This module has a variety of actions and handlers which take care of the stripe integration.  However, before you can use any of them, you must setup the stripe module with clearskies and tell it how to authenticate to Stripe.  The module assumes that your Stripe API key is stored in your secret manager, so you just have to tell it the path to the API key in your secret manager.

**IMPORTANT**: This module is designed to fetch your Stripe API key only when needed and will automatically re-fetch the key from the secrets manager in the event of an authentication failure.  As a result, you can rotate your Stripe API key at anytime: just drop the new API key in your secret manager and your running processes will automatically find it and use it without needing to restart/rebuild/relaunch the application.

In the following example, we configure a clearskies application to use AWS Secrets Manager for the storage of secrets, and then we tell the Stripe integration to fetch its api key from `/path/to/stripe/(api|publishable)/key` in AWS Secrets Manager.  Of course, you can use any secrets manager you want: just swap out `secrets` in the dependency injection configuration.

```
import clearskies
import clearskies_stripe
import clearskies_aws

application = clearskies.Application(
    SomeHandler,
    {
        "your": "application config",
    },
    bindings={
        "stripe": clearskies_stripe.di.stripe(
            "/path/to/stripe/api/key",
            "/path/to/stripe/publishable/key",
        ),
        "secrets": clearskies_aws.secrets.SecretsManager,
    },
)
```

## Models

To use any of the models you must import the `clearskies_stripe` module into your application (you still have to configure authentication per the above):

```
import clearskies
import clearskies_stripe
import clearskies_aws

application = clearskies.Application(
    SomeHandler,
    {
        "your": "application config",
    },
    binding_modules=[
        clearskies_stripe,
    ],
    bindings={
        "stripe": clearskies_stripe.di.stripe(
            "/path/to/stripe/api/key",
            "/path/to/stripe/publishable/key",
        ),
        "secrets": clearskies_aws.secrets.SecretsManager,
    },
)
```

## Models

This module comes with a limited selection of models.  The columns available in each model match those published via the Stripe API.

| Model               | DI name               | Stripe Docs |
|---------------------|-----------------------|-------------|
| StripeCustomer      | stripe_customer       | TBD |
| StripePayment       | stripe_payment        | TBD |
| StripePaymentMethod | stripe_payment_method | TBD |

## SetupIntent Handler

This handler creates a SetupIntent and returns the details of it, as well as the publishable key.  You can specify any of the parameters for the [create call].  In addition, you can provide `parameters_callable` which can change all the parameters that will be passed to the create call, and you can also provide `output_callable` which changes the response from the handler.  By default, the handler returns the full details of the response from the create call, as well as your publishable key.

### Configuration Options

Here are the list of allowed configurations for this handler (on top of the standard handler configs).  All configs are optional.  With the exception of the callables (described below), all configuration options will be passed along as-is in the call to `stripe.setup_intents.create()`.  See [the stripe docs](https://docs.stripe.com/api/setup_intents/create) for more details:

| Name                           | Type        |
|--------------------------------|-------------|
| `automatic_payment_methods`    | `dict`      |
| `confirm`                      | `bool`      |
| `description`                  | `str`       |
| `metadata`                     | `dict`      |
| `payment_method`               | `str`       |
| `usage`                        | `str`       |
| `attach_to_self`               | `bool`      |
| `confirmation_token`           | `str`       |
| `flow_directions`              | `list[str]` |
| `mandate_data`                 | `dict`      |
| `on_behalf_of`                 | `str`       |
| `payment_method_configuration` | `str`       |
| `payment_method_data,`         | `dict`      |
| `payment_method_options`       | `dict`      |
| `payment_method_types`         | `list[str]` |
| `return_url`                   | `str`       |
| `single_use`                   | `dict`      |
| `use_stripe_sdk`               | `bool`      |
| `parameters_callable`          | `Callable`  |
| `output_callable`              | `Callable`  |

### parameters_callable

The `parameters_callable` is provided with the following kwargs
| Name                 | Type                                   | Value |
|----------------------|----------------------------------------|-------|
| `input_output`       | `clearskies.input_outputs.InputOutput` | The input output object for the request |
| `routing_data`       | `dict`                                 | Any named routing parameters |
| `request_data`       | `dict`                                 | The contents of the JSON request body |
| `authorization_data` | `dict`                                 | Any authorization data for logged in users |
| `context_specifics`  | `dict`                                 | Any additional data provided by the context the application is running under |

It should return a dictionary, which will be passed along in the SDK call to `setup_intents.create()`.  If the parameters callable returns a parameter that is also specified in the handler configuration, the return value from the parameters callable takes preference.

### output_callable

The `output_callable` will be provided the response from the `setup_intents.create()` call and can make additional changes.  Note that this function **MUST** return a dictionary.  If `output_callable` is defined then it's response will be returned to the user as-is, which means that only the data returned by the callable will be sennt along to the client.

The `output_callable` is provided with the following kwargs:

| Name                 | Type                                   | Value |
|----------------------|----------------------------------------|-------|
| `response`           | `dict`                                 | The response from the `setup_intents.create()` call |
| `input_output`       | `clearskies.input_outputs.InputOutput` | The input output object for the request |
| `routing_data`       | `dict`                                 | Any named routing parameters |
| `request_data`       | `dict`                                 | The contents of the JSON request body |
| `authorization_data` | `dict`                                 | Any authorization data for logged in users |
| `context_specifics`  | `dict`                                 | Any additional data provided by the context the application is running under |

### Example

Here's an example of using the CreateSetupIntent handler.  This example assumes that the stripe customer id is stored in the JWT in the key `stripe_customer_id`, and so it uses that fact in conjunction with the `parameters_callable` to set the customer id on the setup intent:

```
import clearskies
import clearskies_stripe
import clearskies_aws

def add_stripe_customer(self, authorization_data):
    return {"customer": authorization_data["stripe_customer_id"]}

application = clearskies.Application(
    clearskies.handlers.SimpleRouting,
    {
        "authentication": clearskies.authentication.jwks("https://example.com/.well-known/jwks.json"),
        "routes": [
            {
                "path": "/setup_intent",
                "methods": "POST",
                "handler_class": clearskies_stripe.handlers.CreateSetupIntent,
                "handler_config": {
                    "usage": "off_session",
                    "parameters_callable": add_stripe_customer,
                },
            },
        ],
    },
    bindings={
        "stripe": clearskies_stripe.di.stripe(
            "/path/to/stripe/api/key/in/secrets/manager",
            "/path/to/stripe/publishable/key/in/secrets/manager",
        ),
        "secrets": clearskies_aws.secrets.SecretsManager,
    },
)

in_wsgi = clearskies_aws.contexts.wsgi(application)
def application(env, start_response):
    return in_wsgi(env, start_response)

in_lambda = clearskies_aws.contexts.lambda_elb(application)
def lambda_handler(event, context):
    return in_lambda(env, start_response)
```

You would then execute and expect something like this:

```
$ curl 'https://your-application/setup_intent' -X POST | jq
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cmancone/clearskies-stripe",
    "name": "clear-skies-stripe",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Conor Mancone",
    "author_email": "cmancone@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/33/f3/869de7be14944780c03d9b408cade18d8a6d9ffdb91eaa399344a0711f40/clear_skies_stripe-0.9.21.tar.gz",
    "platform": null,
    "description": "# clearskies-stripe\n\nclearskies bindings for working with [Stripe](https://stripe.com/).  Mostly this means handlers and actions to control the flow of data to and from stripe.\n\n# Installation\n\nTo install:\n\n```\npip install clear-skies-stripe\n```\n\n# Usage\n\n## Authentication\n\nThis module has a variety of actions and handlers which take care of the stripe integration.  However, before you can use any of them, you must setup the stripe module with clearskies and tell it how to authenticate to Stripe.  The module assumes that your Stripe API key is stored in your secret manager, so you just have to tell it the path to the API key in your secret manager.\n\n**IMPORTANT**: This module is designed to fetch your Stripe API key only when needed and will automatically re-fetch the key from the secrets manager in the event of an authentication failure.  As a result, you can rotate your Stripe API key at anytime: just drop the new API key in your secret manager and your running processes will automatically find it and use it without needing to restart/rebuild/relaunch the application.\n\nIn the following example, we configure a clearskies application to use AWS Secrets Manager for the storage of secrets, and then we tell the Stripe integration to fetch its api key from `/path/to/stripe/(api|publishable)/key` in AWS Secrets Manager.  Of course, you can use any secrets manager you want: just swap out `secrets` in the dependency injection configuration.\n\n```\nimport clearskies\nimport clearskies_stripe\nimport clearskies_aws\n\napplication = clearskies.Application(\n    SomeHandler,\n    {\n        \"your\": \"application config\",\n    },\n    bindings={\n        \"stripe\": clearskies_stripe.di.stripe(\n            \"/path/to/stripe/api/key\",\n            \"/path/to/stripe/publishable/key\",\n        ),\n        \"secrets\": clearskies_aws.secrets.SecretsManager,\n    },\n)\n```\n\n## Models\n\nTo use any of the models you must import the `clearskies_stripe` module into your application (you still have to configure authentication per the above):\n\n```\nimport clearskies\nimport clearskies_stripe\nimport clearskies_aws\n\napplication = clearskies.Application(\n    SomeHandler,\n    {\n        \"your\": \"application config\",\n    },\n    binding_modules=[\n        clearskies_stripe,\n    ],\n    bindings={\n        \"stripe\": clearskies_stripe.di.stripe(\n            \"/path/to/stripe/api/key\",\n            \"/path/to/stripe/publishable/key\",\n        ),\n        \"secrets\": clearskies_aws.secrets.SecretsManager,\n    },\n)\n```\n\n## Models\n\nThis module comes with a limited selection of models.  The columns available in each model match those published via the Stripe API.\n\n| Model               | DI name               | Stripe Docs |\n|---------------------|-----------------------|-------------|\n| StripeCustomer      | stripe_customer       | TBD |\n| StripePayment       | stripe_payment        | TBD |\n| StripePaymentMethod | stripe_payment_method | TBD |\n\n## SetupIntent Handler\n\nThis handler creates a SetupIntent and returns the details of it, as well as the publishable key.  You can specify any of the parameters for the [create call].  In addition, you can provide `parameters_callable` which can change all the parameters that will be passed to the create call, and you can also provide `output_callable` which changes the response from the handler.  By default, the handler returns the full details of the response from the create call, as well as your publishable key.\n\n### Configuration Options\n\nHere are the list of allowed configurations for this handler (on top of the standard handler configs).  All configs are optional.  With the exception of the callables (described below), all configuration options will be passed along as-is in the call to `stripe.setup_intents.create()`.  See [the stripe docs](https://docs.stripe.com/api/setup_intents/create) for more details:\n\n| Name                           | Type        |\n|--------------------------------|-------------|\n| `automatic_payment_methods`    | `dict`      |\n| `confirm`                      | `bool`      |\n| `description`                  | `str`       |\n| `metadata`                     | `dict`      |\n| `payment_method`               | `str`       |\n| `usage`                        | `str`       |\n| `attach_to_self`               | `bool`      |\n| `confirmation_token`           | `str`       |\n| `flow_directions`              | `list[str]` |\n| `mandate_data`                 | `dict`      |\n| `on_behalf_of`                 | `str`       |\n| `payment_method_configuration` | `str`       |\n| `payment_method_data,`         | `dict`      |\n| `payment_method_options`       | `dict`      |\n| `payment_method_types`         | `list[str]` |\n| `return_url`                   | `str`       |\n| `single_use`                   | `dict`      |\n| `use_stripe_sdk`               | `bool`      |\n| `parameters_callable`          | `Callable`  |\n| `output_callable`              | `Callable`  |\n\n### parameters_callable\n\nThe `parameters_callable` is provided with the following kwargs\n| Name                 | Type                                   | Value |\n|----------------------|----------------------------------------|-------|\n| `input_output`       | `clearskies.input_outputs.InputOutput` | The input output object for the request |\n| `routing_data`       | `dict`                                 | Any named routing parameters |\n| `request_data`       | `dict`                                 | The contents of the JSON request body |\n| `authorization_data` | `dict`                                 | Any authorization data for logged in users |\n| `context_specifics`  | `dict`                                 | Any additional data provided by the context the application is running under |\n\nIt should return a dictionary, which will be passed along in the SDK call to `setup_intents.create()`.  If the parameters callable returns a parameter that is also specified in the handler configuration, the return value from the parameters callable takes preference.\n\n### output_callable\n\nThe `output_callable` will be provided the response from the `setup_intents.create()` call and can make additional changes.  Note that this function **MUST** return a dictionary.  If `output_callable` is defined then it's response will be returned to the user as-is, which means that only the data returned by the callable will be sennt along to the client.\n\nThe `output_callable` is provided with the following kwargs:\n\n| Name                 | Type                                   | Value |\n|----------------------|----------------------------------------|-------|\n| `response`           | `dict`                                 | The response from the `setup_intents.create()` call |\n| `input_output`       | `clearskies.input_outputs.InputOutput` | The input output object for the request |\n| `routing_data`       | `dict`                                 | Any named routing parameters |\n| `request_data`       | `dict`                                 | The contents of the JSON request body |\n| `authorization_data` | `dict`                                 | Any authorization data for logged in users |\n| `context_specifics`  | `dict`                                 | Any additional data provided by the context the application is running under |\n\n### Example\n\nHere's an example of using the CreateSetupIntent handler.  This example assumes that the stripe customer id is stored in the JWT in the key `stripe_customer_id`, and so it uses that fact in conjunction with the `parameters_callable` to set the customer id on the setup intent:\n\n```\nimport clearskies\nimport clearskies_stripe\nimport clearskies_aws\n\ndef add_stripe_customer(self, authorization_data):\n    return {\"customer\": authorization_data[\"stripe_customer_id\"]}\n\napplication = clearskies.Application(\n    clearskies.handlers.SimpleRouting,\n    {\n        \"authentication\": clearskies.authentication.jwks(\"https://example.com/.well-known/jwks.json\"),\n        \"routes\": [\n            {\n                \"path\": \"/setup_intent\",\n                \"methods\": \"POST\",\n                \"handler_class\": clearskies_stripe.handlers.CreateSetupIntent,\n                \"handler_config\": {\n                    \"usage\": \"off_session\",\n                    \"parameters_callable\": add_stripe_customer,\n                },\n            },\n        ],\n    },\n    bindings={\n        \"stripe\": clearskies_stripe.di.stripe(\n            \"/path/to/stripe/api/key/in/secrets/manager\",\n            \"/path/to/stripe/publishable/key/in/secrets/manager\",\n        ),\n        \"secrets\": clearskies_aws.secrets.SecretsManager,\n    },\n)\n\nin_wsgi = clearskies_aws.contexts.wsgi(application)\ndef application(env, start_response):\n    return in_wsgi(env, start_response)\n\nin_lambda = clearskies_aws.contexts.lambda_elb(application)\ndef lambda_handler(event, context):\n    return in_lambda(env, start_response)\n```\n\nYou would then execute and expect something like this:\n\n```\n$ curl 'https://your-application/setup_intent' -X POST | jq\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "clearskies bindings for working with Stripe",
    "version": "0.9.21",
    "project_urls": {
        "Homepage": "https://github.com/cmancone/clearskies-stripe",
        "Repository": "https://github.com/cmancone/clearskies-stripe"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "54fe455784cad17f417723bd8847176aed3aa409ff7e461363a1b621b7315f76",
                "md5": "bddb8dbc9f0da31626c73534fcd41360",
                "sha256": "d1c22ce40e0e66c3932b523a70467e6235c42c7774316d0218a60b97e8a70398"
            },
            "downloads": -1,
            "filename": "clear_skies_stripe-0.9.21-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bddb8dbc9f0da31626c73534fcd41360",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 16362,
            "upload_time": "2024-07-12T22:44:33",
            "upload_time_iso_8601": "2024-07-12T22:44:33.668729Z",
            "url": "https://files.pythonhosted.org/packages/54/fe/455784cad17f417723bd8847176aed3aa409ff7e461363a1b621b7315f76/clear_skies_stripe-0.9.21-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "33f3869de7be14944780c03d9b408cade18d8a6d9ffdb91eaa399344a0711f40",
                "md5": "5f24d438c1677948f4b1b491368c0755",
                "sha256": "a45a9cb83bad6a7336fc2d03024bc8af8784dd1baba9d2022f2e1fe60ee014a2"
            },
            "downloads": -1,
            "filename": "clear_skies_stripe-0.9.21.tar.gz",
            "has_sig": false,
            "md5_digest": "5f24d438c1677948f4b1b491368c0755",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 13444,
            "upload_time": "2024-07-12T22:44:35",
            "upload_time_iso_8601": "2024-07-12T22:44:35.283803Z",
            "url": "https://files.pythonhosted.org/packages/33/f3/869de7be14944780c03d9b408cade18d8a6d9ffdb91eaa399344a0711f40/clear_skies_stripe-0.9.21.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-12 22:44:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cmancone",
    "github_project": "clearskies-stripe",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "clear-skies-stripe"
}
        
Elapsed time: 0.28709s