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());
     }
 }
Exemple #2
0
 /**
  * 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();
             }
         }
     }
 }