예제 #1
0
        private async void Approve_Click(object sender, RoutedEventArgs e)
        {
            if (!IsAllFieldsFilled())
            {
                MessageBox.Show(AppResources.Msg_AllFieldsRequired, AppResources.MsgHdr_AllFieldsRequired, MessageBoxButton.OK);
                return;
            }

            gr_busy.Visibility = Visibility.Visible;

            var card = new Card
            {
                Number   = CardNumber.Replace(" ", ""),
                ExpMonth = ExpMonth,
                ExpYear  = ExpYear,
                Cvc      = CVC,
                Name     = NameOnCard
            };

            if (IsAddressRequired)
            {
                card.AddressLine1 = BillingAddress;
                card.AddressZip   = ZipCode;
                card.AddressState = State;
            }

            var api = new PaymentsApi();

            try
            {
                cardToken = await api.CreateCardToken(card, publicKey);
            }
            catch (Exception ex)
            {
                RaiseEventHandler(false, ex);
                return;
            }

            //TODO: just for testing, remove befor rls
            MessageBox.Show(await Task.Factory.StartNew(() => JsonConvert.SerializeObject(cardToken)), "response from simplify", MessageBoxButton.OK);

            var request = new PayNowRequest
            {
                TokenId  = cardToken.Id,
                Amount   = Amount,
                ItemName = ItemName
            };

            var baseLength  = callbackUrl.LastIndexOf('/') + 1;
            var baseAddress = callbackUrl.Substring(0, baseLength);
            var requestUri  = callbackUrl.Substring(baseLength);

            var settings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            var requestBody = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(request, settings));

            try
            {
                using (var client = new HttpClient())
                {
                    client.Timeout     = new TimeSpan(0, 0, TIMEOUT);
                    client.BaseAddress = new Uri(baseAddress);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.UserAgent.Clear();
                    client.DefaultRequestHeaders.UserAgent.ParseAdd("Simplify-PayNowUC-for-WP");
                    using (var response = await client.PostAsync(requestUri, new StringContent(requestBody, Encoding.UTF8, "application/json")))
                    {
                        RaiseEventHandler(true, response);
                    }
                }
            }
            catch (TaskCanceledException)
            {
                RaiseEventHandler(false, new TimeoutException());
            }
            catch (Exception ex)
            {
                RaiseEventHandler(false, ex);
            }
        }