You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot项目中实现Amazon Pay访客结账?

Amazon Pay访客结账实现方案

1. 后端Payload添加访客结账配置

要开启访客结账,核心是在生成payload的后端代码中加入allowGuestCheckout: true字段,示例Payload结构如下:

{
  "webCheckoutDetails": {
    "checkoutReviewReturnUrl": "https://你的域名.com/review",
    "checkoutResultReturnUrl": "https://你的域名.com/result"
  },
  "storeId": "你的店铺ID",
  "allowGuestCheckout": true,
  "estimatedOrderAmount": {
    "amount": "109.99",
    "currencyCode": "USD"
  }
}

生成该Payload后,用你的Amazon Pay私钥签名得到signature,逻辑和通用结账的签名流程一致。

2. 前端代码适配

你的现有前端代码无需大幅修改,只要后端Payload包含allowGuestCheckout: true,Amazon Pay按钮会自动展示访客结账选项。调整后的完整前端代码示例:

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <h1>结账</h1>

    <div id="AmazonPayButton"></div>
    <script src="https://static-na.payments-amazon.com/checkout.js" th:inline="javascript"></script>
    <script type="text/javascript" charset="utf-8" th:inline="javascript">
        var payload = /*[[${payload}]]*/ "here";
        var signature = /*[[${signature}]]*/ "here";
        console.log(payload);
        console.log(signature);

        const amazonPayButton = amazon.Pay.renderButton('#AmazonPayButton', {
          merchantId: 'xxxx',
          publicKeyId: 'SANDBOX-xxxx',
          ledgerCurrency: 'USD',
          checkoutLanguage: 'en_US',
          productType: 'PayAndShip',
          placement: 'Home',
          buttonColor: 'Gold',
          estimatedOrderAmount: { "amount": "109.99", "currencyCode": "USD"},
          createCheckoutSessionConfig: {
              payloadJSON: payload,
              signature: signature
          }
        });
    </script>
</body>
</html>

3. 关键注意事项

  • 沙箱环境默认支持访客结账,生产环境需在Amazon Pay卖家后台确认开启该权限
  • checkoutReviewReturnUrl和checkoutResultReturnUrl必须是你域名下的有效地址,用于跳转回订单确认页面
  • 生成签名时要保证Payload的JSON格式严格合规,不能有多余空格,否则会导致签名验证失败

内容的提问来源于stack exchange,提问作者Sahan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:21:35