public async Task <IActionResult> GetInvoice(string id)
        {
            ViewModels.Invoice result = null;
            // query the Dynamics system to get the invoice record.
            if (string.IsNullOrEmpty(id))
            {
                return(new NotFoundResult());
            }
            else
            {
                // get the current user.
                string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
                UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

                Guid adoxio_legalentityid           = new Guid(id);
                MicrosoftDynamicsCRMinvoice invoice = await _dynamicsClient.GetInvoiceById(adoxio_legalentityid);

                if (invoice == null)
                {
                    return(new NotFoundResult());
                }

                // setup the related account.
                if (invoice._accountidValue != null)
                {
                    Guid accountId = Guid.Parse(invoice._accountidValue);
                    invoice.CustomeridAccount = await _dynamicsClient.GetAccountById(accountId);
                }

                result = invoice.ToViewModel();
            }

            return(Json(result));
        }
        public async Task <IActionResult> GetInvoice(string id)
        {
            if (TestUtility.InUnitTestMode())
            {
                ViewModels.Invoice result = null;
                // query the Dynamics system to get the invoice record.
                if (string.IsNullOrEmpty(id))
                {
                    return(new NotFoundResult());
                }

                // get the current user.
                UserSettings userSettings = UserSettings.CreateFromHttpContext(_httpContextAccessor);

                Guid adoxio_legalentityid           = new Guid(id);
                MicrosoftDynamicsCRMinvoice invoice = await _dynamicsClient.GetInvoiceById(adoxio_legalentityid);

                if (invoice == null)
                {
                    return(new NotFoundResult());
                }

                // setup the related account.
                if (invoice._accountidValue != null)
                {
                    Guid accountId = Guid.Parse(invoice._accountidValue);
                    invoice.CustomeridAccount = await _dynamicsClient.GetAccountByIdAsync(accountId);
                }

                result = invoice.ToViewModel();

                return(new JsonResult(result));
            }
            return(new NotFoundResult());
        }
        public async Task <IActionResult> GetPaymentUrl(string id)
        {
            _logger.LogDebug("Called GetPaymentUrl(" + id + ")");

            // get the application and confirm access (call parse to ensure we are getting a valid id)
            Guid applicationId = Guid.Parse(id);
            MicrosoftDynamicsCRMadoxioApplication application = await GetDynamicsApplication(id);

            if (application == null)
            {
                return(NotFound());
            }
            if (application.AdoxioInvoice?.Statuscode == (int?)Adoxio_invoicestatuses.Paid)
            {
                return(NotFound("Payment already made"));
            }

            // set the application invoice trigger to create an invoice
            // no need to copy the whole record over as we are doing a Patch for a single field.
            MicrosoftDynamicsCRMadoxioApplication patchApplication = new MicrosoftDynamicsCRMadoxioApplication()
            {
                // this is the money - setting this flag to "Y" triggers a dynamics workflow that creates an invoice
                AdoxioInvoicetrigger = (int?)ViewModels.GeneralYesNo.Yes
            };

            try
            {
                _dynamicsClient.Applications.Update(id, patchApplication);
            }
            catch (HttpOperationException httpOperationException)
            {
                _logger.LogError(httpOperationException, "Error updating application");
                // fail
                throw (httpOperationException);
            }
            application = await GetDynamicsApplication(id);

            // now load the invoice for this application to get the pricing
            string invoiceId = application._adoxioInvoiceValue;
            int    retries   = 0;

            while (retries < 10 && (invoiceId == null || invoiceId.Length == 0))
            {
                // should happen immediately, but ...
                // pause and try again - in case Dynamics is slow ...
                retries++;
                _logger.LogError($"No application {id} invoice found, retry = " + retries);
                System.Threading.Thread.Sleep(1000);
                application = await GetDynamicsApplication(id);

                invoiceId = application._adoxioInvoiceValue;
            }

            if (!string.IsNullOrEmpty(invoiceId))
            {
                _logger.LogDebug("Created invoice for application = " + invoiceId);

                /*
                 * When the applicant submits their Application, we will set the application "Application Invoice Trigger" to "Y" - this will trigger a workflow that will create the Invoice
                 *  - we will then re-query the Application to get the Invoice number,
                 *  - and then query the Invoice to get the amount
                 *  - the Invoice will also contain a Transaction Id (starting at 0500000000)
                 *  - the Invoice status will be New
                 * Notes:
                 *  - If there is already an invoice with Status New, don't need to create a new Invoice
                 *  - If there is already an invoice with Status Complete, it is an error (can't pay twice)
                 *  - We will deal with the history later (i.e. there can be multiple "Cancelled" Invoices - we need to keep them for reconciliation but we don't need them for MVP
                 */

                MicrosoftDynamicsCRMinvoice invoice = await _dynamicsClient.GetInvoiceById(invoiceId);

                // dynamics creates a unique transaction id per invoice, used as the "order number" for payment
                var ordernum = invoice.AdoxioTransactionid;
                // dynamics determines the amount based on the licence type of the application
                var orderamt = invoice.Totalamount;

                Dictionary <string, string> redirectUrl;
                redirectUrl = new Dictionary <string, string>();

                bool isAlternateAccount = application.IsLiquor(_dynamicsClient); // set to true for Liquor.

                redirectUrl["url"] = _bcep.GeneratePaymentRedirectUrl(ordernum, id, String.Format("{0:0.00}", orderamt), isAlternateAccount);

                _logger.LogDebug(">>>>>" + redirectUrl["url"]);

                return(new JsonResult(redirectUrl));
            }
            else
            {
                _logger.LogError("GetPaymentUrl failed - Unable to get invoice for application {id}");
                return(NotFound());
            }
        }
