如何在Django中集成Coinbase Commerce Webhook API

问题描述

我正在尝试将Coinbase Commerce Webhook API集成到我的Django应用中;但似乎是在以正确的方式做事。我在网上搜索了超过2天,但没有针对此的解决方案。 Coinbase商业官方文档没有提供将其集成到Django中的方法。请您的帮助将不胜感激。这就是我尝试过的;但继续抛出错误

from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from coinbase_commerce.error import WebhookInvalidPayload,SignatureVerificationError
from coinbase_commerce.webhook import Webhook
from django.http import HttpResponse
import json

WEBHOOK_SECRET = settings.COINBASE_SECRET

@csrf_exempt
def payment_webhook(request):
  
    request_data = request.data.decode('utf-8')
    request_sig  = request.headers.get('X-CC-Webhook-Signature',None)

    try:
        event = Webhook.construct_event(request_data,request_sig,WEBHOOK_SECRET)
    except (WebhookInvalidPayload,SignatureVerificationError) as e:
        return HttpResponse(e,status=400)

    print("Received event: id={id},type={type}".format(id=event.id,type=event.type))
    return HttpResponse('ok',status=200)

解决方法

我遇到了同样的问题,但是由于我只是创建一个端点来接收来自“ charge:confirmed”的webhook,从而确认有人付款了。我决定以其他方式对请求进行验证。

在收到来自Webhook的请求后,我检查event['data']['timeline']对象是否存在status COMPLETED,根据https://commerce.coinbase.com/docs/api/指出已付款完成。