diff --git a/app/Config/Routes.php b/app/Config/Routes.php index dc81c505..646a6e8e 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -201,7 +201,7 @@ $routes->get('/payment_failure', 'Payment::payment_failure'); $routes->get('/payment_process','Payment::payment_process'); $routes->get('payment_success','Payment::payment_success'); $routes->match(['get','post'],'/subscription_renewal/(:any)','Payment::index/$1'); - +$routes->match(['get','post'],'payment_status','Payment::paymentStatus'); // Logs diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index 5f8a636a..fe04f228 100755 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -9,12 +9,19 @@ use App\Models\BusinessModel; use App\Models\BooksModel; use App\Helpers\NotificationHelper; use App\Helpers\date_picker; +use App\Models\PaymentStatusModel; use App\Models\SubscriptionModel; use Mpdf\Mpdf; use Dompdf\Dompdf; class Invoice extends BaseController { + protected $paymentModel; + + public function __construct() + { + $this->paymentModel = new PaymentStatusModel(); + } ## For Invoice Listing.. public function index() { @@ -410,6 +417,7 @@ class Invoice extends BaseController if ((int)$invoiceType === 2) { $membership = (!empty($this->request->getPost('membership_id'))) ? $this->request->getPost('membership_id') : ""; $membership_id = substr($membership,0,6); + log_message('error','Membership id : '.$membership != null && $membership != ''? $membership : ""); $this->create_or_update_subscription($invoice_id,$invoice_status, $msg_flag_name,$membership_id); } @@ -546,6 +554,8 @@ public function create_or_update_subscription($invoice_id,$invoice_status, $msg_ $renewal = 0; if (!empty($membership_id)){ $sub_id = $subModel->select('sub_id')->where('membership_id',$membership_id)->first(); + log_message('error','going to call the function'); + $this->checkPaymentStatus($membership_id); } foreach ($invoiceItems as $item) { $product_id = $item['product']; @@ -561,7 +571,7 @@ public function create_or_update_subscription($invoice_id,$invoice_status, $msg_ $membership_id = $this->generate_membership_id(6); } - + // Prepare subscription data $subscription_data = [ 'customer_id' => $customer_id, @@ -594,6 +604,24 @@ public function create_or_update_subscription($invoice_id,$invoice_status, $msg_ } } + + public function checkPaymentStatus($membership_id){ + log_message("error","Function Called"); + try { + $id = $this->paymentModel->where('membership_id', $membership_id)->first()['id']; + if ($id) { + $data['status'] = 9999; + $data['updated_by'] = (int)get_logged_user_id(); + $this->paymentModel->update($id, $data); + log_message('error', 'Payment Status Found and updated by User' . $data['updated_by']); + } else { + log_message("error", "id not found" . $membership_id); + } + }catch (\Exception $e){ + log_message('error','Id not found on the checkpaymentstatus '); + } + + } ## For Updating Events Details .. diff --git a/app/Controllers/Payment.php b/app/Controllers/Payment.php index 30bdc0c0..4a902538 100644 --- a/app/Controllers/Payment.php +++ b/app/Controllers/Payment.php @@ -8,6 +8,7 @@ use App\Models\InvoiceModel; use App\Models\TransactionModel; use App\Models\SubscriptionModel; use App\Models\AuthenticationModel; +use App\Models\PaymentStatusModel; require_once('vendor/autoload.php'); define('PROJECT', 'vendor/paytm/paytm-pg'); @@ -15,8 +16,11 @@ define('PROJECT', 'vendor/paytm/paytm-pg'); class Payment extends BaseController { + protected $paymentModel; + public function __construct() { helper('encdec_paytm'); + $this->paymentModel = new PaymentStatusModel(); } public function index($membership_id = null) @@ -84,6 +88,7 @@ class Payment extends BaseController public function pay() { + // $payment_model = new PaymentStatusModel(); $userAgent = $_SERVER['HTTP_USER_AGENT']; $payment_method = preg_match('/mobile/i', $userAgent) ? "mobile" : 'website'; $data = $_POST; @@ -107,6 +112,18 @@ class Payment extends BaseController $data['checkSum'] = $checkSum; $data['paramList'] = $paramList; $decryptedChecksum = verifychecksum_e($data['paramList'], env('Merchant_Key'),$data['checkSum']); + $payment_status_data = [ + 'customer_id' => $CUST_ID, + 'membership_id' => $membership_id, + 'order_id' => $ORDER_ID, + 'scheme_id' => env('RENEWAL_SCHEME_ID'), + 'amount' => $TXN_AMOUNT, + 'device' => $payment_method == "mobile"? "Mobile":"Web" + ]; + $result = $this->paymentModel->insert($payment_status_data); + if ($result){ + log_message('info','Data Inserted to Payment Status '.json_encode($payment_status_data)); + } log_message('error','Data Sent to Paytm: '.json_encode($data['paramList'])); return view('/tresponse', $data); } @@ -153,7 +170,7 @@ public function tresponse() { } } -public function payment_failure(){ +public function payment_failure(){ $paramList = [ 'BANKTXNID' => $this->request->getVar('BANKTXNID'), 'CHECKSUMHASH' => $this->request->getVar('CHECKSUMHASH'), @@ -323,8 +340,18 @@ public function payment_process() { $model4->save($paramList); log_message('error','transaction details added to transaction table'); + $insertId = $model4->insertID(); + $payment_status_id = $this->paymentModel->select('id')->where('order_id',$this->request->getVar('ORDERID'))->first()['id']; + // echo "payment status id :"; + // var_dump($payment_status_id);die(); + $payement_status_update['status'] = $insertId; + // $payement_status_update['status'] = $payment_status_id; - log_message('info','sent data' .json_encode($data)); + $updated = $this->paymentModel->update($payment_status_id, $payement_status_update); + log_message('error', 'Payment Status Updated: ' . json_encode($updated)); + + + log_message('info', 'saved data' . json_encode($data)); return redirect()->to(base_url('/payment_success')); } @@ -406,6 +433,39 @@ public function viewLog($fileName) return redirect()->back()->with('error', 'Log file not found.'); } +public function paymentStatus(){ + + if ($this->request->is('get')){ + $data['page_name'] = "Payment Status"; + $data['selected_data'] = ""; + $data['payment_status'] = $this->paymentModel->where('is_active',1)->findAll(); + // dd($data); + return $this->render_page('payment_status_view',$data); + }else{ + $received_data = $this->request->getPost(); + log_message('error','data received from post '.json_encode($received_data)); + $dateParts = explode(' - ', $received_data['dateRange']); + $status = $received_data['status']; + + $fromDate = $dateParts[0]; + $toDate = $dateParts[1]; + + // Update date conversion format using the global namespace + $fromDate = \DateTime::createFromFormat('d/m/Y', $dateParts[0])->format('Y-m-d'); + $toDate = \DateTime::createFromFormat('d/m/Y', $dateParts[1])->format('Y-m-d'); + + $payment_data['payment_status_data'] = $this->paymentModel->getPaymentData($fromDate, $toDate,$status); + log_message('debug',json_encode($payment_data)); + + $html = view('payment_status_table',$payment_data); + $data['selected_data'] = $received_data['dateRange']; + // $data = ""; + return $this->response->setJSON(['status'=> true,'data' => $data,"html" => $html]); + + } + +} + } diff --git a/app/Models/PaymentStatusModel.php b/app/Models/PaymentStatusModel.php new file mode 100644 index 00000000..0f2bb9cc --- /dev/null +++ b/app/Models/PaymentStatusModel.php @@ -0,0 +1,52 @@ +db->table('payment_status')->select('payment_status.*,concat(customers.first_name," ",customers.last_name) as customer_name,books.short_code as scheme_name') + ->join('customers','customers.customer_id = payment_status.customer_id and customers.isactive = 1') + ->join('books','books.book_id = payment_status.scheme_id and books.isactive = 1'); + + if (!empty($fromDate) && !empty($toDate)) { + $builder->where('payment_status.created_at >=', $fromDate.' 00:00:00') + ->where('payment_status.created_at <=', $toDate. ' 23:59:59'); + } + + if ($status == 1) { + $builder->where('payment_status.status >', 0); // Correct usage of where() for status + }else{ + $builder->where('payment_status.status', null); + } + + $builder->where('payment_status.is_active',1); + $data = $builder->get()->getResultArray(); + log_message('error',json_encode($this->db->getLastQuery()->getQuery())); + return $data; // Fetch and return as an array + } +} diff --git a/app/Views/payment_status_table.php b/app/Views/payment_status_table.php new file mode 100644 index 00000000..1b7efcfd --- /dev/null +++ b/app/Views/payment_status_table.php @@ -0,0 +1,23 @@ +
| Customer Name | +Membership ID | +SCHEME Name | +ORDER ID | +Amount | +
|---|---|---|---|---|
| = isset($row['customer_name'])?$row['customer_name']:"-"?> | += isset($row['membership_id'])?$row['membership_id']:"-"?> | += isset($row['scheme_name'])?$row['scheme_name']:"-"?> | += isset($row['order_id'])?$row['order_id']:"-"?> | += isset($row['amount'])?$row['amount']:"-"?> | +