2017-09-04 120 views
0

我正在查看我的代码和我的结果,但没有看到任何明显的错误,所以我认为可能有一些额外的眼睛看起来很有用结束了。WordPress中的奇怪行为MySQL插入JSON编码数据

我有一个自定义的PayPal IPN侦听器,用于更新数据库中的事务表。我在离开工作前的星期五部署了它,今天返回后,它似乎正常工作主要是;但我想弄清楚为什么一个插入行为奇怪。

下面是其中发生在上周末的插入的捕捉: PayPal IPN log

正如你可以看到,第四届交易log列预期JSON值为空。我觉得很奇怪,因为transaction_id列的值是从同一个数组中解析出来的。

下面是相关数据库insert代码:

// Generate valid IPN log 
private function generateIpnLog() { 
    global $wpdb; 

    // prepare log 
    $array_log = []; 
    $array_log['verified'] = true; 
    $array_log['ipn_response'] = (isset($this->PayPal_Response)) ? : 'Error reading from POST array'; 

    // Parse transaction ID 
    $transaction_id = (isset($this->PayPal_Response['txn_id'])) ? $this->PayPal_Response['txn_id'] : null; 

    // Generate log 
    $log = json_encode($array_log); 

    // Update DB 
    $wpdb->insert(
     'log_paypal', 
     [ 
      'transaction_id' => ($transaction_id) ? $transaction_id : 'Error getting transaction ID', 
      'log' => ($log) ? $log : 'Error generating transaction log' 
     ], 
     [ 
      '%s', 
      '%s' 
     ] 
    ); 

    // json log response 
    $this->json_return = $log; 
} 

看到,因为事务ID解析罚款从PayPal的反应,因为我们知道$array_log['verified']有一个显式声明价值我的猜测是,必须有一个问题与json_encode($array_log)

我还检查了贝宝IPN历史记录中有关PayPal账户的数据,并且可以验证数据在空值log列与其他列中形成的方式没有任何不同。

任何人都知道在这种情况下会发生什么?

+0

如果我这样做是正确,你可以看到'log'('$ array_log')应该在空行中?你应该编辑它,它非常相关。 – ishegg

+0

@ishegg我不确定你的问题是什么,但如果你问我是否可以用IPN数据手动更新该行 - 是的,我可以,而且已经有了。但从长远来看,这显然没有帮助。 – DrewT

+1

不,我的意思是,你可以手动重建'$ array_log',看看它为什么没有正确编码? – ishegg

回答

0

正如@ishegg所示,这是一个编码问题,因为PayPal IPN使用windows-1252编码并且DB字段编码为UTF-8

在这种情况下很容易修复,因为PayPal返回数据不是嵌套的/多维的(见下文)。

// Process IPN response 
$this->PayPal_Response = $this->processIpn(); 

然后,函数本身:

在PayPal的IPN入境后的早期功能堪称是加密由证书链核实

// Manipulate IPN response and prepare 
// for database update and log 
private function processIpn() { 
    // Response ref. 
    $response = $this->post_array; 

    if (isset($response['charset'])) { 
     if ($response['charset'] == "windows-1252") { 
      foreach ($response as $key => $ipn_value) { 
       $response[$key] = mb_convert_encoding($ipn_value, 'UTF-8', 'Windows-1252'); 
      } 
     } 
    } 
    return $response; 
}