【问题标题】:Unable to verify webhooks from PayPal in Python无法在 Python 中验证来自 PayPal 的 webhook
【发布时间】:2021-07-06 07:06:34
【问题描述】:

我正在尝试使用 django python 在 paypal 中验证订阅的 webhook。我收到了 webhook,但是当我发送它们进行验证时,我收到了这个错误:{'name': 'VALIDATION_ERROR', 'message': 'Invalid request - see details', 'debug_id': 'ccc873865982', 'details': [{'field': '/', 'location': 'body', 'issue': 'MALFORMED_REQUEST_JSON'}], 'links': []}。我已经检查了给出 400 响应的响应的状态代码是什么。通过查看API Docs,我看到无效请求 + 400 响应是验证错误(与 Json 格式有关,因此很可能是语法错误)或授权错误(表示我需要更改范围)。我认为这是验证错误,因为错误指向正文。

以下是相关代码:

header_params = {
    "Accept": "application/json",
    "Accept-Language": "en_US",
}

param = {
    
    "grant_type": "client_credentials",
}

cid = settings.PAYPAL_CLIENT_ID

secret = settings.PAYPAL_CLIENT_SECRET

token_i = requests.post('https://api-m.sandbox.paypal.com/v1/oauth2/token', auth=(cid, secret), headers=header_params, data=param).json()

token = token_i["access_token"]

bearer_token =  "Bearer x".replace('x', token)

headers = {
    "Content-Type": "application/json",
    "Authorization": bearer_token,
}

print(request.body)

webhook_event = request.body.decode("utf-8")

data = {
    
    "transmission_id": request.headers["PAYPAL-TRANSMISSION-ID"],
    "transmission_time": request.headers["PAYPAL-TRANSMISSION-TIME"],
    "cert_url": request.headers["PAYPAL-CERT-URL"],
    "auth_algo": request.headers["PAYPAL-AUTH-ALGO"],
    "transmission_sig": request.headers["PAYPAL-TRANSMISSION-SIG"],
    "webhook_id": "3AJ143072C221060T",
    "webhook_event": webhook_event,
    
}

print(json.dumps(data))

response = requests.post('https://api-m.sandbox.paypal.com/v1/notifications/verify-webhook-signature', headers=headers, json=json.dumps(data)).json()

print('worked')

print(response)

if response["verification_status"] == "SUCCESS":

    print('success')

这是print(json.dumps(data)) 的打印(我删除了一些ID):

{
    "transmission_id": "id_is_here",
    "transmission_time": "2021-07-04T23:37:29Z",
    "cert_url": "https://api.sandbox.paypal.com/v1/notifications/certs/CERT-360caa42-fca2a594-7a8abba8",
    "auth_algo": "auth_algo",
    "transmission_sig": "EODOx5y8kIDycYiBYcIgByiBzHyEfu1/NS2nsumOIksVuw/2vJwHj2FcuHYxIa4n/s+s25xkeqk0CXPiSuqtNUGv4pvFtpwbCVAOCU+Msn304+wBgyb7G24rwUPwrof/5jHYQxqKKX5RzxTrff4oPnisKBDUUXV4s2+KO3h2RYAhrXtwTSPt7cK5ZbGZ6SmfpYJ8qDnYFh4PIesLeflSPQ4vHQrFbgr3NiW63sZruGFJc0hTWWc8L3BhzDuUfiSrxBJLAtrqReC8R0HSV8D+Ywmdeipep54yZeJZXfbmUUGvSYbmVMsVggyzZnltyl1hP5xUd3iIi2jdNWYpLESZzA==",
    "webhook_id": "Webhook_id_is_here",
    "webhook_event": "{\"id\":\"id_here\",\"event_version\":\"1.0\",\"create_time\":\"2021-07-04T23:37:26.733Z\",\"resource_type\":\"subscription\",\"resource_version\":\"2.0\",\"event_type\":\"BILLING.SUBSCRIPTION.CREATED\",\"summary\":\"Subscription created\",\"resource\":{\"start_time\":\"2021-07-04T23:37:26Z\",\"quantity\":\"1\",\"create_time\":\"2021-07-04T23:37:26Z\",\"custom_id\":\"custom_id_here\",\"links\":[{\"href\":\"https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=BA-2UF06918UT180770Y\",\"rel\":\"approve\",\"method\":\"GET\"},{\"href\":\"https://api.sandbox.paypal.com/v1/billing/subscriptions/I-4S15814RUE18\",\"rel\":\"edit\",\"method\":\"PATCH\"},{\"href\":\"https://api.sandbox.paypal.com/v1/billing/subscriptions/I-4S15814RUE18\",\"rel\":\"self\",\"method\":\"GET\"}],\"id\":\"sub_id_here_\",\"plan_overridden\":false,\"plan_id\":\"P-0DA33732CG980003EMDQJ6BA\",\"status\":\"APPROVAL_PENDING\"},\"links\":[{\"href\":\"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-91E32247D9338170B-4RF07261WK370823W\",\"rel\":\"self\",\"method\":\"GET\"},{\"href\":\"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/id_here/resend\",\"rel\":\"resend\",\"method\":\"POST\"}]}"
}

【问题讨论】:

  • 尝试从请求中删除json.dumps
  • @KlausD。这给了我错误{'name': 'VALIDATION_ERROR', 'message': 'Invalid request - see details', 'debug_id': 'eb8249fc5cf0f', 'details': [{'field': '/webhook_event', 'location': 'body', 'issue': 'MALFORMED_REQUEST_JSON'}], 'links': []}
  • 看起来你必须 loads() 来自 webhook_event 的 JSON。

标签: python json django paypal python-requests


【解决方案1】:

您的密钥 webhook_event 的数据被双重编码为​​ JSON。

你必须用它来解码

webhook_event = json.loads(webhook_event)

或者一开始就不对其进行编码。

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 2020-08-07
    • 1970-01-01
    • 2020-01-30
    • 2014-12-26
    • 2020-09-19
    • 2021-07-26
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多