我正在尝试将 PagSeguro(支付网关 - 巴西版的 PayPal)合并到我的网站中。客户完成 PagSeguro 后,他们将数据(通过 POST)发送到我指定的函数。但是,我没有收到 POST。在完成了我能想到的所有故障排除后,我联系了 PagSeguro。他们说他们的日志表明 POST 正在正常发送,但他们正在接收 HTTP 302 响应。
为了弄清楚为什么会发生这种情况,我创建了一个带有隐藏值的表单来模拟向我的函数发送 POST。我将此表格放在不同的域下,以防万一它与此有关。每次从我的模拟表单发送 POST 时,我都会收到 HTTP 200 响应,并且我的日志表明已收到 POST。
PagSeguro 怎么可能收到与我的模拟不同的响应?它可能与服务器有关还是与我的脚本有关?
这是应该接收 POST 的函数(使用 CodeIgniter):
function pagseguro_retorno(){
if (count($_POST) == 0) {
return FALSE;
}
$msg = 'POST RECEIVED';
$simulate = $this->input->post('Simulate');
if ( ! empty($simulate)){
$result = 'VERIFICADO';
$msg .= ' FROM SIMULATOR';
} else {
$this->load->library(PagSeguroNpi);
$result = $this->PagSeguroNpi->notificationPost();
}
$this->log($msg);
if ($result == "VERIFICADO") {
$id = $this->input->post('Referencia');//cart id
$this->load->model('transacao_model');
$trans_row = $this->transacao_model->get_transaction($id);
if ( ! is_object($trans_row)){
//LOAD NEW TRANSACTION
if ( ! $this->new_transaction($id)){
$notice = "Unable to load new transaction</p><p>";
$this->log($notice);
$notice .= '<pre>'.print_r($_POST, TRUE).'</pre>';
$this->email_notice($notice);
}
}
$this->load->model('carrinho_model');
if($_POST['StatusTransacao'] == 'Aprovado'){
$status = 'apr';
}elseif($_POST['StatusTransacao'] == 'Em Análise'){
$status = 'anl';
}elseif($_POST['StatusTransacao'] == 'Aguardando Pagto'){
$status = 'wtg';
}elseif($_POST['StatusTransacao'] == 'Completo'){
$status = 'cmp';
//nothing more happens here - client must click on 'mark as shipped' before cart is removed and history data is loaded
}elseif($_POST['StatusTransacao'] == 'Cancelado'){
//reshelf - don't set $status, because the cart's about to be destroyed
$this->carrinho_model->reshelf(array($id));
}
if (isset($status)){
$this->carrinho_model->update_carrinho($id, array('status' => $status));
}
} else if ($result == "FALSO") {
$notice = "PagSeguro return was invalid.";
$this->log($notice);
$notice .= '<pre>'.print_r($_POST, TRUE).'</pre>';
$this->email_notice($notice);
} else {
$notice = "Error in PagSeguro request";
$this->log($notice);
$notice .= '<pre>'.print_r($_POST, TRUE).'</pre>';
$this->email_notice($notice);
}
}
安全更新:
发布后,我很快意识到我正在向黑客尝试敞开心扉。该函数必须是公开的,因此任何知道该函数名称的人都可以访问它并发布“模拟”以立即进行验证。然后他们可以传递他们想要的任何数据。
我将函数的名称更改为无法猜测的名称,并且当不在生产模式下时,我禁用了模拟选项。