public function receive()
 {
     $request = \Request::instance();
     if (!$this->goCardless->validateWebhook($request->getContent())) {
         return \Response::make('', 403);
     }
     $parser = new \BB\Services\Payment\GoCardlessWebhookParser();
     $parser->parseResponse($request->getContent());
     switch ($parser->getResourceType()) {
         case 'bill':
             switch ($parser->getAction()) {
                 case 'created':
                     $this->processNewBills($parser->getBills());
                     break;
                 case 'paid':
                     $this->processPaidBills($parser->getBills());
                     break;
                 default:
                     $this->processBills($parser->getAction(), $parser->getBills());
             }
             break;
         case 'pre_authorization':
             $this->processPreAuths($parser->getAction(), $parser->getPreAuthList());
             break;
         case 'subscription':
             $this->processSubscriptions($parser->getSubscriptions());
             break;
     }
     return \Response::make('Success', 200);
 }
 /**
  * Create a charge and immediately bill it through the direct debit process
  *
  * @param integer   $userId
  * @param \DateTime $date
  * @param integer   $amount
  * @param string    $status
  * @param string    $DDAuthId
  * @return SubscriptionCharge
  */
 public function createChargeAndBillDD($userId, $date, $amount, $status, $DDAuthId)
 {
     $charge = $this->createCharge($userId, $date, $amount, $status);
     $bill = $this->goCardless->newBill($DDAuthId, $amount, $this->goCardless->getNameFromReason('subscription'));
     if ($bill) {
         $this->paymentRepository->recordSubscriptionPayment($userId, 'gocardless-variable', $bill->id, $bill->amount, $bill->status, $bill->gocardless_fees, $charge->id);
     }
 }
 /**
  * Bill members based on the sub charges that are due
  */
 public function billMembers()
 {
     $subCharges = $this->subscriptionChargeRepository->getDue();
     foreach ($subCharges as $charge) {
         if ($charge->user->payment_method == 'gocardless-variable') {
             //Look the the previous attempts - there may be multiple failures
             $existingPayments = $this->paymentRepository->getPaymentsByReference($charge->id);
             if ($existingPayments->count() > 0) {
                 //We will let the user retry the payment if it fails
                 continue;
             }
             $bill = $this->goCardless->newBill($charge->user->subscription_id, $charge->user->monthly_subscription, $this->goCardless->getNameFromReason('subscription'));
             if ($bill) {
                 $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'gocardless-variable', $bill->id, $bill->amount, $bill->status, $bill->gocardless_fees, $charge->id);
             }
         }
     }
 }
 /**
  * Bill members based on the sub charges that are due
  */
 public function billMembers()
 {
     $subCharges = $this->subscriptionChargeRepository->getDue();
     //Check each of the due charges, if they have previous attempted payments ignore them
     // we don't want to retry failed payments as for DD's this will generate bank charges
     $subCharges->reject(function ($charge) {
         return $this->paymentRepository->getPaymentsByReference($charge->id)->count() > 0;
     });
     //Filter the list into two gocardless and balance subscriptions
     $goCardlessUsers = $subCharges->filter(function ($charge) {
         return $charge->user->payment_method == 'gocardless-variable';
     });
     $balanceUsers = $subCharges->filter(function ($charge) {
         return $charge->user->payment_method == 'balance';
     });
     //Charge the balance users
     foreach ($balanceUsers as $charge) {
         if ($charge->user->monthly_subscription * 100 > $charge->user->cash_balance) {
             //user doesn't have enough money
             //If they have a secondary payment method of gocardless try that
             if ($charge->user->secondary_payment_method == 'gocardless-variable') {
                 //Add the charge to the gocardless list for processing
                 $goCardlessUsers->push($charge);
                 event(new SubscriptionPayment\InsufficientFundsTryingDirectDebit($charge->user->id, $charge->id));
             } else {
                 event(new SubscriptionPayment\FailedInsufficientFunds($charge->user->id, $charge->id));
             }
             continue;
         }
         $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'balance', '', $charge->user->monthly_subscription, 'paid', 0, $charge->id);
         event(new MemberBalanceChanged($charge->user->id));
     }
     //Charge the gocardless users
     foreach ($goCardlessUsers as $charge) {
         $bill = $this->goCardless->newBill($charge->user->subscription_id, $charge->user->monthly_subscription, $this->goCardless->getNameFromReason('subscription'));
         if ($bill) {
             $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'gocardless-variable', $bill->id, $bill->amount, $bill->status, $bill->gocardless_fees, $charge->id);
         }
     }
 }
 /**
  * Process a direct debit payment when we have a preauth
  *
  * @param $amount
  * @param $reason
  * @param User $user
  * @param $ref
  * @param $returnPath
  * @return mixed
  */
 private function handleBill($amount, $reason, $user, $ref, $returnPath)
 {
     $bill = $this->goCardless->newBill($user->subscription_id, $amount, $this->goCardless->getNameFromReason($reason));
     if ($bill) {
         //Store the payment
         $fee = $bill->amount - $bill->amount_minus_fees;
         $paymentSourceId = $bill->id;
         $amount = $bill->amount;
         $status = $bill->status;
         //The record payment process will make the necessary record updates
         $this->paymentRepository->recordPayment($reason, $user->id, 'gocardless-variable', $paymentSourceId, $amount, $status, $fee, $ref);
         if (\Request::wantsJson()) {
             return \Response::json(['message' => 'The payment was submitted successfully']);
         }
         \Notification::success("The payment was submitted successfully");
     } else {
         //something went wrong - we still have the pre auth though
         if (\Request::wantsJson()) {
             return \Response::json(['error' => 'There was a problem charging your account'], 400);
         }
         \Notification::error("There was a problem charging your account");
     }
     return \Redirect::to($returnPath);
 }