Ejemplo n.º 1
0
        private void ProcessPayment()
        {
            var details = GetOrderDetails();

            var config = new HpsServicesConfig
            {
                // The following variables will be provided to you during certification
                SecretApiKey = "skapi_cert_MYl2AQAowiQAbLp5JesGKh7QFkcizOP2jcX9BrEMqQ",                
                VersionNumber = "0000",
                DeveloperId = "000000"
            };

            var chargeService = new HpsCreditService(config);

            var numbers = new Regex("^[0-9]+$");

            var address = new HpsAddress
            {
                Address = details.Address,
                City = details.City,
                State = details.State,
                Country = "United States",
                Zip = numbers.Match(details.Zip ?? string.Empty).ToString()
            };

            var validCardHolder = new HpsCardHolder
            {
                FirstName = details.FirstName,
                LastName = details.LastName,
                Address = address,
                Phone = numbers.Match(details.PhoneNumber ?? string.Empty).ToString()
            };

            var suToken = new HpsTokenData
            {
                TokenValue = details.Token_value
            };

            try
            {
                var authResponse = chargeService.Charge(15.15m, "usd", suToken.TokenValue, validCardHolder);

                SendEmail();

                Response.Write("<h1>Success!</h1><p>Thank you, " + 
                               details.FirstName + 
                               ", for your order of $15.15.</p>" +
                               "Transaction Id: " + authResponse.TransactionId);                
            }
            catch (HpsInvalidRequestException e)
            {
                // handle error for amount less than zero dollars
                Response.Write("<h3>Error</h3>" +  "<strong>amount less than zero dollars: " + e.Message + "</strong>");
            }
            catch (HpsAuthenticationException e)
            {
                // handle errors related to your HpsServiceConfig
                Response.Write("<h3>Error</h3>" + "<strong>Bad Config: " + e.Message + "</strong>");
            }
            catch (HpsCreditException e)
            {
                // handle card-related exceptions: card declined, processing error, etc
                Response.Write("<h3>Error</h3>" + "<strong>card declined, processing error, etc: " + e.Message + "</strong>");
            }
            catch (HpsGatewayException e)
            {
                // handle gateway-related exceptions: invalid cc number, gateway-timeout, etc
                Response.Write("<h3>Error</h3>" + "<strong>invalid cc number, gateway-timeout, etc: " + e.Message + "</strong>");
            }            
        }
        public ActionResult ProcessPayment(OrderDetails details)
        {
            var config = new HpsServicesConfig
            {
                SecretApiKey = "skapi_cert_MYl2AQAowiQAbLp5JesGKh7QFkcizOP2jcX9BrEMqQ",
                // The following variables will be provided to you during certification
                VersionNumber = "0000",
                DeveloperId = "000000"
            };

            var chargeService = new HpsCreditService(config);

            var numbers = new Regex("^[0-9]+$");

            var address = new HpsAddress
            {
                Address = details.Address,
                City = details.City,
                State = details.State,
                Country = "United States",
                Zip = numbers.Match(details.Zip ?? string.Empty).ToString()
            };

            var validCardHolder = new HpsCardHolder
            {
                FirstName = details.FirstName,
                LastName = details.LastName,
                Address = address,
                Phone = numbers.Match(details.PhoneNumber ?? string.Empty).ToString()
            };

            var suToken = new HpsTokenData
            {
                TokenValue = details.Token_value
            };

            try
            {
                var authResponse = chargeService.Charge(15.15m, "usd", suToken.TokenValue, validCardHolder);                

                SendEmail();

                return View("Success", new SuccessModel {
                    FirstName = details.FirstName,
                    TransactionId = authResponse.TransactionId
                });
            }
            catch (HpsInvalidRequestException e)
            {
                // handle error for amount less than zero dollars
                return View("Error", model: "amount less than zero dollars: " + e.Message);
            }
            catch (HpsAuthenticationException e)
            {
                // handle errors related to your HpsServiceConfig
                return View("Error", model: "Bad Config: " + e.Message);
            }
            catch (HpsCreditException e)
            {
                // handle card-related exceptions: card declined, processing error, etc
                return View("Error", model: "card declined, processing error, etc: " + e.Message);
            }
            catch (HpsGatewayException e)
            {
                // handle gateway-related exceptions: invalid cc number, gateway-timeout, etc
                return View("Error", model: "invalid cc number, gateway-timeout, etc: " + e.Message);
            }            
        }