public function receiveNotification()
 {
     $ipnMessage = new \PayPal\IPN\PPIPNMessage('', PayPalConfig::getConfig());
     if (!$ipnMessage->validate()) {
         \Log::error("Invalid IPN");
     }
     $ipnData = $ipnMessage->getRawData();
     if (isset($ipnData['txn_type']) && $ipnData['txn_type'] == 'subscr_payment') {
         if ($ipnData['payment_status'] != 'Completed') {
             \Log::error("PayPal IPN: Received unknown payment status for sub payment: \"" . $ipnData['payment_status'] . "\" Email: " . $ipnData['payer_email']);
             return;
         }
         $user = User::where('email', $ipnData['payer_email'])->first();
         if (!$user) {
             $user = User::where('secondary_email', $ipnData['payer_email'])->first();
         }
         if (!$user) {
             //\Log::error("PayPal IPN Received for unknown email " . $ipnData['payer_email']);
             $paymentId = $this->paymentRepository->recordPayment('donation', 0, 'paypal', $ipnData['txn_id'], $ipnData['mc_gross'], 'paid', $ipnData['mc_fee'], $ipnData['payer_email']);
             event(new \BB\Events\UnknownPayPalPaymentReceived($paymentId, $ipnData['payer_email']));
             return;
         }
         //It looks like the user might be joining again
         if ($user->status == 'left') {
             $user->rejoin();
         }
         //If its a new user set them up by creating the first sub charge record and setting the payment day
         if ($user->status == 'setting-up') {
             $this->setupNewMember($user, $ipnData['mc_gross']);
         }
         //Record the subscription payment, this will automatically deal with locating the sub charge and updating that
         $this->paymentRepository->recordSubscriptionPayment($user->id, 'paypal', $ipnData['txn_id'], $ipnData['mc_gross'], 'paid', $ipnData['mc_fee']);
     } elseif (isset($ipnData['txn_type']) && $ipnData['txn_type'] == 'subscr_cancel') {
         $user = User::where('email', $ipnData['payer_email'])->first();
         if ($user) {
             //The user may have already changed to another method, only cancel if its still paypal
             if ($user->payment_method == 'paypal') {
                 $user->cancelSubscription();
             }
             //@TODO: Deal with any open sub payment records
         }
     }
 }
<?php

use Mockery as m;
$I = new FunctionalTester($scenario);
$I->am('a guest');
$I->wantTo('sign up to build brighton');
$I->amOnPage('/');
$I->click('Become a Member');
$I->seeCurrentUrlEquals('/register');
$I->fillField('First Name', 'Jon');
$I->fillField('Family Name', 'Doe');
$I->fillField('Email', '*****@*****.**');
$I->fillField('Password', '12345678');
$I->fillField(['name' => 'address[line_1]'], 'Street Address');
$I->fillField(['name' => 'address[postcode]'], 'BN3 1AN');
$I->fillField('Phone', '0123456789');
$I->fillField('Emergency Contact', 'Contact Details');
$I->attachFile('Profile Photo', 'test-image.png');
$I->checkOption('rules_agreed');
//$userImageService = m::mock('\BB\Helpers\UserImage');
//$userImageService->shouldReceive('uploadPhoto')->times(1);
//$this->app->instance('\BB\Helpers\UserImage',$userImageService);
//$I->haveEnabledFilters();
$I->click('Join');
//Make sure we are now on an account page with the new id
$I->seeCurrentUrlMatches('^/account/(\\d+)^');
$user = \BB\Entities\User::where('email', '*****@*****.**')->first();
$I->assertNotEmpty($user->hash);
 public function getPendingInductionConfirmation()
 {
     return $this->model->where('status', '!=', 'left')->where('induction_completed', true)->where('inducted_by', null)->get();
 }
 /**
  * Get a users latest sub payment
  * @param $userId
  * @return bool
  */
 public function lastUserChargeExpires($userId)
 {
     $charge = User::where('user_id', $userId)->where('status', ['processing', 'paid'])->orderBy('charge_date', 'DESC')->first();
     if ($charge) {
         return $charge->charge_date->addMonth();
     }
     return false;
 }
 private function processSubscriptions($subscriptions)
 {
     foreach ($subscriptions as $sub) {
         //Setup messages aren't used as we deal with them directly.
         if ($sub['status'] == 'cancelled') {
             //Make sure our local record is correct
             $user = User::where('payment_method', 'gocardless')->where('subscription_id', $sub['id'])->first();
             if ($user) {
                 $user->cancelSubscription();
             }
         }
     }
 }