public function save($publicId = null, $input)
 {
     if ($publicId) {
         $payment = Payment::scope($publicId)->firstOrFail();
     } else {
         $payment = Payment::createNew();
     }
     $paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null;
     $payment->payment_type_id = $paymentTypeId;
     $payment->payment_date = Utils::toSqlDate($input['payment_date']);
     $payment->transaction_reference = trim($input['transaction_reference']);
     if (!$publicId) {
         $clientId = Client::getPrivateId($input['client']);
         $amount = Utils::parseFloat($input['amount']);
         if ($paymentTypeId == PAYMENT_TYPE_CREDIT) {
             $credits = Credit::scope()->where('client_id', '=', $clientId)->where('balance', '>', 0)->orderBy('created_at')->get();
             $applied = 0;
             foreach ($credits as $credit) {
                 $applied += $credit->apply($amount);
                 if ($applied >= $amount) {
                     break;
                 }
             }
         }
         $payment->client_id = $clientId;
         $payment->invoice_id = isset($input['invoice']) && $input['invoice'] != "-1" ? Invoice::getPrivateId($input['invoice']) : null;
         $payment->amount = $amount;
     }
     $payment->save();
     return $payment;
 }
 public function save($input, $payment = null)
 {
     $publicId = isset($input['public_id']) ? $input['public_id'] : false;
     if ($payment) {
         // do nothing
     } elseif ($publicId) {
         $payment = Payment::scope($publicId)->firstOrFail();
         if (Utils::isNinjaDev()) {
             \Log::warning('Entity not set in payment repo save');
         }
     } else {
         $payment = Payment::createNew();
     }
     if ($payment->is_deleted) {
         return $payment;
     }
     $paymentTypeId = false;
     if (isset($input['payment_type_id'])) {
         $paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null;
         $payment->payment_type_id = $paymentTypeId;
     }
     if (isset($input['payment_date_sql'])) {
         $payment->payment_date = $input['payment_date_sql'];
     } elseif (isset($input['payment_date'])) {
         $payment->payment_date = Utils::toSqlDate($input['payment_date']);
     } else {
         $payment->payment_date = date('Y-m-d');
     }
     if (isset($input['transaction_reference'])) {
         $payment->transaction_reference = trim($input['transaction_reference']);
     }
     if (!$publicId) {
         $clientId = $input['client_id'];
         $amount = Utils::parseFloat($input['amount']);
         if ($paymentTypeId == PAYMENT_TYPE_CREDIT) {
             $credits = Credit::scope()->where('client_id', '=', $clientId)->where('balance', '>', 0)->orderBy('created_at')->get();
             $remaining = $amount;
             foreach ($credits as $credit) {
                 $remaining -= $credit->apply($remaining);
                 if (!$remaining) {
                     break;
                 }
             }
         }
         $payment->invoice_id = $input['invoice_id'];
         $payment->client_id = $clientId;
         $payment->amount = $amount;
     }
     $payment->save();
     return $payment;
 }
Exemplo n.º 3
0
 public function createPayment($invitation, $ref, $payerId = null)
 {
     $invoice = $invitation->invoice;
     $accountGateway = $invoice->client->account->getGatewayByType(Session::get('payment_type'));
     // sync pro accounts
     if ($invoice->account->account_key == NINJA_ACCOUNT_KEY && $invoice->amount == PRO_PLAN_PRICE) {
         $account = Account::with('users')->find($invoice->client->public_id);
         if ($account->pro_plan_paid && $account->pro_plan_paid != '0000-00-00') {
             $date = DateTime::createFromFormat('Y-m-d', $account->pro_plan_paid);
             $account->pro_plan_paid = $date->modify('+1 year')->format('Y-m-d');
         } else {
             $account->pro_plan_paid = date_create()->format('Y-m-d');
         }
         $account->save();
         $user = $account->users()->first();
         $this->accountRepo->syncAccounts($user->id, $account->pro_plan_paid);
     }
     $payment = Payment::createNew($invitation);
     $payment->invitation_id = $invitation->id;
     $payment->account_gateway_id = $accountGateway->id;
     $payment->invoice_id = $invoice->id;
     $payment->amount = $invoice->getRequestedAmount();
     $payment->client_id = $invoice->client_id;
     $payment->contact_id = $invitation->contact_id;
     $payment->transaction_reference = $ref;
     $payment->payment_date = date_create()->format('Y-m-d');
     if ($payerId) {
         $payment->payer_id = $payerId;
     }
     $payment->save();
     Event::fire(new InvoicePaid($payment));
     return $payment;
 }
 public function createPayment($ref = false, $paymentMethod = null)
 {
     $invitation = $this->invitation;
     $invoice = $this->invoice();
     $payment = Payment::createNew($invitation);
     $payment->invitation_id = $invitation->id;
     $payment->account_gateway_id = $this->accountGateway->id;
     $payment->invoice_id = $invoice->id;
     $payment->amount = $invoice->getRequestedAmount();
     $payment->client_id = $invoice->client_id;
     $payment->contact_id = $invitation->contact_id;
     $payment->transaction_reference = $ref;
     $payment->payment_date = date_create()->format('Y-m-d');
     $payment->ip = Request::ip();
     $payment = $this->creatingPayment($payment, $paymentMethod);
     if ($paymentMethod) {
         $payment->last4 = $paymentMethod->last4;
         $payment->expiration = $paymentMethod->expiration;
         $payment->routing_number = $paymentMethod->routing_number;
         $payment->payment_type_id = $paymentMethod->payment_type_id;
         $payment->email = $paymentMethod->email;
         $payment->bank_name = $paymentMethod->bank_name;
         $payment->payment_method_id = $paymentMethod->id;
     }
     $payment->save();
     // TODO move this code
     // enable pro plan for hosted users
     if ($invoice->account->account_key == NINJA_ACCOUNT_KEY) {
         foreach ($invoice->invoice_items as $invoice_item) {
             // Hacky, but invoices don't have meta fields to allow us to store this easily
             if (1 == preg_match('/^Plan - (.+) \\((.+)\\)$/', $invoice_item->product_key, $matches)) {
                 $plan = strtolower($matches[1]);
                 $term = strtolower($matches[2]);
                 $price = $invoice_item->cost;
                 if ($plan == PLAN_ENTERPRISE) {
                     if (count($matches)) {
                         $numUsers = $matches[1];
                     } else {
                         $numUsers = 5;
                     }
                 } else {
                     $numUsers = 1;
                 }
             }
         }
         if (!empty($plan)) {
             $account = Account::with('users')->find($invoice->client->public_id);
             $company = $account->company;
             if ($company->plan != $plan || DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create('-7 days')) {
                 // Either this is a different plan, or the subscription expired more than a week ago
                 // Reset any grandfathering
                 $company->plan_started = date_create()->format('Y-m-d');
             }
             if ($company->plan == $plan && $company->plan_term == $term && DateTime::createFromFormat('Y-m-d', $company->plan_expires) >= date_create()) {
                 // This is a renewal; mark it paid as of when this term expires
                 $company->plan_paid = $company->plan_expires;
             } else {
                 $company->plan_paid = date_create()->format('Y-m-d');
             }
             $company->payment_id = $payment->id;
             $company->plan = $plan;
             $company->plan_term = $term;
             $company->plan_price = $price;
             $company->num_users = $numUsers;
             $company->plan_expires = DateTime::createFromFormat('Y-m-d', $account->company->plan_paid)->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d');
             $company->save();
         }
     }
     return $payment;
 }
