public CreditCardAuthorizationResult Authorize(CreditCardPayment payment, String description, Project.MVC.Entity.Models.Address billingAddress) { if (billingAddress == null) { throw new ArgumentNullException("billingAddress"); } if (payment == null) { throw new ArgumentNullException("payment"); } var result = new CreditCardAuthorizationResult(); AuthorizeNetAuthorizationResult authResult = _authorizeNetService .Authorize(payment.CreditCardNumber, payment.Expiration.ToString("MM/yy"), payment.CVV, payment.Amount, description, billingAddress); if (authResult.Success) { result.Success = true; CreditCardTransaction transaction = new CreditCardTransaction() { Amount = authResult.Amount, AuthorizationCode = authResult.AuthorizationCode, CardLastFour = payment.CreditCardNumber.Substring(payment.CreditCardNumber.Length - 4), PaymentMethod = PaymentMethod.CreditCard, TransactionDate = DateTime.Now, TransactionType = TransactionType.Authorization }; TransactionDetails transactionDetails = new TransactionDetails() { TransactionProcessor = TransactionProcessor.AuthorizeNet, TransactionProcessorTransactionID = authResult.MerchantTransactionID }; transaction.TransactionDetails = transactionDetails; result.Transaction = transaction; return(result); } result.Success = false; result.Errors.AddRange(authResult.Errors); return(result); }
public AuthorizeNetAuthorizationResult Authorize(String creditCardNumber, String expiration, String cvv, Decimal amount, String description, Project.MVC.Entity.Models.Address billingAddress) { if (billingAddress == null) { throw new ArgumentNullException("billingAddress"); } if (String.IsNullOrEmpty(creditCardNumber)) { throw new ArgumentNullException("creditCardNumber"); } if (String.IsNullOrEmpty(cvv)) { throw new ArgumentNullException("cvv"); } if (String.IsNullOrEmpty(expiration)) { throw new ArgumentNullException("expiration"); } if (amount <= 0) { throw new InvalidOperationException("Attempt to authorize by an amount less than or equal to 0."); } var result = new AuthorizeNetAuthorizationResult(); try { var request = new AuthorizationRequest(creditCardNumber, expiration, amount, description); request.Type = "AUTH_ONLY"; //Cvv number request.AddCardCode(cvv); //No Partial Auth request.AllowPartialAuth = "false"; //20 minute duplicate window request.DuplicateWindow = "1200"; //Billing Address request.Address = billingAddress.AddressLine1; request.City = billingAddress.City; request.State = billingAddress.State; request.Zip = billingAddress.PostalCode; request.Country = "US"; var gate = new Gateway(_apiLoginID, _transactionKey); #if DEBUG request.DuplicateWindow = "1"; #endif IGatewayResponse response = null; response = gate.Send(request); result.ResponseCode = (AuthorizeNetResponseCode)Enum.Parse(typeof(AuthorizeNetResponseCode), response.ResponseCode); result.AuthorizeNetResponseMessage = response.Message; //If the transaction was approved if (result.ResponseCode == AuthorizeNetResponseCode.Approved) { result.Success = true; result.Amount = response.Amount; result.AuthorizationCode = response.AuthorizationCode; result.MerchantTransactionID = response.TransactionID; result.Success = true; return(result); } //The transaction was not approved, find out why result.Success = false; var responseBase = response as GatewayResponse; //if (responseBase.CCVResponse != "M") //{ // result.Errors.Add("Invalid cvv number."); //} //if (responseBase.AVSResponse != "A") //{ // result.Errors.Add("Invalid billing address."); //} Int32 failureCode; if (Int32.TryParse(responseBase.RawResponse[2], out failureCode)) { result.ReasonCode = failureCode; } if (result.ResponseCode == AuthorizeNetResponseCode.Declined) { result.Errors.Add("The credit card was declined."); #region reasons //if (responseBase.ReasonCode == 37) //{ // result.Errors.Add("Invalid credit card number."); //} ////38 Invalid merchant configuration ////41 declined transaction fraudscreen.net //if (responseBase.ReasonCode == 44) //{ // result.Errors.Add("Invalid cvv number"); //} //if (responseBase.ReasonCode == 45) //{ // result.Errors.Add("There was a problem with the billing address or card code."); //} ////Fraud filter or block ip address //if (responseBase.ReasonCode == 250 || responseBase.ReasonCode == 251) //{ // result.Errors.Add("Were sorry your payment cannot be processed at this time."); //} ////processor issued declines //if (responseBase.ReasonCode == 315) //{ // result.Errors.Add("Invalid credit card number."); //} //if (responseBase.ReasonCode == 316) //{ // result.Errors.Add("Invalid credit card expiration date."); //} //if (responseBase.ReasonCode == 317) //{ // result.Errors.Add("The credit card has expired."); //} //if (responseBase.ReasonCode == 318) //{ // result.Errors.Add("A duplicate transaction has been submitted."); //} #endregion } else if (result.ResponseCode == AuthorizeNetResponseCode.Error) { result.Errors.Add("There was a problem processing the credit card."); #region reasons //if (responseBase.ReasonCode == 6) //{ // result.Errors.Add("The credit card number is invalid."); //} //if (responseBase.ReasonCode == 7) //{ // result.Errors.Add("The credit card expiration date is invalid."); //} //if (responseBase.ReasonCode == 8) //{ // result.Errors.Add("The credit card has expired."); //} ////Duplicate transaction amount + card information withing specified time (+invoice number) //if (responseBase.ReasonCode == 11) //{ // result.Errors.Add("A duplicate transaction has been detected."); //} //if (responseBase.ReasonCode == 13) //{ // throw new InvalidOperationException("Merchant account credentials are invalid."); //} //if (responseBase.ReasonCode == 17) //{ // result.Errors.Add("The credit card supplied is not supported by this merchant."); //} //if (responseBase.ReasonCode >= 19 && responseBase.ReasonCode <= 23 || // responseBase.ReasonCode >= 25 && responseBase.ReasonCode <= 26) //{ // result.Errors.Add("An error occured while processing, please try again in 5 minutes."); //} //if (responseBase.ReasonCode == 27) //{ // result.Errors.Add("The billing address supplied does not match the card holders billing address."); //} //if (responseBase.ReasonCode == 28) //{ // result.Errors.Add("The credit card supplied is not supported by this merchant."); //} //if (responseBase.ReasonCode >= 57 && responseBase.ReasonCode <= 63) //{ // result.Errors.Add("An error occured while processing, please try again in 5 minutes."); //} ////29 30 31 34 35 merchant configuration errors ////33 blank fields //if (responseBase.ReasonCode == 78) //{ // result.Errors.Add("Invalid cvv number."); //} #endregion } else if (result.ResponseCode == AuthorizeNetResponseCode.UnderReview) { result.Errors.Add("The credit card transaction could not be processed " + ", the processor flagged the transaction for review. Please contact your card issuer and try again later."); } else { throw new InvalidOperationException("Invalid response code."); } if (IsUserFriendlyAuthorizationMessage(result.ReasonCode)) { result.Errors.Add(response.Message); } return(result); } catch (InvalidDataException dataEx) { //We most likely submitted invalid data to authorize.net //Log throw dataEx; } catch (WebException webEx) { //Log webEx.GetBaseException(); result.Success = false; result.Errors.Add("A network error occured."); result.AuthorizeNetResponseMessage = "Could not contact the payment processor."; return(result); } }