📜  2checkout ipn验证响应python - 任何代码示例

📅  最后修改于: 2022-03-11 14:57:56.598000             🧑  作者: Mango

代码示例1
import hmac
import hashlib
import datetime

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt


def hmac_md5(key, value):
    message = value.encode('utf-8')
    return hmac.new(key.encode('utf-8'), message, digestmod=hashlib.md5).hexdigest()

@csrf_exempt
def subscription_listener(request):
    if request.method == "GET":
        return HttpResponse("Hello")

    if request.method == "POST":
        pass_str = "SECRET_KEY_HERE"
        DATE = datetime.datetime.now().strftime("%Y%m%d%H%M%S")

        original_dict = dict(request.POST)
        base_string_for_return_hash = str(len(original_dict['IPN_PID[]'][0])) + str(
            original_dict['IPN_PID[]'][0]) + str(
            len(original_dict['IPN_PNAME[]'][0])) + str(original_dict['IPN_PNAME[]'][0]) + str(
            len(original_dict['IPN_DATE'][0])) + str(original_dict['IPN_DATE'][0]) + str(len(DATE)) + str(DATE)

        hashed = hmac_md5(pass_str, base_string_for_return_hash)
        
        response = "{}|{}".format(DATE, hashed)

        return HttpResponse(response)