veritrans / veritrans-php

PHP client library for Veritrans
93 stars 85 forks source link

Notice: Trying to get property of non-object in #67

Closed christianovan closed 6 years ago

christianovan commented 7 years ago

Bisa bantu untuk menyelesaikan masalah ini? Fatal error: Uncaught exception 'Exception' with message 'Veritrans Error (): ' in C:....\Veritrans\ApiRequestor.php:96 Stack trace: #0 C:...\Veritrans\ApiRequestor.php(17): Veritrans_ApiRequestor::remoteCall('https://api.ver...', 'VT-server-......', false, false) #1 C:....\Veritrans\Transaction.php(17): Veritrans_ApiRequestor::get('https://api.ver...', 'VT-server-.....', false) #2 C:....\Veritrans\Notification.php(20): Veritrans_Transaction::status(NULL) #3 C:....\examples\index.php(7): Veritrans_Notification->__construct() #4 {main} thrown in

robeth commented 7 years ago

Berdasarkan stacktrace di atas, maka yang terjadi adalah:

Apakah benar begitu? Mungkin bisa di-share bentuk notifikasi json yang anda terima 😄

radhyr commented 7 years ago

Saya juga sama, di error log muncul error persis seperti itu. Test callback-nya yang dari sandbox midtrans juga error 500. (sudah tak cek di json-nya test callback itu ada atribut transaction_id nya)

File url notifnya saya pakai yang dari /examples/notification-handler.php cuman saya pindah ke /bayar/notification-handler.php terus server keynya saya isi sama server key punya saya

kalau line $notif = new Veritrans_Notification(); saya hilangkan, test callback di sandbox midtrans sukses jadi status 200. Tapi bukannya fungsi itu untuk ngambil data json yang dikirim dari server midtrans kan ya? ada solusi?

mohon pencerahan dan bantuannya

edit: saya lihat di file Veritrans.php ada komen tentang facebook sdk, apa karena bentrok sama facebook sdk ya? Saya pakai facebook sdk soalnya.

edit2: saya belum selesaikan registrasi akun midtrans saya karena belum mengirim dokumen. ngaruh gak ya?

asisten commented 7 years ago

Saya juga mengalami masalah serupa:

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 93

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 94

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 95

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 96

Fatal error: Uncaught exception 'Exception' with message 'Veritrans Error (): ' in /home/payment/Veritrans/ApiRequestor.php:96 Stack trace: #0 /home/payment/Veritrans/ApiRequestor.php(17): Veritrans_ApiRequestor::remoteCall('https://api.mid...', 'VT-server-XHB_V...', false, false) #1 /home/payment/Veritrans/Transaction.php(17): Veritrans_ApiRequestor::get('https://api.mid...', 'VT-server-XHB_V...', false) #2 /home/payment/Veritrans/Notification.php(20): Veritrans_Transaction::status(NULL) #3 /home/payment/template/bayar.php(11)

Pada header saya set seperti ini (sebelum html head ):

header('Content-Type: application/json');
header('Accept: application/json');
header('Basic base64(key_server_midtrans_saya , :)');

