Skip to content

Commit ac38c7d

Browse files
committed
Added sample test for charging a profile using CreateTransaction
1 parent e268d80 commit ac38c7d

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

AuthorizeNETtest/Api/Controllers/SampleTest/CreateTransactionSampleTest.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,111 @@ public void SampleCodeCreateTransaction()
8888
}
8989
}
9090

91+
/// <summary>
92+
/// This sample demonstrates charging a profile using the CreateTransaction API method
93+
/// See API example here http://developer.authorize.net/api/reference/#payment-transactions-charge-a-customer-profile
94+
/// </summary>
95+
[Test]
96+
public void SampleCodeCreateTransactionUsingProfile()
97+
{
98+
LogHelper.info(Logger, "Sample createTransaction using Profile");
99+
100+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
101+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;
102+
103+
// Use CIM to create the profile we're going to charge
104+
var customerProfileId = "0";
105+
var paymentProfileId = "0";
106+
Assert.IsTrue(createProfile(out customerProfileId, out paymentProfileId));
107+
108+
//create a transaction
109+
customerProfilePaymentType profileToCharge = new customerProfilePaymentType();
110+
profileToCharge.customerProfileId = customerProfileId;
111+
profileToCharge.paymentProfile = new paymentProfile { paymentProfileId = paymentProfileId};
112+
113+
var transactionRequestType = new transactionRequestType
114+
{
115+
transactionType = transactionTypeEnum.authCaptureTransaction.ToString(),
116+
//amount = SetValidTransactionAmount(Counter),
117+
amount = 10,
118+
profile = profileToCharge
119+
};
120+
var createRequest = new createTransactionRequest
121+
{
122+
refId = RefId,
123+
transactionRequest = transactionRequestType,
124+
};
125+
//create
126+
var createController = new createTransactionController(createRequest);
127+
createController.Execute();
128+
var createResponse = createController.GetApiResponse();
129+
Assert.IsNotNull(createResponse.transactionResponse);
130+
LogHelper.info(Logger, "Response: {0}", createResponse);
131+
DisplayResponse(createResponse, "Create Transaction Response");
132+
LogHelper.info(Logger, "Created Transaction: {0}", createResponse.transactionResponse);
133+
Assert.IsNotNull(createResponse.transactionResponse.transId);
134+
long transId;
135+
Assert.IsTrue(Int64.TryParse(createResponse.transactionResponse.transId, out transId));
136+
if (0 == transId)
137+
{
138+
ValidateFailure<createTransactionRequest, createTransactionResponse, createTransactionController>(createController, createResponse);
139+
Assert.IsNotNull(createResponse.transactionResponse.errors);
140+
foreach (var error in createResponse.transactionResponse.errors)
141+
{
142+
LogHelper.info(Logger, "Error-> Code:{0}, Text:{1}", error.errorCode, error.errorText);
143+
}
144+
}
145+
else
146+
{
147+
Assert.AreNotEqual(0, transId);
148+
ValidateSuccess<createTransactionRequest, createTransactionResponse, createTransactionController>(createController, createResponse);
149+
}
150+
}
151+
152+
private Boolean createProfile(out String customerProfileId, out String paymentProfileId)
153+
{
154+
155+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
156+
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;
157+
158+
var creditCard = new creditCardType { cardNumber = "4111111111111111", expirationDate = "0622" };
159+
var paymentType = new paymentType {Item = creditCard};
160+
161+
var paymentProfile = new customerPaymentProfileType{ payment = paymentType };
162+
163+
var createRequest = new createCustomerProfileRequest
164+
{
165+
profile = new customerProfileType{
166+
merchantCustomerId = "TSTCSTER78",
167+
paymentProfiles = new customerPaymentProfileType[]{ paymentProfile }
168+
}
169+
};
170+
171+
//create
172+
var createController = new createCustomerProfileController(createRequest);
173+
var createResponse = createController.ExecuteWithApiResponse();
174+
175+
//validate
176+
177+
if (messageTypeEnum.Ok != createResponse.messages.resultCode)
178+
{
179+
customerProfileId = "0";
180+
paymentProfileId = "0";
181+
return false;
182+
}
183+
else
184+
{
185+
Assert.NotNull(createResponse.customerProfileId);
186+
Assert.NotNull(createResponse.customerPaymentProfileIdList);
187+
Assert.AreNotEqual(0, createResponse.customerPaymentProfileIdList.Length);
188+
189+
customerProfileId = createResponse.customerProfileId;
190+
paymentProfileId = createResponse.customerPaymentProfileIdList[0];
191+
192+
return true;
193+
}
194+
}
195+
91196
[Test]
92197
public void SampleCodeCreateTransactionWithCreditCard()
93198
{

0 commit comments

Comments
 (0)