Amazon Pay SDK CompleteCheckoutSession 错误
Amazon Pay SDK CompleteCheckoutSession Error
我正在沙盒中使用来自亚马逊的 v2 c# SDK(MVC 应用程序)将亚马逊支付与我的网站集成。设置一切正常,我创建了密钥、return url 等
在我的结帐过程中,我创建了成功的 CheckoutSession。我点击亚马逊支付按钮,使用我的测试买家账户登录,然后使用他们有效的测试信用卡“支付”商品。
亚马逊重定向到我的 return URL,并按预期使用结帐会话 ID。
当我尝试 CompleteCheckoutSession 时,结果是从亚马逊返回的错误 API
error: InvalidCheckoutSessionStatus
message: You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed
我在尝试完成之前输入了一行代码来检索 CheckoutSession 查看它,它显示当前状态为“打开”,这是尝试完成时的正确状态,所以我不知道为什么结帐会话状态无效。
编辑:
请注意,我将此流程用于我的交易,因此没有对交易的“审查”。买家在我的网站上选择他的商品。
另请注意,我正在根据此动态创建有效载荷:
因此,当单击 amazonpay 按钮时,它会调用我网站上的一个方法,该方法构建开始 CheckoutSession 的有效负载。然后,我将 Amazon Session ID 保存在我的缓存中,并保存总数。有效负载被 returned 到亚马逊支付脚本,然后将我带到亚马逊网站。我选择付款类型并单击“继续结帐”,这会将我带回我的网站以执行“完成”步骤。
我对 CompleteCheckoutSession(sessionId) 的请求
{"chargeAmount":{"amount":99,"currencyCode":"USD"}}
result.RawResponse 来自 client.CompleteCheckoutSession(sessionId) 方法:
{
"reasonCode":"InvalidCheckoutSessionStatus",
"message":"You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed"
}
编辑示例代码:
我创建了一个具有基本功能的全新测试 MVC 应用程序:
public ActionResult Index()
{
var client = InitiateClient(); //hidden for security
// prepare the request
var request = new CreateCheckoutSessionRequest
(
checkoutReviewReturnUrl: "http://localhost:44300/home/completecheckout",
storeId: "amzn1.application-oa2-client.mystoreid"
);
request.PaymentDetails.PaymentIntent = Amazon.Pay.API.WebStore.Types.PaymentIntent.AuthorizeWithCapture;
request.PaymentDetails.ChargeAmount.Amount = 99;
request.PaymentDetails.ChargeAmount.CurrencyCode = Currency.USD;
// generate the signature and payload string that is passed back to the frontend
ViewBag.Signature = client.GenerateButtonSignature(request);
ViewBag.Payload = request.ToJson();
return View();
}
在 Index.cshtml 文件中:
<div id="AmazonPayButton"></div>
<script src="https://static-na.payments-amazon.com/checkout.js"></script>
<script type="text/javascript" charset="utf-8">
amazon.Pay.renderButton('#AmazonPayButton', {
merchantId: 'mymerchantid',
ledgerCurrency: 'USD',
sandbox: true,
checkoutLanguage: 'en_US',
productType: 'PayOnly',
placement: 'Checkout',
buttonColor: 'Gold',
createCheckoutSessionConfig: {
payloadJSON: '@Html.Raw(ViewBag.Payload)', // string generated in step 2
signature: '@Html.Raw(ViewBag.Signature)', // signature generated in step 3
publicKeyId: 'AGPTYXGL5VH6PSYLJUSHTKW6'
}
});
</script>
最后,完整的结帐代码,未成功:
public ActionResult CompleteCheckout(string amazonCheckoutSessionId)
{
var client = InitiateClient(); //hidden for security
var request = new CompleteCheckoutSessionRequest(99.00M, Currency.USD);
// send the request
var result = client.CompleteCheckoutSession(amazonCheckoutSessionId, request);
// check if API call was successful
if (!result.Success)
{
throw new Exception("API Call unsuccessful");
}
return View();
}
注意:某些键被混淆了,但实际键在示例代码中。
手动创建 CheckoutSession 不适用于您正在遵循的“无评论页面”流程。如果您想集成此流程,则必须让 Amazon Pay 使用按钮代码中的 createCheckoutSessionConfig 参数为您创建 CheckoutSession,请参见此处:https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-apb-checkout/add-the-amazon-pay-button.html#4-render-the-button
编辑:
下面的代码示例展示了如何为 'no order review' 支付流程构建有效负载和签名。请注意 CheckoutMode 参数,它指示 Amazon Pay 立即处理付款,以及 checkoutResultReturnUrl,它定义了 URL 买家被发送到哪里以完成结账。当用户到达 URL 时,您必须调用 Complete CheckoutSession,现在应该会成功。
public ActionResult Index()
{
var client = InitiateClient(); //hidden for security
// prepare the request
var request = new CreateCheckoutSessionRequest
(
checkoutReviewReturnUrl: null,
storeId: "amzn1.application-oa2-client.mystoreid"
);
// instructs Amazon Pay to immediately process the payment
request.WebCheckoutDetails.CheckoutMode = CheckoutMode.ProcessOrder;
request.WebCheckoutDetails.CheckoutResultReturnUrl = "http://localhost:44300/home/completecheckout";
// payment details
request.PaymentDetails.PaymentIntent = Amazon.Pay.API.WebStore.Types.PaymentIntent.AuthorizeWithCapture;
request.PaymentDetails.ChargeAmount.Amount = 99;
request.PaymentDetails.ChargeAmount.CurrencyCode = Currency.USD;
// generate the signature and payload string that is passed back to the frontend
ViewBag.Signature = client.GenerateButtonSignature(request);
ViewBag.Payload = request.ToJson();
return View();
}
我遇到了类似的问题,我花了很长时间才弄明白。这就是发生在我身上的事情。
我无意创建生产代码。我只想创建一个只有 Amazon Pay 按钮的测试页面。我用了几个node js脚本来getCheckoutSession/updateCheckoutSession
。但是,当我尝试 completeCheckoutSession
时,我得到了“InvalidCheckoutSessionStatus”。
最后,我发现,在我设置所有必需的参数并删除所有约束后,updateCheckoutSession
在 API [=37= 中给了我一个“amazonPayRedirectUrl
” ]. URL 真的很重要。您需要完成 URL 才能完成审核...
本文来自亚马逊官网:
"Once there are no constraints, the response will return a unique
amazonPayRedirectUrl. Redirect the buyer to that URL to complete
checkout."
“Daniel Lemke”提到了解决此问题的另一种方法。这样,购物者就不需要确认会话。但是,您需要确保在webCheckoutDetails
(付款详情、地址详情)中提供所有有用的信息。如果你只给checkoutMode
,你会得到“MissingParameterValue”。
本文来自亚马逊官网:
"paymentDetails is required when using 'ProcessOrder'. addressDetails
is also required if you use 'ProcessOrder' with productType set to
'PayAndShip'"
我正在沙盒中使用来自亚马逊的 v2 c# SDK(MVC 应用程序)将亚马逊支付与我的网站集成。设置一切正常,我创建了密钥、return url 等
在我的结帐过程中,我创建了成功的 CheckoutSession。我点击亚马逊支付按钮,使用我的测试买家账户登录,然后使用他们有效的测试信用卡“支付”商品。
亚马逊重定向到我的 return URL,并按预期使用结帐会话 ID。
当我尝试 CompleteCheckoutSession 时,结果是从亚马逊返回的错误 API
error: InvalidCheckoutSessionStatus
message: You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed
我在尝试完成之前输入了一行代码来检索 CheckoutSession 查看它,它显示当前状态为“打开”,这是尝试完成时的正确状态,所以我不知道为什么结帐会话状态无效。
编辑:
请注意,我将此流程用于我的交易,因此没有对交易的“审查”。买家在我的网站上选择他的商品。
另请注意,我正在根据此动态创建有效载荷:
因此,当单击 amazonpay 按钮时,它会调用我网站上的一个方法,该方法构建开始 CheckoutSession 的有效负载。然后,我将 Amazon Session ID 保存在我的缓存中,并保存总数。有效负载被 returned 到亚马逊支付脚本,然后将我带到亚马逊网站。我选择付款类型并单击“继续结帐”,这会将我带回我的网站以执行“完成”步骤。
我对 CompleteCheckoutSession(sessionId) 的请求
{"chargeAmount":{"amount":99,"currencyCode":"USD"}}
result.RawResponse 来自 client.CompleteCheckoutSession(sessionId) 方法:
{
"reasonCode":"InvalidCheckoutSessionStatus",
"message":"You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed"
}
编辑示例代码:
我创建了一个具有基本功能的全新测试 MVC 应用程序:
public ActionResult Index()
{
var client = InitiateClient(); //hidden for security
// prepare the request
var request = new CreateCheckoutSessionRequest
(
checkoutReviewReturnUrl: "http://localhost:44300/home/completecheckout",
storeId: "amzn1.application-oa2-client.mystoreid"
);
request.PaymentDetails.PaymentIntent = Amazon.Pay.API.WebStore.Types.PaymentIntent.AuthorizeWithCapture;
request.PaymentDetails.ChargeAmount.Amount = 99;
request.PaymentDetails.ChargeAmount.CurrencyCode = Currency.USD;
// generate the signature and payload string that is passed back to the frontend
ViewBag.Signature = client.GenerateButtonSignature(request);
ViewBag.Payload = request.ToJson();
return View();
}
在 Index.cshtml 文件中:
<div id="AmazonPayButton"></div>
<script src="https://static-na.payments-amazon.com/checkout.js"></script>
<script type="text/javascript" charset="utf-8">
amazon.Pay.renderButton('#AmazonPayButton', {
merchantId: 'mymerchantid',
ledgerCurrency: 'USD',
sandbox: true,
checkoutLanguage: 'en_US',
productType: 'PayOnly',
placement: 'Checkout',
buttonColor: 'Gold',
createCheckoutSessionConfig: {
payloadJSON: '@Html.Raw(ViewBag.Payload)', // string generated in step 2
signature: '@Html.Raw(ViewBag.Signature)', // signature generated in step 3
publicKeyId: 'AGPTYXGL5VH6PSYLJUSHTKW6'
}
});
</script>
最后,完整的结帐代码,未成功:
public ActionResult CompleteCheckout(string amazonCheckoutSessionId)
{
var client = InitiateClient(); //hidden for security
var request = new CompleteCheckoutSessionRequest(99.00M, Currency.USD);
// send the request
var result = client.CompleteCheckoutSession(amazonCheckoutSessionId, request);
// check if API call was successful
if (!result.Success)
{
throw new Exception("API Call unsuccessful");
}
return View();
}
注意:某些键被混淆了,但实际键在示例代码中。
手动创建 CheckoutSession 不适用于您正在遵循的“无评论页面”流程。如果您想集成此流程,则必须让 Amazon Pay 使用按钮代码中的 createCheckoutSessionConfig 参数为您创建 CheckoutSession,请参见此处:https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-apb-checkout/add-the-amazon-pay-button.html#4-render-the-button
编辑:
下面的代码示例展示了如何为 'no order review' 支付流程构建有效负载和签名。请注意 CheckoutMode 参数,它指示 Amazon Pay 立即处理付款,以及 checkoutResultReturnUrl,它定义了 URL 买家被发送到哪里以完成结账。当用户到达 URL 时,您必须调用 Complete CheckoutSession,现在应该会成功。
public ActionResult Index()
{
var client = InitiateClient(); //hidden for security
// prepare the request
var request = new CreateCheckoutSessionRequest
(
checkoutReviewReturnUrl: null,
storeId: "amzn1.application-oa2-client.mystoreid"
);
// instructs Amazon Pay to immediately process the payment
request.WebCheckoutDetails.CheckoutMode = CheckoutMode.ProcessOrder;
request.WebCheckoutDetails.CheckoutResultReturnUrl = "http://localhost:44300/home/completecheckout";
// payment details
request.PaymentDetails.PaymentIntent = Amazon.Pay.API.WebStore.Types.PaymentIntent.AuthorizeWithCapture;
request.PaymentDetails.ChargeAmount.Amount = 99;
request.PaymentDetails.ChargeAmount.CurrencyCode = Currency.USD;
// generate the signature and payload string that is passed back to the frontend
ViewBag.Signature = client.GenerateButtonSignature(request);
ViewBag.Payload = request.ToJson();
return View();
}
我遇到了类似的问题,我花了很长时间才弄明白。这就是发生在我身上的事情。
我无意创建生产代码。我只想创建一个只有 Amazon Pay 按钮的测试页面。我用了几个node js脚本来getCheckoutSession/updateCheckoutSession
。但是,当我尝试 completeCheckoutSession
时,我得到了“InvalidCheckoutSessionStatus”。
最后,我发现,在我设置所有必需的参数并删除所有约束后,updateCheckoutSession
在 API [=37= 中给了我一个“amazonPayRedirectUrl
” ]. URL 真的很重要。您需要完成 URL 才能完成审核...
本文来自亚马逊官网:
"Once there are no constraints, the response will return a unique amazonPayRedirectUrl. Redirect the buyer to that URL to complete checkout."
“Daniel Lemke”提到了解决此问题的另一种方法。这样,购物者就不需要确认会话。但是,您需要确保在webCheckoutDetails
(付款详情、地址详情)中提供所有有用的信息。如果你只给checkoutMode
,你会得到“MissingParameterValue”。
本文来自亚马逊官网:
"paymentDetails is required when using 'ProcessOrder'. addressDetails is also required if you use 'ProcessOrder' with productType set to 'PayAndShip'"