Ejemplo n.º 1
0
        public void Can_Authorize_A_Payment()
        {
            //// Arrange
            var creditCardMethod = Provider.GetPaymentGatewayMethodByPaymentCode("CreditCard");
            Assert.NotNull(creditCardMethod);

            var ccEntry = new CreditCardFormData()
            {
                CreditCardType = "VISA",
                CardholderName = "Rusty Swayne",
                CardNumber = "4111111111111111",
                CardCode = "111",
                ExpireMonth = "09",
                ExpireYear = "15",
                CustomerIp = "10.0.0.15"
            };

            //// Act
            var result = creditCardMethod.AuthorizePayment(_invoice, ccEntry.AsProcessorArgumentCollection());

            //// Assert
            Assert.NotNull(result);
            Assert.IsTrue(result.Payment.Success);
            var payment = result.Payment.Result;
            Console.WriteLine(payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.AuthorizationTransactionCode));
            Console.WriteLine(payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.AuthorizationTransactionResult));
            Console.WriteLine(payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.AvsResult));
        }
 public static ProcessorArgumentCollection AsProcessorArgumentCollection(this CreditCardFormData creditCard)
 {
     return(new ProcessorArgumentCollection()
     {
         { "creditCardType", creditCard.CreditCardType },
         { "cardholderName", creditCard.CardholderName },
         { "cardNumber", creditCard.CardNumber },
         { "expireMonth", creditCard.ExpireMonth },
         { "expireYear", creditCard.ExpireYear },
         { "cardCode", creditCard.CardCode },
         { "customerIp", creditCard.CustomerIp }
     });
 }
        /// <summary>
        /// Processes the Authorize and AuthorizeAndCapture transactions
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/> to be paid</param>
        /// <param name="payment">The <see cref="IPayment"/> record</param>
        /// <param name="transactionMode">Authorize or AuthorizeAndCapture</param>
        /// <param name="amount">The money amount to be processed</param>
        /// <param name="creditCard">The <see cref="CreditCardFormData"/></param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        public IPaymentResult ProcessPayment(IInvoice invoice, IPayment payment, TransactionMode transactionMode, decimal amount, CreditCardFormData creditCard)
        {
            var address = invoice.GetBillingAddress();
            
            var form = GetInitialRequestForm(invoice.CurrencyCode());

            var names = creditCard.CardholderName.Split(' ');

            form.Add("x_type", 
                    transactionMode == TransactionMode.Authorize ?
                    "AUTH_ONLY" : "AUTH_CAPTURE"
                );
           
            // Credit card information            
            form.Add("x_card_num", creditCard.CardNumber);
            form.Add("x_exp_date", creditCard.ExpireMonth.PadLeft(2) + creditCard.ExpireYear);
            form.Add("x_card_code", creditCard.CardCode);
            form.Add("x_customer_ip", creditCard.CustomerIp);

            // Billing address
            form.Add("x_first_name", names.Count() > 1 ? names[0] : creditCard.CardholderName);
            form.Add("x_last_name", names.Count() > 1 ? names[1] : string.Empty);
            form.Add("x_email", address.Email);
            if(!string.IsNullOrEmpty(address.Organization)) form.Add("x_company", address.Organization);
            form.Add("x_address", address.Address1);
            form.Add("x_city", address.Locality);
            if(!string.IsNullOrEmpty(address.Region)) form.Add("x_state", address.Region);
            form.Add("x_zip", address.PostalCode);
            if(!string.IsNullOrEmpty(address.CountryCode)) form.Add("x_country", address.CountryCode);

            // Invoice information
            form.Add("x_amount", amount.ToString("0.00", CultureInfo.InstalledUICulture));

            // maximum length 20 chars
            form.Add("x_invoice_num", invoice.PrefixedInvoiceNumber());
            form.Add("x_description", string.Format("Full invoice #{0}", invoice.PrefixedInvoiceNumber()));

            var reply = GetAuthorizeNetReply(form);

            // API Error
            if(string.IsNullOrEmpty(reply)) return new PaymentResult(Attempt<IPayment>.Fail(payment, new Exception("Authorize.NET unknown error")), invoice, false);

            var fields = reply.Split('|');

            switch (fields[0])
            {
                case "3" :
                    return new PaymentResult(Attempt<IPayment>.Fail(payment, new Exception(string.Format("Error {0}", reply))), invoice, false);
                case "2" :                    
                    payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.AuthorizeDeclinedResult, string.Format("Declined ({0} : {1})", fields[2], fields[3]));
                    return new PaymentResult(Attempt<IPayment>.Fail(payment, new Exception(string.Format("Declined ({0} : {1})", fields[2], fields[3]))), invoice, false);
                case "1" :
                    payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.AuthorizationTransactionCode, string.Format("{0},{1}", fields[6], fields[4]));
                    payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.AuthorizationTransactionResult, string.Format("Approved ({0}: {1})", fields[2], fields[3]));
                    payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.AvsResult, fields[5]);
                    payment.Authorized = true;
                    if (transactionMode == TransactionMode.AuthorizeAndCapture) payment.Collected = true;                    
                    return new PaymentResult(Attempt<IPayment>.Succeed(payment), invoice, true);
            }

            return new PaymentResult(Attempt<IPayment>.Fail(payment, new Exception(string.Format("Error {0}", reply))), invoice, false);
        }