Exemplo n.º 1
0
        public static TransactionResult ProcessCapture(TransactionRequest captureRequest, SquareSettings squareSettings, ILogger logger)
        {
            var order       = captureRequest.Order;
            var config      = GetConfiguration(squareSettings);
            var paymentId   = captureRequest.GetParameterAs <string>("paymentId");
            var paymentsApi = new PaymentsApi(config);
            //perform the call
            var paymentResponse   = paymentsApi.CompletePayment(paymentId);
            var transactionResult = new TransactionResult()
            {
                OrderGuid       = order.Guid,
                TransactionGuid = Guid.NewGuid().ToString(),
            };

            if (paymentResponse != null)
            {
                var payment = paymentResponse.Payment;
                transactionResult.Success            = true;
                transactionResult.TransactionAmount  = (decimal)(payment.AmountMoney.Amount ?? 0) / 100;
                transactionResult.ResponseParameters = new Dictionary <string, object>()
                {
                    { "PaymentId", payment.Id },
                    { "ReferenceId", payment.ReferenceId }
                };

                if (payment.Status == "COMPLETED")
                {
                    transactionResult.NewStatus = PaymentStatus.Complete;
                }
                else
                {
                    transactionResult.NewStatus = PaymentStatus.Failed;
                    var errors = string.Join(",", paymentResponse.Errors);
                    logger.Log <TransactionResult>(LogLevel.Warning, "The capture for Order#" + order.Id + " by square failed." + errors);
                    transactionResult.Exception = new Exception("An error occurred while capturing payment. Error Details: " + errors);
                    transactionResult.Success   = false;
                }
            }
            else
            {
                logger.Log <TransactionResult>(LogLevel.Warning, "The capture for Order#" + order.Id + " by square failed. No response received.");
                transactionResult.Success   = false;
                transactionResult.Exception = new Exception("An error occurred while capturing payment");
            }

            return(transactionResult);
        }