src/services/Customers.php
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;
    }
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;
    }
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 ; }
Returns a customer by gateway and user

@param int $gatewayId The stripe gateway
@param User $user The user

@return Customer
@throws CustomerException
Returns a customer by gateway and user