简体   繁体   中英

is it possible for me to update database with data gotten from ajax GET REQUEST in my laravel controller without further ajax request on success?

I am trying to Update users table after PAYSTACK payment is successful using an AJAX GET REQUEST inside the laravel controller using data gotten from the ajax request. You can go straight to the controller code which is where i am having issues...

Payment Form and Ajax is working very well:

 <form id="paymentForm">
  <div class="form-group">
    <label for="email">Email Address</label> <br>
    <input type="email" id="email-address" required />
  </div>
  <div class="form-group">
    <label for="amount">Amount</label><br>
    <input type="tel" id="amount" required />
  </div>

  <div class="form-submit">
    <button type="submit" onclick="payWithPaystack(event)"> Pay </button>
  </div>
</form>
<script src="https://js.paystack.co/v1/inline.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script> 
const paymentForm = document.getElementById('paymentForm');
paymentForm.addEventListener("submit", payWithPaystack, false);
function payWithPaystack(e) {
  e.preventDefault();
  let handler = PaystackPop.setup({
    key: 'pk_test_058624cb23f26e6f1a75325ec7cdc84ead42b999', // Replace with your public key
    email: document.getElementById("email-address").value,
    amount: document.getElementById("amount").value * 100,
    ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
    // label: "Optional string that replaces customer email"
    onClose: function(){
      alert('Window closed.');
    },
    callback: function(response){

      let reference = response.reference;

      // verify pay
      $.ajax({
         type: "GET",
         url: "{{URL::to('verify-payment')}}/"+reference,

         success: function(response){
            console.log(response);
         }
      });
    }
  });
  handler.openIframe();
}
</script> 

Ajax Get route in Web.php:

Route::get('verify-payment/{reference}', 'UsersController@verify_pay')->name('verify-payment');

This is where i am having issues in controller:

public function verify_pay($reference)
    {
        $sec = "sk_test_1ee78b7e4524cf19d3450e198812d1b8f0ee6562";
        $curl = curl_init();
  
        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://api.paystack.co/transaction/verify/$reference",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_SSL_VERIFYHOST => 0,
          CURLOPT_SSL_VERIFYPEER => 0,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $sec",
            "Cache-Control: no-cache",
          ),
        ));
        
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        $newData = json_decode($response);
        $u_input = [];

        if ([$newData][0]->status == true) {
            $ff = [[$newData][0]->data->amount];
            $up_input["donation_sum"] = strval($ff[0]/100); This line is not running Nor the Line Below
            $up_input["donation_sum"] = $ff[0]/100; This line is not running
            Auth::User()->update($u_input);
            return "paid";
        }else {
            return "something went wrong";
        }
        
    }

它现在正在工作,我只添加了 csrf 令牌并使用单个 $input['donation_sum'] 而不是 double $input["donation_sum"]。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM