【问题标题】:How to integrate coinbase commerce Webhook API in Django如何在 Django 中集成 coinbase commerce Webhook API
【发布时间】:2020-08-29 02:33:20
【问题描述】:

我正在尝试将 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)

【问题讨论】:

  • “但继续抛出错误”:错误是什么?
  • 错误请求:/"POST / HTTP/1.1" 400 53
  • 错误响应中是否有额外的信息?
  • 你找到解决办法了吗?

标签: python django api webhooks coinbase-api


【解决方案1】:

我面临同样的问题,但因为我只是创建一个端点来接收来自“charge:confirmed”的 webhook,它确认有人付款。我决定以不同的方式验证请求。

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

【讨论】:

    【解决方案2】:

    在 Django 中,您必须使用 request.body.decode('utf-8') 而不是 request.data.decode('utf-8')

        request_data = request.body.decode('utf-8')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2023-01-14
      • 2014-11-26
      • 1970-01-01
      • 2019-12-19
      • 2022-08-05
      相关资源
      最近更新 更多