Test.py 3.4 KB

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