/// <summary>
 /// Handle the form submisson
 /// </summary>
 /// <param name="sender">reference to the sender</param>
 /// <param name="e">arguments for the event</param>
 protected void btn_submit_Click(object sender, EventArgs e)
 {
     OffAmazonPaymentsServiceMultipleShipment instance = new OffAmazonPaymentsServiceMultipleShipment(orderReferenceId);
     InitializeShipmentArray();
     float amount = GetTotalOrderAmount();
     RunSampleMultiShipments(amount.ToString(), instance);
 }
        public static void Main(string[] args)
        {
            Console.WriteLine("===========================================");
            Console.WriteLine("Welcome to Off Amazon Payments Service Multiple Shipments Sample!");
            Console.WriteLine("===========================================");
            Console.WriteLine();

            string orderReferenceId = CLIHelper.getStringFromConsole("order reference id");
            OffAmazonPaymentsServiceMultipleShipment instance = new OffAmazonPaymentsServiceMultipleShipment(orderReferenceId);
            string orderAmount = instance.OrderTotal;
            RunSampleMultiShipments(orderReferenceId, orderAmount, instance);

            Console.WriteLine();
            Console.WriteLine("===========================================");
            Console.WriteLine("End of output. You can close this window");
            Console.WriteLine("===========================================");
            System.Threading.Thread.Sleep(50000);
        }
        private static void RunSampleMultiShipments(string orderReferenceId, string orderAmount, OffAmazonPaymentsServiceMultipleShipment instance)
        {
            /************************************************************************
            * Invoke Get Order Reference Details Action
            ***********************************************************************/
            GetOrderReferenceDetailsResponse getOrderDetails = instance.GetOrderReferenceDetails();
            if (getOrderDetails == null)
                throw new OffAmazonPaymentsServiceException("The response from GetOrderReference request is null");
            Console.WriteLine("=======================================================");

            /************************************************************************
            * Invoke Set Order Reference Details Action
            ***********************************************************************/
            SetOrderReferenceDetailsResponse setOrderDetailsResponse = instance.SetOrderReferenceDetails(orderAmount);
            if (setOrderDetailsResponse == null)
                throw new OffAmazonPaymentsServiceException("The response from SetOrderReference request is null");
            Console.WriteLine("=======================================================");

            /************************************************************************
            * Invoke Confirm Order Reference Action
            ***********************************************************************/
            if (instance.ConfirmOrderReferenceObject() == null)
                throw new OffAmazonPaymentsServiceException("The response from ConfirmOrderResponse request is null");
            Console.WriteLine("=======================================================");
            Console.WriteLine("===========================================");
            Console.WriteLine("Here is the demo of one capture per authorization.");
            Console.WriteLine("===========================================");
            Console.WriteLine();

            IDictionary<string, OrderItem> orderList = instance.OrderList;
            Console.WriteLine("Current orders are listed as below: ");
            instance.ShowCurrentOrders();
            System.Threading.Thread.Sleep(3000);

            //iterate the authoriztion amount in the authList
            foreach (string item in orderList.Keys)
            {
                String eachOrderAmount = (orderList[item].Price * orderList[item].Number).ToString();
                /************************************************************************
                * Invoke Authorize Action
                ***********************************************************************/
                AuthorizeResponse authResponse = instance.AuthorizeAction(eachOrderAmount, indicator);
                if (authResponse == null)
                    throw new OffAmazonPaymentsServiceException("The response from Authorization Response request is null");
                Console.WriteLine("=======================================================");

                /************************************************************************
                * Check the authorization status unitl it is not "PENDING" any more
                 * GetAuthorizeDetails is contained in this method
                ***********************************************************************/
                instance.CheckAuthorizationStatus(authResponse);

                /************************************************************************
                * Invoke Capture Action
                ***********************************************************************/
                CaptureResponse captureResponse = instance.CaptureAction(authResponse, eachOrderAmount, indicator);
                if (captureResponse == null)
                    throw new OffAmazonPaymentsServiceException("The response from Caputre Response request is null");
                Console.WriteLine("=======================================================");

                /************************************************************************
                * Invoke GetCaptureDetails Action
                ***********************************************************************/
                if (instance.GetCaptureDetails(captureResponse) == null)
                    throw new OffAmazonPaymentsServiceException("The response from GetCaputreDetails Response request is null");
                Console.WriteLine("=======================================================");

                indicator++;
            }

            /************************************************************************
            * Invoke CloseOrderReference Action
            ***********************************************************************/
            if (instance.CloseOrderReference() == null)
                throw new OffAmazonPaymentsServiceException("The response from CloseOrderReference Response request is null");
        }
        private void RunSampleMultiShipments(string orderAmount, OffAmazonPaymentsServiceMultipleShipment instance)
        {
            /************************************************************************
            * Invoke Get Order Reference Details Action
            ***********************************************************************/
            GetOrderReferenceDetailsResponse getOrderDetails = instance.GetOrderReferenceDetails();
            if (getOrderDetails == null)
                throw new OffAmazonPaymentsServiceException("The response from GetOrderReference request is null");

            Address address = getOrderDetails.GetOrderReferenceDetailsResult.OrderReferenceDetails.Destination.PhysicalDestination;
            lblShipping.Text = "The shipping address is: " + address.City + "<br>" + address.StateOrRegion + "<br>" + address.PostalCode + "<br>";

            /************************************************************************
            * Invoke Set Order Reference Details Action
            ***********************************************************************/
            SetOrderReferenceDetailsResponse setOrderDetailsResponse = instance.SetOrderReferenceDetails(orderAmount);
            if (setOrderDetailsResponse == null)
                throw new OffAmazonPaymentsServiceException("The response from SetOrderReference request is null");

            /************************************************************************
            * Invoke Confirm Order Reference Action
            ***********************************************************************/
            if (instance.ConfirmOrderReferenceObject() == null)
                throw new OffAmazonPaymentsServiceException("The response from ConfirmOrderResponse request is null");

            //iterate the authoriztion amount in the authList
            for (int i = 0; i < numberOfShipment; i++)
            {
                String eachOrderAmount = amountEachShip[i].ToString();
                /************************************************************************
                * Invoke Authorize Action
                ***********************************************************************/
                AuthorizeResponse authResponse = instance.AuthorizeAction(eachOrderAmount, indicator);
                if (authResponse == null)
                    throw new OffAmazonPaymentsServiceException("The response from Authorization Response request is null");

                /************************************************************************
                * Wait for the notification from ipn.aspx page in a loop, then print the corresponding information
                ***********************************************************************/
                lblNotification.Text += WaitAndGetNotificationDetails(authResponse.AuthorizeResult.AuthorizationDetails.AmazonAuthorizationId + "_Authorize");

                instance.CheckAuthorizationStatus(authResponse);

                /************************************************************************
                * Invoke Capture Action
                ***********************************************************************/
                CaptureResponse captureResponse = instance.CaptureAction(authResponse, eachOrderAmount, indicator);
                if (captureResponse == null)
                    throw new OffAmazonPaymentsServiceException("The response from Caputre Response request is null");

                lblNotification.Text += WaitAndGetNotificationDetails(captureResponse.CaptureResult.CaptureDetails.AmazonCaptureId + "_Capture");

                /************************************************************************
                * Invoke Get Capture Details Action
                ***********************************************************************/
                if (instance.GetCaptureDetails(captureResponse) == null)
                    throw new OffAmazonPaymentsServiceException("The response from GetCaputreDetails Response request is null");

                indicator++;
            }

            /************************************************************************
            * Invoke Close Order Reference Action
            ***********************************************************************/
            if (instance.CloseOrderReference() == null)
                throw new OffAmazonPaymentsServiceException("The response from CloseOrderReference Response request is null");

            lblNotification.Text += WaitAndGetNotificationDetails(orderReferenceId + "_OrderReference");
        }