public function commission($driver, $ipn) { try { \DB::beginTransaction(); if ($driver->id == $ipn['custom']) { // Get the pre-month commissions. $sales = new SaleCollection($driver->sales(Carbon::now()->subMonth(), Carbon::now())); if ($sales) { $paypalHelper = new PaypalHelper($driver); // get authorized commission payment if ($authorizedCommissionPayment = $paypalHelper->createPaypalFuturePayment($sales->toArray()['totals'])) { // capture authorized commission payment if ($capture = $paypalHelper->capturePaypalPayment($authorizedCommissionPayment)) { if ($authorizedCommissionPayment['id'] == $capture['parent_payment']) { $commission_payment = ['driver_id' => $driver->id, 'commissions' => $authorizedCommissionPayment['transactions'][0]['amount']['total'], 'currency' => $authorizedCommissionPayment['transactions'][0]['amount']['currency'], 'status' => $authorizedCommissionPayment['state'], 'commission_ipn' => $authorizedCommissionPayment, 'commission_payment_id' => $authorizedCommissionPayment['id'], 'capture_id' => $capture['id'], 'capture_created_at' => $capture['create_time'], 'capture_updated_at' => $capture['update_time'], 'capture_status' => $capture['state'], 'capture_ipn' => $capture]; if ($commission = EloquentCommissionRepository::create($commission_payment)) { \DB::commit(); // Todo:: fire notification event the commission paid. $commission->type = "billing"; \Event::fire('paxifi.notifications.billing', [$commission]); return $this->setStatusCode(201)->respondWithItem($commission); } } } } } } } catch (\Exception $e) { return $this->setStatusCode(400)->respondWithError($e->getMessage()); } }
public function report(EloquentDriverRepository $driver = null) { try { if (is_null($driver)) { $driver = $this->getAuthenticatedDriver(); } $email = \Input::get('email', $driver->email); $year = \Input::get('year', Carbon::now()->year); $report_path = \Input::get('pdf.reports', 'reports/pdf/') . $driver->id . '-' . $year . '-report' . '.pdf'; $report_template = \Input::get('report.sales', 'report.sales'); $salesIds = $driver->sales(Carbon::create($year, 1, 1, 0, 0), Carbon::create($year + 1, 1, 1, 0, 0)); // Total Sales $sales = new SaleCollection($salesIds); $statistics = $sales->toArray(); $reports = []; // Monthly Reports. for ($i = 1; $i <= 12; $i++) { $reports[$i][Carbon::create($year, $i)->format('F')] = ["total_sales" => 0, "total_tax" => 0, "profit" => 0, "commission" => 0]; // Monthly Report. $sales->each(function ($sale) use(&$reports, &$year, &$i) { if ($sale->toArray()['created_at_year'] == $year && $sale->toArray()['created_at_month'] == $i) { $reports[$i][Carbon::create($year, $i)->format('F')]['total_sales'] += $sale->toArray()['total_sales']; $reports[$i][Carbon::create($year, $i)->format('F')]['total_tax'] += $sale->toArray()['total_tax']; $reports[$i][Carbon::create($year, $i)->format('F')]['profit'] += $sale->toArray()['profit']; $reports[$i][Carbon::create($year, $i)->format('F')]['commission'] += $sale->toArray()['commission']; } }); } $htmlTemplate = \View::make($report_template)->with(compact('year', 'statistics', 'driver', 'reports')); $converter = new PdfConverter(); $converter->setPdfDirection('landscape'); $converter->setPdfFilePath($report_path); $converter->setHtmlTemplate($htmlTemplate); $converter->saveHtmlToPdf(); // Config email options for send sales report. $emailOptions = array('template' => 'report.email', 'context' => $this->translator->trans('email.report'), 'to' => $email, 'attach' => $this->flysystem->getAdapter()->getClient()->getObjectUrl(getenv('AWS_S3_BUCKET'), $report_path), 'as' => 'Paxifi Sales Monthly Report -' . $year . '.pdf', 'mime' => 'application/pdf', 'data' => ['name' => $driver->name, 'year' => $year]); if (\Event::fire('paxifi.email', array($emailOptions))) { return $this->setStatusCode(200)->respond(["success" => true]); } } catch (\Exception $e) { return $this->errorInternalError($e->getMessage()); } }
/** * Pay diver commissions. * * @param $subscription * @param $driver */ private function payCommission($subscription, $driver) { $from = $subscription->current_period_start; $to = $subscription->current_period_end; // Get driver total sales ($from -> $to). $sales = new SaleCollection($driver->sales($from, $to)); if (!is_null($sales->toArray()['totals']['commission'])) { \DB::beginTransaction(); if ($commissionPayment = $this->paypal->commissionPayment($sales->toArray()['totals']['commission'], $driver)) { // Todo:: record PayPal commission payment success. $commission_payment = ['driver_id' => $driver->id, 'commissions' => $commissionPayment->amount->total, 'currency' => $commissionPayment->amount->currency, 'status' => 'completed', 'commission_ipn' => $commissionPayment, 'commission_payment_id' => $commissionPayment->id, 'commission_start' => $subscription->current_period_start, 'commission_end' => $subscription->current_period_end]; if ($commission = EloquentCommissionRepository::create($commission_payment)) { \DB::commit(); // Todo:: fire notification event the commission paid. } } else { // Todo:: record commission PayPal payment failed. $commission_payment = ['driver_id' => $driver->id, 'commissions' => $sales->toArray()['totals']['commission'], 'currency' => $driver->currency, 'status' => 'pending', 'commission_start' => $subscription->current_period_start, 'commission_end' => $subscription->current_period_end]; if ($commission = EloquentCommissionRepository::create($commission_payment)) { \DB::commit(); } } } }