Quick start =========== From scratch ------------ To integrate ``itemshop`` into your app, you need to add the ``ItemBP`` blueprint to your application, and create some templates with a bit of javascript to handle the form: * application code * instantiate an :class:`ItemBP` object with: a unique name, a ``stripe.Charge`` (one-time) or ``stripe.Customer`` (recurring) payment, and some default arguments for the payment (e.g. the price) * mount the item blueprint on your app using ``app.register_blueprint(item.blueprint, **options)`` * HTML and JS * create a template called "``itemshop/default_index.html``", which contains a form with credit card fields and one hidden field called "``stripe_token``" * wire up the ``stripe.js`` API with this form, so that when the user submits the form, their credit card is processed with ``stripe.js``, and your ``stripe_token`` hidden field is populated with the token returned by stripe * create a template called "``itemshop/default_process.html``", which will be shown to the user after their purchase is complete The sections below will go into more detail about each of these steps. You can also re-use the `demo code`_ found in the source repo. .. _demo code: https://bitbucket.org/lost_theory/flask-stripe-blueprint/src/tip/demos/ Re-using the demo code ---------------------- You can copy from the demos that most closely match what you are trying to do. First, clone the source repo:: hg clone https://bitbucket.org/lost_theory/flask-stripe-blueprint/ Then, to integrate one of the demos into your app: 1. copy over the static CSS, JS, images from ``demos/static`` into your app's ``static`` directory 2. copy over the templates in the "itemshop" directory to your app's ``template`` directory 3. copy over the code in ``app.py`` that instantiates the :class:`ItemBP`, then mount the blueprint on your app From scratch, step-by-step ========================== App code: skeleton ------------------ ``itemshop`` was designed to be integrated into an existing app, so let's grab the sample app from `flask's home page`__. __ http://flask.pocoo.org/ .. code-block:: python from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Welcome to my item shop." if __name__ == "__main__": app.run(use_debugger=True, use_reloader=True) App code: ItemBP ---------------- .. sidebar:: Public/private keys Note: this is my testing private key. It corresponds to the public key we will set in the Javascript. You can get your own keys `here`__. __ https://manage.stripe.com/#account/apikeys Now, at the top of ``app.py``, we need to import the ``stripe`` library, the ``itemshop`` library, and set the stripe API key: .. code-block:: python import itemshop import stripe stripe.api_key = 'UwK0UE4tdPGNJckqfiu5UbzUJxHClRjW' Now let's instantiate the :class:`ItemBP` object by passing in three arguments: .. code-block:: python item = itemshop.ItemBP('my_item', stripe.Charge, dict( amount=1500, currency='usd', )) 1. First, a unique ID ``'my_item'`` 2. Next, we pass in the stripe payment object to use: * ``stripe.Charge`` is used for one-time payments, all it needs is an ``amount`` to charge * ``stripe.Customer`` is used for recurring payments, it needs a ``plan`` argument, which is created through the stripe web interface (e.g. ``plan='silver'`` might correspond to a monthly charge of $10.00) 3. Finally, we pass in a dict of default values that will be used to instantiate the stripe payment. In this case, we pass in the ``amount`` (``1500`` in cents = $15.00) and the currency 'usd'. Any other values, like the ``stripe_token`` will come from the form data. You can also use this dict to pass in an ``api_key``, which will override the global ``stripe.api_key``. After that, all you need to do is mount the blueprint on your app using ``register_blueprint``: .. code-block:: python app.register_blueprint(item.blueprint) By default, this will mount the blueprint onto the root of your app ("/"). Let's mount the blueprint somewhere else, so we don't override the existing view at "/". To do this, use ``url_prefix``: .. code-block:: python app.register_blueprint(item.blueprint, url_prefix="/shop") Now, run your application:: $ python app.py * Running on http://127.0.0.1:5000/ * Restarting with reloader... Go to "/shop" in your browser and you should see an error page, because the template is missing. That means it's time for the next step! HTML ---- Now we need to create the template that contains the credit card form. In the ````, include the following Javascript: .. code-block:: html .. sidebar:: Finished code To see a finished version of this code, check out `default_index.html`_ in the ``01-simplest`` demo directory. .. _default_index.html: https://bitbucket.org/lost_theory/flask-stripe-blueprint/src/tip/demos/01-simplest/templates/itemshop/default_index.html We are using the stripe.js API library, jQuery, a ``demo.js`` file we'll define in a bit, and setting our Stripe account's public key. The key in that example (``pk_uo5...``) is my test key, please try to use your own if you have one. Next, we'll include some item details in the body of the page: .. code-block:: html

{{item.name}}

Please fill out the form below to complete your purchase for {{item.name}}. {% if item.price %} The price is ${{item.price}}. {% else %} This is a subscription. {% endif %}

Next, we'll use a hidden ``div`` to contain the validation message when the stripe API rejects the payment: .. code-block:: html Now we create the purchase form: .. code-block:: html
/
Two important things to note: 1. The very first field is the hidden field for "``stripe_token``". We will use this element to store the token obtained from the ``stripe.js`` API call. 2. None of the credit card fields (card number, CVC, expiration month, or expiration year) have a ``name`` attribute. This means that even if the form gets submitted without javascript, the credit card fields will not be sent to your backend. This is a safety precaution. The last step for the HTML templates is to create the "thanks" template, ``default_process.html``, which is what gets shown to users after they successfully make a purchase. It could be as simple as this: .. code-block:: html Thank you

Thank you!

Thank you for purchasing {{item.name}}!

Back to order form.

Now we need to write the javascript to process the credit card form. Javascript ---------- Create a new static JS file at ``static/demo.js`` (you could also just put this in a ``