与付款网关联系时将PHP代码延迟一段时间

时间:2018-10-23 05:54:16

标签: php laravel sleep payment-processing

我正在开发一个Laravel PHP项目,通过该项目我可以到达Payment API,以检查用户是否已付款的状态,并将用户重定向到付款确认页面。

默认情况下,来自“支付”网关的支付状态为0。当用户支付状态更改为1时。在用户点击网站上的“支付”按钮后,我需要延迟PHP代码的执行(以允许用户一些是时候通过他/她的电话进行付款了)。

15秒后,我到达付款网关,检查状态是否已更改为1,如果为true,则将用户重定向到付款确认页面。

我尝试使用睡眠功能,但是它无法正常工作。我还使用了沙箱帐户进行了测试,同时进行了付款,但是15秒后却没有按预期的方向进行重定向。

我从API中获取的示例JSON对象,具体取决于付款状态

//When not paid
{
    status: 0,
    message: 'Not Paid',
    amount: 20
}

//When paid
{
    status: 1,
    message: 'Paid',
    amount: 20
}

//When cancelled
{
    status: 2,
    message: 'Cancelled',
    amount: 20
}

正在使用AJAX代码将数据发布到控制器

<script type="text/javascript">
  //Mpesa Payment code
$('.mpesa').on('click', function () {

    //Gets the MPESA type
    var type = $('.mpesa').prop('id');
    var quote = $('#quote').val();
    var phone = $('#phone').val();
    //Converts to a JSON object
    var type ={
      'type': type,
      'quote' : quote,
      'phone' : phone,
    };

    console.log(type);

    $.ajax({
        //Contains controller of payment
        type: 'POST',
        url: 'paymentFinal',
        data: JSON.stringify(type),
        contentType: 'application/json',
        dataType: "json",
        success: function success(response) {
            window.location.href="success" ;
        },
        error: function error(data) {
            console.log(data);
        }
    });
});
//End Payment API

Laravel Controller正在从上面的AJAX代码发布数据

 public
    function payFinal(Request $request)
    {
        // dd($request->all());

         //Convert to a JSON object the request 
        $data =(object)$request->all();

        //Session get of some data fetched from another controller
        $quote = $request->session()->get('quoteID');

        //Store all the data in an array
         $all = array(
            'phone' => $data->phone,
            'quote_id' => $quote,
            'payment_type' => $data->type,
        );

        //Posts data to Payment Checkout using curl
        $response = $this->global_Curl($all, 'api/payment/checkout');
        //dd($response);

        //Get checkoutresponseId from response
        $checkID = $response->data->CheckoutRequestID;

        //Payment type
        $type = $data->type;

        $data = array(
            'payment_reference' => $checkID,
            'payment_type' => $type
        );

        //1st call to the Payment API before sleep
        $paySt = $this->global_Curl($data, 'api/payment/status')->data;

        sleep(15);

        //Second call to the API after sleep to check if status has changed
        $payStat = $this->global_Curl($data, 'api/payment/status')->data;

        if($payStat->status == '1'){
            return 'true';   
        }
    }

正在使用新的AJAX代码

$('.mpesa').on('click', function () {
    setInterval(function() {
       alert('clicked');
      //Gets the MPESA type
       var type = $('.mpesa').prop('id');
      var quote = $('#quote').val();
      var phone = $('#phone').val();
      //Converts to a JSON object
      var type ={
        'type': type,
        'quote' : quote,
        'phone' : phone,
      };

    console.log(type);
    $.ajax({
        //Contains controller of payment
        type: 'POST',
        url: 'paymentFinal',
        data: JSON.stringify(type),
        contentType: 'application/json',
        dataType: "json",
        success: function success(response) {
          if(response) {
              window.location.href="success";
          }
        },
        error: function error(data) {
            console.log(data);
        }
    });
}, 15000); // Execute every 15 seconds
});

1 个答案:

答案 0 :(得分:0)

好,让我们来分解您的问题。首先,您希望延迟AJAX代码以每15秒执行一次。为此,您将AJAX包装在setInterval() javascript方法中。所以它应该最终看起来像这样:

setInterval(function() {
    $.ajax({
        //Contains controller of payment
        type: 'POST',
        url: 'paymentFinal',
        data: JSON.stringify(type),
        contentType: 'application/json',
        dataType: "json",
        success: function success(response) {
            window.location.href="success" ;
        },
        error: function error(data) {
            console.log(data);
        }
    });
}, 15000); // Execute every 15 seconds

接下来,您要根据代码返回的状态执行某些操作。为此,您需要将AJAX方法的成功案例更改为以下内容:

    success: function success(response) {
        if(response) {
            window.location.href="success"
        }
    }

在javascript方面就是这样。对于PHP方面,您可以删除它,因为您现在正在处理前端的时间间隔:

    sleep(15);

    //Second call to the API after sleep to check if status has changed
    $payStat = $this->global_Curl($data, 'api/payment/status')->data;

并将返回类型从字符串更改为布尔值:

if($payStat->status == 1){
        return response()->json(true);   // Sends back a JSON response to your AJAX
    }

那应该按照您想要的去做。

现在,关于您的代码的一些建议:

  • 您可能想为更多的案件做出准备,而不仅仅是 成功的AJAX案例
  • 确保在用户使用时禁用该按钮 单击“付款”按钮,否则每次他们按该按钮时 开始一个新的15秒间隔
  • 尝试将您的PHP代码包装在try-catch块中,以进行优美的错误处理
  • 为成功案例做更多准备