public static void Run(String ApiLoginID, String ApiTransactionKey)
        {
            Console.WriteLine("Charge Customer Profile");

            ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;

            // define the merchant information (authentication / transaction id)
            ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()
            {
                name = ApiLoginID,
                ItemElementName = ItemChoiceType.transactionKey,
                Item = ApiTransactionKey
            };

            //create a customer payment profile
            customerProfilePaymentType profileToCharge = new customerProfilePaymentType();
            profileToCharge.customerProfileId = "36731856";
            profileToCharge.paymentProfile = new paymentProfile { paymentProfileId = "33211899" };

            var transactionRequest = new transactionRequestType
            {
                transactionType = transactionTypeEnum.authCaptureTransaction.ToString(),    // refund type
                amount = 2.00m,
                profile = profileToCharge
            };

            var request = new createTransactionRequest { transactionRequest = transactionRequest };

            // instantiate the collector that will call the service
            var controller = new createTransactionController(request);
            controller.Execute();

            // get the response from the service (errors contained if any)
            var response = controller.GetApiResponse();

            //validate
            if (response.messages.resultCode == messageTypeEnum.Ok)
            {
                if (response.transactionResponse != null)
                {
                    Console.WriteLine("Success, Auth Code : " + response.transactionResponse.authCode);
                }
            }
            else
            {
                Console.WriteLine("Error: " + response.messages.message[0].code + "  " + response.messages.message[0].text);
                if (response.transactionResponse != null)
                {
                    Console.WriteLine("Transaction Error : " + response.transactionResponse.errors[0].errorCode + " " + response.transactionResponse.errors[0].errorText);
                }
            }

        }
        public void CreateTransactionFromProfile()
        {
            //Creates a customer profile and customer payment profile
            //Then uses those profiles to create a transaction request

            //Common code to set for all requests
            ApiOperationBase <ANetApiRequest, ANetApiResponse> .MerchantAuthentication = CustomMerchantAuthenticationType;
            ApiOperationBase <ANetApiRequest, ANetApiResponse> .RunEnvironment         = TestEnvironment;

            Random rnd = new Random(DateTime.Now.Millisecond);

            string profileRandom = rnd.Next(9999).ToString();

            //Create profile to use in transaction creation
            var profileShipTo = new customerAddressType
            {
                address     = profileRandom + " First St NE",
                city        = "Bellevue",
                state       = "WA",
                zip         = "98007",
                company     = "Sample Co " + profileRandom,
                country     = "USA",
                firstName   = "Sample" + profileRandom,
                lastName    = "Name" + profileRandom,
                phoneNumber = "425 123 4567",
            };

            var paymentProfile = new customerPaymentProfileType
            {
                billTo       = profileShipTo,
                customerType = customerTypeEnum.individual,
                payment      = new paymentType {
                    Item = new creditCardType {
                        cardNumber = "4111111111111111", expirationDate = "0622"
                    }
                },
            };

            var createProfileReq = new createCustomerProfileRequest
            {
                profile = new customerProfileType
                {
                    description     = "SampleProfile " + profileRandom,
                    email           = "SampleEmail" + profileRandom + "@Visa.com",
                    shipToList      = new customerAddressType[] { profileShipTo },
                    paymentProfiles = new customerPaymentProfileType[] { paymentProfile }
                }
            };

            var createProfileCont = new createCustomerProfileController(createProfileReq);

            createProfileCont.Execute();
            var createProfileResp = createProfileCont.GetApiResponse();

            //Get profile using getCustomerProfileRequest
            var getCustReq = new getCustomerProfileRequest {
                customerProfileId = createProfileResp.customerProfileId
            };
            var getCustCont = new getCustomerProfileController(getCustReq);

            getCustCont.Execute();
            var getCustResp = getCustCont.GetApiResponse();


            //Create Transaction
            //Create instance of customer payment profile using the profile IDs from the profile we loaded above.
            var custPaymentProfile = new AuthorizeNet.Api.Contracts.V1.customerProfilePaymentType {
                customerProfileId = getCustResp.profile.customerProfileId, paymentProfile = new paymentProfile {
                    paymentProfileId = getCustResp.profile.paymentProfiles[0].customerPaymentProfileId
                }
            };

            var testTxn = new transactionRequestType
            {
                profile         = custPaymentProfile,
                amount          = (decimal)rnd.Next(9999) / 100,
                transactionType = transactionTypeEnum.authCaptureTransaction.ToString()
            };

            var txnControler = new createTransactionController(new createTransactionRequest {
                transactionRequest = testTxn
            });

            txnControler.Execute();
            var txnControlerResp = txnControler.GetApiResponse();

            //verify transaction succeeded.
            Assert.AreEqual("1", txnControlerResp.transactionResponse.messages[0].code);
        }
 public static void customerProfilePaymentType(customerProfilePaymentType argument)
 {
     if (null != argument)
     {
         if (argument.createProfile) { argument.createProfileSpecified = true; }
         paymentProfile(argument.paymentProfile);
     }
 }
        public createProfileController(customerProfilePaymentType apiRequest)// : base(apiRequest) 
        {
	    }
        public static ANetApiResponse Run(String ApiLoginID, String ApiTransactionKey, string customerProfileId,
            string customerPaymentProfileId, decimal Amount)
        {
            Console.WriteLine("Charge Customer Profile");

            ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;

            // define the merchant information (authentication / transaction id)
            ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()
            {
                name = ApiLoginID,
                ItemElementName = ItemChoiceType.transactionKey,
                Item = ApiTransactionKey
            };

            //create a customer payment profile
            customerProfilePaymentType profileToCharge = new customerProfilePaymentType();
            profileToCharge.customerProfileId = customerProfileId;
            profileToCharge.paymentProfile = new paymentProfile { paymentProfileId = customerPaymentProfileId };

            var transactionRequest = new transactionRequestType
            {
                transactionType = transactionTypeEnum.authCaptureTransaction.ToString(),    // refund type
                amount = Amount,
                profile = profileToCharge
            };

            var request = new createTransactionRequest { transactionRequest = transactionRequest };

            // instantiate the collector that will call the service
            var controller = new createTransactionController(request);
            controller.Execute();

            // get the response from the service (errors contained if any)
            var response = controller.GetApiResponse();

            //validate
            if (response != null)
            {
                if (response.messages.resultCode == messageTypeEnum.Ok)
                {
                    if(response.transactionResponse.messages != null)
                    {
                        Console.WriteLine("Successfully created transaction with Transaction ID: " + response.transactionResponse.transId);
                        Console.WriteLine("Response Code: " + response.transactionResponse.responseCode);
                        Console.WriteLine("Message Code: " + response.transactionResponse.messages[0].code);
                        Console.WriteLine("Description: " + response.transactionResponse.messages[0].description);
                        Console.WriteLine("Success, Auth Code : " + response.transactionResponse.authCode);
                    }
                    else
                    {
                        Console.WriteLine("Failed Transaction.");
                        if (response.transactionResponse.errors != null)
                        {
                            Console.WriteLine("Error Code: " + response.transactionResponse.errors[0].errorCode);
                            Console.WriteLine("Error message: " + response.transactionResponse.errors[0].errorText);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Failed Transaction.");
                    if (response.transactionResponse != null && response.transactionResponse.errors != null)
                    {
                        Console.WriteLine("Error Code: " + response.transactionResponse.errors[0].errorCode);
                        Console.WriteLine("Error message: " + response.transactionResponse.errors[0].errorText);
                    }
                    else
                    {
                        Console.WriteLine("Error Code: " + response.messages.message[0].code);
                        Console.WriteLine("Error message: " + response.messages.message[0].text);
                    }
                }
            }
            else
            {
                Console.WriteLine("Null Response.");
            }

            return response;
        }
 public static void customerProfilePaymentType ( customerProfilePaymentType request) { }
        public void CreateTransactionFromProfile()
        {
            //Creates a customer profile and customer payment profile
            //Then uses those profiles to create a transaction request

            //Common code to set for all requests
            ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
            ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;

            Random rnd = new Random(DateTime.Now.Millisecond);

            string profileRandom = rnd.Next(9999).ToString();

            //Create profile to use in transaction creation
            var profileShipTo = new customerAddressType
            {
                address = profileRandom + " First St NE",
                city = "Bellevue",
                state = "WA",
                zip = "98007",
                company = "Sample Co " + profileRandom,
                country = "USA",
                firstName = "Sample" + profileRandom,
                lastName = "Name" + profileRandom,
                phoneNumber = "425 123 4567",
            };

            var paymentProfile = new customerPaymentProfileType
            {
                billTo = profileShipTo,
                customerType = customerTypeEnum.individual,
                payment = new paymentType { Item = new creditCardType { cardNumber = "4111111111111111", expirationDate = "0622" } },
            };

            var createProfileReq = new createCustomerProfileRequest
            {
                profile = new customerProfileType
                {
                    description = "SampleProfile " + profileRandom,
                    email = "SampleEmail" + profileRandom + "@Visa.com",
                    shipToList = new customerAddressType[] { profileShipTo },
                    paymentProfiles = new customerPaymentProfileType[] { paymentProfile }
                }
            };

            var createProfileCont = new createCustomerProfileController(createProfileReq);
            createProfileCont.Execute();
            var createProfileResp = createProfileCont.GetApiResponse();

            //Get profile using getCustomerProfileRequest
            var getCustReq = new getCustomerProfileRequest { customerProfileId = createProfileResp.customerProfileId };
            var getCustCont = new getCustomerProfileController(getCustReq);
            getCustCont.Execute();
            var getCustResp = getCustCont.GetApiResponse();


            //Create Transaction
            //Create instance of customer payment profile using the profile IDs from the profile we loaded above.
            var custPaymentProfile = new AuthorizeNet.Api.Contracts.V1.customerProfilePaymentType { customerProfileId = getCustResp.profile.customerProfileId, paymentProfile = new paymentProfile { paymentProfileId = getCustResp.profile.paymentProfiles[0].customerPaymentProfileId } };

            var testTxn = new transactionRequestType
            {
                profile = custPaymentProfile,
                amount = (decimal)rnd.Next(9999) / 100,
                transactionType = transactionTypeEnum.authCaptureTransaction.ToString()
            };

            var txnControler = new createTransactionController(new createTransactionRequest { transactionRequest = testTxn });
            txnControler.Execute();
            var txnControlerResp = txnControler.GetApiResponse();

            //verify transaction succeeded.
            Assert.AreEqual("1", txnControlerResp.transactionResponse.messages[0].code);

        }
        public void SampleCodeCreateTransactionUsingProfile()
        {
            LogHelper.info(Logger, "Sample createTransaction using Profile");

            ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = CustomMerchantAuthenticationType;
            ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = TestEnvironment;

            // Use CIM to create the profile we're going to charge
            var customerProfileId = "0";
            var paymentProfileId = "0";
            Assert.IsTrue(createProfile(out customerProfileId, out paymentProfileId));

            //create a customer payment profile
            customerProfilePaymentType profileToCharge = new customerProfilePaymentType();
            profileToCharge.customerProfileId = customerProfileId;
            profileToCharge.paymentProfile = new paymentProfile { paymentProfileId = paymentProfileId};

            var transactionRequestType = new transactionRequestType
            {
                transactionType = transactionTypeEnum.authCaptureTransaction.ToString(),
                amount = SetValidTransactionAmount(Counter),
                profile = profileToCharge
            };
            var createRequest = new createTransactionRequest
            {
                refId = RefId,
                transactionRequest = transactionRequestType,
            };
            //create controller, execute and get response
            var createController = new createTransactionController(createRequest);
            createController.Execute();
            var createResponse = createController.GetApiResponse();

            //test response
            Assert.IsNotNull(createResponse.transactionResponse);
            LogHelper.info(Logger, "Response: {0}", createResponse);
            DisplayResponse(createResponse, "Create Transaction Response");
            LogHelper.info(Logger, "Created Transaction: {0}", createResponse.transactionResponse);
            Assert.IsNotNull(createResponse.transactionResponse.transId);
            long transId;
            Assert.IsTrue(long.TryParse(createResponse.transactionResponse.transId, out transId));
            if (0 == transId)
            {
                ValidateFailure<createTransactionRequest, createTransactionResponse, createTransactionController>(createController, createResponse);
                Assert.IsNotNull(createResponse.transactionResponse.errors);
                foreach (var error in createResponse.transactionResponse.errors)
                {
                    LogHelper.info(Logger, "Error-> Code:{0}, Text:{1}", error.errorCode, error.errorText);
                }
            }
            else
            {
                Assert.AreNotEqual(0, transId);
                ValidateSuccess<createTransactionRequest, createTransactionResponse, createTransactionController>(createController, createResponse);
            }
        }