Exemple #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);
        }