public async Task <HttpResponseMessage> UpdateResponseAsAccepted(ResponseForm responseForm)
        {
            var formValidation = responseForm.Validate(this.AccountSession, ValidationMode.Accept, responseForm);

            if (formValidation.IsValid)
            {
                try
                {
                    // get the current response
                    var response = this.Uow.Responses.FindById(responseForm.Id);

                    // create the payment processor
                    var paymentProcessor = new PaymentProcessor()
                    {
                        AccountSession = this.AccountSession, Logger = this.Logger
                    };

                    // wait until the payment is processed
                    var result = await paymentProcessor.ProcessPaymentAsync(response, responseForm.Payment);

                    if (result.IsSuccess())
                    {
                        // log invalid transaction
                        this.Logger.Warn("Payment transaction succeeded for Id {0} via user {1}", responseForm.Id, this.AccountSession.MemberId);

                        // extract payment transaction data
                        var payment = paymentProcessor.CreatePaymentTransaction(result.Target);

                        // handle the submission to the pending state
                        var results = this.Uow.Responses.UpdateResponseAsAccepted(responseForm.Id, payment);

                        // return the updated results
                        return(CreateSuccessResponse(new { success = true, results = results }, HttpStatusCode.OK));
                    }
                    else
                    {
                        // log invalid transaction
                        this.Logger.Warn("Payment transaction failed for Id {0} via user {1}: {2}", responseForm.Id, this.AccountSession.MemberId, result.Message);

                        // generate an invalid payment validation response
                        return(CreateInvalidResponse(BraintreeValidator.AsInvalidPayment(result)));
                    }
                }
                catch (Exception ex)
                {
                    // log exception
                    Logger.Error(string.Format("Exception detected attempting to set state to accepted for Id {0} via user {1}", responseForm.Id, this.AccountSession.MemberId), ex);

                    return(CreateErrorResponse(ex));
                }
            }

            // invalid parameters, generate response
            return(CreateInvalidResponse(formValidation));
        }
Пример #2
0
        public async Task <HttpResponseMessage> TestPayment(Payment payment)
        {
            var modelValidation = payment.Validate(this.AccountSession, ValidationMode.Accept);

            if (modelValidation.IsValid)
            {
                try
                {
                    // create the payment processor
                    var paymentProcessor = new PaymentProcessor()
                    {
                        AccountSession = this.AccountSession, Logger = this.Logger
                    };

                    // wait until the payment is processed
                    var result = await paymentProcessor.ProcessTestPaymentAsync(payment);

                    if (result.IsSuccess())
                    {
                        // extract payment transaction data
                        var transaction = paymentProcessor.CreatePaymentTransaction(result.Target);

                        // return the updated results
                        return(CreateSuccessResponse(new { success = true, results = transaction }, HttpStatusCode.OK));
                    }
                    else
                    {
                        // generate an invalid payment validation response
                        return(CreateInvalidResponse(BraintreeValidator.AsInvalidPayment(result)));
                    }
                }
                catch (Exception ex)
                {
                    return(CreateErrorResponse(ex));
                }
            }

            // invalid parameters, generate response
            return(CreateInvalidResponse(modelValidation));
        }