Exemplo n.º 1
0
        public void SendTest_PriorAuthCapture_Approved()
        {
            //check login / password
            string sError = CheckLoginPassword();
            Assert.IsTrue(sError == "", sError);

            //setup
            decimal amount = (decimal)20.13;
            string transID = SendAuthOnly(amount + 1, true);
            Assert.IsTrue(transID.Trim().Length > 0);
            Assert.IsTrue(long.Parse(transID) > 0);

            //start testing
            string responseString = "1|1|1|This transaction has been approved.|P9A0ET|P|2207700131||PriorAuthCapture transaction approved testing|20.13|CC|prior_auth_capture||||||||||||||||||||||||||4C66E6649DF48EDEBBD917A1656CD68C|||||||||||||XXXX1111|Visa||||||||||||||||";
            LocalRequestObject.ResponseString = responseString;
            IGatewayResponse expected = new GatewayResponse(responseString.Split('|'));

            Gateway target = new Gateway(ApiLogin, TransactionKey, true);

            IGatewayRequest request = new PriorAuthCaptureRequest(amount, transID);
            request.DuplicateWindow = "0";
            string description = "PriorAuthCapture transaction approved testing";

            IGatewayResponse actual = target.Send(request, description);

            Assert.AreEqual(expected.Amount, actual.Amount);
            Assert.AreEqual(expected.Approved, actual.Approved);
            Assert.AreEqual(expected.CardNumber, actual.CardNumber);
            Assert.AreEqual(expected.Message, actual.Message);
            Assert.AreEqual(expected.ResponseCode, actual.ResponseCode);

            Assert.IsTrue(actual.AuthorizationCode.Trim().Length > 0);
            Assert.IsTrue(actual.TransactionID.Trim().Length > 0);
            Assert.IsTrue(long.Parse(actual.TransactionID) > 0);
        }
        public override bool ProcessPayment(Payment payment, ref string message)
        {
            var info = payment as CreditCardPayment;

            if (ReferenceEquals(info, null))
            {
                payment.Status = PaymentStatus.Failed.ToString();
                message = "AuthorizeNet gateway supports only CreditCardPayment";
                return false;
            }

            string[] validateSettings = { "MerchantLogin", "MerchantPassword" };

            foreach (var validateSetting in validateSettings)
            {
                if (!Settings.ContainsKey(validateSetting) || string.IsNullOrWhiteSpace(Settings[validateSetting]))
                {
                    payment.Status = PaymentStatus.Failed.ToString();
                    message = string.Format("{0} not configured", validateSetting);
                    return false;
                }
            }

            var transactionType = (TransactionType)Enum.Parse(typeof(TransactionType), info.TransactionType);
            payment.Status = PaymentStatus.Processing.ToString();

            var gateway = new Gateway(Settings["MerchantLogin"], Settings["MerchantPassword"]);

            bool isTestMode;
            if (Settings.ContainsKey("TestMode") && bool.TryParse(Settings["TestMode"], out isTestMode))
            {
                gateway.TestMode = isTestMode;
            }

            var description = string.Format("{0} transaction for order id {1}", transactionType, info.OrderForm.OrderGroupId);
            IGatewayRequest request = null;

            switch (transactionType)
            {
                case TransactionType.Authorization:
                case TransactionType.Sale:
                    request = new AuthorizationRequest(info.CreditCardNumber,
                        string.Format("{0}{1}", info.CreditCardExpirationMonth, info.CreditCardExpirationYear),
                        info.Amount,
                        description,
                        transactionType == TransactionType.Sale);
                    break;
                case TransactionType.Capture:
                    request = new PriorAuthCaptureRequest(info.Amount, info.ValidationCode);
                    break;
                case TransactionType.Credit:
                    request = new CreditRequest(info.ValidationCode, info.Amount, info.CreditCardNumber);
                    break;
                case TransactionType.Void:
                    request = new VoidRequest(info.ValidationCode);
                    break;
            }

            if (request == null)
            {
                payment.Status = PaymentStatus.Failed.ToString();
                message = string.Format("Unsupported transation type {0}", transactionType);
                return false;
            }

            request.AddCardCode(info.CreditCardSecurityCode);

            var invoice = info.OrderForm.OrderGroupId;

            var order = info.OrderForm.OrderGroup as Order;
            if (order != null)
            {
                invoice = order.TrackingNumber;
            }

            request.AddInvoice(invoice);
            request.AddTax(info.OrderForm.TaxTotal);

            // Find the address
            var address = info.OrderForm.OrderGroup.OrderAddresses
                .FirstOrDefault(a => String.Compare(a.OrderAddressId, info.BillingAddressId, StringComparison.OrdinalIgnoreCase) == 0);

            if (address != null)
            {
                request.AddCustomer(address.Email, address.FirstName, address.LastName,
                    address.Line1 + " " + address.Line2, address.StateProvince, address.PostalCode);
            }

            //foreach (var lineItem in info.OrderForm.LineItems)
            //{
            //    request.AddLineItem(lineItem.LineItemId, lineItem.DisplayName, lineItem.Description,
            //        (int)lineItem.Quantity, lineItem.PlacedPrice, false);
            //}


            try
            {
                var response = gateway.Send(request, description);

                if (!response.Approved)
                {
                    payment.Status = PaymentStatus.Denied.ToString();
                    message = "Transaction Declined: " + response.Message;
                    return false;
                }
                info.StatusCode = response.ResponseCode;
                info.StatusDesc = response.Message;
                info.ValidationCode = response.TransactionID;
                info.AuthorizationCode = response.AuthorizationCode;

                // transaction is marked as completed every time the payment operation succeeds even if it is void transaction type
                payment.Status = PaymentStatus.Completed.ToString();          
            }
            catch (Exception ex)
            {
                payment.Status = PaymentStatus.Failed.ToString();
                throw new ApplicationException(ex.Message);
            }

            return true;

        }