php - 将支付状态从 AJAX 重定向到 PHP 的问题
问题描述
我正在使用 PHP 开发一个支付集成网关,其中正在监听来自网关的响应,即 1 或 2 或 0 。成功后,我想将任何字符串重定向到 AJAX 成功,它不能按预期工作
PHP 代码
for($try=1; $try<=3; $try++) {
sleep(15);
$payStat = $this->global_Curl($data, 'api/payment/status')->data;
//dd($payStat);
//Check if staus is zero meaning not paid
if ($payStat->status === 0) {
return 'notPaid';
}
//check if status is 2 meaning cancelled
elseif ($payStat->status === 2) {
return 'Cancelled';
}
//check if status is 1 meaning paid
elseif ($payStat->status === 1) {
return 'Paid';
}
}
我想监听响应的 AJAX 代码
<script type="text/javascript">
$('.mpesa').on('click', function () {
//alert('clicked');
//Adds Class to the page when it loads
$('.PAY').addClass("loading");
//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) {
//Log the reponse from PHP code
console.log(response);
},
error: function error(data) {
//alert('Error');
}
});
});
</script>
解决方案
将return
语句更改为echo
语句,然后您可以在前端使用这些值
if ($payStat->status === 0) {
echo 'notPaid';
return; // if you need to exit
}
如果您使用 json 对象作为响应,那就更好了。很容易在前端做好准备。
if ($payStat->status === 0) {
echo ['status' => 'notPaid'];
return; // if you need to exit
}
推荐阅读
- protractor - 在量角器自动化的记录器文件中打印控制台错误(响应代码为 4xx 或 5xx)
- excel - 将单元格值从另一个工作簿导入工作表
- perl - 聚类(组)字符串数组
- javascript - 为什么在 hashchange 事件侦听器中重复输出?
- linux - 如何优雅地关闭运行在 Kubernetes 上的 Go 服务
- .net - 将一个表单实例的 TextBox 控件的更改值更新为所有实例
- r - 如何在 R 中安装 XCMS 包
- python-3.x - pymodm 找不到对象,而 pymongo 成功找到它
- javascript - 如何使用一个功能自行选择每个下拉列
- html - 垂直对齐的 inline-flexbox 的宽度随着子元素的数量而增长