//---------------------------------------------------------------------
        // Private Methods
        //---------------------------------------------------------------------
        /*
         * Helper method for verifyCreditCard() to handle usage of real CC and
         * storageTokenId specifier.
         */
        private CreditCardReceipt verifyCreditCardHelper(Object creditCardSpecifier, VerificationRequest verificationRequest, bool isActualCreditCard, bool addToStorage, String secureTokenId)
        {
            if (verificationRequest == null) {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, "verificationRequest is required", null);
            }

            // create the request string
            StringBuilder req = new StringBuilder();
            try {
                this.appendHeader(req, "verifyCreditCard");
                if (isActualCreditCard) {
                    if (addToStorage) {
                        this.appendStorageFlag(req, true);
                        this.appendStorageTokenId(req, secureTokenId);
                    }

                    this.appendCreditCard(req, (CreditCard) creditCardSpecifier);
                } else {
                    this.appendStorageTokenId(req, creditCardSpecifier.ToString());
                }
                this.appendVerificationRequest(req, verificationRequest);
            } catch (Exception e) {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, e.ToString(), null);
            }

            return this.send(req);
        }
        /*
         * Helper method for singlePurchase() to handle usage of real CC and
         * storageTokenId specifier.
         */
        private CreditCardReceipt singlePurchaseHelper(String orderId, Object creditCardSpecifier, long amount, VerificationRequest verificationRequest, PurchaseCardRequest purchaseCard, bool isActualCreditCard, bool addToStorage, String secureTokenId)
        {
            if (orderId == null)
            {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, "orderId is required", null);
            }

            // create the request string
            StringBuilder req = new StringBuilder();
            try {
                this.appendHeader(req, "singlePurchase");
                this.appendOrderId(req, orderId);

                // real CC, or using storage token
                if (isActualCreditCard) {
                    if (addToStorage) {
                        this.appendStorageFlag(req, true);
                        this.appendStorageTokenId(req, secureTokenId);
                    }

                    this.appendCreditCard(req, (CreditCard) creditCardSpecifier);
                } else {
                    this.appendStorageTokenId(req, creditCardSpecifier.ToString());
                }
                this.appendAmount(req, amount);
                this.appendVerificationRequest(req, verificationRequest);
                this.appendPurchaseCard(req, purchaseCard);

                // render indicator
                foreach (CreditCardIndicator indicator in indicatorList) {
                    this.appendIndicator(req, indicator);
                }
            } catch (Exception e) {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, e.ToString(), null);
            }
            return this.send(req);
        }
        // helper method for all update + change state recurring methods
        private CreditCardReceipt updateRecurringPurchaseHelper(PeriodicPurchaseInfo periodicPurchaseInfo, Object creditCardSpecifier, VerificationRequest verificationRequest, bool isActualCreditCard)
        {
            if (periodicPurchaseInfo.getPeriodicTransactionId() < 0) {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, "recurringPurchaseId is required", null);
            }

            // create the request string
            StringBuilder req = new StringBuilder();
            try {
                this.appendHeader(req, "recurringPurchase");
                this.appendOperationType(req, "update");
                this.appendTransactionId(req, periodicPurchaseInfo.getPeriodicTransactionId());
                if (creditCardSpecifier != null) {
                    if (isActualCreditCard) {
                        this.appendCreditCard(req, (CreditCard) creditCardSpecifier);
                    } else {
                        this.appendStorageTokenId(req, creditCardSpecifier.ToString());
                    }
                }
                if (periodicPurchaseInfo.getPerPaymentAmount() > 0) {
                    this.appendAmount(req, periodicPurchaseInfo.getPerPaymentAmount());
                }
                if (verificationRequest != null) {
                    this.appendVerificationRequest(req, verificationRequest);
                }
                if (periodicPurchaseInfo.getState() != PeriodicPurchaseInfo.State.NULL) {
                    this.appendPeriodicPurchaseState(req, periodicPurchaseInfo.getState());
                }
            } catch (Exception e) {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, e.ToString(), null);
            }
            return this.send(req);
        }
        public CreditCardReceipt verifyCreditCard(String storageTokenId, VerificationRequest verificationRequest)
        {
            if (storageTokenId == null) {
                throw new ArgumentNullException("storageTokenId is required");
            }

            if (verificationRequest == null) {
                throw new ArgumentNullException("verificationRequest is required");
            }
            // create the request string
            StringBuilder req = new StringBuilder();
            this.appendHeader(req, "verifyCreditCard");
            this.appendStorageTokenId(req, storageTokenId);
            this.appendVerificationRequest(req, verificationRequest);
            return this.send(req);
        }
 private void appendVerificationRequest(StringBuilder req, VerificationRequest vr)
 {
     if (vr != null)
     {
         this.appendParam(req, "avsRequestCode", vr.isAvsEnabled() ?  vr.getAvsRequest().ToString() : null);
         this.appendParam(req, "cvv2RequestCode", vr.isCvv2Enabled() ? vr.getCvv2Request().ToString() : null);
     }
 }
 public CreditCardReceipt updateRecurringPurchase(PeriodicPurchaseInfo periodicPurchaseInfo, CreditCard creditCard, String storageTokenId, VerificationRequest verificationRequest)
 {
     if (storageTokenId == null && creditCard == null){
         return new CreditCardReceipt(REQ_INVALID_REQUEST, "creditCard or storageTokenId is required", null);
     }
     else if (storageTokenId != null)
     {
         return this.updateRecurringPurchaseHelper(periodicPurchaseInfo, storageTokenId, verificationRequest, false);
     }
     else
     {
         return this.updateRecurringPurchaseHelper(periodicPurchaseInfo, creditCard, verificationRequest, true);
     }
 }
        // Verify credit card and add credit card to secure storage with
        // secureTokenId
        public CreditCardReceipt verifyCreditCard(CreditCard creditCard, VerificationRequest verificationRequest, String secureTokenId)
        {
            if (creditCard == null)
            {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, "creditCard is required", null);
            }

            return this.verifyCreditCardHelper(creditCard, verificationRequest, true, true, secureTokenId);
        }
 /*
  *
  *
  * @see com.admeris.creditcard.api.CreditCardService#singlePurchase(
  * java.lang.String, java.lang.String, long,
  * com.admeris.creditcard.api.VerificationRequest,
  * com.admeris.creditcard.api.PurchaseCardRequest)
  */
 public CreditCardReceipt singlePurchase(String orderId, String storageTokenId, long amount, VerificationRequest verificationRequest, PurchaseCardRequest purchaseCard)
 {
     if (storageTokenId == null || storageTokenId.Length <= 0)
     {
         return new CreditCardReceipt(REQ_INVALID_REQUEST, "storageTokenId is required", null);
     }
     return this.singlePurchaseHelper(orderId, storageTokenId, amount, verificationRequest, purchaseCard, false, false, null);
 }
 public CreditCardReceipt updateRecurringPurchase(long recurringPurchaseId, String storageTokenId, long perPaymentAmount, VerificationRequest verificationRequest)
 {
     return this.updateRecurringPurchaseHelper(new PeriodicPurchaseInfo(recurringPurchaseId, PeriodicPurchaseInfo.State.NULL, perPaymentAmount),storageTokenId, verificationRequest, false);
 }
        public CreditCardReceipt recurringPurchaseHelper(PeriodicPurchaseInfo periodicPurchaseInfo, Object creditCardSpecifier, VerificationRequest verificationRequest, bool isActualCreditCard)
        {
            if (periodicPurchaseInfo.getOrderId() == null)
            {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, "orderId is required", null);
            }

            // create the request string
            StringBuilder req = new StringBuilder();
            try {
                this.appendHeader(req, "recurringPurchase");
                this.appendOperationType(req, "create");
                if (isActualCreditCard) {
                    this.appendCreditCard(req, (CreditCard) creditCardSpecifier);
                } else {
                    this.appendStorageTokenId(req, creditCardSpecifier.ToString());
                }
                this.appendPeriodicPurchaseInfo(req, periodicPurchaseInfo);
                this.appendVerificationRequest(req, verificationRequest);
            } catch (Exception e) {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, e.ToString(), null);
            }
            return this.send(req);
        }
 // Make single purchase and add credit card to secure storage with
 // secureTokenId
 public CreditCardReceipt singlePurchase(String orderId, CreditCard creditCard, long amount, VerificationRequest verificationRequest, PurchaseCardRequest purchaseCard, String secureTokenId)
 {
     if (creditCard == null)
     {
         return new CreditCardReceipt(REQ_INVALID_REQUEST, "creditCard is required", null);
     }
     return this.singlePurchaseHelper(orderId, creditCard, amount, verificationRequest, purchaseCard, true, true, secureTokenId);
 }
        // recurring purchase, storage token ID ver
        public CreditCardReceipt recurringPurchase(String orderId, String storageTokenId, long perPaymentAmount, DateTime startDate, DateTime endDate, PeriodicPurchaseInfo.Schedule schedule,  VerificationRequest verificationRequest)
        {
            if (storageTokenId == null)
            {
                return new CreditCardReceipt(REQ_INVALID_REQUEST, "storageTokenId is required", null);
            }

            return this.recurringPurchaseHelper(new PeriodicPurchaseInfo(0L, null, schedule, perPaymentAmount, orderId, null, startDate, endDate, DateTime.MinValue), storageTokenId, verificationRequest, false);
        }
 //---------------------------------------------------------------------
 // Service Methods
 //---------------------------------------------------------------------
 public CreditCardReceipt installmentPurchase(String orderId,
     CreditCard creditCard, long perInstallmentAmount, DateTime startDate,
     int totalNumberInstallments,
     VerificationRequest verificationRequest)
 {
     if (orderId == null) {
         throw new ArgumentNullException("orderId is required");
     }
     if (creditCard == null) {
         throw new ArgumentNullException("creditCard is required");
     }
     // create the request string
     StringBuilder req = new StringBuilder();
     this.appendHeader(req, "installmentPurchase");
     this.appendOrderId(req, orderId);
     this.appendCreditCard(req, creditCard);
     this.appendAmount(req, perInstallmentAmount);
     this.appendStartDate(req, startDate);
     this.appendTotalNumberInstallments(req, totalNumberInstallments);
     this.appendVerificationRequest(req, verificationRequest);
     return this.send(req);
 }
    public static void Main(string [] args)
    {
        int merchantId=0;
        string apiToken="";
        long pan = 0;
        short expiry = 0;
        String cvv2 = null;
        string avsPostal = "";
        string avsAddress = "";
        string purchaseInvoice = "";
        string invoice="";
        long amount=0;
        string url="";
        string method="";
        long transactionId=0;
        string customerId="";
        DateTime startDate = DateTime.Now;
        DateTime endDate = DateTime.Now;
        DateTime nextPaymentDate = DateTime.Now;
        if(args.Length >=6){
            method=args[0];
            url=args[1];
            merchantId=Convert.ToInt32(args[2]);
            apiToken=args[3];
            if(method.Equals("purchase")){
                pan = Convert.ToInt64(args[4]);
                expiry = Convert.ToInt16(args[5]);
                invoice = args[6];
                amount=Convert.ToInt64(args[7]);
            }
            else if(method.Equals("refund")) {
                transactionId=Convert.ToInt64(args[4]);
                purchaseInvoice = args[5];
                invoice = args[6];
                amount = Convert.ToInt64(args[7]);
            }
            else if (method.Equals("void"))
            {
                transactionId = Convert.ToInt64(args[4]);
                invoice = args[5];
            }
            else if (method.Equals("verify"))
            {
                pan = Convert.ToInt64(args[4]);
                expiry = Convert.ToInt16(args[5]);
                cvv2 = args[6];
                avsAddress = args[7];
                avsPostal = args[8];
            }
            else if (method.Equals("recurring"))
            {
                pan = Convert.ToInt64(args[4]);
                expiry = Convert.ToInt16(args[5]);
                invoice = args[6];
                amount=Convert.ToInt64(args[7]);
                startDate = DateTime.ParseExact(args[8], "yyyyMMdd", null);
                endDate = DateTime.ParseExact(args[9], "yyyyMMdd", null);
                customerId = args[10];
            }
            else if (method.Equals("queryRecurring"))
            {
                pan = Convert.ToInt64(args[4]);
                expiry = Convert.ToInt16(args[5]);
                transactionId = Convert.ToInt64(args[6]);
            }
            System.Net.ServicePointManager.ServerCertificateValidationCallback = null;

            // Service
            HttpsCreditCardService ccService = new HttpsCreditCardService(merchantId, apiToken, url);
            CreditCardReceipt resp=null;
            // invoke txn method
            if(method.Equals("purchase")){
                CreditCard creditCard = new CreditCard(pan, expiry);
                resp = ccService.singlePurchase(invoice, creditCard, amount, null);
            } else if (method.Equals("refund")){
                resp = ccService.refund(transactionId, purchaseInvoice, invoice, amount);
            } else if (method.Equals("void")) {
                resp = ccService.voidTransaction(transactionId, invoice);
            } else if (method.Equals("verify")){
                CreditCard creditCard = new CreditCard(pan, expiry, cvv2, avsAddress, avsPostal);
                VerificationRequest vr = new VerificationRequest(AvsRequest.VERIFY_STREET_AND_ZIP, Cvv2Request.CVV2_PRESENT);
                resp = ccService.verifyCreditCard(creditCard, vr);
            } else if (method.Equals("recurring")) {
                CreditCard creditCard = new CreditCard(pan, expiry);
                PeriodicPurchaseInfo.Schedule schedule = new PeriodicPurchaseInfo.Schedule(PeriodicPurchaseInfo.ScheduleType.MONTH, 1);
                PeriodicPurchaseInfo periodicPurchaseInfo = new PeriodicPurchaseInfo(
                    0L, PeriodicPurchaseInfo.State.IN_PROGRESS, schedule, amount, invoice, customerId, startDate, endDate, endDate);
                resp = ccService.recurringPurchase(invoice, creditCard, amount, startDate, endDate, schedule, null);
            } else if (method.Equals("queryRecurring")){
                CreditCard creditCard = new CreditCard (pan, expiry);
                resp = ccService.queryRecurringPurchase(transactionId);
            }
            else  {
                Console.WriteLine("args[0] must be purchase, refund or verify");
            }
            if(resp.isApproved()){
                Console.WriteLine("isApproved: {0}", resp.isApproved());
                Console.WriteLine("getTransactionId: {0}",resp.getTransactionId());
                if (resp.getApprovalInfo() != null && !method.Equals("void"))
                {
                    // void does not have approval, it just cancels a pending txn
                    Console.WriteLine("getApprovalInfo: \n {0}", resp.getApprovalInfo().ToString());
                }
                if (resp.getCvv2Response() != null)
                {
                    Console.WriteLine("getCvv2Response: \n {0}", resp.getCvv2Response().ToString());
                }
                if (resp.getAvsResponse() != null)
                {
                    Console.WriteLine("getAvsResponse: \n {0}", resp.getAvsResponse().ToString());
                }
                if (resp.getPeriodicPurchaseInfo() !=null)
                {
                    Console.WriteLine("getPeriodicPurchaseInfo: \n {0}", resp.getPeriodicPurchaseInfo().ToString());
                }
            } else {
                //display error
                Console.WriteLine("isApproved: {0}", resp.isApproved());
                Console.WriteLine("Error Code: {0} Message: {1}",resp.getErrorCode(),resp.getErrorMessage());
                Console.WriteLine("Debug Mesg: Message: {0}",resp.getDebugMessage());
            }
        } else {
            Console.WriteLine("[Invalid Command]");
            Console.WriteLine("");
            Console.WriteLine("Usage:");
            Console.WriteLine("");
            Console.WriteLine("Purchase: purchase url(string) merchantId(int) apiToken(string) pan(long) expiry(short) invoice(string) amount(long)");
            Console.WriteLine("      CCGatewayTest purchase https://test.admeris.com/ccgateway/cc/processor.do 1 abc 4242424242424242 1212 dotNet2_2BlueElephant1 1200");
            Console.WriteLine("------------------------------------*---------------------------------------");
            Console.WriteLine("Refund: refund url(string) merchantId(int) apiToken(string) purchaseTxnId(long) purchaseInvoice(string), invoice(string) amount(long)");
            Console.WriteLine("      CCGatewayTest refund https://test.admeris.com/ccgateway/cc/processor.do 1 abc 123456789 order-123 refund-456 1200");
            Console.WriteLine("------------------------------------*---------------------------------------");
            Console.WriteLine("Void: void url(string) merchantId(int) apiToken(string) transactionId(long) originalInvoice(string)");
            Console.WriteLine("      CCGatewayTest void https://test.admeris.com/ccgateway/cc/processor.do 1 abc 123456789 order-123");
            Console.WriteLine("------------------------------------*---------------------------------------");
            Console.WriteLine("Verify: verify url(string) merchantId(int) apiToken(string) pan(long) expiry(short) cvv2(string) avsAddress(string) avsPostal(string)");
            Console.WriteLine("      CCGatewayTest verify https://test.admeris.com/ccgateway/cc/processor.do 1 abc 4242424242424242 1212 456 \"100 Easy Street\" A1B2C3");
            Console.WriteLine("------------------------------------*---------------------------------------");
            Console.WriteLine("Recurring: recurring url(string) merchantId(int) apiToken(string) pan(long) expiry(short) invoice(string) amount(long) start(yyyyMMdd) end(yyyyMMdd) customerId(string)");
            Console.WriteLine("      CCGatewayTest recurring https://test.admeris.com/ccgateway/cc/processor.do 1 abc 4242424242424242 1212 rec-123 1000 20090505 20100505 customer");
            Console.WriteLine("------------------------------------*---------------------------------------");
            Console.WriteLine("Query Recurring: queryRecurring url(string) merchantId(int) apiToken(string) pan(long) expiry(short) recurringPurchaseId(long)");
            Console.WriteLine("      CCGatewayTest queryRecurring https://test.admeris.com/ccgateway/cc/processor.do 1 abc 4242424242424242 1212 670");
        }
    }
 public CreditCardReceipt verifyCreditCard(CreditCard creditCard,
     VerificationRequest verificationRequest)
 {
     if (creditCard == null) {
         throw new Exception("creditCard is required");
     }
     if (verificationRequest == null) {
         throw new Exception("verificationRequest is required");
     }
     // create the request string
     StringBuilder req = new StringBuilder();
     this.appendHeader(req, "verifyCreditCard");
     this.appendCreditCard(req, creditCard);
     this.appendVerificationRequest(req, verificationRequest);
     return this.send(req);
 }
 public CreditCardReceipt singlePurchase(String orderId,
     String storageTokenId, long amount,
     VerificationRequest verificationRequest)
 {
     if (orderId == null) {
         throw new ArgumentNullException("orderId is required");
     }
     if (storageTokenId == null) {
         throw new ArgumentNullException("storageTokenId is required");
     }
     // create the request string
     StringBuilder req = new StringBuilder();
     this.appendHeader(req, "singlePurchase");
     this.appendOrderId(req, orderId);
     this.appendStorageTokenId(req, storageTokenId);
     this.appendAmount(req, amount);
     this.appendVerificationRequest(req, verificationRequest);
     return this.send(req);
 }