API documentation

class itemshop.ItemBP(name, pmt_class, pmt_default_args=None)

BlueprintWrapper that models a shop item. There are two view methods: index, to show the form, and process, to process the incoming stripe_token and show the thank you page. There are also two hooks: pre_purchase and post_purchase, that can be overridden to add behavior before and after the purchase is made.

Parameters:
  • name – unique name / ID for the item, used to build the URL rules and to search for custom templates
  • pmt_class – should be a stripe.Charge or stripe.Customer class or subclass
  • pmt_class_args – a dict that contains any default values you want to use when instatiating pmt_class (e.g. the price); you could also leave it empty and set the arguments by overriding pre_purchase. See the stripe documentation for what arguments you can use.
get_template(template_name)

Locate the custom item template, or fall back to the default template. Templates are kept in the itemshop directory inside your app’s template directory. The default templates are default_index.html and default_process.html. To use a custom template use the item’s name in the filename instead of “default”.

You can override this method to change how templates are looked up.

index(*a, **kw)

View method. Renders the item page with the credit card form. The item is passed into the template.

post_purchase(args, purchase)

Override this to save completed purchases to a database, send email receipts, etc.

You can also return a dictionary whose values will be sent to the render_template call that displays the ‘thanks’ page.

This method has access to the request object.

pre_purchase(args)

Override this to validate form data / purchase arguments, record purchase attempts before the Stripe API call is made, modify the purchase arguments, etc.

You can modify the purchase arguments by returning a new dictionary. If you don’t want to modify the purchase args, just return the same args object that was passed in.

This method has access to the request object.

price

Format self.pmt_default_args['amount'] as a string "XX.YY", if it exists. Otherwise returns an empty string.

process(*a, **kw)

View method. Processes incoming stripe_token (obtained by the client with stripe.js) to actually charge the user’s credit card.

You can change / add behavior to this method by overriding pre_purchase(args) and post_purchase(args, purchase).

The item, purchase, and any extra args from post_purchase are passed into the template.

Internals

class itemshop.BlueprintWrapperMeta

Collects all the methods that have been decorated with bp_route. The route arguments are stored in a dictionary self._routes as method_name -> (rule, options).

class itemshop.BlueprintWrapper

Inherit from this class to automatically collect all your routes that have been decorated with bp_route.

itemshop.bp_route(rule, **options)

Used with BlueprintWrapperMeta to register routes.

Decorates the view method with route_rule and route_options attributes that can then be used to instantiate Flask URL rules.

itemshop

Table Of Contents

Related Topics

This Page