AUTHENTICATION_FAILURE。贝宝付款错误 api。 Python

AUTHENTICATION_FAILURE. Error with paypal payment api. Python

我想为客户提供付款方式。您需要收款并检查客户是否已付款。我成功创建了付款表单,但出现错误。

import requests

resp = requests.post(
    url="https://api-m.sandbox.paypal.com/v1/oauth2/token",
    auth=("XXXX", "XXXXX"),
    data=b"grant_type=client_credentials",
    headers={
        "Accept": "application/json",
        "Accept-Language": "en_US",
    },
)
resp.raise_for_status()

access_token = resp.json()['access_token']
print(access_token)
# =======================================
r = requests.post(url="https://api-m.sandbox.paypal.com/v1/payments/payment", headers={"Content-Type": "application/json", "Authorization": f"Bearer {access_token}",},
                  data='''{
  "intent": "sale",
  "payer": {
    "payment_method": "paypal"
  },
  "transactions": [
    {
      "amount": {
        "total": "30.11",
        "currency": "USD",
        "details": {
          "subtotal": "30.00",
          "tax": "0.07",
          "shipping": "0.03",
          "handling_fee": "1.00",
          "shipping_discount": "-1.00",
          "insurance": "0.01"
        }
      },
      "description": "The payment transaction description.",
      "custom": "EBAY_EMS_90048630024435",
      "invoice_number": "48787589673",
      "payment_options": {
        "allowed_payment_method": "INSTANT_FUNDING_SOURCE"
      },
      "soft_descriptor": "ECHI5786786",
      "item_list": {
        "items": [
          {
            "name": "hat",
            "description": "Brown hat.",
            "quantity": "5",
            "price": "3",
            "tax": "0.01",
            "sku": "1",
            "currency": "USD"
          },
          {
            "name": "handbag",
            "description": "Black handbag.",
            "quantity": "1",
            "price": "15",
            "tax": "0.02",
            "sku": "product34",
            "currency": "USD"
          }
        ],
        "shipping_address": {
          "recipient_name": "Brian Robinson",
          "line1": "4th Floor",
          "line2": "Unit #34",
          "city": "San Jose",
          "country_code": "US",
          "postal_code": "95131",
          "phone": "011862212345678",
          "state": "CA"
        }
      }
    }
  ],
  "note_to_payer": "Contact us for any questions on your order.",
  "redirect_urls": {
    "return_url": "https://example.com/return",
    "cancel_url": "https://example.com/cancel"
  }
}''')

print(r.status_code, r.json())

作为响应,我得到了代码 201。创建的付款和链接的详细信息。如果我没理解错,授权错误。

201 {'id': 'PAYID-MD5GFYY780432685H998443M', 'intent': 'sale', 'state': 'created', 'payer': {'payment_method': 'paypal'}, 'transactions': [{'amount': {'total': '30.11', 'currency': 'USD', 'details': {'subtotal': '30.00', 'tax': '0.07', 'shipping': '0.03', 'insurance': '0.01', 'handling_fee': '1.00', 'shipping_discount': '-1.00'}}, 'description': 'The payment transaction description.', 'custom': 'EBAY_EMS_90048630024435', 'invoice_number': '48787589673', 'soft_descriptor': 'ECHI5786786', 'payment_options': {'allowed_payment_method': 'INSTANT_FUNDING_SOURCE', 'recurring_flag': False, 'skip_fmf': False}, 'item_list': {'items': [{'name': 'hat', 'sku': '1', 'description': 'Brown hat.', 'price': '3.00', 'currency': 'USD', 'tax': '0.01', 'quantity': 5}, {'name': 'handbag', 'sku': 'product34', 'description': 'Black handbag.', 'price': '15.00', 'currency': 'USD', 'tax': '0.02', 'quantity': 1}], 'shipping_address': {'recipient_name': 'Brian Robinson', 'line1': '4th Floor', 'line2': 'Unit #34', 'city': 'San Jose', 'state': 'CA', 'postal_code': '95131', 'country_code': 'US', 'phone': '011862212345678'}}, 'related_resources': []}], 'note_to_payer': 'Contact us for any questions on your order.', 'create_time': '2021-07-23T06:34:11Z', 'links': [{'href': 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MD5GFYY780432685H998443M', 'rel': 'self', 'method': 'GET'}, {'href': 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3NK43651M3347822W', 'rel': 'approval_url', 'method': 'REDIRECT'}, {'href': 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MD5GFYY780432685H998443M/execute', 'rel': 'execute', 'method': 'POST'}]}

Process finished with exit code 0

当我点击链接时,我看到了这个:

{"name":"AUTHENTICATION_FAILURE","message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]}

我设法得到 access_token。而且我确信其他标记是正确的。告诉我如何解决这个问题。我一定是在某个地方犯了错误。预先感谢您的帮助。

您似乎“点击”了错误的 link,rel:execute 的 href,而不是 rel:approve 的 href。

rel:execute link 用于 付款人批准付款并重定向回 return_url 您的网站应该显示其最后的确认步骤。在您执行最终执行 API 确认调用之前,不会创建任何交易(销售)。


以上所有术语均适用于已弃用的 v1/payments API,无论如何您都不应该将其用于任何用途。对于这样的结帐,请改用当前的 v2/checkout/orders API。