Gets the local customer object searching for it in the database or in the newly created entities persisted but
not yet flushed.

@param $stripeCustomerId

@return bool|StripeLocalCustomer false if the StripeLocalCustomer was not found
12654
protected function getLocalCustomer($stripeCustomerId)
    {
        // First try to get the customer from the database
        $localCustomer = $this->getEntityManager()->getRepository('SHQStripeBundle:StripeLocalCustomer')->findOneByStripeId($stripeCustomerId);

        // If we found it, return it
        if (null !== $localCustomer) {
            return $localCustomer;
        }

        // Try to find the customer in the newly created one that are not already persisted
        return $this->getEntityManager()->getUnitOfWork()->tryGetById($stripeCustomerId, StripeLocalCustomer::class);
    }
310959
protected function getStripeCustomer($token = null, array $options = [])
    {
        if (!$this->user->stripe_id) {
            $customer = $this->user->createAsStripeCustomer(
                $token, array_merge($options, array_filter(['coupon' => $this->coupon]))
            );
        } else {
            $customer = $this->user->asStripeCustomer();
            if ($token) {
                $this->user->updateCard($token);
            }
        }

        return $customer;
    }
1189492
public function updateCustomer(StripeLocalCustomer $localCustomer, $syncSources): bool
    {
        // Get the stripe object
        $stripeCustomer = $this->retrieveCustomer($localCustomer);

        // The retrieving failed: return false
        if (false === $stripeCustomer) {
            return false;
        }

        // Update the stripe object with info set in the local object
        $this->customerSyncer->syncStripeFromLocal($stripeCustomer, $localCustomer);

        // Save the customer object
        $stripeCustomer = $this->callStripeObject($stripeCustomer, 'save');

        // If the update failed, return false
        if (false === $stripeCustomer) {
            return false;
        }

        // Set the data returned by Stripe in the LocalCustomer object
        $this->customerSyncer->syncLocalFromStripe($localCustomer, $stripeCustomer);

        if (true === $syncSources) {
            $this->customerSyncer->syncLocalSources($localCustomer, $stripeCustomer);
        }

        return true;
    }
291074
public static Customer retrieve(String customer) throws StripeException {
    return retrieve(customer, (Map<String, Object>) null, (RequestOptions) null);
  }
426445
public function guess(Event $stripeEvent, StripeLocalWebhookEvent $localEventEntity)
    {
        $pieces = $this->guessEventPieces($stripeEvent->type);

        switch ($pieces['kind']) {
            case 'account':
                $disptachingEvent = new StripeWebhookAccountEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookAccountEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'application_fee':
                $disptachingEvent = new StripeWebhookApplicationFeeEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookApplicationFeeEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'balance':
                $disptachingEvent = new StripeWebhookBalanceEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookBalanceEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'bitcoin':
                $disptachingEvent = new StripeWebhookBitcoinEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookBitcoinEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'charge':
                $disptachingEvent = new StripeWebhookChargeEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookChargeEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'coupon':
                $disptachingEvent = new StripeWebhookCouponEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookCouponEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'customer':
                $disptachingEvent = new StripeWebhookCustomerEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookCustomerEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'invoice':
                $disptachingEvent = new StripeWebhookInvoiceEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookInvoiceEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'invpiceitem':
                $disptachingEvent = new StripeWebhookInvoiceItemEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookInvoiceItemEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'order':
                $disptachingEvent = new StripeWebhookOrderEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookOrderEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'order_return':
                $disptachingEvent = new StripeWebhookOrderReturnEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookOrderReturnEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'plan':
                $disptachingEvent = new StripeWebhookPlanEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookPlanEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'product':
                $disptachingEvent = new StripeWebhookProductEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookProductEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'recipient':
                $disptachingEvent = new StripeWebhookRecipientEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookRecipientEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'review':
                $disptachingEvent = new StripeWebhookReviewEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookReviewEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'sku':
                $disptachingEvent = new StripeWebhookSkuEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookSkuEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'source':
                $disptachingEvent = new StripeWebhookSourceEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookSourceEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'transfer':
                $disptachingEvent = new StripeWebhookTransferEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookTransferEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            case 'ping':
                $disptachingEvent = new StripeWebhookPingEventEvent($localEventEntity);

                return [
                    'type'   => constant(StripeWebhookPingEventEvent::class . '::' . $pieces['type']),
                    'object' => $disptachingEvent,
                ];
                break;

            default:
                if ($this->debug) {
                    throw new \RuntimeException('Event type not recognized. Maybe it is a new one or is not yet supported by the bundle. Report this issue to the GitHub Issue tracker.');
                }

                return ['type' => null, 'object' => null];
        }
    }
557271
public function createAsStripeCustomer(array $options = [])
    {
        $options = array_key_exists('email', $options)
                ? $options
                : array_merge($options, ['email' => $this->email]);

        // Here we will create the customer instance on Stripe and store the ID of the
        // user from Stripe. This ID will correspond with the Stripe user instances
        // and allow us to retrieve users from Stripe later when we need to work.
        $customer = StripeCustomer::create(
            $options, $this->getStripeKey()
        );

        $this->stripe_id = $customer->id;

        $this->save();

        return $customer;
    }
