一款集成TouchNet的Django应用程序。
django-touchnet的Python项目详细描述
基于Ecollege为KDVS筹款者构建的用于TouchNet集成的Django应用程序。
概述
每笔付款都与一个可以是任何字符串的transaction_id相关联 不带空格,但通常是主键。
应用程序包括一个基本的模板标记,它有一个带有引导类的表单 可用于重定向到TouchNet的。
对于payment_received和payment_cancelled,有两个信号 将在来自TouchNet的回发时触发。
PostbackLog模型存储所有事务、接收到的事务和 返回,并在管理中注册。
万事达卡的测试卡为545454545454。
安装
从BitBucket下载并使用^{TT5}$在虚拟环境中安装。
配置
包含在INSTALLED_APPS:
INSTALLED_APPS = ( ... 'touchnet', ... )
在基本设置中包括所需的设置,例如用于开发:
TOUCHNET = { 'accounting': { 'fid': '117', # FID number for account 'fau': '3804148111300', # FAU number for account }, 'posting_key': 'CM-KDVS-jfeiowajfiewao', # Random posting key for site in Touchnet Test 'site_id': '24', # Site id in Touchnet Test 'url': 'https://secure.touchnet.com:8443/C21642test_upay/web/index.jsp', # Touchnet Test endpoint }
与生产设置类似:
TOUCHNET = { 'accounting': { 'fid': '117', 'fau': '3804148111300', }, 'posting_key': 'CM-KDVS-jfeaiwjfeioaw', 'site_id': '17', 'url': 'https://marketplace.ucdavis.edu/C21642_upay/web/index.jsp', # Production Touchnet endpoint }
在urls.py:
urlpatterns = patterns('', ... url(r'^touchnet/', include('touchnet.urls')), ... )
还包括成功、错误和取消重定向的URL,例如:
urlpatterns = patterns('fundraiser.views', ... url(r'^donate/success/$', 'donate_success_detail', name='fundraiser_donate_success_detail'), # Touchnet success url(r'^donate/error/$', 'donate_error_detail', name='fundraiser_donate_error_detail'), # Touchnet error url(r'^donate/cancel/$', 'donate_cancel_detail', name='fundraiser_donate_cancel_detail'), # Touchnet cancel ... )
最后,登录touchnet并验证回发url、posting key、success url、错误url和取消url设置匹配。回发URL应以 /touchnet/postback,例如https://fundraiser.kdvs.org/touchnet/postback。 url必须是https,并且服务器的ssl证书和ip地址 必须被触网允许。
用法
要处理付款,可以选择将接收器连接到models.py中的信号 连接一个用于取消预订,例如:
# Receivers def received_payment(sender, **kwargs): amount = kwargs['amount'] transaction_id = kwargs['transaction_id'] pledge = Pledge.objects.get(pk=transaction_id) pledge.amount_paid = amount pledge.save() payment_received.connect(received_payment, dispatch_uid='fundraiser_received_payment') def received_cancellation(sender, **kwargs): transaction_id = kwargs['transaction_id'] pledge = Pledge.objects.get(pk=transaction_id) pledge.timestamp_cancelled = timezone.now() pledge.save() payment_cancelled.connect(received_cancellation, dispatch_uid='fundraiser_received_cancellation')
若要重定向到TouchNet,请使用 视图中的交易ID(如质押PK)和金额(如捐赠金额):
def donate_redirect_detail(request): context = {} pledge_pk = request.session['pledge'] pledge = Pledge.objects.get(pk=pledge_pk) context['redirect_form'] = RedirectForm(pledge.pk, pledge.donation_amount) return render(request, 'fundraiser/donate_redirect_detail.html', context)
并在模板中显示表单,可以选择自动提交表单。如果 使用bootstrap,使用show_redirect_formtemplate标记:
{% extends 'core/base.html' %} {% load touchnet_extras %} {% block page-title %}Payment{% endblock %} {% block donate-status %}active{% endblock %} {% block content %} <h2>Continue To Payment</h2> <p>You will be redirected to a third-party payment site.</p> {% show_redirect_form redirect_form %} {% endblock %} {% block scripts %} <script> (function($) { $(document).ready(function() { $('button').attr('data-loading-text', 'Redirecting...') .button('loading'); $('form').submit(); }); })($); </script> {% endblock %}
如果付款成功,您可以在 带有实用功能的成功视图,例如:
def donate_success_detail(request): ... pledge_pk = touchnet.utils.get_transaction_id_from_request(request) ...
过程
- 通过保存事务的模型实例创建transaction_id。 (例如质押)或创建唯一的ID
- 用transaction_id和amount制作RedirectForm,并显示
- 可选地,使用javascript自动提交RedirectForm。
- 用户被重定向到TouchNet URL
成功:
- 用户输入信用卡信息
- TouchNet处理信用卡
- touchnet发送post请求到/touchnet/postback/
- django touchnet验证有效性并将信号发送到payment_received
- 接收器接收信号并更新付款金额(例如pledge.amount_paid)
- TouchNet将用户重定向到配置的成功URL
- 显示成功和确认信息
错误:
- 出现错误
- TouchNet将用户重定向到配置的错误URL
- 显示错误
取消:
- 用户取消
- touchnet发送post请求到/touchnet/postback/
- django touchnet验证有效性并将信号发送到payment_cancelled
- 接收器接收信号并进一步处理(例如,可能删除顺序)
- TouchNet将用户重定向到取消配置的URL
- 显示取消