App\Ninja\PaymentDrivers\BasePaymentDriver::createPayment PHP Method

createPayment() public method

public createPayment ( $ref = false, $paymentMethod = null )
    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) {
                        preg_match('/###[\\d] [\\w]* (\\d*)/', $invoice_item->notes, $numUserMatches);
                        if (count($numUserMatches)) {
                            $numUsers = $numUserMatches[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;
    }