Test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. print(data)
  56. return response.json(0,data)
  57. response = ResponseObject()
  58. signature = data["sign"]
  59. data.pop('sign')
  60. print(json.dumps(data))
  61. print(signature)
  62. # verify
  63. app_private_key_string = open(BASE_DIR + '/Controller/alipay_private_2048.pem').read()
  64. alipay_public_key_string = open(BASE_DIR + '/Controller/alipay_public_2048.pem').read()
  65. alipay = AliPay(
  66. appid="2016092200569234",
  67. app_notify_url=None, # the default notify path
  68. app_private_key_string=app_private_key_string,
  69. alipay_public_key_string=alipay_public_key_string,
  70. sign_type="RSA2", # RSA or RSA2
  71. debug=False # False by default
  72. )
  73. success = alipay.verify(data, signature)
  74. if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
  75. print("trade succeed")
  76. return response.json(0, signature)
  77. # 修改 资源改变
  78. def put(self, request):
  79. response = ResponseObject()
  80. return response.json(0, '')
  81. # 修改 属性改变
  82. def PATCH(self, request):
  83. response = ResponseObject()
  84. return response.json(0)
  85. # 删除
  86. def delete(self, request):
  87. response = ResponseObject()
  88. return response.json(0)
  89. def validation(self, request_dict, *args, **kwargs):
  90. response = ResponseObject()
  91. return response.json(0)