Test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/5/22 13:58
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: Test.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.views.generic.base import View
  15. from django.utils.decorators import method_decorator
  16. from django.views.decorators.csrf import csrf_exempt
  17. # from Ansjer.settings import *
  18. from django.contrib import auth
  19. from django.http import HttpResponseRedirect
  20. '''
  21. http://192.168.136.40:8077/Test
  22. '''
  23. import json
  24. import requests
  25. from Service.ModelService import ModelService
  26. from django.middleware.csrf import get_token
  27. from django.http import JsonResponse
  28. from ratelimit.decorators import ratelimit
  29. from Object.ResponseObject import ResponseObject
  30. from Object.TokenObject import TokenObject
  31. from Object.RedisObject import RedisObject
  32. from django.shortcuts import render_to_response,render,redirect
  33. import paypalrestsdk
  34. from django.http import HttpResponseRedirect,HttpResponse
  35. class Test(View):
  36. '''
  37. limit=10:指定返回记录的数量
  38. offset=10:指定返回记录的开始位置。
  39. page=2&per_page=100:指定第几页,以及每页的记录数。
  40. sortby=name&order=asc:指定返回结果按照哪个属性排序,以及排序顺序。
  41. animal_type_id=1:指定筛选条件
  42. '''
  43. @method_decorator(csrf_exempt)
  44. def dispatch(self, *args, **kwargs):
  45. return super(Test, self).dispatch(*args, **kwargs)
  46. # 查询
  47. '''
  48. /Test?paymentId=PAY-4VL25365CA733482BLPMAHBI&token=EC-1SU951197H0006730&PayerID=A29TCMY2J8G9C
  49. '''
  50. @ratelimit(key='ip', rate='1/m')
  51. def get(self, request, *args, **kwargs):
  52. # flake8: noqa
  53. from qiniu import Auth
  54. # 需要填写你的 Access Key 和 Secret Key
  55. access_key = 'dwdHw1pQ9wFNiHDdflBUiZyWmiLppd5VCC2yF0O_'
  56. secret_key = 'uTn7hdKD9Mbf2dzMWjLj2e9V1nXN7HAsOGRKwqDF'
  57. # 构建鉴权对象
  58. q = Auth(access_key, secret_key)
  59. # 要上传的空间
  60. bucket_name = 'test'
  61. # 上传到七牛后保存的文件名
  62. key = '/*'
  63. # 生成上传 Token,可以指定过期时间等
  64. # 上传策略示例
  65. # https://developer.qiniu.com/kodo/manual/1206/put-policy
  66. policy = {
  67. # 'callbackUrl':'https://requestb.in/1c7q2d31',
  68. # 'callbackBody':'filename=$(fname)&filesize=$(fsize)'
  69. # 'persistentOps':'imageView2/1/w/200/h/200'
  70. }
  71. # 3600为token过期时间,秒为单位。3600等于一小时
  72. token = q.upload_token(bucket_name, key, 3600, policy)
  73. print(token)
  74. return HttpResponse(content=token,status=200)
  75. '''
  76. :param request:
  77. :param args:
  78. :param kwargs:
  79. :return:
  80. '''
  81. response = ResponseObject()
  82. # return response.json(0)
  83. paymentId = request.GET.get('paymentId',None)
  84. PayerID = request.GET.get('PayerID',None)
  85. if paymentId and PayerID:
  86. paypalrestsdk.configure({
  87. "mode": "sandbox", # sandbox or live
  88. "client_id": "AfnfDqezODOoWGS-W2Itu-Zl1ay1R95IsGlMqPghPA3KGhkPndNMnQT0bdEewvSv92XAFIfLiinmyhBL",
  89. "client_secret": "EErLskwYA1xXY3890mHx5OhzgK83B2rNc57zIozGNyKc8i6RJuhPTF9WyhhdZgyDEih0heo1MH9Jk1lj"
  90. })
  91. # ID of the payment. This ID is provided when creating payment.
  92. payment = paypalrestsdk.Payment.find(paymentId)
  93. payres = payment.execute({"payer_id": PayerID})
  94. print(payres)
  95. if payres:
  96. print("Payment execute successfully")
  97. else:
  98. print(payment.error) # Error Hash
  99. return response.json(0)
  100. else:
  101. pass
  102. paypalrestsdk.configure({
  103. "mode": "sandbox", # sandbox or live
  104. "client_id": "AfnfDqezODOoWGS-W2Itu-Zl1ay1R95IsGlMqPghPA3KGhkPndNMnQT0bdEewvSv92XAFIfLiinmyhBL",
  105. "client_secret": "EErLskwYA1xXY3890mHx5OhzgK83B2rNc57zIozGNyKc8i6RJuhPTF9WyhhdZgyDEih0heo1MH9Jk1lj"})
  106. payment = paypalrestsdk.Payment({
  107. "intent": "sale",
  108. "payer": {
  109. "payment_method": "paypal"},
  110. "redirect_urls": {
  111. # "return_url": "http://192.168.136.45:3000/payment/execute",
  112. "return_url": "http://192.168.136.40:8077/Test",
  113. "cancel_url": "http://localhost:3000/"},
  114. "transactions": [{
  115. "item_list": {
  116. "items": [{
  117. "name": "item",
  118. "sku": "item",
  119. "price": "5.00",
  120. "currency": "USD",
  121. "quantity": 1}]},
  122. "amount": {
  123. "total": "5.00",
  124. "currency": "USD"},
  125. "description": "This is the payment transaction description."}]})
  126. if payment.create():
  127. print("Payment created successfully")
  128. else:
  129. print(payment.error)
  130. print(payment)
  131. for link in payment.links:
  132. if link.rel == "approval_url":
  133. # Convert to str to avoid Google App Engine Unicode issue
  134. # https://github.com/paypal/rest-api-sdk-python/pull/58
  135. approval_url = str(link.href)
  136. print("Redirect for approval: %s" % (approval_url))
  137. return HttpResponseRedirect(approval_url)
  138. # 增加
  139. def post(self, request, *args, **kwargs):
  140. response = ResponseObject()
  141. return response.json(0)
  142. # 修改 资源改变
  143. def put(self, request):
  144. response = ResponseObject()
  145. return response.json(0)
  146. # 修改 属性改变
  147. def PATCH(self, request):
  148. response = ResponseObject()
  149. return response.json(0)
  150. # 删除
  151. def delete(self, request):
  152. response = ResponseObject()
  153. return response.json(0)
  154. def validation(self, request_dict, *args, **kwargs):
  155. response = ResponseObject()
  156. return response.json(0)