Created
May 25, 2017 12:19
-
-
Save iaditya/658868d8fa9b9a63d692b44fafd01428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Controller\AppController; | |
use Cake\I18n\Date; | |
use Cake\I18n\Time; | |
use Cake\Log\Log; | |
use Cake\ORM\TableRegistry; | |
use Notifier\Utility\NotificationManager; | |
/** | |
* Payments Controller | |
* | |
* @property \App\Model\Table\PaymentsTable $Payments | |
*/ | |
class PaymentsController extends AppController | |
{ | |
public $working_key; | |
public $access_code; | |
public function initialize() | |
{ | |
parent::initialize(); // TODO: Change the autogenerated stub | |
$this->Auth->allow(['oMResponseHandler']); | |
$this->loadComponent('Notifier.Notifier'); | |
if (IS_PROD) { | |
$this->working_key = '228F775F1BB38264D557A0EE5714B974';//Shared by CCAVENUES | |
$this->access_code = 'AVJW66DG00AN57WJNA'; | |
} | |
if (IS_TEST) { | |
$this->working_key = 'E0CA64536E8C45C6924B39AEFE426595';//Shared by CCAVENUES | |
$this->access_code = 'AVIW66DG00AN56WINA';//Shared by CCAVENUES | |
} | |
} | |
/** | |
* Index method | |
* | |
* @return \Cake\Network\Response|null | |
*/ | |
public function index() | |
{ | |
$this->paginate = [ | |
'contain' => ['Users', 'Patients' => ['Users'], 'Providers' => ['Users'], 'Events' => ['Facilities'], 'Discounts'] | |
]; | |
$payments = $this->paginate($this->Payments); | |
$this->set(compact('payments')); | |
$this->set('_serialize', ['payments']); | |
} | |
/** | |
* View method | |
* | |
* @param string|null $id Payment id. | |
* @return \Cake\Network\Response|null | |
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. | |
*/ | |
public function view($id = null) | |
{ | |
$payment = $this->Payments->get($id, [ | |
'contain' => ['Users', 'Patients', 'Providers', 'Events', 'Discounts'] | |
]); | |
$this->set('payment', $payment); | |
$this->set('_serialize', ['payment']); | |
} | |
/** | |
* Add method | |
* | |
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise. | |
*/ | |
public function add() | |
{ | |
include('Crypto.php'); | |
$event_id = isset($this->request->query['event_id']) ? $this->request->query['event_id'] : null; | |
$patient_id = isset($this->request->query['patient_id']) ? $this->request->query['patient_id'] : null; | |
$provider_id = isset($this->request->query['provider_id']) ? $this->request->query['provider_id'] : null; | |
$facility_id = isset($this->request->query['facility_id']) ? $this->request->query['facility_id'] : null; | |
$facility_id = isset($this->request->query['facility_id']) ? $this->request->query['facility_id'] : null; | |
$slots = isset($this->request->query['slots']) ? $this->request->query['slots'] : null; | |
$this->set(compact('patient_id', 'provider_id', 'event_id', 'facility_id', 'slots')); | |
$patient_user_id = $this->request->session()->read('Auth.User.id'); | |
$user = $this->Payments->Users->get($patient_user_id); | |
//$patient = $this->Payments->Patients->get($patient_id, ['contain' => ['PatientAddresses'=> ['States']]]); | |
$patientquery = $this->Payments->Patients->findById($patient_id)->contain(['PatientAddresses']); | |
$patient = $patientquery->first(); | |
if (!empty($patient->patient_address)) { | |
$addpatient = $patientquery->select(['States.id', | |
'States.name' | |
])->contain(['PatientAddresses' => ['States']]); | |
$patient = $addpatient->first(); | |
} | |
// Get the global payment settings | |
$Tdsettings = TableRegistry::get('tdsettings'); | |
$tdsetting = $Tdsettings->find("all")->first(); | |
// Get the provider payment settings for the facility | |
$PaymentSettings = TableRegistry::get('facilities_providers'); | |
$paymentsetting = $PaymentSettings->find("all") | |
->where(['facility_id' => $facility_id, 'provider_id' => $provider_id]) | |
->first(); | |
$this->set(compact('tdsetting', 'paymentsetting')); | |
$this->request->data['payment_date'] = Date::today(); | |
Log::debug("Data" . $this->request->data); | |
$transactionid = Time::now()->toUnixString(); | |
$payment = $this->Payments->newEntity(); | |
if ($this->request->is('post')) { | |
$payment = $this->Payments->patchEntity($payment, $this->request->data); | |
$payment->transactionid = $transactionid; | |
if ($this->Payments->save($payment)) { | |
$this->Flash->success(__('The payment has been saved.')); | |
$merchant_data = ''; | |
$merchant_data .= 'tid' . '=' . $transactionid . '&'; | |
$merchant_data .= 'merchant_id' . '=' . '101002' . '&'; | |
$merchant_data .= 'order_id' . '=' . $payment->id . '&'; | |
$merchant_data .= 'currency=INR&'; | |
$merchant_data .= 'amount' . '=' . $this->request->data['final_amount'] . '&'; | |
$merchant_data .= 'redirect_url' . '=' . $this->request->data['redirect_url'] . '&'; | |
$merchant_data .= 'cancel_url' . '=' . $this->request->data['cancel_url'] . '&'; | |
$merchant_data .= 'language' . '=' . 'EN' . '&integration_type=iframe_normal&'; | |
$merchant_data .= 'billing_name=' . $this->request->data['billing_name'] . '&'; | |
$merchant_data .= 'billing_address=' . $this->request->data['billing_address'] . '&'; | |
$merchant_data .= 'billing_city=' . $this->request->data['billing_city'] . '&'; | |
$merchant_data .= 'billing_state=' . $this->request->data['billing_state'] . '&'; | |
$merchant_data .= 'billing_zip=' . $this->request->data['billing_zip'] . '&'; | |
$merchant_data .= 'billing_country=' . $this->request->data['billing_country'] . '&'; | |
$merchant_data .= 'billing_tel=' . $this->request->data['billing_tel'] . '&'; | |
$merchant_data .= 'billing_email=' . $this->request->data['billing_email'] . '&'; | |
$merchant_data .= 'promo_code=&'; | |
$merchant_data .= 'customer_identifier=&'; | |
$merchant_data .= 'integration_type=iframe_normal¤cy=INR&'; | |
/* | |
$merchant_data = "currency=INR& | |
tid=1467977558575& | |
merchant_id=101002& | |
order_id=123654789& | |
amount=1.00& | |
redirect_url=http://testapp.talkadoc.com/IFRAME/ccavResponseHandler.php& | |
cancel_url=http://testapp.talkadoc.com/IFRAME/ccavResponseHandler.php& | |
language=EN& | |
billing_name=Charli& | |
billing_address=Room no 1101, near Railway station Ambad& | |
billing_city=Indore& | |
billing_state=MP& | |
billing_zip=425001& | |
billing_country=India& | |
billing_tel=9876543210& | |
billing_email=test@test.com& | |
delivery_name=Chaplin& | |
delivery_address=room no.701 near bus stand& | |
delivery_city=Hyderabad& | |
delivery_state=Andhra& | |
delivery_zip=425001& | |
delivery_country=India& | |
delivery_tel=9876543210& | |
merchant_param1=additional Info.& | |
merchant_param2=additional Info.& | |
merchant_param3=additional Info.& | |
merchant_param4=additional Info.& | |
merchant_param5=additional Info.& | |
promo_code=& | |
customer_identifier=& | |
integration_type=iframe_normal&"; | |
*/ | |
Log::debug("access key " . $this->access_code); | |
Log::debug("working key " . $this->working_key); | |
Log::debug("Merchant Data" . $merchant_data); | |
$encrypted_data = encrypt($merchant_data, $this->working_key); // Method for encrypting the data. | |
$this->redirect(['action' => 'ccavRequestHandler', $encrypted_data]); | |
} else { | |
$this->Flash->error(__('The payment could not be saved. Please, try again.')); | |
} | |
} | |
//$users = $this->Payments->Users->find('list', ['limit' => 200]); | |
// $patients = $this->Payments->Patients->find('list', ['limit' => 200]); | |
// $providers = $this->Payments->Providers->find('list', ['limit' => 200]); | |
// $events = $this->Payments->Events->find('list', ['limit' => 200]); | |
$discounts = $this->Payments->Discounts->find('list', ['limit' => 200]); | |
$this->set(compact('payment', 'user', 'patient', 'providers', 'events', 'discounts')); | |
$this->set('_serialize', ['payment']); | |
} | |
public function ccavRequestHandler($encrypted_data = null) | |
{ | |
include('Crypto.php'); | |
// $this->layout = 'ajax'; | |
//error_reporting(1); | |
$rcvdString = decrypt($encrypted_data, $this->working_key); //Crypto Decryption used as per the specified working key. | |
$endata = encrypt($rcvdString, $this->working_key); // Method for encrypting the data. | |
Log::debug("access key " . $this->access_code); | |
Log::debug("working key " . $this->working_key); | |
Log::debug("decrypted data" . $rcvdString); | |
$production_url = 'https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction&encRequest=' . $endata . '&access_code=' . $this->access_code; | |
$this->set(compact('production_url')); | |
$this->set('_serialize', ['production_url']); | |
} | |
public function ccavResponseHandler() | |
{ | |
include('Crypto.php'); | |
// $this->layout = 'ajax'; | |
//error_reporting(1); | |
$encResponse = $_POST["encResp"]; //This is the response sent by the CCAvenue Server | |
$rcvdString = decrypt($encResponse, $this->working_key); //Crypto Decryption used as per the specified working key. | |
$order_status = ""; | |
$result = explode('&', $rcvdString); | |
Log::debug("decrypted data" . $rcvdString); | |
$dataSize = sizeof($result); | |
for ($i = 0; $i < $dataSize; $i++) { | |
$information = explode('=', $result[$i]); | |
if ($i == 0) $orderid = $information[1]; | |
if ($i == 1) $trackingid = $information[1]; | |
if ($i == 3) $order_status = $information[1]; | |
} | |
if (!empty($orderid)) { | |
$payment = $this->Payments->get($orderid); | |
$payment->trackingid = $trackingid; | |
$payment->status = $order_status; | |
$this->Payments->save($payment); | |
$eventid = $payment->event_id; | |
} | |
if (!empty($eventid)) { | |
$event = $this->Payments->Events->get($eventid); | |
if ($order_status === "Success") { | |
$event->status = "Confirmed"; | |
// send notification for confirmed event | |
$this->sendNotificationForSuccessEvent($event->id); | |
} else { | |
$event->status = "PaymentFailed"; | |
// send notification for payment failed event | |
$this->sendNotificationForFailedEvent($event->id); | |
} | |
$this->Payments->Events->save($event); | |
} | |
$this->set(compact('result')); | |
$this->set('_serialize', ['result']); | |
} | |
public function oMResponseHandler() | |
{ | |
Log::debug("oMResponseHandler"); | |
if ($this->request->is('post')) { | |
$transactionid = $this->request->data['transactionid']; | |
Log::debug("oMResponseHandler :" . $transactionid); | |
if (!empty($transactionid)) { | |
$payment = $this->Payments->findByTransactionid($transactionid)->first(); | |
$payment->trackingid = $this->request->data['tracking_id']; | |
$payment->status = $this->request->data['status']; | |
$this->Payments->save($payment); | |
$eventid = $payment->event_id; | |
Log::debug("oMResponseHandler event :" . $eventid); | |
} | |
if (!empty($eventid)) { | |
$event = $this->Payments->Events->get($eventid); | |
if ($payment->status === "Success") { | |
$event->status = "Confirmed"; | |
} else { | |
$event->status = "PaymentFailed"; | |
} | |
$result['status'] = $event->status; | |
$this->Payments->Events->save($event); | |
} | |
} | |
$this->set(compact('result')); | |
$this->set('_serialize', ['result']); | |
} | |
/** | |
* Edit method | |
* | |
* @param string|null $id Payment id. | |
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise. | |
* @throws \Cake\Network\Exception\NotFoundException When record not found. | |
*/ | |
public function edit($id = null) | |
{ | |
$payment = $this->Payments->get($id, [ | |
'contain' => [] | |
]); | |
if ($this->request->is(['patch', 'post', 'put'])) { | |
$payment = $this->Payments->patchEntity($payment, $this->request->data); | |
if ($this->Payments->save($payment)) { | |
$this->Flash->success(__('The payment has been saved.')); | |
return $this->redirect(['action' => 'index']); | |
} else { | |
$this->Flash->error(__('The payment could not be saved. Please, try again.')); | |
} | |
} | |
$users = $this->Payments->Users->find('list', ['limit' => 200]); | |
$patients = $this->Payments->Patients->find('list', ['limit' => 200]); | |
$providers = $this->Payments->Providers->find('list', ['limit' => 200]); | |
$events = $this->Payments->Events->find('list', ['limit' => 200]); | |
$discounts = $this->Payments->Discounts->find('list', ['limit' => 200]); | |
$this->set(compact('payment', 'users', 'patients', 'providers', 'events', 'discounts')); | |
$this->set('_serialize', ['payment']); | |
} | |
/** | |
* Delete method | |
* | |
* @param string|null $id Payment id. | |
* @return \Cake\Network\Response|null Redirects to index. | |
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. | |
*/ | |
public function delete($id = null) | |
{ | |
$this->request->allowMethod(['post', 'delete']); | |
$payment = $this->Payments->get($id); | |
if ($this->Payments->delete($payment)) { | |
$this->Flash->success(__('The payment has been deleted.')); | |
} else { | |
$this->Flash->error(__('The payment could not be deleted. Please, try again.')); | |
} | |
return $this->redirect(['action' => 'index']); | |
} | |
private function sendNotificationForFailedEvent($event_id) { | |
$notificationManager = new NotificationManager(); | |
$event = $this->Payments->Events->findById($event_id)->first(); | |
$patient = $this->Payments->Events->Patients->findById($event->patient_id)->first(); | |
$patient_user = $this->Payments->Events->Patients->Users->findById($patient->user_id)->first(); | |
$notificationManager->addRecipientList('cancelTeleAppointmentByPatient', [$patient->user_id]); | |
$this->Notifier->notify([ | |
'recipientLists' => 'cancelTeleAppointmentByPatient', | |
'template' => 'cancelTeleAppointmentByPatient', | |
'vars' => [ | |
'patient_name' => $patient_user->first_name, | |
], | |
], $this->request->session()->read('Auth.User.id')); | |
} | |
private function sendNotificationForSuccessEvent($event_id) { | |
$notificationManager = new NotificationManager(); | |
$event = $this->Payments->Events->findById($event_id)->first(); | |
$patient_id = $event->patient_id; | |
$facility_id = $event->facility_id; | |
$provider_id = $event->provider_id; | |
$event_type = "Teleconsult"; | |
$role = $this->request->session()->read('Auth.User.role'); | |
$facility_admin_ids = []; | |
$i = 0; | |
$Users = TableRegistry::get('users'); | |
$facilities = TableRegistry::get('facilities'); | |
$facility_admins = TableRegistry::get('facility_admins'); | |
$facility = $facilities->findById($this->request->data['facility_id'])->first(); | |
$user = $Users->get($this->request->session()->read('Auth.User.id')); | |
$provider = $this->Events->Providers->findById($provider_id)->first(); | |
$provider_user = $Users->find()->where(['id' => $provider->user_id])->first(); | |
$f_admins = $facility_admins->findByFacilityId($facility_id); | |
foreach ($f_admins as $f_admin): | |
$facility_admin_ids[$i] = $f_admin->user_id; | |
$i++; | |
endforeach; | |
$notificationManager->addRecipientList('patientAddAppointmentToProvider', [$provider->user_id]); | |
$notificationManager->addRecipientList('patientAddAppointmentToFacilityAdmins', $facility_admin_ids); | |
$notificationManager->addRecipientList('patientAddAppointmentToSelf', [$user->id]); | |
$this->Notifier->notify([ | |
'recipientLists' => 'patientAddAppointmentToProvider', | |
'template' => 'patientAddAppointmentToProvider', | |
'vars' => [ | |
'name' => $user->first_name, | |
'username' => $user->username, | |
'role' => $role, | |
'event_type' => $event_type, | |
'start_date' => date_format($event->start, 'd/m/y'), | |
'start_time' => date_format($event->start, 'g:i A'), | |
'end_date' => date_format($event->end, 'Y-m-d H:i:s'), | |
'provider_name' => $provider_user->first_name, | |
'facility_name' => $facility->name, | |
], | |
], $this->request->session()->read('Auth.User.id')); | |
$this->Notifier->notify([ | |
'recipientLists' => 'patientAddAppointmentToFacilityAdmins', | |
'template' => 'patientAddAppointmentToFacilityAdmins', | |
'vars' => [ | |
'name' => $user->first_name, | |
'username' => $user->username, | |
'role' => $role, | |
'event_type' => $event_type, | |
'start_date' => date_format($event->start, 'd/m/y'), | |
'start_time' => date_format($event->start, 'g:i A'), | |
'end_date' => date_format($event->end, 'Y-m-d H:i:s'), | |
'provider_name' => $provider_user->first_name, | |
'facility_name' => $facility->name, | |
], | |
], $this->request->session()->read('Auth.User.id')); | |
$this->Notifier->notify([ | |
'recipientLists' => 'patientAddAppointmentToSelf', | |
'template' => 'patientAddAppointmentToSelf', | |
'vars' => [ | |
'name' => $user->first_name, | |
'username' => $user->username, | |
'role' => $role, | |
'event_type' => $event_type, | |
'start_date' => date_format($event->start, 'd/m/y'), | |
'start_time' => date_format($event->start, 'g:i A'), | |
'end_date' => date_format($event->end, 'Y-m-d H:i:s'), | |
'provider_name' => $provider_user->first_name, | |
'facility_name' => $facility->name, | |
], | |
], $this->request->session()->read('Auth.User.id')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment