php - 我是否可以使用从我的 laravel 控制器中的 ajax GET REQUEST 获取的数据更新数据库,而无需进一步的 ajax 请求成功?
问题描述
我正在尝试在 PAYSTACK 付款成功后使用 laravel 控制器中的 AJAX GET REQUEST 使用从 ajax 请求中获取的数据来更新用户表。您可以直接转到我遇到问题的控制器代码...
付款表单和 Ajax 运行良好:
<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 获取 Web.php 中的路由:
Route::get('verify-payment/{reference}', 'UsersController@verify_pay')->name('verify-payment');
这是我在控制器中遇到问题的地方:
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'] 而不是双 $input["donation_sum"]。
推荐阅读
- angular - Html2pdf 仅在 Mac OS 上随机位置对齐
- python - 字典没有识别正确的键
- git - 如何使用上次提交哈希将最新更新拉入本地?
- android - 在 Android Jetpack Compose 中使用 State 时出现 java.lang.IllegalStateException
- regex - Google 数据洞察计算字段/正则表达式协助请求
- c - 如何使用文件描述符使用 posix 库将二进制数据写入和读取 C 中的文件?
- javascript - 正则表达式以不同方式匹配第一个单词
- php - json和php中的这段代码有什么问题... consol错误?
- angular - 为什么我的 Angular 应用程序中出现此错误?
- c++ - C++ 中的模板参数类型分配器