查看 Square 结帐过程是否完成的正确方法

Proper Way To See If Square Checkout Process Completed

我目前正在开发一个 C# Windows Form 项目,该项目要求用户在进行任何处理之前付款。我正在使用 Square .Net SDK 进行支付处理,并使用 Checkout API 生成的 Checkout URL 成功地通过沙箱环境获得支付。我的问题是是否有一种简单的方法来获取付款过程是否已完成。现在,我只是用相同的顺序(使用相同的幂等键)轮询 API 并等待它 return 一个订单不再有效的错误。这是我当前代码的 backbone:

var bodyOrderLineItems = new List<CreateOrderRequestLineItem>();
long totalCost = 100;
var charge0 = new Money.Builder().Amount(totalCost).Currency("USD").Build();
bodyOrderLineItems.Add(new CreateOrderRequestLineItem.Builder("1").Name("Incredibly Valuable Product").BasePriceMoney(charge0).Build());
var order = new CreateOrderRequest.Builder()
                    .ReferenceId("reference_id")
                    .LineItems(bodyOrderLineItems)
                    .Build();
var body = new CreateCheckoutRequest.Builder("Test_iKey4", order)
                    .AskForShippingAddress(false)
                    .Build();
var checkoutApi = client.CheckoutApi;

try
{
     CreateCheckoutResponse result = checkoutApi.CreateCheckout(locationsString, body);
     System.Diagnostics.Process.Start(result.Checkout.CheckoutPageUrl);
     while (true)
     {

         System.Threading.Thread.Sleep(5000);
         //while payment hasn't gone through
         try
         {
             result = checkoutApi.CreateCheckout(locationsString, body);
         }
         catch (ApiException e)
         {
             MessageBox.Show(e.Errors[0].Detail);
             break;
         }
     }
     MessageBox.Show("Payment Must Have Gone Through");
}
catch (ApiException e) { MessageBox.Show(e.Errors[0].Detail); };

这是一种有效的方法吗?虽然这似乎确实有效,但我觉得我正在用请求淹没 api。我对 Square API 非常缺乏经验,所以任何帮助将不胜感激。

通常在网站内使用 Checkout API,因为它包含一个参数 redirect_url,因此当付款完成时,用户会被重定向回您这边,它包含 url交易id等参数。如果您没有网站,可以注册 webhooksPAYMENT_UPDATED webhook 会在支付完成后发送出去,所以你不需要做轮询;只听 webhook。