簡體   English   中英

如何僅在交易完成后重新定向?

[英]How can I redirect only after transaction is finalized?

在我用於與paypal簽出的示例代碼中,有一個重定向請求,該請求到route purchase/complete 我想確保僅在對/paypal/purchase/complete的發布請求/paypal/purchase/complete后才請求此路由,以便我可以獲取在服務器端創建的orderID並將其作為get參數傳遞給purchase/complete路由,使其類似於purchase/complete?orderId=XXXX XXXX就是接收到的data.orderID

客戶端:

    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({

      // Set up the transaction
      createOrder: function(data, actions) {
        var payableAmount = $("#finalAmountTotalOrders").attr("amountData");
        return actions.order.create({
          purchase_units: [{
            amount: {
              value: payableAmount
            }
          }]
        });
      },

      // Finalize the transaction
      onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
          //will redirect user to custom page change values as desired
          window.location.replace("/purchase/complete");
          // Call your server to save the transaction
          return fetch('/paypal/purchase/complete', {
            method: 'post',
            headers: {
              'content-type': 'application/json',
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            body: JSON.stringify({
              orderID: data.orderID,
              details: details

            })

          });

        });
      }


    }).render('#paypal-button-container');

編輯:

我想執行此操作的原因是,盡管在發出/paypal/purchase/complete帖子請求后,我能夠創建訂單並將其存儲在數據庫中,但是我不確定何時會執行類似電子郵件的操作具有我剛剛創建的訂單ID(數據庫行)的客戶和商人。 嘗試在發布請求中執行此操作會引發錯誤,因此,如果我只能在/purchase/complete路線上獲得ID,則可以查詢數據庫並獲取訂單詳細信息並通過電子郵件發送。 任何其他方法對我都是有用的。

我已經知道您可以在第一個actions.order.capture().then鏈接另一個.thenactions.order.capture().then

onApprove: function(data, actions) {
    return actions.order.capture().then(function(details) {
        //will redirect user to custom page change values as desired
        // Call your server to save the transaction
        return fetch('/paypal/purchase/complete', {
        method: 'post',
        headers: {
            'content-type': 'application/json',
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        body: JSON.stringify({
            orderID: data.orderID,
            details:details

        })

        }).then((response) => {
          window.location.replace("/purchase/complete?paypalOrderId="+data.orderID);
        });

    });
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM