Skip to content
Open
5 changes: 5 additions & 0 deletions Authorize.NET/ARB/ISubscriptionGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ public interface ISubscriptionGateway {
ISubscriptionRequest CreateSubscription(ISubscriptionRequest subscription);
ARBSubscriptionStatusEnum GetSubscriptionStatus(string subscriptionID);
bool UpdateSubscription(ISubscriptionRequest subscription);

System.Collections.Generic.List<SubscriptionDetail> GetSubscriptionList(
ARBGetSubscriptionListSearchTypeEnum searchType = ARBGetSubscriptionListSearchTypeEnum.subscriptionActive,
int page = 0,
int pageSize = 100);
}
}
9 changes: 5 additions & 4 deletions Authorize.NET/ARB/ISubscriptionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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);
}
}
51 changes: 45 additions & 6 deletions Authorize.NET/ARB/SubscriptionGateway.cs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -88,7 +84,50 @@ public ARBSubscriptionStatusEnum GetSubscriptionStatus(string subscriptionID) {
return response.status;
}


/// <summary>
/// Gets the subscription details.
/// </summary>
/// <param name="subscriptionID">The subscription ID.</param>
/// <returns></returns>
public ARBGetSubscriptionResponse GetSubscription(string subscriptionID)
{
var req = new ARBGetSubscriptionRequest();
req.subscriptionId = subscriptionID;
var response = (ARBGetSubscriptionResponse)_gateway.Send(req);

return response;
}

/// <summary>
/// Gets a list of all subscriptions
/// </summary>
/// <returns></returns>
public List<SubscriptionDetail> 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();
}
}
}
183 changes: 128 additions & 55 deletions Authorize.NET/ARB/SubscriptionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AuthorizeNet.APICore;

Expand Down Expand Up @@ -119,7 +117,7 @@ public static SubscriptionRequest CreateWeekly(string email, string subscription
/// <param name="cardExpirationYear">The card expiration year.</param>
/// <param name="cardExpirationMonth">The card expiration month.</param>
/// <returns></returns>
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;
Expand All @@ -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;
}

/// <summary>
/// Adds a full billing address - which is required for a credit card.
/// </summary>
/// <param name="add">The add.</param>
/// <returns></returns>
public SubscriptionRequest WithBillingAddress(Address add) {
public ISubscriptionRequest WithBillingAddress(Address add)
{
this.BillingAddress = add;
return this;
}
Expand All @@ -148,7 +156,8 @@ public SubscriptionRequest WithBillingAddress(Address add) {
/// </summary>
/// <param name="add">The address to ship to</param>
/// <returns></returns>
public SubscriptionRequest WithShippingAddress(Address add) {
public ISubscriptionRequest WithShippingAddress(Address add)
{
this.ShippingAddress = add;
return this;
}
Expand All @@ -159,7 +168,8 @@ public SubscriptionRequest WithShippingAddress(Address add) {
/// <param name="trialBillingCycles">The trial billing cycles.</param>
/// <param name="trialAmount">The trial amount.</param>
/// <returns></returns>
public SubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal trialAmount) {
public ISubscriptionRequest SetTrialPeriod(short trialBillingCycles, decimal trialAmount)
{
this.TrialBillingCycles = trialBillingCycles;
this.TrialAmount = trialAmount;

Expand All @@ -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; }
Expand All @@ -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; }

Expand All @@ -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;
Expand All @@ -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()
{
Expand All @@ -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) {
Expand All @@ -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;

Expand All @@ -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) &&
Expand All @@ -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;

}
Expand Down
Loading