/// <summary> /// The CIM API to add customer /// </summary> private static void AddCustomer() { var target = new CustomerGateway(ApiKey, TransactionKey); try { var actual = target.CreateCustomer("*****@*****.**", "new customer profile"); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void CreateCustomerTest_CustomerID() { //check login / password string sError = CheckLoginPassword(); Assert.IsTrue(sError == "", sError); string responseString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><createCustomerProfileResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\"><messages><resultCode>Ok</resultCode><message><code>I00001</code><text>Successful.</text></message></messages><customerProfileId>27092230</customerProfileId><customerPaymentProfileIdList /><customerShippingAddressIdList /><validationDirectResponseList /></createCustomerProfileResponse>"; LocalRequestObject.ResponseString = responseString; CustomerGateway target = new CustomerGateway(ApiLogin, TransactionKey); string email = "*****@*****.**"; string description = "CreateCustomerTest Success"; string customerID = "Cust ID 1234"; Customer expected = new Customer() { ID = customerID, Email = email, Description = description }; Customer actual = null; // if choose "USELOCAL", the test should pass with no exception // Otherwise, the test might fail for error, i.e. duplicated request. try { actual = target.CreateCustomer(email, description, customerID); } catch (Exception e) { string s = e.Message; } Assert.AreEqual(expected.Email, actual.Email); Assert.AreEqual(expected.Description, actual.Description); Assert.AreEqual(expected.ID, actual.ID); Assert.IsFalse(string.IsNullOrEmpty(actual.ProfileID)); Assert.IsTrue(actual.ProfileID.Trim().Length > 0); }
private static void AddCustomerWithPaymentProfile() { var target = new CustomerGateway(ApiKey, TransactionKey); try { //var s = target.GetCustomerIDs(); var actual = target.CreateCustomer("*****@*****.**", "new customer profile", "102"); var profileId = actual.ProfileID; target.AddCreditCard(profileId, "4111111111111111", 12, 16, "087", new Address { City = "WeiHai", State = "Shangdong", Zip = "264209", Street = "Touch Road" }); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void StoreInVault(int peopleId, string type, string cardNumber, string expires, string cardCode, string routing, string account, bool giving) { var person = db.LoadPersonById(peopleId); var paymentInfo = person.PaymentInfo(); if (paymentInfo == null) { paymentInfo = new PaymentInfo(); person.PaymentInfos.Add(paymentInfo); } var billToAddress = new AuthorizeNet.Address { First = paymentInfo.FirstName ?? person.FirstName, Last = paymentInfo.LastName ?? person.LastName, Street = paymentInfo.Address ?? person.PrimaryAddress, City = paymentInfo.City ?? person.PrimaryCity, State = paymentInfo.State ?? person.PrimaryState, Country = paymentInfo.Country ?? person.PrimaryCountry, Zip = paymentInfo.Zip ?? person.PrimaryZip, Phone = paymentInfo.Phone ?? person.HomePhone ?? person.CellPhone }; Customer customer; if (paymentInfo.AuNetCustId == null) // create a new profilein Authorize.NET CIM { // NOTE: this can throw an error if the email address already exists... // TODO: Authorize.net needs to release a new Nuget package, because they don't have a clean way to pass in customer ID (aka PeopleId) yet... the latest code has a parameter for this, though // - we could call UpdateCustomer after the fact to do this if we wanted to customer = CustomerGateway.CreateCustomer(person.EmailAddress, person.Name); customer.ID = peopleId.ToString(); paymentInfo.AuNetCustId = customer.ProfileID.ToInt(); } else { customer = CustomerGateway.GetCustomer(paymentInfo.AuNetCustId.ToString()); } customer.BillingAddress = billToAddress; var isSaved = CustomerGateway.UpdateCustomer(customer); if (!isSaved) { throw new Exception($"UpdateCustomer failed to save for {peopleId}"); } if (type == PaymentType.Ach) { SaveECheckToProfile(routing, account, paymentInfo, customer); paymentInfo.MaskedAccount = Util.MaskAccount(account); paymentInfo.Routing = Util.Mask(new StringBuilder(routing), 2); } else if (type == PaymentType.CreditCard) { var normalizeExpires = DbUtil.NormalizeExpires(expires); if (normalizeExpires == null) { throw new ArgumentException($"Can't normalize date {expires}", nameof(expires)); } var expiredDate = normalizeExpires.Value; SaveCreditCardToProfile(cardNumber, cardCode, expiredDate, paymentInfo, customer); paymentInfo.MaskedCard = Util.MaskCC(cardNumber); paymentInfo.Expires = expires; } else { throw new ArgumentException($"Type {type} not supported", nameof(type)); } if (giving) { paymentInfo.PreferredGivingType = type; } else { paymentInfo.PreferredPaymentType = type; } db.SubmitChanges(); }
/// <summary> /// This ensures that the user profile has been created on Authorize.Net CIM. /// This is only called when Adding or Editing a card or finalizing a purchase. All other viewing using the card cache table. /// </summary> /// <param name="outGateway">The customer gateway created and used to verify the customer has been created. /// Just returned for efficiency, there is no consequence to creating another gateway in the calling function</param> /// <param name="user">The current user.</param> /// <returns>All the customer information from CIM.</returns> private Customer EnsureProfile(out CustomerGateway outGateway, UserEntity user = null) { if (user == null) { user = Membership.GetUser().GetUserEntity(); } // create the authorize.net gateway bool live; bool.TryParse(ConfigurationManager.AppSettings.Get("AuthorizeLive"), out live); var cg = new CustomerGateway(ConfigurationManager.AppSettings.Get("API_LOGIN_ID"), ConfigurationManager.AppSettings.Get("TRANSACTION_KEY"), live ? ServiceMode.Live : ServiceMode.Test); outGateway = cg; // get the users e-mail address var email = user.EmailAddress; var setting = user.Settings.FirstOrDefault(x => x.Name == "SupportUser"); if (setting != null) { email = setting.Value + "@" + SupportController.DOMAIN; } // create a new customer profile for the currently logged in user var profile = new UserSettingEntity(user.UserId, "AuthorizeId"); if (profile.IsNew) { try { // set up new user profile.UserId = user.UserId; profile.Name = "AuthorizeId"; var customer = cg.CreateCustomer(email, user.Username); profile.Value = customer.ProfileID; profile.Save(); return(customer); } catch (Exception ex) { // user exists, so just get the ID if (ex.Message.Contains("duplicate")) { profile.UserId = user.UserId; profile.Name = "AuthorizeId"; var start = ex.Message.IndexOf("ID", StringComparison.InvariantCulture) + 3; profile.Value = ex.Message.Substring(start, ex.Message.IndexOf(" ", start, StringComparison.InvariantCulture) - start); profile.Save(); } Log.Error(Purchase.AuthError, ex); } } var existingCustomer = cg.GetCustomer(profile.Value); if (existingCustomer.Email != email) { existingCustomer.Email = email; cg.UpdateCustomer(existingCustomer); } return(existingCustomer); }