Django和Stripe图书馆

django-zebra的Python项目详细描述


概述:Zebra是一个使使用Stripe和Django更加容易的库。


如何集成Zebra的示例应用程序,它也可以作为它的测试套件。

pull请求非常受欢迎!



用法
=


` pip安装django zebra`

2。编辑"settings.py:`

```
已安装的应用程序+=("斑马",)
stripe secret="your-secret-api-key"
stripe publishable="your-publishable-api-key"
设置任何可选设置(如下)
```

3。(可选)`./manage.py syncdb`如果您有'zebra_enable_app=true`


4。(可选)添加webhook url:

```
url patterns+=模式('',
url(r'zebra/',include('zebra.urls',namespace="zebra",app_name='zebra'),

````

5。享受轻松的计费。



\作为zebra的一部分,启用customer、plan和subscription django模型。
*`zebra_customer_model`
实现stripecustomermixin的模型的app+模型字符串。ie`"myapp.mycustomer`.如果"zebra_enable_app"为true,则默认为"zebra.customer"。
*`zebra_auto_create_stripe_customers`
默认为'true'。如果Stripe嫒u Customer访问不存在,则自动在Stripe嫒u Customer访问上创建一个Stripe Customer对象。




webhooks嫒webhooks


*包括斑马url
*更新条带帐户以指向您的webhook url(也称为https://www.mysite.com/zebra/webhooks)
*插入您关心的任何webhook信号。

**注意:不推荐使用初始条带Webhook系统。有关Zebra对新系统支持的说明,请参见下面的内容。**

>Zebra提供:

*`Zebra_Webhook_Required_Payment_Failed`
*`Zebra_Webhook_Invoice_Ready`
*`Zebra_Webhook_Required_Payment_Succeeded`
*`Zebra_Webhook_Subscription_Trial_Ending`
*` zebra_webhook_subscription_final_payment_attemption_failed`


>所有webhook都提供相同的参数:

*`customer`-如果设置了"zebra_customer_model",则返回与"stripe_customer_id"或"none"匹配的实例。如果未设置"zebra_customer_model",则返回"none"。
*"full_json"-用simplejson解析的完整json响应。




例如,要在成功付款后更新客户的新帐单日期,可以:

(假设您已设置"zebra_customer_model"或正在使用` Zebra_enable_app`):

```
从Zebra.signals导入Zebra_webhook_recurrent_payment_succeeded


def update_last_invoice_date(sender,**kwargs):
customer=kwargs.pop("customer",none)
full_json=kwargs.pop("full_json",无)
customer.billing_date=full_json.date
customer.save()

zebra_webhook_recurrent_payment_succeeded.connect(update_last_invoice_date)
````


stripe最近更新了其webhook实现(请参阅https://stripe.com/blog/webhooks)。Zebra包括一个新系统的实现。

*包括Zebra URL
*更新条带帐户以指向您的Webhook URL(即https://www.mysite.com/zebra/webhooks/v2/)
*插入您关心的任何Webhook信号。

斑马提供:

*`zebra_webhook_charge_succeeded`
*`zebra_webhook_charge_facceeded`
*`zebra_webhook_charge_returned`
*`zebra_webhook_charge_discounted`
*`zebra_webhook_customer_created`
*`zebra webhook_customer_updated`
*` zebra_webhook_customer_已删除`
*`zebRa_Webhook_customer_subscription_created`
*`Zebra_Webhook_customer_subscription_updated`
*`Zebra_Webhook_customer_subscription_deleted`
*`Zebra_Webhook_customer_subscription_trial_will_end`
*`Zebra_Webhook_customer_discount_created`
*` Zebra_Webhook_customer_discount_updated`
*`Zebra_Webhook_customer_discount_deleted`
*`Zebra_Webhook_invoice_created`
*`Zebra_Webhook_invoice_updated`
*`Zebra_Webhook_invoice_payment_failed`
**`斑马钩子计划已创建`
*`斑马钩子计划已更新`
*`斑马钩子计划已删除`
*`斑马钩子计划已创建`
*`斑马钩子计划已更新`
*`斑马钩子计划已删除`
*`斑马钩子计划已创建`
*`斑马钩子优惠券`
*`斑马钩子优惠券更新`
*`斑马钩子优惠券删除`
*`斑马钩子转移`
*`斑马钩子转移`
*`斑马钩子转移失败`
*`斑马钩子平`

它将事件(`charge_succeeded`)映射到斑马信号(`zebra_webhook_charge_succeeded`)。要将处理程序分配给zebra发送的所有信号,例如,循环映射中的项:

for event\u key,webhook_map.iteritems()中的webhook_signal:
webhook_signal.connect(webhook_logger)



\forms_

StripePaymentForm设置了一个带有字段的表单,如[正式的条带示例](https://gist.github.com/1204718\file_stripe_tutorial_page.html)。


特别是,表单中的任何信用卡字段的name属性都被删除,以防止意外提交。还提供了用于设置stripe.js的媒体(它假设您有jquery)。


在这样的视图中使用它:

````
如果请求,则使用它。方法=‘post’:
zebra_form=stripepaymentform(request.post)
如果zebra_form.is_valid():
my_profile=request.user.get_profile()
stripe_customer=stripe.customer.retrieve(my_profile.stripe_customer_id)
stripe_customer.card=zebra_form.cleaned_data['stripe_token']
stripe_customer.save()

my_profile.last_4_digits=zebra_form.cleaned_data['last_4_digits']
my profile.stripe customer=stripecustomer.id
myprofile.save()


条带环境,并呈现一个基本的cc更新表单。请注意,您的"stripePaymentForm"应该称为"zebra_form"或"form"。


在模板中使用:

````
{%扩展"base.html"%}{%load zebra_tags%}

{%block head%}{{block.super}
{%zebra head和{stripe_key%}
{%endblock%}

{%block%{%blockcontent%}
{%zebra_card_form%}
{%endblock%}

````


就这样-所有条带标记的优点都会发生,错误会显示给用户。


目前,代码是非常自解释的,并以内联方式正式记录。



\要使用它,请运行:

```
./manage.py clear_stripe_test_customers
```

它响应`--verbosity=[0-3]`.



credits
=


它只是让我乐于使用,并激励我为我的用户制作更好的api。有关条带信息,请询问他们:[stripe.com](http://stripe.com)


代码信用在authors文件中。拉请求欢迎!



欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
swing Java按钮/网格布局   java列出Google日历中的所有事件   java无效:单击API publisher test按钮后连接到后端时出错   带有内部赋值的java While循环导致checkstyle错误   java为什么trimToSize/ensureCapacity方法提供“公共”级访问?   文件输出流的java问题   ListIterator和并发修改异常的java问题   java如何使用两个URL映射   无法识别使用“./../”构造的字符串java相对路径,为什么?   首次写入remotelyclosedsocket不会触发异常,对吗?JAVA   java OneDrive REST API为文件上载提供了400个无效谓词   Java泛型、集合接口和对象类的问题   OpenSSL Java安全提供程序   jmeter java运行jmx禁用操作