Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public static IEnumerable <PaymentProfileWrapper> GetPaymentProfiles(int customerId, string email)
        {
            var profileId = GetProfileId(customerId);

            if (profileId <= 0)
            {
                yield break;
            }

            var profileMgr = new ProfileManager(customerId, email, profileId);

            using (var connection = new SqlConnection(DB.GetDBConn()))
            {
                connection.Open();

                using (var command = new SqlCommand(
                           @"select AuthorizeNetProfileId, CardType, ExpirationMonth, ExpirationYear
						from CIM_AddressPaymentProfileMap
						where CustomerId = @customerId"                        , connection))
                {
                    command.Parameters.AddRange(new[]
                    {
                        new SqlParameter("@customerId", customerId),
                    });

                    using (var reader = command.ExecuteReader())
                    {
                        var authorizeNetProfileIdColumn = reader.GetOrdinal("AuthorizeNetProfileId");
                        var cardTypeColumn        = reader.GetOrdinal("CardType");
                        var expirationMonthColumn = reader.GetOrdinal("ExpirationMonth");
                        var expirationYearColumn  = reader.GetOrdinal("ExpirationYear");

                        while (reader.Read())
                        {
                            var authorizeNetProfileId = reader.GetInt64(authorizeNetProfileIdColumn);
                            var cardType        = reader.GetString(cardTypeColumn);
                            var expirationMonth = reader.GetString(expirationMonthColumn);
                            var expirationYear  = reader.GetString(expirationYearColumn);

                            var cimProfile = profileMgr.GetPaymentProfile(authorizeNetProfileId);
                            if (cimProfile == null)
                            {
                                continue;
                            }

                            var 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,
                            });
                        }
                    }
                }
            }
        }