public async Task <IActionResult> VerifyPaymentStatus(string id) { MicrosoftDynamicsCRMadoxioApplication application = await GetDynamicsApplication(id); if (application == null) { return(NotFound()); } // load the invoice for this application string invoiceId = application._adoxioInvoiceValue; Guid invoiceGuid = Guid.Parse(invoiceId); _logger.LogDebug("Found invoice for application = " + invoiceId); MicrosoftDynamicsCRMinvoice invoice = await _dynamicsClient.GetInvoiceById(invoiceGuid); var ordernum = invoice.AdoxioTransactionid; var orderamt = invoice.Totalamount; var response = await _bcep.ProcessPaymentResponse(ordernum, id); response["invoice"] = invoice.Invoicenumber; foreach (var key in response.Keys) { _logger.LogDebug(">>>>>" + key + ":" + response[key]); } /* * - if the invoice status is not "New", skip * - we will update the Invoice status to "Complete" (if paid) or "Cancelled" (if payment was rejected) * - if payment is successful, we will also set the Application "Payment Received" to "Y" and "Method" to "Credit Card" */ if (invoice.Statecode == (int?)Adoxio_invoicestates.New || invoice.Statecode == null) { _logger.LogDebug("Processing invoice with status New"); // if payment was successful: var pay_status = response["trnApproved"]; if (pay_status == "1") { _logger.LogDebug("Transaction approved"); MicrosoftDynamicsCRMinvoice invoice2 = new MicrosoftDynamicsCRMinvoice() { Statecode = (int?)Adoxio_invoicestates.Paid, Statuscode = (int?)Adoxio_invoicestatuses.Paid, AdoxioReturnedtransactionid = response["trnId"] }; // set invoice status to Complete try { _dynamicsClient.Invoices.Update(invoice.Invoiceid, invoice2); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating invoice"); // fail throw (httpOperationException); } MicrosoftDynamicsCRMadoxioApplication adoxioApplication2 = new MicrosoftDynamicsCRMadoxioApplication() { // set the Application payment status AdoxioPaymentrecieved = (bool?)true, AdoxioPaymentmethod = (int?)Adoxio_paymentmethods.CC, AdoxioAppchecklistpaymentreceived = (int?)ViewModels.GeneralYesNo.Yes }; try { _dynamicsClient.Applications.Update(id, adoxioApplication2); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating application"); // fail throw (httpOperationException); } } // if payment failed: else { _logger.LogDebug("Transaction NOT approved"); // set invoice status to Cancelled MicrosoftDynamicsCRMinvoice invoice2 = new MicrosoftDynamicsCRMinvoice() { Statecode = (int?)Adoxio_invoicestates.Cancelled, Statuscode = (int?)Adoxio_invoicestatuses.Cancelled }; try { _dynamicsClient.Invoices.Update(invoice.Invoiceid, invoice2); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating invoice"); // fail throw (httpOperationException); } // set the Application invoice status back to No MicrosoftDynamicsCRMadoxioApplication adoxioApplication2 = new MicrosoftDynamicsCRMadoxioApplication() { AdoxioInvoicetrigger = (int?)ViewModels.GeneralYesNo.No }; try { _dynamicsClient.Applications.Update(id, adoxioApplication2); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating application"); // fail throw (httpOperationException); } } } else { // that can happen if we are re-validating a completed invoice (paid or cancelled) _logger.LogDebug("Invoice status is not New, skipping updates ..."); } return(new JsonResult(response)); }
public async Task <IActionResult> VerifyPaymentStatus(string id) { MicrosoftDynamicsCRMadoxioApplication adoxioApplication = await GetDynamicsApplication(id); if (adoxioApplication == null) { return(NotFound()); } // load the invoice for this application string invoiceId = adoxioApplication._adoxioInvoiceValue; _logger.LogError("Found invoice for application = " + invoiceId); MicrosoftDynamicsCRMinvoice invoice = await _dynamicsClient.GetInvoiceById(Guid.Parse(invoiceId)); var ordernum = invoice.AdoxioTransactionid; var orderamt = invoice.Totalamount; var response = await _bcep.ProcessPaymentResponse(ordernum, id); response["invoice"] = invoice.Invoicenumber; foreach (var key in response.Keys) { _logger.LogError(">>>>>" + key + ":" + response[key]); } /* * - if the invoice status is not "New", skip * - we will update the Invoice status to "Complete" (if paid) or "Cancelled" (if payment was rejected) * - if payment is successful, we will also set the Application "Payment Received" to "Y" and "Method" to "Credit Card" */ if (invoice.Statecode == (int?)Adoxio_invoicestates.New || invoice.Statecode == null) { _logger.LogError("Processing invoice with status New"); ViewModels.Invoice vmi = invoice.ToViewModel(); MicrosoftDynamicsCRMinvoice invoice2 = new MicrosoftDynamicsCRMinvoice(); invoice2.CopyValues(vmi); ViewModels.AdoxioApplication vma = await adoxioApplication.ToViewModel(_dynamicsClient); MicrosoftDynamicsCRMadoxioApplication adoxioApplication2 = new MicrosoftDynamicsCRMadoxioApplication(); adoxioApplication2.CopyValues(vma); // if payment was successful: var pay_status = response["trnApproved"]; if (pay_status == "1") { _logger.LogError("Transaction approved"); // set invoice status to Complete invoice2.Statecode = (int?)Adoxio_invoicestates.Paid; invoice2.Statuscode = (int?)Adoxio_invoicestatuses.Paid; invoice2.AdoxioReturnedtransactionid = response["trnId"]; _dynamicsClient.Invoices.Update(invoice2.Invoiceid, invoice2); // set the Application payment status adoxioApplication2.AdoxioPaymentrecieved = (bool?)true; adoxioApplication2.AdoxioPaymentmethod = (int?)Adoxio_paymentmethods.CC; adoxioApplication2.AdoxioAppchecklistpaymentreceived = (int?)ViewModels.GeneralYesNo.Yes; _dynamicsClient.Applications.Update(id, adoxioApplication2); adoxioApplication2 = await GetDynamicsApplication(id); } // if payment failed: else { _logger.LogError("Transaction NOT approved"); // set invoice status to Cancelled invoice2.Statecode = (int?)Adoxio_invoicestates.Cancelled; invoice2.Statuscode = (int?)Adoxio_invoicestatuses.Cancelled; _dynamicsClient.Invoices.Update(invoice2.Invoiceid, invoice2); // set the Application invoice status back to No adoxioApplication2.AdoxioInvoicetrigger = (int?)ViewModels.GeneralYesNo.No; // don't clear the invoice, leave the previous "Cancelled" so we can report status //adoxioApplication2._adoxioInvoiceValue = null; //adoxioApplication2.AdoxioInvoice = null; _dynamicsClient.Applications.Update(id, adoxioApplication2); adoxioApplication2 = await GetDynamicsApplication(id); } } else { // that can happen if we are re-validating a completed invoice (paid or cancelled) _logger.LogError("Invoice status is not New, skipping updates ..."); } return(Json(response)); }