@param \Magento\SalesRule\Model\Coupon $couponModel
@param \DateTime $expireDate
@param \Magento\SalesRule\Model\Rule $rule

@return \Magento\SalesRule\Model\Coupon
199522
private function setExpiryDate($couponModel, $expireDate, $rule)
    {
        $newExpiryDate = $expireDate;
        if ($rule->getToDate() && ! $expireDate) {
            $newExpiryDate = $rule->getToDate();
        }

        $couponModel->setExpirationDate($newExpiryDate);

        return $couponModel;
    }
242751
private function addCouponToOrder($coupons, $new_order)
    {
        $errors = new \Illuminate\Support\MessageBag;
        
        if (count($coupons) > 0) {
            foreach ($coupons as $item) {
                $model = Coupon::find($item);
                if ($model != null) {
                    try {
                        $new_order->addCoupon($model);
                    } catch (Exception $exp) {
                        $errors->add(
                            'couponError',
                            "Coupon " . $model->code . " cannot be added because: " . $exp->getMessage()
                        );
                        
                    }
                }
            }
            
            // Set coupon discount
            $new_order->setDiscounts();
        }
        
        return $errors;
    }
1250198
public function create(CouponCreateOrUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
    {
        $coupon = new CouponModel();

        $this->createOrUpdate($coupon, $event, $dispatcher);
    }
593781
public function makeUse(CouponOnUsedEvent $event)
    {
        $coupon = $event->getCoupon();
        $coupon->makeUse();

        $this
            ->couponObjectManager
            ->flush($coupon);
    }
787559
public function process()
	{
		$view = $this->getView();
		$context = $this->_getContext();

		switch( $view->param( 'b-action' ) )
		{
			case 'coupon-delete':

				if( ( $coupon = $view->param( 'b-coupon' ) ) != '' )
				{
					$this->_clearCached();
					$cntl = Controller_Frontend_Factory::createController( $context, 'basket' );
					$cntl->deleteCoupon( $coupon );
				}

				break;

			default:

				if( ( $coupon = $view->param( 'b-coupon' ) ) != '' )
				{
					$this->_clearCached();
					$cntl = Controller_Frontend_Factory::createController( $context, 'basket' );

					/** client/html/basket/standard/coupon/allowed
					 * Number of coupon codes a customer is allowed to enter
					 *
					 * This configuration option enables shop owners to limit the number of coupon
					 * codes that can be added by a customer to his current basket. By default, only
					 * one coupon code is allowed per order.
					 *
					 * Coupon codes are valid until a payed order is placed by the customer. The
					 * "count" of the codes is decreased afterwards. If codes are not personalized
					 * the codes can be reused in the next order until their "count" reaches zero.
					 *
					 * @param integer Positive number of coupon codes including zero
					 * @since 2014.05
					 * @category User
					 * @category Developer
					 */
					$allowed = $context->getConfig()->get( 'client/html/basket/standard/coupon/allowed', 1 );

					if( $allowed <= count( $cntl->get()->getCoupons() ) ) {
						throw new Client_Html_Exception( sprintf( 'Number of coupon codes exceeds the limit' ) );
					}

					$cntl->addCoupon( $coupon );
				}

				break;
		}

		parent::process();
	}
1247258
public function getBackOfficeInputsAjaxAction($couponServiceId)
    {
        if (null !== $response = $this->checkAuth(AdminResources::COUPON, [], AccessManager::VIEW)) {
            return $response;
        }

        if (! empty($couponServiceId)) {
            $this->checkXmlHttpRequest();

            /** @var CouponInterface $coupon */
            $couponManager = $this->container->get($couponServiceId);

            if (!$couponManager instanceof CouponInterface) {
                $this->pageNotFound();
            }

            $response = new ResponseRest($couponManager->drawBackOfficeInputs(), 'text');
        } else {
            // Return an empty response if the service ID is not defined
            // Typically, when the user chooses "Please select a coupon type"
            $response = new ResponseRest('');
        }

        return $response;
    }
1236383
public function validateDuplicatedCoupon(
        CartInterface $cart,
        CouponInterface $coupon
    ) {
        $cartCoupon = $this
            ->cartCouponRepository
            ->findOneBy([
                'cart' => $cart,
                'coupon' => $coupon,
            ]);

        if (null !== $cartCoupon) {
            throw new CouponAlreadyAppliedException();
        }
    }
170673
def add_coupon(self, coupon, idempotency_key=None):
		
		if isinstance(coupon, StripeModel):
			coupon = coupon.id

		stripe_customer = self.api_retrieve()
		stripe_customer["coupon"] = coupon
		stripe_customer.save(idempotency_key=idempotency_key)
		return self.__class__.sync_from_stripe_data(stripe_customer)
517280
protected function _addCodes( $couponId, array $data )
	{
		$manager = MShop_Factory::createManager( $this->_getContext(), 'coupon/code' );

		foreach( $data as $entry )
		{
			$item = $manager->createItem();
			$item->setCouponId( $couponId );
			$item->setCode( $entry['code'] );
			$item->setCount( $entry['count'] );
			$item->setDateStart( $entry['datestart'] );
			$item->setDateEnd( $entry['dateend'] );

			$manager->saveItem( $item );
		}
	}
567398
public function addCoupon(CouponContract $coupon)
    {
        $coupon->appliedToCart = false;
        app('laracart')->addCoupon($coupon);
        $this->code = $coupon->code;
        $this->couponInfo = $coupon->options;
        $this->discount = $coupon->forItem($this);

        return $this;
    }
85313
public function addCoupon(CouponContract $coupon)
    {
        if (!$this->cart->multipleCoupons) {
            $this->cart->coupons = [];
        }

        $this->cart->coupons[$coupon->code] = $coupon;

        $this->update();
    }
1281504
public function getProvider( MShop_Coupon_Item_Interface $item, $code )
	{
		$names = explode( ',', $item->getProvider() );

		if( ( $providername = array_shift( $names ) ) === null ){
			throw new MShop_Coupon_Exception( sprintf( 'Provider in "%1$s" not available', $item->getProvider() ) );
		}

		if ( ctype_alnum( $providername ) === false ) {
			throw new MShop_Coupon_Exception( sprintf( 'Invalid characters in provider name "%1$s"', $providername ) );
		}

		$interface = 'MShop_Coupon_Provider_Factory_Interface';
		$classname = 'MShop_Coupon_Provider_' . $providername;

		if( class_exists( $classname ) === false ) {
			throw new MShop_Coupon_Exception( sprintf( 'Class "%1$s" not available', $classname ) );
		}

		$context = $this->_getContext();
		$provider = new $classname( $context, $item, $code );

		if( ( $provider instanceof $interface ) === false )
		{
			$msg = sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $interface );
			throw new MShop_Coupon_Exception( $msg );
		}

		$decorators = $context->getConfig()->get( 'mshop/coupon/provider/decorators', array() );

		$object = $this->_addCouponDecorators( $item, $code, $provider, $names );
		$object = $this->_addCouponDecorators( $item, $code, $object, $decorators );
		$object->setObject( $object );

		return $object;
	}
