Callback Duitku

mail@pastecode.io avatar
unknown
php
2 years ago
2.1 kB
2
Indexable
Never
public function index()
	{
		$this->load->model('TransaksiModel');

		$apiKey = 'f7bab717a968313ccc8fa212570a1f53'; // API key anda
		$merchantCode = isset($_POST['merchantCode']) ? $_POST['merchantCode'] : null;
		$amount = isset($_POST['amount']) ? $_POST['amount'] : null;
		$merchantOrderId = isset($_POST['merchantOrderId']) ? $_POST['merchantOrderId'] : null;
		$productDetail = isset($_POST['productDetail']) ? $_POST['productDetail'] : null;
		$additionalParam = isset($_POST['additionalParam']) ? $_POST['additionalParam'] : null;
		$paymentMethod = isset($_POST['paymentCode']) ? $_POST['paymentCode'] : null;
		$resultCode = isset($_POST['resultCode']) ? $_POST['resultCode'] : null;
		$merchantUserId = isset($_POST['merchantUserId']) ? $_POST['merchantUserId'] : null;
		$reference = isset($_POST['reference']) ? $_POST['reference'] : null;
		$signature = isset($_POST['signature']) ? $_POST['signature'] : null;

		//log callback untuk debug 
		file_put_contents('./callback.txt', "* Callback *\r\n", FILE_APPEND | LOCK_EX);

		if (!empty($merchantCode) && !empty($amount) && !empty($merchantOrderId) && !empty($signature)) {
			$params = $merchantCode . $amount . $merchantOrderId . $apiKey;
			$calcSignature = md5($params);

			if ($signature == $calcSignature) {
				$id = $merchantOrderId;
				$data['status_pembayaran'] = 'terbayar';
				$data['tanggal_pembayaran'] = date('Y-m-d H:i:s');
				$this->TransaksiModel->updateByOrderId($data, $id);

				file_put_contents('./callback.txt', "* Success *\r\n\r\n", FILE_APPEND | LOCK_EX);
				echo 'success';
			} else {
				$id = $merchantOrderId;
				$data['status_pembayaran'] = 'expired';

				$this->TransaksiModel->updateByOrderId($data, $id);
				file_put_contents('./callback.txt', "* Bad Signature *\r\n\r\n", FILE_APPEND | LOCK_EX);
				// $data['tanggal_pembayaran'] = date('Y-m-d H:i:s');
				// echo 'not success';
				throw new Exception('Bad Signature');
			}
		} else {
			http_response_code(400);
			file_put_contents('./callback.txt', "* Bad Parameter *\r\n\r\n", FILE_APPEND | LOCK_EX);
			var_dump($_POST);
		}
	}