public function postEditJob()
 {
     $data = \Input::get('data');
     $_hash = new Hash();
     $_hash = $_hash->getHasher();
     if (!($job = \Job::findJobById(trim(\Input::get('job'))))) {
         return \Response::json(['type' => 'danger', 'message' => 'Job not found.']);
     }
     if (!($company = \Company::findCompanyById($_hash->decode($data['company'])))) {
         return \Response::json(['type' => 'danger', 'message' => 'Company not found.']);
     }
     if (!($agency = \Agency::getAgency())) {
         return \Response::json(['type' => 'danger', 'message' => 'Not an agency account.']);
     }
     if ($data['job_post_duration'] !== '0') {
         if (!$agency->is_vip) {
             $value = ['1' => -1, '2' => -3];
             $checkValue = abs($value[$data['job_post_duration']]);
             if ($agency->credit < $checkValue) {
                 return \Response::json(['type' => 'warning', 'message' => 'You have ' . $agency->credit . ' credit. You need at least ' . $checkValue . ' credit(s) to post this job.']);
             }
         }
     }
     $status = $company->agencies()->wherePivot('company_id', $company->id)->wherePivot('status', 'accept')->first();
     if (!$status) {
         return \Response::json(['type' => 'danger', 'message' => 'You do not have permission from this company to post a job']);
     }
     $data['agency_id'] = $agency->id;
     $data['company_id'] = $company->id;
     if ($data['job_apply_type'] === 'job_apply_by_email') {
         if ($data['job_apply_application_email'] === '' && $data['job_apply_direct_email'] === '') {
             return \Response::json(['type' => 'warning', 'message' => 'Job application by email is chosen, please input at least one email.']);
         }
         $data['job_apply_details'] = json_encode(['type' => 'email', 'application_email' => $data['job_apply_application_email'], 'direct_email' => $data['job_apply_direct_email']]);
     } elseif ($data['job_apply_type'] === 'job_apply_by_url') {
         if ($data['job_apply_url'] === '') {
             return \Response::json(['type' => 'warning', 'message' => 'Job application by url is chosen, please input the application url.']);
         }
         $data['job_apply_details'] = json_encode(['type' => 'url', 'url' => $data['job_apply_url']]);
     }
     try {
         $job = \Job::updateJob($job, $data);
         if (!$agency->is_vip && $data['job_post_duration'] !== '0') {
             \Agency::updateCredit($agency, $value[$data['job_post_duration']]);
         }
         return \Response::json(['type' => 'success', 'message' => 'Job updated successfully.']);
     } catch (\Exception $e) {
         return \Response::json(['type' => 'danger', 'message' => env('APP_DEBUG') ? $e->getMessage() : 'Error, please contact webmaster.']);
     }
 }
 public function getAgencyPaymentDone(Request $request)
 {
     if (!\Session::has('_temp_payment_sess')) {
         return redirect(url('agency'))->with('flashMessage', ['class' => 'danger', 'message' => 'Your session has expired, please try again.']);
     }
     $data = session('_temp_payment_sess');
     $id = $request->get('paymentId');
     $token = $request->get('token');
     $payer_id = $request->get('PayerID');
     $payment = PayPal::getById($id, $this->paypalApiContext);
     $paymentExecution = PayPal::PaymentExecution();
     $paymentExecution->setPayerId($payer_id);
     $executePayment = $payment->execute($paymentExecution, $this->paypalApiContext);
     $agency = \Agency::getAgency();
     try {
         switch ($data['type']) {
             case 'credit':
                 \Agency::updateCredit($agency, $data['xCreditAmount']);
                 break;
             case 'contract':
                 \Agency::updateVIP($agency, true);
                 break;
             default:
                 break;
         }
         session(['_sess_agency' => ['model' => $agency]]);
         \Session::forget('_temp_payment_sess');
     } catch (\Exception $e) {
         // TODO: Need better error handler
         // re-run try with queue?
         \Session::forget('_temp_payment_sess');
         return redirect(url('agency'))->with('flashMessage', ['class' => 'danger', 'message' => $e->getMessage()]);
     }
     return redirect(url('agency'))->with('flashMessage', ['class' => 'success', 'message' => $data['successMessage']]);
 }