Test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. '''
  16. http://192.168.136.40:8077/Test
  17. '''
  18. from Object.ResponseObject import ResponseObject
  19. from Ansjer.config import BASE_DIR
  20. import json
  21. from alipay import AliPay
  22. import time
  23. # 测试接口sdk
  24. class Test(View):
  25. def get(self, request, *args, **kwargs):
  26. response = ResponseObject()
  27. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  28. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  29. orderID = int(time.time()) + int(time.time())
  30. alipay = AliPay(
  31. appid="2016092200569234",
  32. app_notify_url=None, # the default notify path
  33. app_private_key_string=app_private_key_string,
  34. # alipay public key, do not use your own public key!
  35. alipay_public_key_string=alipay_public_key_string,
  36. sign_type="RSA2", # RSA or RSA2
  37. debug=False # False by default
  38. )
  39. order_string = alipay.api_alipay_trade_wap_pay(
  40. out_trade_no=str(orderID),
  41. total_amount=0.01,
  42. subject="实用性充气式玩具",
  43. return_url="http://192.168.136.40:8077/",
  44. notify_url="http://test.dvema.com:8077/Test" # this is optional
  45. )
  46. print(order_string)
  47. from django.http import HttpResponseRedirect
  48. # return response.json(0,"https://openapi.alipay.com/gateway.do? + {order_string}".format(order_string=order_string))
  49. print(order_string)
  50. return HttpResponseRedirect("https://openapi.alipaydev.com/gateway.do?" + order_string)
  51. return HttpResponseRedirect("https://openapi.alipay.com/gateway.do?" + order_string)
  52. def post(self, request, *args, **kwargs):
  53. response = ResponseObject()
  54. data = request.POST.dict()
  55. signature = data["sign"]
  56. data.pop('sign')
  57. print(json.dumps(data))
  58. print(signature)
  59. # verify
  60. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  61. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  62. alipay = AliPay(
  63. appid="2016092200569234",
  64. app_notify_url=None, # the default notify path
  65. app_private_key_string=app_private_key_string,
  66. alipay_public_key_string=alipay_public_key_string,
  67. sign_type="RSA2", # RSA or RSA2
  68. debug=False # False by default
  69. )
  70. success = alipay.verify(data, signature)
  71. if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
  72. print("trade succeed")
  73. return response.json(0, signature)
  74. # 修改 资源改变
  75. def put(self, request):
  76. response = ResponseObject()
  77. return response.json(0, '')
  78. # 修改 属性改变
  79. def PATCH(self, request):
  80. response = ResponseObject()
  81. return response.json(0)
  82. # 删除
  83. def delete(self, request):
  84. response = ResponseObject()
  85. return response.json(0)
  86. def validation(self, request_dict, *args, **kwargs):
  87. response = ResponseObject()
  88. return response.json(0)