用于python的unipag客户端
unipag的Python项目详细描述
要求
python版本2.6或2.7,或pypy。为了更好的安全,我们建议 安装Python Requests库,因为它支持ssl证书验证。 要安装请求,只需运行:
$ pip install requests
或者使用简易安装:
$ easy_install requests
请求库是可选的。如果没有安装,unipag客户端将使用 取而代之的是urllib2。unipag客户端的所有功能将保持完全正常,但是 它不会验证unipag api的ssl证书。
示例用法
创建发票
importunipagimportunipag.defaults# Get your key at https://my.unipag.comunipag.defaults.api_key='<your-secret-key>'invoice=unipag.Invoice.create(amount=42,currency='USD')# Done. invoice.id now contains unique id of this invoice at Unipag.
安装unipag小部件
尝试我们的小部件进行支付工作流处理。这是很随意的,但是你 可能会觉得很方便而且省时。
<scripttype="text/javascript"src="//d3oe3cumn3db7.cloudfront.net/uw3/js/uw3.min.js"charset="utf-8"id="uw3js"data-key="<your-public-key>"></script>
请注意,对于小部件使用public key非常重要。 公钥限制了对数据的访问,应该是安全的 在浏览器中使用。
从unipag处理webhook
在您的网站上创建一个独立页面,用于处理由 联合国教科文组织。在https://my.unipag.com>;设置>;网络挂钩处注册此页的URL。 初始化页面代码如下(例如django):
importunipagimportunipag.defaultsfromdjango.httpimportHttpResponse,HttpResponseBadRequest# Get your key at https://my.unipag.comunipag.defaults.api_key='<your-secret-key>'defhandle_unipag_hook(request):""" An example of Django view for handling hooks from Unipag. """event=unipag.objects_from_json(request.body)# Unipag should send correctly constructed event objectsifnotisinstance(event,unipag.Event):returnHttpResponseBadRequest('Bad request')# In this example we subscribe to invoice-related events onlyifisinstance(event.related_object,unipag.Invoice):# Always reload information from Unipag for security reasons:invoice=event.related_object.reload()# Now invoice object contains the most recent information,# securely loaded from Unipag.# ... do something with invoice data ...# Return HTTP 200 to let Unipag know that we successfully received messagereturnHttpResponse('OK')
提示:webhooks可能很难调试。查看unipag网络活动日志,它 在https://my.unipag.com>;网络活动中提供。你可以找到它 对Webhook处理程序调试很有用。
发票“自定义数据”属性的使用
发票对象有一个可选的“自定义数据”属性,可用于 以json格式存储多达32kb的任意数据。你可以自由使用这个领域 存储有关发票的附加信息 申请。
在python中,可以使用dict、list、string、numbers、boolean和none值 以任意组合方式将它们存储在自定义数据中。所有这些类型都是 发送到unipag时正确序列化,获取时反序列化 回来。考虑下面的例子,它们都是 自定义数据属性:
importunipagimportunipag.defaults# Get your key at https://my.unipag.comunipag.defaults.api_key='<your-secret-key>'invoice=unipag.Invoice.create(amount=42,currency='USD')# Store dicts, lists and single valuesinvoice.custom_data={'address':{'billing':'5863 Gentle Pond Rise, Suspension, Ontario, CA','shipping':'9215 Red Ridge, Lancer, Idaho, US',},'contact_phones':['555-4242','555-9000'],'magic_number':42,}invoice.save()# Clean everything outinvoice.custom_data=Noneinvoice.save()# Store a single value. Yes, it will be a valid JSON.invoice.custom_data=Trueinvoice.save()# Store list as a root element. Let's assume that we need to save cart items:invoice.custom_data=[{"product":"apples","price":10.0,"quantity":1},{"product":"oranges","price":12.5,"quantity":2}]invoice.save()
报告错误
在github上向项目的Issues Tracking报告问题。