Exemplo n.º 5
0
 public function createPayment($invitation, $accountGateway, $ref, $payerId = null)
 {
     $invoice = $invitation->invoice;
     // enable pro plan for hosted users
     if ($invoice->account->account_key == NINJA_ACCOUNT_KEY && $invoice->amount == PRO_PLAN_PRICE) {
         $account = Account::with('users')->find($invoice->client->public_id);
         $account->pro_plan_paid = $account->getRenewalDate();
         $account->save();
         // sync pro accounts
         $user = $account->users()->first();
         $this->accountRepo->syncAccounts($user->id, $account->pro_plan_paid);
     }
     $payment = Payment::createNew($invitation);
     $payment->invitation_id = $invitation->id;
     $payment->account_gateway_id = $accountGateway->id;
     $payment->invoice_id = $invoice->id;
     $payment->amount = $invoice->getRequestedAmount();
     $payment->client_id = $invoice->client_id;
     $payment->contact_id = $invitation->contact_id;
     $payment->transaction_reference = $ref;
     $payment->payment_date = date_create()->format('Y-m-d');
     if ($payerId) {
         $payment->payer_id = $payerId;
     }
     $payment->save();
     return $payment;
 }
Exemplo n.º 6
0
 public function createPayment($invitation, $accountGateway, $ref, $payerId = null)
 {
     $invoice = $invitation->invoice;
     $payment = Payment::createNew($invitation);
     $payment->invitation_id = $invitation->id;
     $payment->account_gateway_id = $accountGateway->id;
     $payment->invoice_id = $invoice->id;
     $payment->amount = $invoice->getRequestedAmount();
     $payment->client_id = $invoice->client_id;
     $payment->contact_id = $invitation->contact_id;
     $payment->transaction_reference = $ref;
     $payment->payment_date = date_create()->format('Y-m-d');
     if ($payerId) {
         $payment->payer_id = $payerId;
     }
     $payment->save();
     // enable pro plan for hosted users
     if ($invoice->account->account_key == NINJA_ACCOUNT_KEY) {
         foreach ($invoice->invoice_items as $invoice_item) {
             // Hacky, but invoices don't have meta fields to allow us to store this easily
             if (1 == preg_match('/^Plan - (.+) \\((.+)\\)$/', $invoice_item->product_key, $matches)) {
                 $plan = strtolower($matches[1]);
                 $term = strtolower($matches[2]);
             } elseif ($invoice_item->product_key == 'Pending Monthly') {
                 $pending_monthly = true;
             }
         }
         if (!empty($plan)) {
             $account = Account::with('users')->find($invoice->client->public_id);
             if ($account->company->plan != $plan || DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create('-7 days')) {
                 // Either this is a different plan, or the subscription expired more than a week ago
                 // Reset any grandfathering
                 $account->company->plan_started = date_create()->format('Y-m-d');
             }
             if ($account->company->plan == $plan && $account->company->plan_term == $term && DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create()) {
                 // This is a renewal; mark it paid as of when this term expires
                 $account->company->plan_paid = $account->company->plan_expires;
             } else {
                 $account->company->plan_paid = date_create()->format('Y-m-d');
             }
             $account->company->payment_id = $payment->id;
             $account->company->plan = $plan;
             $account->company->plan_term = $term;
             $account->company->plan_expires = DateTime::createFromFormat('Y-m-d', $account->company->plan_paid)->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d');
             if (!empty($pending_monthly)) {
                 $account->company->pending_plan = $plan;
                 $account->company->pending_term = PLAN_TERM_MONTHLY;
             } else {
                 $account->company->pending_plan = null;
                 $account->company->pending_term = null;
             }
             $account->company->save();
         }
     }
     return $payment;
 }