gpt4 book ai didi

c# - Amazon Pay SDK CompleteCheckoutSession 错误

转载 作者:行者123 更新时间:2023-12-02 16:20:55 25 4
gpt4 key购买 nike

我在沙盒中使用亚马逊的 v2 c# SDK(MVC 应用程序)将亚马逊支付与我的网站集成。设置一切正常,我创建了 key 、返回 url 等。

在我的结帐过程中,我创建了成功的 CheckoutSession。我点击亚马逊支付按钮,使用我的测试买家账户登录,然后使用他们有效的测试信用卡“支付”商品。

亚马逊按预期使用结账 session ID 重定向到我的返回 URL。

当我尝试 CompleteCheckoutSession 时,结果是从 Amazon 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 查看它,它显示当前状态为“打开”,这是尝试完成时的正确状态,所以我不知道为什么结帐 session 状态无效。

编辑:

请注意,我将此流程用于我的交易,因此没有对交易的“审查”。买家在我的网站上选择他的商品。

https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-apb-checkout/additional-payment-button-overview.html

另请注意,我正在根据此动态创建有效载荷:

https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-checkout/amazon-pay-script.html#render-button-using-a-checkout-session-object

因此,当单击 amazonpay 按钮时,它会调用我网站上的一个方法,该方法构建开始 CheckoutSession 的有效负载。然后,我将 Amazon Session ID 保存在我的缓存中,并保存总数。有效载荷返回到亚马逊支付脚本,然后将我带到亚马逊网站。我选择付款类型并点击“继续结帐”,这会将我带回我的网站以执行“完成”步骤。

我对 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

编辑:

下面的代码示例展示了如何为“无订单审查”支付流程构建负载和签名。请注意 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();
}

关于c# - Amazon Pay SDK CompleteCheckoutSession 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65535379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com