コード例 #1
0
        public async Task <Registration> RegisterAttendee(Registration newRegistration)
        {
            var eventRegistrationAudit = await _registrationRepo.CreateEventRegistrationAudit(newRegistration);

            if (eventRegistrationAudit == null)
            {
                Console.WriteLine("Audit was not created");
            }

            bool isPaidEvent = !string.IsNullOrEmpty(newRegistration.DataDescriptor) &&
                               !string.IsNullOrEmpty(newRegistration.DataValue);

            var registrationTypeDetails =
                await _eventsRepository.GetEventTypeById(newRegistration.RegistrationTypeId);

            if (!newRegistration.RegistrationCode.IsEmpty())
            {
                if (newRegistration.RegistrationCode != registrationTypeDetails.RegistrationCode)
                {
                    throw new ExecutionError("Invalid Registration Code");
                }
            }

            //Retrieve Contact from WildApricot
            //Create Contact if needed
            var contact = await _waRepo.GetContact(newRegistration.Email) ?? await _waRepo.CreateContact(newRegistration);

            await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit, $"Contact Created - {contact.Id}");


            //Create Event Registration

            var eventRegistrationId = await _waRepo.AddEventRegistration(newRegistration, contact.Id);

            await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit, $"Event Registration Created - {eventRegistrationId}");

            if (isPaidEvent)
            {
                //Create Invoice

                var invoiceId = await _waRepo.GenerateEventRegistrationInvoice(eventRegistrationId);

                await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit,
                                                                     $"Event Invoice Created - {invoiceId}");

                //Process Payment... What happens if the payment fails? Will the user be able to register again? What happens to their other event registration?

                var processTransaction = new ProcessTransaction
                {
                    CreateTransactionRequest = new CreateTransactionRequest
                    {
                        MerchantAuthentication = new MerchantAuthentication
                        {
                            Name           = _name,
                            TransactionKey = _transactionKey
                        },
                        TransactionRequest = new TransactionRequest
                        {
                            TransactionType = "authCaptureTransaction",
                            Amount          = registrationTypeDetails.BasePrice.ToString(CultureInfo.InvariantCulture),
                            Payment         = new Payment
                            {
                                OpaqueData = new OpaqueData
                                {
                                    DataDescriptor = newRegistration.DataDescriptor,
                                    DataValue      = newRegistration.DataValue
                                }
                            },
                            Order = new Order
                            {
                                InvoiceNumber = $"{invoiceId}",
                                Description   = $"Registration for {registrationTypeDetails.Name}"
                            },
                            LineItems = new LineItems
                            {
                                LineItem = new LineItem
                                {
                                    ItemId      = $"{invoiceId}",
                                    Name        = $"Invoice #{invoiceId}",
                                    Description = $"Registration for {registrationTypeDetails.Name}",
                                    Quantity    = "1",
                                    UnitPrice   = registrationTypeDetails.BasePrice.ToString(CultureInfo.InvariantCulture)
                                }
                            }
                            //Use if we have Address Authentication turned on
                            //,
                            //BillTo = new BillTo
                            //{
                            //    FirstName = "Bob",
                            //    LastName = "Anderson",
                            //    Company = "Bob Anderson",
                            //    Address = "35 Testing Rd.",
                            //    City = "Montgomery",
                            //    Country = "USA",
                            //    State = "AL",
                            //    //Can be used to generate errors in testing
                            //    Zip = "46203"

                            //}
                        }
                    }
                };

                var transactionResponse = await AuthorizeOps.CreateTransaction(processTransaction);

                var transactionResponseContent = await transactionResponse.Content.ReadAsStringAsync();

                var transactionResponseResult =
                    Newtonsoft.Json.JsonConvert
                    .DeserializeObject <CreateTransactionResponse>(transactionResponseContent);

                //handle all the errors in the transactionResponseResult

                if (transactionResponseResult != null)
                {
                    if (transactionResponseResult.Messages.ResultCode == "Ok")
                    {
                        if (transactionResponseResult.TransactionResponse.Messages != null)
                        {
                            await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit,
                                                                                 "Event Payment Processed");
                        }
                        else
                        {
                            if (transactionResponseResult.TransactionResponse.Errors != null)
                            {
                                await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit,
                                                                                     "Event Payment Failed " + transactionResponseResult.TransactionResponse.Errors[0].ErrorCode);

                                if (!await _waRepo.DeleteEventRegistration(eventRegistrationId))
                                {
                                    throw new ExecutionError(
                                              "Transaction Failed and event Registration Cleanup Failed. Please contact our Help Desk to complete your registration");
                                }

                                throw new ExecutionError(transactionResponseResult.TransactionResponse.Errors[0].ErrorText);
                            }
                        }
                    }
                    else
                    {
                        await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit,
                                                                             "Event Payment Failed " + transactionResponseResult.Messages.Message[0].Code);

                        if (!await _waRepo.DeleteEventRegistration(eventRegistrationId))
                        {
                            throw new ExecutionError(
                                      "Transaction Failed and event Registration Cleanup Failed. Please contact our Help Desk to complete your registration");
                        }

                        if (transactionResponseResult.TransactionResponse != null && transactionResponseResult.TransactionResponse.Errors != null)
                        {
                            await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit,
                                                                                 "Event Payment Failed " + transactionResponseResult.TransactionResponse.Errors[0].ErrorCode);

                            throw new ExecutionError(transactionResponseResult.TransactionResponse.Errors[0].ErrorText);
                        }


                        throw new ExecutionError(transactionResponseResult.Messages.Message[0].Text);
                    }
                }

                //Create payment for invoice
                var paymentId = await _waRepo.MarkInvoiceAsPaid(registrationTypeDetails, invoiceId, contact.Id);

                await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit,
                                                                     $"Invoice Marked as Paid - {paymentId}");
            }


            //Generate HTML Ticket/QR Code and store on server in WWWRoot
            var registrantGuid = TicketOps.GenerateTicket(eventRegistrationId, newRegistration, registrationTypeDetails);

            //Send email with Confirmation and QR code
            _mailOps.Send(eventRegistrationId, registrantGuid, newRegistration, registrationTypeDetails, registrantGuid);

            await _registrationRepo.UpdateEventRegistrationAudit(eventRegistrationAudit, $"Registration Complete - {eventRegistrationId}");

            //Return Event Registration Id and QR Code Bitmap
            return(new Registration()
            {
                EventRegistrationId = eventRegistrationId,
                QrCode = registrantGuid
            });
        }