国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

django 實現電子支付功能的示例代碼

2020-02-15 22:16:52
字體:
來源:轉載
供稿:網友

思路:調用第三方支付 API 接口實現支付功能。本來想用支付寶來實現第三方網站的支付功能的,但是在實際操作中發現支付寶沒有 Python 接口,網上雖然有他人二次封裝的的 Python 接口,但是對我這個小白白來說上手還是有點難度,后來發現 PayPal 有現成的 Django 模塊,想著以學習的目的來實現這一功能(其實還是自己辣雞),就決定以 PayPal 的電子支付功能來練手。

首先,安裝 PayPal 的 Django 模塊:django-paypal,具體介紹可以參考 GitHub上說明: https://github.com/spookylukey/django-paypal

pip install django-paypal

然后在 settings.py 中的 INSTALLED_APPS 將 'paypal.standard.ipn' 加入。并在 settings.py 中添加下列語句。

# 此付款機制作為測試用PAYPAL_TEST = True# 設置收款的 PayPal 電子郵件賬戶PAYPAL_REVEIVER_EMAIL = 'your email'

執行同步數據庫操作。

./manage.py migrate

urls.py 中加入下列樣式。分別為付款完成通知,處理賬務,顯示完成付款,取消付款操作。

url(r'^paypal/', include('paypal.standard.ipn.urls')), # 付款完成通知url(r'^payment/(/d+)/$', views.payment),url(r'^done/$', views.payment_done),url(r'^canceled/$', views.payment_canceled),

PayPal 付款操作,建立含有正確數據的付款按鈕。

@login_requireddef payment(request, order_id): all_categories = models.Category.objects.all() try:  order = models.Order.objects.get(id=order_id) except:  messages.add_message(request, messages.WARNING, "訂單編號錯誤,無法處理付款。")  return redirect('/myorders/') all_order_items = models.OrderItem.objects.filter(order=order) items = list() total = 0 for order_item in all_order_items:  t = dict()  t['name'] = order_item.product.name  t['price'] = order_item.product.price  t['quantity'] = order_item.quantity  t['subtotal'] = order_item.product.price * order_item.quantity  total = total + order_item.product.price  items.append(t) host = request.get_host() paypal_dict = {  "business": settings.PAYPAL_REVEIVER_EMAIL,  "amount": total,  "item_name": "迷你小電商商品編號:{}".format(order_id),  "invoice": "invoice-{}".format(order_id),  "currency_code": 'CNY',  "notify_url": "http://{}{}".format(host, reverse('paypal-ipn')),  "return_url": "http://{}/done/".format(host),  "cancel_return": "http://{}/canceled/".format(host),  } paypal_form = PayPalPaymentsForm(initial=paypal_dict) template = get_template('payment.html') html = template.render(context=locals(), request=request) return HttpResponse(html)

由于用到了 django-paypal 提供的 PayPalPaymentForm 類。因此在 views.py 的前面也要導入這個類。另外,因為用到了 settings.py 中的常數,所以也要導入 settings,語句如下:

from django.conf import settingsfrom paypal.standard.forms import PayPalPaymentsFormfrom django.core.urlresolvers import reverse            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 缙云县| 富顺县| 淳化县| 依安县| 普定县| 嵩明县| 越西县| 大庆市| 宜君县| 呼伦贝尔市| 桐梓县| 聂荣县| 旌德县| 烟台市| 信阳市| 洛南县| 门头沟区| 高碑店市| 敖汉旗| 民丰县| 安岳县| 三门县| 凤凰县| 和林格尔县| 察雅县| 东方市| 莱州市| 桂平市| 堆龙德庆县| 松阳县| 包头市| 新乡市| 英山县| 海南省| 古田县| 株洲县| 六枝特区| 香河县| 津市市| 攀枝花市| 武强县|