public TransactionResponse RefundCreditCard(string reference, decimal amt, string lastDigits = "")
        {
            if (string.IsNullOrWhiteSpace(lastDigits))
                throw new ArgumentException("Last four of credit card number are required for refunds against Authorize.net", "lastDigits");

            var req = new CreditRequest(reference, amt, lastDigits);
            var response = Gateway.Send(req);

            return new TransactionResponse
            {
                Approved = response.Approved,
                AuthCode = response.AuthorizationCode,
                Message = response.Message,
                TransactionId = response.TransactionID
            };
        }
Beispiel #2
0
        public void SendTest_Credit_Approved()
        {
            // Test setup.
            const string transId = "????";               // A settled transaction id
            const decimal creditAmount = (decimal) 1.50; // Amount to request credit for; less than the settled amount minus refunded amounts.
            const string accountType = "????";           // The account type used in the transaction, such as Visa
            const string accountLast4Digits = "????";    // The last 4 digitals of the account number used in the transaction, such as 1111

            //check login / password
            string sError = CheckLoginPassword();
            Assert.IsTrue(sError == "", sError);

            var responseString = "1|1|1|This transaction has been approved.||P|2207741772||Credit transaction approved testing|"+creditAmount+"|CC|credit||||||||||||[email protected]||||||||||||||574B2D5282D8A2914AEB7272AECD4B71|||||||||||||XXXX"+accountLast4Digits+"|"+accountType+"||||||||||||||||";
            LocalRequestObject.ResponseString = responseString;
            IGatewayResponse expected = new GatewayResponse(responseString.Split('|'));

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

            IGatewayRequest request = new CreditRequest(transId, creditAmount, accountLast4Digits);
            const string description = "Credit 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.TransactionID.Trim().Length > 0);
            Assert.Greater(long.Parse(actual.TransactionID),  0);
        }
Beispiel #3
0
        public void SendTest_FailedCredit_ReasonResponseCode()
        {
            //check login / password
            string sError = CheckLoginPassword();
            Assert.IsTrue(sError == "", sError);

            string transID = "1";

            string responseString = "3|1|54|The referenced transaction does not meet the criteria for issuing a credit.|||0||Fail to Credit invalid transaction|6.14|CC|credit||||||||||||||||||||||||||E5FBFF01C6A66AA75C1EE966943CAEAC|||||||||||||XXXX1111|Visa||||||||||||||||";
            LocalRequestObject.ResponseString = responseString;
            IGatewayResponse expected = new GatewayResponse(responseString.Split('|'));

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

            IGatewayRequest request = new CreditRequest(transID, (decimal)6.14, "1111");
            string description = "Fail to Credit invalid transaction";

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

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

            Assert.IsTrue(actual.TransactionID.Trim().Length > 0);
            Assert.AreEqual(expected.TransactionID, actual.TransactionID);
        }
        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;

        }