public override void OnTick()
        {
            Trace.TraceInformation("OnTick call started.");
            string nextPaymentToken = default(string);

            try
            {
                var nextPaymentLink = PaymentLinkRepository.GetNextLinkForCheck(ClusterId);
                nextPaymentToken = nextPaymentLink.Token;
                Trace.TraceInformation("Next payment link fetched from database. Token: '{0}', FollowId: '{1}'", nextPaymentLink.Token, nextPaymentLink.FollowId);

                Trace.TraceInformation("Checking payment link status for token '{0}'", nextPaymentLink.Token);
                var result = PayRequestService.Check(Options.ApiKey, Options.Password, nextPaymentLink.Token);

                var e = new PaymentLinkChangedEventArgs();
                e.Success    = result.Success;
                e.Message    = result.Message;
                e.Token      = nextPaymentLink.Token;
                e.FollowId   = nextPaymentLink.FollowId;
                e.Status     = result.RequestStatus;
                e.ResultDate = result.VerifyDate;

                if (result.Success)
                {
                    Trace.TraceInformation("IPayRequest.Check called successfully.");
                }
                else
                {
                    Trace.TraceError("IPayRequest.Check call was not successful. Error Type: {0}, Server Message: '{1}'", result.ExceptionType, result.Message);
                }

                OnPaymentLinkChanged(e);
                if (e.Handled)
                {
                    nextPaymentLink.PaymentStatus          = (int)result.RequestStatus;
                    nextPaymentLink.BankReferenceId        = result.BankReferenceId;
                    nextPaymentLink.ResultDate             = result.VerifyDate;
                    nextPaymentLink.LastCheckForUpdateDate = DateTime.Now;
                    PaymentLinkRepository.Update(nextPaymentLink);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("An exception was thrown when trying to update payment link token '{0}'. See exception details: {1}", nextPaymentToken, ex);
            }

            Trace.TraceInformation("OnTick call ended.");
        }
        public void Dispose()
        {
            if (!_isDisposed)
            {
                if (PaymentLinkNotificationService != null)
                {
                    PaymentLinkNotificationService.Dispose();
                }

                if (PaymentLinkRepository != null)
                {
                    PaymentLinkRepository.Dispose();
                }

                _isDisposed = true;
            }
        }
        public PaymentLink Create(decimal amount, string followId, string invoiceNumber, DateTime invoiceDate, ushort expireAfterDays, string description)
        {
            var service = PayRequestFactory.CreateV2();
            var request = new EPayRequestModel();

            request.Amount           = amount;
            request.Description      = description;
            request.ExpiresAfterDays = expireAfterDays;
            request.InvoiceNumber    = invoiceNumber;
            request.InvoiceDate      = invoiceDate;
            request.IsAutoRedirect   = false;

            var result = service.Create(Options.ApiKey, Options.Password, request);

            if (result.Success)
            {
                var link = new PaymentLink();
                link.Amount                 = amount;
                link.Description            = description;
                link.ExpireDays             = expireAfterDays;
                link.FollowId               = followId;
                link.CreateDate             = DateTime.Now;
                link.LastCheckForUpdateDate = DateTime.Now;
                link.PaymentStatus          = (int)RequestStatus.Initiated;
                link.Token      = result.RequestToken;
                link.Url        = result.PaymentUrl;
                link.ResultDate = null;
                link.Id         = Guid.NewGuid();
                link.ClusterId  = PaymentLinkNotificationService.ClusterId;
                PaymentLinkRepository.Insert(link);

                return(link);
            }
            else
            {
                throw new InvalidOperationException(result.Message);
            }
        }