Beispiel #4
0
        public async Task <IActionResult> GetPaymentUrl(string id)
        {
            _logger.LogError("Called GetPaymentUrl(" + id + ")");

            // get the application and confirm access (call parse to ensure we are getting a valid id)
            Guid applicationId = Guid.Parse(id);
            MicrosoftDynamicsCRMadoxioApplication adoxioApplication = await GetDynamicsApplication(id);

            if (adoxioApplication == null)
            {
                return(NotFound());
            }

            // set the application invoice trigger to create an invoice
            ViewModels.AdoxioApplication vm = await adoxioApplication.ToViewModel(_dynamicsClient);

            MicrosoftDynamicsCRMadoxioApplication adoxioApplication2 = new MicrosoftDynamicsCRMadoxioApplication();

            adoxioApplication2.CopyValues(vm);
            // this is the money - setting this flag to "Y" triggers a dynamics workflow that creates an invoice
            adoxioApplication2.AdoxioInvoicetrigger = (int?)ViewModels.GeneralYesNo.Yes;
            _dynamicsClient.Applications.Update(id, adoxioApplication2);
            adoxioApplication2 = await GetDynamicsApplication(id);

            // now load the invoice for this application to get the pricing
            string invoiceId = adoxioApplication2._adoxioInvoiceValue;
            int    retries   = 0;

            while (retries < 10 && (invoiceId == null || invoiceId.Length == 0))
            {
                // should happen immediately, but ...
                // pause and try again - in case Dynamics is slow ...
                retries++;
                _logger.LogError("No invoice found, retry = " + retries);
                System.Threading.Thread.Sleep(1000);
                invoiceId = adoxioApplication2._adoxioInvoiceValue;
            }
            _logger.LogError("Created invoice for application = " + invoiceId);

            /*
             * When the applicant submits their Application, we will set the application "Application Invoice Trigger" to "Y" - this will trigger a workflow that will create the Invoice
             *  - we will then re-query the Application to get the Invoice number,
             *  - and then query the Invoice to get the amount
             *  - the Invoice will also contain a Transaction Id (starting at 0500000000)
             *  - the Invoice status will be New
             * Notes:
             *  - If there is already an invoice with Status New, don't need to create a new Invoice
             *  - If there is already an invoice with Status Complete, it is an error (can't pay twice)
             *  - We will deal with the history later (i.e. there can be multiple "Cancelled" Invoices - we need to keep them for reconciliation but we don't need them for MVP
             */

            MicrosoftDynamicsCRMinvoice invoice = await _dynamicsClient.GetInvoiceById(Guid.Parse(invoiceId));

            // dynamics creates a unique transaction id per invoice, used as the "order number" for payment
            var ordernum = invoice.AdoxioTransactionid;
            // dynamics determines the amount based on the licence type of the application
            var orderamt = invoice.Totalamount;

            Dictionary <string, string> redirectUrl;

            redirectUrl        = new Dictionary <string, string>();
            redirectUrl["url"] = _bcep.GeneratePaymentRedirectUrl(ordernum, id, String.Format("{0:0.00}", orderamt));

            _logger.LogError(">>>>>" + redirectUrl["url"]);

            return(Json(redirectUrl));
        }