1258440
public function getDefaultCard()
    {
        if (!$this->stripe_id) {
            return null;
        }

        $customer = $this->asStripeCustomer();

        foreach ($customer->sources->data as $card) {
            if ($card->id === $customer->default_source) {
                return $card;
            }
        }

        return null;
    }
1162260
private function getCustomerFromToken()
    {
        if (!($this->tokenStorage instanceof TokenStorageInterface)) {
            return null;
        }

        $token = $this->tokenStorage->getToken();
        if (!($token instanceof TokenInterface)) {
            return null;
        }

        $customer = $token->getUser();
        if (!($customer instanceof CustomerInterface)) {
            return null;
        }

        return $customer;
    }
979965
public function get(int $id, CustomerRequest $customerRequest = null): Customer
    {
        $customerResource = $this->restClient->getResource(
            Customer::class,
            sprintf('/v2/customers/%d', $id)
        );

        return $this->hydrateCustomerWithSubEntities(
            $customerResource,
            $customerRequest ?: new CustomerRequest()
        );
    }
217756
protected function getExistingObject()
    {
        $orderStatus = OrderStatusQuery::create()
            ->findOneById($this->getRequest()->get('order_status_id', 0));

        if (null !== $orderStatus) {
            $orderStatus->setLocale($this->getCurrentEditionLocale());
        }

        return $orderStatus;
    }
772484
public function getCustomer(int $gatewayId, User $user): Customer
    {
        $result = $this->_createCustomerQuery()
            ->where(['userId' => $user->id, 'gatewayId' => $gatewayId])
            ->one();

        if ($result !== null) {
            return new Customer($result);
        }

        Stripe::setApiKey(Craft::parseEnv(Commerce::getInstance()->getGateways()->getGatewayById($gatewayId)->apiKey));
        Stripe::setAppInfo(StripePlugin::getInstance()->name, StripePlugin::getInstance()->version, StripePlugin::getInstance()->documentationUrl);
        Stripe::setApiVersion(Gateway::STRIPE_API_VERSION);

        /** @var StripeCustomer $stripeCustomer */
        $stripeCustomer = StripeCustomer::create([
            'description' => Craft::t('commerce-stripe', 'Customer for Craft user with ID {id}', ['id' => $user->id]),
            'email' => $user->email
        ]);

        $customer = new Customer([
            'userId' => $user->id,
            'gatewayId' => $gatewayId,
            'reference' => $stripeCustomer->id,
            'response' => $stripeCustomer->jsonSerialize()
        ]);

        if (!$this->saveCustomer($customer)) {
            throw new CustomerException('Could not save customer: ' . implode(', ', $customer->getErrorSummary(true)));
        }

        return $customer;
    }
292937
public function getCustomerByExtUid($customerExtUid)
    {
        /** @var CustomerFbApi $response */
        $response = $this->call(new Request('customer.get', array('customer_ext_uid' => $customerExtUid)), 'DVelopment\FastBill\Model\CustomerFbApi');
        $customers = $response->getResponse()->getCustomers();

        return reset($customers);
    }
455531
public function getCustomer($customerId)
    {
        $requestData = new Get\RequestData();
        $requestData->setCustomerId($customerId);

        $customers = $this->getCustomers($requestData)->getResponse()->getCustomers();

        return isset($customers[0]) ? $customers[0] : null;
    }
1019578
public function updateStripeCustomer(array $options = [])
    {
        $customer = StripeCustomer::update(
            $this->stripe_id, $options, $this->getStripeKey()
        );

        return $customer;
    }
1161621
public function getCustomer(PropelPDO $con = null, $doQuery = true)
    {
        if ($this->aCustomer === null && ($this->customer_id !== null) && $doQuery) {
            $this->aCustomer = CustomerQuery::create()->findPk($this->customer_id, $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aCustomer->addUserCustomerRelations($this);
             */
        }

        return $this->aCustomer;
    }
1203210
private function _determineCustomerId($customerOrId)
    {
        $customerId = ($customerOrId instanceof Customer) ? $customerOrId->id : $customerOrId;
        $this->_validateCustomerId($customerId);
        return $customerId;

    }
990822
public function createAsStripeCustomer(string $token, array $options = []): Customer
    {
        $options = array_key_exists('email', $options)
            ? $options : array_merge($options, ['email' => $this->email]);

        // Here we will create the customer instance on Stripe and store the ID of the
        // user from Stripe. This ID will correspond with the Stripe user instances
        // and allow us to retrieve users from Stripe later when we need to work.
        $customer = Customer::create($options, $this->getStripeKey());

        $this->stripe_id = $customer->id;

        $this->save();

        // Next we will add the credit card to the user's account on Stripe using this
        // token that was provided to this method. This will allow us to bill users
        // when they subscribe to plans or we need to do one-off charges on them.
        if (!is_null($token)) {
            $this->updateCard($token);
        }

        return $customer;
    }