public static IEnumerable <PaymentProfileWrapper> GetPaymentProfiles(int customerId, string email) { Int64 profileId = DataUtility.GetProfileId(customerId); if (profileId <= 0) { yield break; } var profileMgr = new ProfileManager(customerId, email, profileId); using (SqlConnection connection = new SqlConnection(DB.GetDBConn())) { connection.Open(); using (SqlCommand command = new SqlCommand("select AuthorizeNetProfileId, CardType, ExpirationMonth, ExpirationYear from CIM_AddressPaymentProfileMap where CustomerId = @customerId", connection)) { command.Parameters.AddRange(new[] { new SqlParameter("@customerId", customerId), }); using (SqlDataReader reader = command.ExecuteReader()) { int authorizeNetProfileIdColumn = reader.GetOrdinal("AuthorizeNetProfileId"); int cardTypeColumn = reader.GetOrdinal("CardType"); int expirationMonthColumn = reader.GetOrdinal("ExpirationMonth"); int expirationYearColumn = reader.GetOrdinal("ExpirationYear"); while (reader.Read()) { long authorizeNetProfileId = reader.GetInt64(authorizeNetProfileIdColumn); string cardType = reader.GetString(cardTypeColumn); string expirationMonth = reader.GetString(expirationMonthColumn); string expirationYear = reader.GetString(expirationYearColumn); var cimProfile = profileMgr.GetPaymentProfile(authorizeNetProfileId); if (cimProfile == null) { continue; } CreditCardMaskedType maskedCard = (CreditCardMaskedType)cimProfile.payment.Item; yield return(new PaymentProfileWrapper() { CreditCardNumberMasked = string.Format("**** **** **** {0}", maskedCard.cardNumber.Substring(4)), CardType = cardType, ExpirationMonth = expirationMonth, ExpirationYear = expirationYear, CustomerId = customerId, ProfileId = authorizeNetProfileId, }); } } } } }
public static string ProcessCard(int orderNumber, int customerId, decimal orderTotal, long paymentProfileId, string transactionMode, bool useLiveTransactions, out string AVSResult, out string AuthorizationResult, out string AuthorizationCode, out string AuthorizationTransID, out string TransactionCommandOut, out string TransactionResponse) { AVSResult = string.Empty; AuthorizationResult = string.Empty; AuthorizationCode = string.Empty; AuthorizationTransID = string.Empty; TransactionCommandOut = string.Empty; TransactionResponse = string.Empty; var status = ERROR; var paymentProcessor = new PaymentProcessor(); CIMResponse processStatus = null; long profileId = DataUtility.GetProfileId(customerId); if (profileId > 0) { if (paymentProfileId > 0) { if (transactionMode.ToUpperInvariant() == "AUTH") { processStatus = paymentProcessor.Authorize(profileId, paymentProfileId, orderNumber, orderTotal); } else if (transactionMode.ToUpperInvariant() == "AUTHCAPTURE") { processStatus = paymentProcessor.AuthCapture(profileId, paymentProfileId, orderNumber, orderTotal); } } } if (processStatus != null) { if (processStatus.Success) { status = OK; AVSResult = processStatus.AvsCode; AuthorizationResult = processStatus.AuthMessage; AuthorizationCode = processStatus.AuthCode; AuthorizationTransID = processStatus.TransactionId; } else { status = processStatus.AuthMessage; if (string.IsNullOrEmpty(status)) { status = "There was an error processing your payment. Please try again, choose a different payment method, or contact customer support."; } } } return(status); }
public static PaymentProfileWrapper GetPaymentProfileWrapper(int customerId, string email, Int64 paymentProfileId) { long profileID = DataUtility.GetProfileId(customerId); var profileMgr = new ProfileManager(customerId, email, profileID); var paymentProfile = profileMgr.GetPaymentProfile(paymentProfileId); var creditCardMasked = (CreditCardMaskedType)paymentProfile.payment.Item; var cimPaymentProfile = DataUtility.GetPaymentProfile(customerId, paymentProfileId); if (creditCardMasked != null && cimPaymentProfile != null) { return(new PaymentProfileWrapper() { CreditCardNumberMasked = creditCardMasked.cardNumber.Replace("XXXX", "****"), CardType = cimPaymentProfile.CardType, ExpirationMonth = cimPaymentProfile.ExpirationMonth, ExpirationYear = cimPaymentProfile.ExpirationYear, CustomerId = customerId, ProfileId = profileID }); } return(null); }
public static long SaveProfileAndPaymentProfile(int customerId, string email, string storeName, long paymentProfileId, int addressId, string cardNumber, string cardCode, string expMonth, string expYear, out string errorMessage, out string errorCode) { errorMessage = string.Empty; errorCode = string.Empty; long profileId = DataUtility.GetProfileId(customerId); ProfileManager profileMgr = profileId > 0 ? new ProfileManager(customerId, email, profileId) : null; // Create profile if needed if (profileMgr == null || !profileMgr.UpdateProfile(email, ProfileManager.GetProfileDescription(storeName))) { //Clear out the profile id in case the auth.net account has changed and we need to create //a new profile for a customer that already has a profile id paymentProfileId = 0; profileMgr = ProfileManager.CreateProfile(customerId, email, out errorMessage); if (profileMgr == null) { return(0); } profileId = profileMgr.ProfileId; if (profileId <= 0) { return(0); } DataUtility.SaveProfileId(customerId, profileId); } AuthorizeNetApi.CustomerAddressType cimAddress = DataUtility.GetCustomerAddressFromAddress(addressId); if (paymentProfileId <= 0) { // create new payment profile var paymentProfileResponse = profileMgr.CreatePaymentProfile(cimAddress, cardNumber.Trim(), cardCode.Trim(), int.Parse(expMonth), int.Parse(expYear)); if (paymentProfileResponse == null) { errorMessage = "Null payment profile response."; return(0); } if (!paymentProfileResponse.Success) { errorMessage = paymentProfileResponse.ErrorMessage; errorCode = paymentProfileResponse.ErrorCode; return(0); } paymentProfileId = paymentProfileResponse.PaymentProfileId; } else { // update profile var response = profileMgr.UpdatePaymentProfile(paymentProfileId, cimAddress, cardNumber.Trim(), cardCode.Trim(), int.Parse(expMonth), int.Parse(expYear)); errorCode = response.ErrorCode; errorMessage = response.ErrorMessage; } if (paymentProfileId > 0) { string cardType = DetectCreditCardType(cardNumber.Trim()); DataUtility.SavePaymentProfile(customerId, addressId, paymentProfileId, expMonth, expYear, cardType != null ? cardType : string.Empty); } return(paymentProfileId); }
public static Int64 SaveProfileAndPaymentProfile(Int32 customerId, String email, String storeName, Int64 paymentProfileId, Int32 addressId, String cardNumber, String cardCode, String expMonth, String expYear, out String errorMessage, out string errorCode) { errorMessage = String.Empty; errorCode = String.Empty; Int64 profileId = DataUtility.GetProfileId(customerId); ProfileManager profileMgr = profileId > 0 ? new ProfileManager(customerId, email, profileId) : null; // Create profile if needed if (profileMgr == null || !profileMgr.UpdateProfile(email, storeName + " CIM Profile")) { //Clear out the profile id incase this is a case where the auth.net account has changed and we need to create //a new profile for a customer that already has a profile id paymentProfileId = 0; profileMgr = ProfileManager.CreateProfile(customerId, email, storeName + " CIM Profile", out errorMessage); if (profileMgr == null) { return(0); } profileId = profileMgr.ProfileId; if (profileId <= 0) { return(0); } DataUtility.SaveProfileId(customerId, profileId); } GatewayAuthorizeNet.AuthorizeNetApi.CustomerAddressType cimAddress = DataUtility.GetCustomerAddressFromAddress(addressId); if (paymentProfileId <= 0) { // create new payment profile var paymentProfileResponse = profileMgr.CreatePaymentProfile(cimAddress, cardNumber.Trim(), cardCode.Trim(), Int32.Parse(expMonth), Int32.Parse(expYear)); if (paymentProfileResponse == null) { errorMessage = "Null payment profile response."; return(0); } if (!paymentProfileResponse.Success) { errorMessage = paymentProfileResponse.ErrorMessage; errorCode = paymentProfileResponse.ErrorCode; return(0); } paymentProfileId = paymentProfileResponse.PaymentProfileId; } else { // update profile profileMgr.UpdatePaymentProfile(paymentProfileId, cimAddress, cardNumber.Trim(), cardCode.Trim(), Int32.Parse(expMonth), Int32.Parse(expYear)); } if (paymentProfileId > 0) { String cardType = DetectCreditCardType(cardNumber.Trim()); DataUtility.SavePaymentProfile(customerId, addressId, paymentProfileId, expMonth, expYear, cardType != null ? cardType : String.Empty); } return(paymentProfileId); }