416968
public function setCoupon($coupon)
    {
        $this->discountAmount = $coupon->discount;
        $this->couponCode = $coupon->code;

        return $this;
    }
381861
public function buildCouponFromCode($couponCode)
    {
        /** @var Coupon $couponModel */
        $couponModel = $this->facade->findOneCouponByCode($couponCode);
        if ($couponModel === null) {
            return false;
        }

        // check if coupon is enabled
        if (!$couponModel->getIsEnabled()) {
            throw new InactiveCouponException($couponCode);
        }

        $nowDateTime = new \DateTime();

        // Check coupon start date
        if ($couponModel->getStartDate() !== null && $couponModel->getStartDate() > $nowDateTime) {
            throw new CouponNotReleaseException($couponCode);
        }

        // Check coupon expiration date
        if ($couponModel->getExpirationDate() < $nowDateTime) {
            throw new CouponExpiredException($couponCode);
        }

        // Check coupon usage count
        if (! $couponModel->isUsageUnlimited()) {
            if (null === $customer = $this->facade->getCustomer()) {
                throw new UnmatchableConditionException($couponCode);
            }

            if ($couponModel->getUsagesLeft($customer->getId()) <= 0) {
                throw new CouponNoUsageLeftException($couponCode);
            }
        }

        /** @var CouponInterface $couponInterface */
        $couponInterface = $this->buildCouponFromModel($couponModel);

        if ($couponInterface && $couponInterface->getConditions()->count() == 0) {
            throw new InvalidConditionException(
                \get_class($couponInterface)
            );
        }

        return $couponInterface;
    }
1249379
public function addCoupon(
        CartInterface $cart,
        CouponInterface $coupon
    ) {
        $this
            ->cartCouponEventDispatcher
            ->dispatchCartCouponOnApplyEvent(
                $cart,
                $coupon
            );

        return $this;
    }
418960
private function canBeUsed(CouponInterface $coupon)
    {
        $count = $coupon->getCount();

        if ($count === 0) {
            return true;
        }

        if ($coupon->getUsed() < $count) {
            return true;
        }

        return false;
    }
1300504
private function getPriceRule($priceRuleId)
    {
        $rule = $this->ruleFactory->create();
        $this->ruleResource->load($rule, $priceRuleId);
        $rule->setCouponCodeGenerator($this->couponCodeGenerator);
        $rule->setCouponType(
            \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO
        );

        return $rule;
    }