我正在使用API接收付款网关的付款交易确认,如下所示。但是我无法使用响应并将其插入数据库中。
我的控制器:
$transaction_id= filter_input(INPUT_POST, 'pay_id');
$api = new Api('ID', 'key');
//getting object of transation
$payment = $api->payment->fetch($transaction_id);
//passing payment details to database
$this->Payment_model->set_payment($transaction_id,true);
$payment
数组的当前结构:
pay\Api\Payment Object ( [attributes:protected] => Array ( [id] => pay_BVzDNK3vsp4Owl [entity] => payment [amount] => 35400 [currency] => INR [status] => authorized [order_id] => ) )
我的pay_model.php
:
public function set_payment($payment,$first)
{
$data = array(
'id' => $id,
'entity' => $entity,
'amount' => $amount,
'currency' => $currency,
'status' => $status,
'order_id' => $order_id,
'updated_at' => date('Y-m-d H:i:s')
);
if ($first == TRUE) {
//print_r($data);
// echo "breakpoint--i am in set user model id 0";
return $this->db->insert('payments', $data);
} else {
//echo "breakpoint--i am in set user model id not 0";
$this->db->where('id', $id);
return $this->db->update('payments', $data);
}
}
问题:我的$ data数组始终接收NULL值。如何使用我在这里接收的数据并传递给模型进行插入/更新操作。
答案 0 :(得分:0)
在函数作用域中未定义$data
数组的值(这将为您提供NULL值)。
您有2个错误。首先是将正确的数据发送到set_payment
函数:
$payment = $api->payment->fetch($transaction_id);
$this->Payment_model->set_payment($transaction_id,true);
我认为第二行应该是:
$this->Payment_model->set_payment($payment,true);
发送$payment
而不是$transaction_id
。
第二个错误是在set_payment
函数中使用数据时。创建$data
时,应将其用作:
'id' => $payment['id'], //and no just $id
'entity' => $payment['entity'],//and no just $entity as those var not exist in this scope
// and so on...
答案 1 :(得分:0)
在set_payment()
中:
您的数据数组应如下所示:
$data = array(
'id' => $payment['id'],
'entity' => $payment['entity'],
'amount' => $payment['amount'],
'currency' => $payment['currency'],
'status' => $payment['status'],
'order_id' => $payment['order_id'],
'updated_at' => date('Y-m-d H:i:s'));