Jika memang halaman error jika diakses langsung, apa bisa kita membatasinya dari request tertentu? maksud saya apa parameter post atau get dr midtrans sehingga saya bisa set misalnya:

 if (isset($_POST['params-midtrans']) {
// kode handler
}`
eronzih commented 7 years ago

The reason of this error is because there is no post data, specifically 'transaction_id' from Midtrans. To get the status of transaction is only required 'transaction_id'. To send post data to your notification handler, it can be done using cUrl in PHP. example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            "http://file/to/your/notification_handler" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     '//REPLACE WITH JSON REQUEST FROM MIDTRANS' ); 
curl_setopt($ch, CURLOPT_HEADER, true);    
$result=curl_exec ($ch);
var_dump($result);

this field:

curl_setopt($ch, CURLOPT_POSTFIELDS,     '//REPLACE WITH JSON REQUEST FROM MIDTRANS' );

should be replaced with the full json: eg:-

{"transaction_time": "2017-08-29 15:57:32","transaction_status": "capture","transaction_id": "9e912e2b-2ecf-4ce2-996c-d1ea2cabcc57","status_message": "Veritrans payment notification","status_code": "200","signature_key": "8f80dfe76a15540c525b280857d685052dc299123135417e74cef2b83263356ba573182ad6412e2bc3812bbb41d94e36796e8d4b79164f1be95a5f5f1597b377","payment_type": "credit_card","order_id": "abc123","masked_card": "481111-1114","gross_amount": "100.00","fraud_status": "accept","eci": "05","bank": "mandiri","approval_code": "1503912354214"}

or for testing purpose, just the 'transaction_id' will work:

{"transaction_id": "9e912e2b-2ecf-4ce2-996c-d1ea2cabcc57"}

*important notes

to test the result, the transaction must be exists in your transaction list here: https://dashboard.midtrans.com/payments

or else it will return

Veritrans Error (404): The requested resource is not found

and that is the reason why we get status code 500 from Test notification callback, because the testing POST data send from Midtrans does not exist in our transaction list (hope Midtrans can fix this)

and finally, when notification handler is working fine on your localhost, you should check is code behind notification handler is fired after making a transaction, even when Test notification callback return 500

asisten commented 7 years ago

@eronzih Thank you for your explanation. about notification handling is solved when I send post data as your suggested.

But I wondering why error also occur even I already define transaction id, so it should'nt need any post data again right?

for example I want to update transaction status:

include('path/to/Veritrans.php');
Veritrans_Config::$serverKey = 'MY_KEY';
Veritrans_Config::$isProduction = true;   // false = sandbox

$order_id = 'transaction_id_from_veritrans_that_i_already_saved_to_my_database';
$status_obj = Veritrans_Transaction::status($order_id); // here are I put trx id as object,
$get_latest_transaction_status = $status_obj->transaction_status; // output will be latest transaction status
// then i do other thing, check transaction status, update to my database etc

when i stopped the process it have result:

var_dump($get_latest_transaction_status); exit;  // output:  settlement/success/etc 

but when continue the process error message is occur

eronzih commented 7 years ago

@asisten Hi, can you show more details on the error message?

asisten commented 7 years ago

@eronzih Exactly same as above, do I still need sending post data? I can serialize post data so it will return array of transaction id from my database, but that little complicated while i can just defined array directly on the file.

Im using this for cron task check, esp for payment through bank transfer that usually has delay from customer.

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 93

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 94

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 95

Notice: Trying to get property of non-object in /home/payment/Veritrans/ApiRequestor.php on line 96

Fatal error: Uncaught exception 'Exception' with message 'Veritrans Error (): ' in /home/payment/Veritrans/ApiRequestor.php:96 Stack trace: #0 /home/payment/Veritrans/ApiRequestor.php(17): Veritrans_ApiRequestor::remoteCall('https://api.mid...', 'VT-server-XHB_V...', false, false) #1 /home/payment/Veritrans/Transaction.php(17): Veritrans_ApiRequestor::get('https://api.mid...', 'VT-server-XHB_V...', false) #2 /home/payment/Veritrans/Notification.php(20): Veritrans_Transaction::status(NULL) #3 /home/payment/template/bayar.php(11)
eronzih commented 7 years ago

@asisten According to this error #2 /home/payment/Veritrans/Notification.php(20): Veritrans_Transaction::status(NULL)

it shows that the 'transaction_id' parameter for Veritrans_Transaction::status() is NULL

Are you geting the transaction_id thru POST? have you verified that you have successful received the transaction_id from POST?

asisten commented 7 years ago

Thats confusing me, i got transaction_id from my database not from post and thats valid transaction_id (see my reply above),

var_dump($get_latest_transaction_status); exit; // output: settlement/success/etc

I saved that transaction_id when customer click buy button and pay through bank transfer through midtrans. So the current status at that time is settlement.

Then I want to check if they have already pay their invoice, through script above.

So i didnt get transaction_id from POST, but from my database. Customer will not generated any other transaction id because i have save condition if they click buy button they will no more generated new transaction_id anymore except the one that i saved in my database.

asisten commented 7 years ago

The request still no error until finished executed page, I have test exit in:

var_dump($get_latest_transaction_status); exit; // output: settlement/success/etc

no error displayed

eronzih commented 7 years ago

@asisten

sorry, my bad I just getting to understand your scenario.

so, for the error, I have tested it just now, one thing I found is :

this method :

Veritrans_Transaction::status()

only can accept transaction_id, NOT order_id. You can double check at https://dashboard.midtrans.com/payments and makes sure that trasaction is exitst

so maybe you are getting error is because midtrans api did not return any transaction response due to that transaction is not found.

I have use the following code to see what has return from Midtrans API, which is I extracted from Veritrans/ApiRequestor.php

$serverKey = //YOUR SEVER KEY
$transaction_id = //YOUR TRANSACTION NO

$ch = curl_init();

$curl_options = array(
      CURLOPT_URL => 'https://api.midtrans.com/v2/' . $transaction_id .'/status/',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json',
        'Authorization: Basic ' . base64_encode($serverKey . ':')
      ),
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_CAINFO => dirname(__FILE__) . "/data/cacert.pem"
    );

curl_setopt_array($ch, $curl_options);

$result = curl_exec($ch);

if ($result === FALSE) {
  throw new Exception('CURL Error: ' . curl_error($ch), curl_errno($ch));
}

var_dump($result);

hope it helps!

asisten commented 7 years ago

The order id is transaction id, I'm just change variable name.

The transcation id itself valid, I have checked and it return status:

// i change $order_id variable to $transaction_id so it not getting confused, $transaction_id is customer $customer_id and i have re-check with midtrans dashboard

$status_obj = Veritrans_Transaction::status($transaction_id); // here are I put trx id as object,
$get_latest_transaction_status = $status_obj->transaction_status; // output will be latest transaction status

//check if $transaction_id valid, no error displayed while i exit the script below

var_dump($get_latest_transaction_status); exit; // SUCCESS WITHOUT ERROR, it will output: settlement/success/etc

// BUT when WITHOUT exit; anymore, it started getting error, see script below
var_dump($get_latest_transaction_status); // return with status but also have error
asisten commented 7 years ago

Based on your last answer (pass post data with curl), so it still need to pass with POST data to verify $transaction_id ? even my transaction id is valid?

eronzih commented 7 years ago

The previos example given is just to check is the response from midtrans api is success or not, based on given transaction id.

Because what I see from the error message is that the transaction_id is null, for method Veritrans_Transaction::status($transaction_id); Issit possible the error is caused by code after this var_dump($get_latest_transaction_status); that is related to midtrans?

asisten commented 7 years ago

No, the rest is query for update invoice status into my database. I successfully update transaction status to my db, just error above occur. Maybe it related to buffering, I will update if buffering can remove this error.

rizdaprasetya commented 6 years ago

Hi guys get status API call accept both $transaction_id or $order_id. So both should work fine. But apparently the transaction is not created properly, so that will cause API return 404 and library throw exception.

Make sure the transaction created properly and the environment correct to do get status.