如何授权 amazon pay 请求到 "get checkout session"?

How to authorize amazon pay request to "get checkout session"?

我一直在尝试将 Amazon Pay 集成为我网站上客户的一种付款方式,但 运行 遇到了文档中详述的一些问题。我希望更好地理解与调用亚马逊支付相关联的请求 headers api。

我正在向 'https://pay-api.amazon.com/v2/checkoutSessions/checkoutSessionId' 发出请求并收到 CORS 策略错误。

Access to fetch at 'https://pay-api.amazon.com/v2/checkoutSessions/d9b4418d-0c6f-4085-8c37-08bef6da6807' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这是我尝试发出请求的提取请求

fetch(`https://pay-api.amazon.com/v2/checkoutSessions/${this.$route.query.amazonCheckoutSessionId}`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'authorization': 'Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE',
      'x-amz-pay-date': `${new Date()}`
    }
  })

this.$route.query.amazonCheckoutSessionId 在用户使用亚马逊支付按钮创建结帐 session 后引用返回的 url 扩展。

文档概述了应按如下方式提出请求

curl "https://pay-api.amazon.com/:version/checkoutSessions/:checkoutSessionId"
-X GET
-H "authorization:Px2e5oHhQZ88vVhc0DO%2FsShHj8MDDg%3DEXAMPLESIGNATURE"
-H "x-amz-pay-date:20201012T235046Z"

谁能解释一下我应该从哪里获得授权字符串及其格式?另外,有没有办法轻松地将日期字符串格式化为文档中显示的格式?或者日期字符串格式无关紧要?

我通过与 Amazon pay 相关的堆栈溢出帖子(其中很少)进行了相当广泛的搜索,并搜索了其他 Amazon 和 AWS 文档以详细说明如何格式化 auth 字符串。不幸的是,我似乎找不到答案。我也试过将我的按钮签名​​作为我的授权字符串传递,但这似乎没有帮助。

感谢您提供的任何帮助。

您的问题分为两个部分:

  1. API 并非设计用于侦听示例中的浏览器 JS (AJAX) 请求。 CORS 限制已到位以防止这种情况。相反,这部分过程将在服务器端完成
  2. 要使用 API,我强烈建议使用其中一种 SDK (https://pay.amazon.co.uk/help/201212390?ld=APUKLPADirect). You will notice in the docs (https://developer.amazon.com/docs/amazon-pay-checkout/add-the-amazon-pay-button.html), that they always have the code samples for all four offered SDKs. So it is much easier to follow those instructions.

Amazon Pay API 不支持直接客户端请求,因此您需要在服务器端发出这些请求。这就是您看到 CORS 错误的原因。

您可以在此处找到签署每个 API 请求所需的签名生成的详细演练:https://developer.amazon.com/docs/amazon-pay-api-v2/signing-requests.html

您应该能够利用 Amazon Pay Node.js SDK,这将节省大量编码 - https://developer.amazon.com/docs/amazon-pay-checkout/get-set-up-for-integration.html#nodejstab

我还建议使用开发人员便笺簿来检查您的工作并获取有关所需代码的提示,因为它会为您提出请求并生成代码片段! https://pay-api.amazon.com/tools/scratchpad/index.html