-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathCreateSubscription.cs
More file actions
88 lines (73 loc) · 3.32 KB
/
CreateSubscription.cs
File metadata and controls
88 lines (73 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using AuthorizeNet.Api.Controllers;
using AuthorizeNet.Api.Contracts.V1;
using AuthorizeNet.Api.Controllers.Bases;
namespace net.authorize.sample
{
public class CreateSubscription
{
public static ANetApiResponse Run(String ApiLoginID, String ApiTransactionKey, short intervalLength)
{
Console.WriteLine("Create Subscription Sample");
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()
{
name = ApiLoginID,
ItemElementName = ItemChoiceType.transactionKey,
Item = ApiTransactionKey,
};
paymentScheduleTypeInterval interval = new paymentScheduleTypeInterval();
interval.length = intervalLength; // months can be indicated between 1 and 12
interval.unit = ARBSubscriptionUnitEnum.days;
paymentScheduleType schedule = new paymentScheduleType
{
interval = interval,
startDate = DateTime.Now.AddDays(1), // start date should be tomorrow
totalOccurrences = 9999, // 9999 indicates no end date
trialOccurrences = 3
};
#region Payment Information
var creditCard = new creditCardType
{
cardNumber = "4111111111111111",
expirationDate = "1028"
};
//standard api call to retrieve response
paymentType cc = new paymentType { Item = creditCard };
#endregion
nameAndAddressType addressInfo = new nameAndAddressType()
{
firstName = "John",
lastName = "Doe"
};
ARBSubscriptionType subscriptionType = new ARBSubscriptionType()
{
amount = 35.55m,
trialAmount = 0.00m,
paymentSchedule = schedule,
billTo = addressInfo,
payment = cc
};
var request = new ARBCreateSubscriptionRequest {subscription = subscriptionType };
var controller = new ARBCreateSubscriptionController(request); // instantiate the controller that will call the service
controller.Execute();
ARBCreateSubscriptionResponse response = controller.GetApiResponse(); // get the response from the service (errors contained if any)
// validate response
if (response != null && response.messages.resultCode == messageTypeEnum.Ok)
{
if (response != null && response.messages.message != null)
{
Console.WriteLine("Success, Subscription ID : " + response.subscriptionId.ToString());
}
}
else if(response != null)
{
Console.WriteLine("Error: " + response.messages.message[0].code + " " + response.messages.message[0].text);
}
return response;
}
}
}