コード例 #1
0
        public static Outcome PlaceOrder(ILogger logger, Config config, Order order, bool validate = true)
        {
            const string OAUTHERROR =
                "An unexpected OAUTH validation error occured!  Please contact support for help in resolving this issue.";

            Contract.Requires(logger != null);
            Contract.Requires(config != null);
            Contract.Requires(order != null);

            if (validate)
            {
                config.Validate();
                order.Validate();
            }

            logger.Log(LogLevel.Info, "Placing Order (OrderId: {0})", order.OrderId);

            var oauth = new OAuthHelper(logger, config);

            var accessToken = oauth.GetAccessToken();

            if (accessToken == null)
            {
                logger.Log(LogLevel.Error, OAUTHERROR);

                throw new Exception(OAUTHERROR);
            }

            var context = new Context(accessToken, order.OrderId);

            var payment = GetPayment(logger, config, context, order, validate);

            var outcome = new Outcome()
            {
                CreatedOn = DateTime.Parse(payment.create_time),
                UpdatedOn = DateTime.Parse(payment.update_time),
                State = (OrderState)Enum.Parse(typeof(OrderState), payment.state, true),
                PaymentId = payment.id
            };

            logger.Log(LogLevel.Info, "OrderId: {0}, Outcome: {1}, PaymentId: {2}",
                order.OrderId, outcome.State, outcome.PaymentId);

            return outcome;
        }