diff --git a/Authorize.NET/ARB/ISubscriptionGateway.cs b/Authorize.NET/ARB/ISubscriptionGateway.cs index a4b67fca..4a92b40e 100644 --- a/Authorize.NET/ARB/ISubscriptionGateway.cs +++ b/Authorize.NET/ARB/ISubscriptionGateway.cs @@ -6,5 +6,10 @@ public interface ISubscriptionGateway { ISubscriptionRequest CreateSubscription(ISubscriptionRequest subscription); ARBSubscriptionStatusEnum GetSubscriptionStatus(string subscriptionID); bool UpdateSubscription(ISubscriptionRequest subscription); + + System.Collections.Generic.List GetSubscriptionList( + ARBGetSubscriptionListSearchTypeEnum searchType = ARBGetSubscriptionListSearchTypeEnum.subscriptionActive, + int page = 0, + int pageSize = 100); } } diff --git a/Authorize.NET/ARB/ISubscriptionRequest.cs b/Authorize.NET/ARB/ISubscriptionRequest.cs index 6d68b479..b09c22d1 100644 --- a/Authorize.NET/ARB/ISubscriptionRequest.cs +++ b/Authorize.NET/ARB/ISubscriptionRequest.cs @@ -15,7 +15,7 @@ public interface ISubscriptionRequest { BankAccount eCheckBankAccount { get; set; } string CustomerEmail { get; set; } string CustomerID { get; set; } - AuthorizeNet.SubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal trialAmount); + AuthorizeNet.ISubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal trialAmount); AuthorizeNet.Address ShippingAddress { get; set; } DateTime StartsOn { get; set; } string SubscriptionID { get; set; } @@ -26,8 +26,9 @@ public interface ISubscriptionRequest { ARBSubscriptionType ToUpdateableAPI(); decimal TrialAmount { get; set; } short TrialBillingCycles { get; set; } - AuthorizeNet.SubscriptionRequest UsingCreditCard(string firstName, string lastName, string cardNumber, int cardExpirationYear, int cardExpirationMonth); - AuthorizeNet.SubscriptionRequest WithBillingAddress(AuthorizeNet.Address add); - AuthorizeNet.SubscriptionRequest WithShippingAddress(AuthorizeNet.Address add); + AuthorizeNet.ISubscriptionRequest UsingCreditCard(string firstName, string lastName, string cardNumber, int cardExpirationYear, int cardExpirationMonth); + AuthorizeNet.ISubscriptionRequest UsingPaymentProfile(string customerProfileId, string customerPaymentProfileId, string customerAddressId); + AuthorizeNet.ISubscriptionRequest WithBillingAddress(AuthorizeNet.Address add); + AuthorizeNet.ISubscriptionRequest WithShippingAddress(AuthorizeNet.Address add); } } diff --git a/Authorize.NET/ARB/SubscriptionGateway.cs b/Authorize.NET/ARB/SubscriptionGateway.cs index 1cb8ecf3..0187aeb1 100644 --- a/Authorize.NET/ARB/SubscriptionGateway.cs +++ b/Authorize.NET/ARB/SubscriptionGateway.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Xml.Serialization; -using System.IO; using AuthorizeNet.APICore; namespace AuthorizeNet { @@ -88,7 +84,50 @@ public ARBSubscriptionStatusEnum GetSubscriptionStatus(string subscriptionID) { return response.status; } - + /// + /// Gets the subscription details. + /// + /// The subscription ID. + /// + public ARBGetSubscriptionResponse GetSubscription(string subscriptionID) + { + var req = new ARBGetSubscriptionRequest(); + req.subscriptionId = subscriptionID; + var response = (ARBGetSubscriptionResponse)_gateway.Send(req); + + return response; + } + /// + /// Gets a list of all subscriptions + /// + /// + public List GetSubscriptionList( + ARBGetSubscriptionListSearchTypeEnum searchType = ARBGetSubscriptionListSearchTypeEnum.subscriptionActive, + int page = 0, + int pageSize = 100) + { + var req = new ARBGetSubscriptionListRequest(); + req.searchType = searchType; + req.paging = new Paging + { + limit = pageSize, + offset = page + }; + req.sorting = new ARBGetSubscriptionListSorting + { + orderBy = ARBGetSubscriptionListOrderFieldEnum.createTimeStampUTC, + orderDescending = true + }; + + var response = (ARBGetSubscriptionListResponse)_gateway.Send(req); + + if (response == null || response.subscriptionDetails == null) + { + return null; + } + + return response.subscriptionDetails.ToList(); + } } } diff --git a/Authorize.NET/ARB/SubscriptionRequest.cs b/Authorize.NET/ARB/SubscriptionRequest.cs index 58b61d25..a20bdbd6 100644 --- a/Authorize.NET/ARB/SubscriptionRequest.cs +++ b/Authorize.NET/ARB/SubscriptionRequest.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; using AuthorizeNet.APICore; @@ -119,7 +117,7 @@ public static SubscriptionRequest CreateWeekly(string email, string subscription /// The card expiration year. /// The card expiration month. /// - public SubscriptionRequest UsingCreditCard(string firstName, string lastName, string cardNumber, int cardExpirationYear, int cardExpirationMonth) { + public ISubscriptionRequest UsingCreditCard(string firstName, string lastName, string cardNumber, int cardExpirationYear, int cardExpirationMonth) { this.CardNumber = cardNumber; this.CardExpirationYear = cardExpirationYear; this.CardExpirationMonth = cardExpirationMonth; @@ -132,12 +130,22 @@ public SubscriptionRequest UsingCreditCard(string firstName, string lastName, st return this; } + public ISubscriptionRequest UsingPaymentProfile(string customerProfileId, string customerPaymentProfileId, string customerAddressId) + { + this.CustomerProfileId = customerProfileId; + this.CustomerPaymentProfileId = customerPaymentProfileId; + this.CustomerAddressId = customerAddressId; + + return this; + } + /// /// Adds a full billing address - which is required for a credit card. /// /// The add. /// - public SubscriptionRequest WithBillingAddress(Address add) { + public ISubscriptionRequest WithBillingAddress(Address add) + { this.BillingAddress = add; return this; } @@ -148,7 +156,8 @@ public SubscriptionRequest WithBillingAddress(Address add) { /// /// The address to ship to /// - public SubscriptionRequest WithShippingAddress(Address add) { + public ISubscriptionRequest WithShippingAddress(Address add) + { this.ShippingAddress = add; return this; } @@ -159,7 +168,8 @@ public SubscriptionRequest WithShippingAddress(Address add) { /// The trial billing cycles. /// The trial amount. /// - public SubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal trialAmount) { + public ISubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal trialAmount) + { this.TrialBillingCycles = trialBillingCycles; this.TrialAmount = trialAmount; @@ -178,7 +188,7 @@ public SubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal tria public decimal Amount { get; set; } public decimal TrialAmount { get; set; } - //CreditCard + // CreditCard public string CardNumber { get; set; } public int CardExpirationYear { get; set; } public int CardExpirationMonth { get; set; } @@ -187,6 +197,11 @@ public SubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal tria // eCheck public BankAccount eCheckBankAccount { get; set; } + // Customer Profile + public string CustomerProfileId { get; set; } + public string CustomerPaymentProfileId { get; set; } + public string CustomerAddressId { get; set; } + public Address BillingAddress { get; set; } public Address ShippingAddress { get; set; } @@ -202,25 +217,38 @@ public ARBSubscriptionType ToAPI(){ var sub = new ARBSubscriptionType(); sub.name = this.SubscriptionName; - bool isCard = true; + bool isCardValid = false; + bool isBankValid = false; + bool isProfileValid = false; StringBuilder sbError = new StringBuilder(""); bool bError = false; - if (String.IsNullOrEmpty(this.CardNumber) || (this.CardNumber.Trim().Length == 0)) + + + if (!String.IsNullOrEmpty(this.CardNumber) && + this.CardNumber.Trim().Length == 0) { - if ((null == this.eCheckBankAccount) || String.IsNullOrEmpty(this.eCheckBankAccount.accountNumber) || - (this.eCheckBankAccount.accountNumber.Trim().Length == 0)) - { - sbError.Append("Need a credit card number or a bank account number to set up this subscription"); - bError = true; - } - else - { - isCard = false; - } + isCardValid = true; + } + else if (this.eCheckBankAccount != null && + !String.IsNullOrEmpty(this.eCheckBankAccount.accountNumber) && + this.eCheckBankAccount.accountNumber.Trim().Length != 0) + { + isBankValid = true; + } + else if (!String.IsNullOrEmpty(this.CustomerProfileId) && + !String.IsNullOrEmpty(this.CustomerPaymentProfileId)) + { + isProfileValid = true; } + if (!(isCardValid || isBankValid || isProfileValid)) + { + sbError.Append("Need a credit card number or a bank account number to set up this subscription"); + bError = true; + } + DateTime dt = new DateTime(); - if ( isCard && !CommonFunctions.ParseDateTime(this.CardExpirationYear, this.CardExpirationMonth, 1, out dt)) + if (isCardValid && !CommonFunctions.ParseDateTime(this.CardExpirationYear, this.CardExpirationMonth, 1, out dt)) { sbError.Append("Need a valid CardExpirationMonth and CardExpirationYear to set up this subscription"); bError = true; @@ -231,15 +259,17 @@ public ARBSubscriptionType ToAPI(){ throw new InvalidOperationException(sbError.ToString()); } - if (isCard) + if (isCardValid) { - var creditCard = new creditCardType(); - creditCard.cardNumber = this.CardNumber; - creditCard.expirationDate = dt.ToString("yyyy-MM"); // required format for API is YYYY-MM - sub.payment = new paymentType(); - sub.payment.Item = creditCard; + var creditCard = new creditCardType + { + cardNumber = this.CardNumber, + // required format for API is YYYY-MM + expirationDate = dt.ToString("yyyy-MM") + }; + sub.payment = new paymentType { Item = creditCard }; } - else + else if (isBankValid) { var eCheck = new bankAccountType() { @@ -255,18 +285,31 @@ public ARBSubscriptionType ToAPI(){ }; sub.payment = new paymentType {Item = eCheck}; } + else if (isProfileValid) + { + var customerProfile = new customerProfileIdType + { + customerProfileId = this.CustomerProfileId, + customerPaymentProfileId = this.CustomerPaymentProfileId, + customerAddressId = this.CustomerAddressId + }; + + sub.profile = customerProfile; + } if(this.BillingAddress!=null) sub.billTo = this.BillingAddress.ToAPINameAddressType(); if (this.ShippingAddress != null) sub.shipTo = this.ShippingAddress.ToAPINameAddressType(); - sub.paymentSchedule = new paymentScheduleType(); - sub.paymentSchedule.startDate = this.StartsOn; - sub.paymentSchedule.startDateSpecified = true; + sub.paymentSchedule = new paymentScheduleType + { + startDate = this.StartsOn, + startDateSpecified = true, + totalOccurrences = this.BillingCycles, + totalOccurrencesSpecified = true + }; - sub.paymentSchedule.totalOccurrences = this.BillingCycles; - sub.paymentSchedule.totalOccurrencesSpecified = true; // free 1 month trial if (this.TrialBillingCycles >= 0) { @@ -290,14 +333,20 @@ public ARBSubscriptionType ToAPI(){ } else { sub.paymentSchedule.interval.unit = ARBSubscriptionUnitEnum.days; } - sub.customer = new customerType(); - sub.customer.email = this.CustomerEmail; - - sub.order = new orderType(); - sub.order.description = this.Description; - sub.order.invoiceNumber = this.Invoice; - - sub.customer.id = this.CustomerID; + if (!isProfileValid) + { + sub.customer = new customerType + { + email = this.CustomerEmail, + id = this.CustomerID + }; + } + + sub.order = new orderType + { + description = this.Description, + invoiceNumber = this.Invoice + }; return sub; @@ -321,11 +370,13 @@ public ARBSubscriptionType ToUpdateableAPI() { throw new InvalidOperationException("Need a valid CardExpirationMonth and CardExpirationYear to set up this subscription"); } - var creditCard = new creditCardType(); - creditCard.cardNumber = this.CardNumber; - creditCard.expirationDate = dt.ToString("yyyy-MM");//string.Format("{0}-{1}", this.CardExpirationYear, this.CardExpirationMonth); // required format for API is YYYY-MM - sub.payment = new paymentType(); - sub.payment.Item = creditCard; + var creditCard = new creditCardType + { + cardNumber = this.CardNumber, + expirationDate = dt.ToString("yyyy-MM") + }; + //string.Format("{0}-{1}", this.CardExpirationYear, this.CardExpirationMonth); // required format for API is YYYY-MM + sub.payment = new paymentType { Item = creditCard }; } if ((this.eCheckBankAccount != null) && !String.IsNullOrEmpty(this.eCheckBankAccount.accountNumber) && @@ -346,26 +397,48 @@ public ARBSubscriptionType ToUpdateableAPI() { sub.payment = new paymentType { Item = eCheck }; } + if (!String.IsNullOrEmpty(this.CustomerProfileId) && !String.IsNullOrEmpty(this.CustomerPaymentProfileId)) + { + var customerProfile = new customerProfileIdType + { + customerProfileId = this.CustomerProfileId, + customerPaymentProfileId = this.CustomerPaymentProfileId, + customerAddressId = this.CustomerAddressId + }; + + sub.profile = customerProfile; + } + else + { + sub.customer = new customerType + { + email = this.CustomerEmail, + id = this.CustomerID + }; + } + if (this.BillingAddress != null) sub.billTo = this.BillingAddress.ToAPINameAddressType(); if (this.ShippingAddress != null) sub.shipTo = this.ShippingAddress.ToAPINameAddressType(); - sub.paymentSchedule = new paymentScheduleType(); - sub.paymentSchedule.totalOccurrences = this.BillingCycles; - sub.paymentSchedule.totalOccurrencesSpecified = true; + sub.paymentSchedule = new paymentScheduleType + { + totalOccurrences = this.BillingCycles, + totalOccurrencesSpecified = true + }; sub.amount = this.Amount; sub.amountSpecified = true; - sub.customer = new customerType(); - sub.customer.email = this.CustomerEmail; - sub.customer.id = this.CustomerID; + + + sub.order = new orderType + { + description = this.Description, + invoiceNumber = this.Invoice + }; - sub.order = new orderType(); - sub.order.description = this.Description; - sub.order.invoiceNumber = this.Invoice; - return sub; } diff --git a/Authorize.NET/Reporting/ReportingGateway.cs b/Authorize.NET/Reporting/ReportingGateway.cs index 050ab666..86866be0 100644 --- a/Authorize.NET/Reporting/ReportingGateway.cs +++ b/Authorize.NET/Reporting/ReportingGateway.cs @@ -142,7 +142,19 @@ public List GetTransactionList(DateTime from, DateTime to) { result.AddRange(GetTransactionList(batch.ID.ToString())); } return result; - } + } + /// + /// Returns all transactions for a given customerProfileId + /// + public List GetTransactionList(string customerProfileId, string customerPaymentProfileId) + { + var request = new getTransactionListForCustomerRequest(); + request.customerProfileId = customerProfileId; + request.customerPaymentProfileId = customerPaymentProfileId; + + var response = (getTransactionListResponse)_gateway.Send(request); + return Transaction.NewListFromResponse(response.transactions); + } } } diff --git a/Authorize.NET/Reporting/Transaction.cs b/Authorize.NET/Reporting/Transaction.cs index b1b9d30d..2469a3d1 100644 --- a/Authorize.NET/Reporting/Transaction.cs +++ b/Authorize.NET/Reporting/Transaction.cs @@ -299,12 +299,12 @@ public static Transaction NewFromResponse(transactionDetailsType trans) { /// /// The transaction ID. public string TransactionID { get; set; } + /// /// Gets or sets the ref transaction ID. /// /// The transaction ID of the original transaction. This appears only for linked credits (transaction type refundTransaction). public string RefTransactionID { get; set; } /// - /// /// Gets or sets the date submitted. /// /// The date submitted. diff --git a/Authorize.NET/Utility/HttpXmlUtility.cs b/Authorize.NET/Utility/HttpXmlUtility.cs index c9a2b489..7d501ef4 100644 --- a/Authorize.NET/Utility/HttpXmlUtility.cs +++ b/Authorize.NET/Utility/HttpXmlUtility.cs @@ -257,6 +257,16 @@ ANetApiResponse DecideResponse(XmlDocument xmldoc) { apiResponse = (ARBGetSubscriptionStatusResponse)serializer.Deserialize(reader); break; + case "ARBGetSubscriptionResponse": + serializer = new XmlSerializer(typeof(ARBGetSubscriptionResponse)); + apiResponse = (ARBGetSubscriptionResponse)serializer.Deserialize(reader); + break; + + case "ARBGetSubscriptionListResponse": + serializer = new XmlSerializer(typeof(ARBGetSubscriptionListResponse)); + apiResponse = (ARBGetSubscriptionListResponse)serializer.Deserialize(reader); + break; + case "ErrorResponse": serializer = new XmlSerializer(typeof(ANetApiResponse)); apiResponse = (ANetApiResponse)serializer.Deserialize(reader); diff --git a/AuthorizeNET.sln b/AuthorizeNET.sln index d23eb032..cd60cb57 100644 --- a/AuthorizeNET.sln +++ b/AuthorizeNET.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.25420.1 +# Visual Studio 2013 +VisualStudioVersion = 12.0.40629.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizeNET", "Authorize.NET\AuthorizeNET.csproj", "{5D52EAEC-42FB-4313-83B8-69E2F55EBF14}" EndProject