Ejemplo n.º 1
0
        private void startPayment()
        {
            checkCredentials();

            PaymentController.Instance.SinglestepEMV = cb_SinglestepEMV.Checked;
            bool           hasProduct     = cb_Product.Checked;
            bool           isRegular      = cb_Regular.Checked;
            PaymentContext paymentContext = isRegular ? new RegularPaymentContext() : new PaymentContext();

            paymentContext.Amount       = decimal.Parse(edt_Amount.Text);
            paymentContext.Currency     = rb_RUB.Checked ? Currency.RUB : Currency.VND;
            paymentContext.Description  = edt_Description.Text;
            paymentContext.ReceiptEmail = edt_Email.Text;
            paymentContext.ReceiptPhone = edt_Phone.Text;
            paymentContext.ExtID        = ExtID;


            if (cb_Purchases.Checked)
            {
                List <Purchase> purchases = dlgPurchases.Purchases;

                if (purchases == null)
                {
                    MessageBox.Show("Couldn't parse purchases!");
                }
                else
                {
                    foreach (Purchase p in purchases)
                    {
                        paymentContext.PutPurchase(p);
                    }
                }
            }

            if (cb_Tags.Checked)
            {
                var tags = dlgTags.Tags;

                if (tags == null)
                {
                    MessageBox.Show("Couldn't parse tags!");
                }
                else
                {
                    foreach (var tag in tags)
                    {
                        paymentContext.PutInvoiceTag(tag.Key, tag.Value);
                    }
                }
            }

            if (rb_Card.Checked)
            {
                paymentContext.Method = PaymentMethod.Card;
            }
            else if (rb_Cash.Checked)
            {
                paymentContext.Method = PaymentMethod.Cash;
                decimal cashGot         = paymentContext.Amount;
                string  s_AmountCashGot = Utils.ShowDialog("Cash got", cashGot.ToString("F2"));
                if (s_AmountCashGot == null || !decimal.TryParse(s_AmountCashGot, out cashGot))
                {
                    return;
                }
                paymentContext.AmountCashGot = cashGot;
            }
            else if (rb_Credit.Checked)
            {
                paymentContext.Method = PaymentMethod.Credit;
            }
            else if (rb_Link.Checked)
            {
                paymentContext.Method = PaymentMethod.Other;
            }
            else if (rb_Outer.Checked)
            {
                paymentContext.Method = PaymentMethod.OuterCard;
            }
            else if (rb_LinkedCard.Checked)
            {
                APIReadLinkedCardsResult result = PaymentController.Instance.GetLinkedCards();
                if (result.ErrorCode == 0)
                {
                    m_LinkedCards = result.LinkedCards;
                }
                paymentContext.Method = PaymentMethod.LinkedCard;
                if (m_LinkedCards != null && m_LinkedCards.Count > 0)
                {
                    int selectedCardIndex = Utils.ShowDialog(m_LinkedCards.ConvertAll(c => c.Alias), "Select card");
                    if (selectedCardIndex >= 0)
                    {
                        var selectedCard = m_LinkedCards[selectedCardIndex];
                        log("CARD " + selectedCard.ID + " ALIAS " + selectedCard.Alias);
                        paymentContext.LinkedCardID = selectedCard.ID;
                    }
                    else
                    {
                        log("ERROR : CANCEL SELECT CARD");
                        return;
                    }
                }
                else
                {
                    log("ERROR : NO LINKED CARDS");
                    return;
                }
            }

            string path = edt_ImageFilePath.Text;

            if (!string.IsNullOrEmpty(path))
            {
                if (File.Exists(path))
                {
                    try
                    {
                        paymentContext.Image = File.ReadAllBytes(path);
                    }
                    catch (Exception e)
                    {
                        log("ERROR : CANT READ IMAGE");
                        return;
                    }
                }
            }

            /*
             * if (true)
             * {
             *  paymentContext.PaymentProductCode = "TELE2";
             *  var paymentProductTextData = new Dictionary<string, string>(3);
             *  paymentProductTextData.Add("PHONE_NUMBER", edt_Field1.Text);
             *  paymentProductTextData.Add("AUTOPAY", "0");
             *  paymentProductTextData.Add("ReceiptEmail", "*****@*****.**");
             *  paymentContext.PaymentProductTextDictionary = paymentProductTextData;
             * }
             */


            if (hasProduct)
            {
                if (dlgProduct.CurrentProduct.Apply == PaymentProduct.PaymentType.None)
                {
                    log("ERROR : invalid product apply");
                    return;
                }
                else if (isRegular)
                {
                    if (dlgProduct.CurrentProduct.Apply == PaymentProduct.PaymentType.Payment)
                    {
                        log("ERROR : invalid product apply");
                        return;
                    }
                }
                else
                {
                    if (dlgProduct.CurrentProduct.Apply == PaymentProduct.PaymentType.Recurrent)
                    {
                        log("ERROR : invalid product apply");
                        return;
                    }
                }


                paymentContext.PaymentProductCode            = dlgProduct.CurrentProduct.Code;
                paymentContext.PaymentProductTextDictionary  = dlgProduct.TextValues;
                paymentContext.PaymentProductImageDictionary = dlgProduct.ImageValues;
                if (dlgProduct.PreparedAmount != null)
                {
                    edt_Amount.Text       = dlgProduct.PreparedAmount.ToString();
                    paymentContext.Amount = dlgProduct.PreparedAmount.Value;
                    log("Amount was changed according to product");
                }
                var email = dlgProduct.ReceiptEmail;
                var phone = dlgProduct.ReceiptPhone;
                if (email != null)
                {
                    edt_Email.Text = email;
                    paymentContext.ReceiptEmail = email;
                    log("Receipt notification data was changed according to product");
                }
                if (phone != null)
                {
                    edt_Phone.Text = phone;
                    paymentContext.ReceiptPhone = phone;
                    log("Receipt notification data was changed according to product");
                }
            }

            if (isRegular)
            {
                RegularPaymentContext regPaymentContext = paymentContext as RegularPaymentContext;

                if (string.IsNullOrEmpty(regPaymentContext.ReceiptEmail) && string.IsNullOrEmpty(regPaymentContext.ReceiptPhone))
                {
                    log("ERROR : email or phone required");
                    return;
                }

                if (hasProduct && dlgProduct.CurrentProduct.RecurrentMode == PaymentProduct.ProductRecurrentMode.Managed)
                {
                    regPaymentContext.Managed           = true;
                    regPaymentContext.PaymentRepeatType = RepeatType.DelayedOnce;
                    regPaymentContext.PaymentEndType    = EndType.ByQuantity;
                    regPaymentContext.RepeatCount       = 1;
                    regPaymentContext.StartDate         = DateTime.Now;
                    regPaymentContext.EndDate           = DateTime.Now;
                    regPaymentContext.ArbitraryDays     = null;
                    regPaymentContext.Day       = 0;
                    regPaymentContext.DayOfWeek = 0;
                    regPaymentContext.Hour      = 0;
                    regPaymentContext.Minute    = 0;
                    regPaymentContext.Month     = 0;
                    log("Recurrent payment is managed by host!");
                }
                else
                {
                    dlgRegular.SetContextValues(regPaymentContext);
                }
            }

            if (paymentContext.Method != null)
            {
                if (m_AuthResult != null && m_AuthResult.ErrorCode == 0 && m_AuthResult.Account != null)
                {
                    var acquirersByMethod = m_AuthResult.Account.AcquirersByMethods;
                    if (acquirersByMethod != null && acquirersByMethod.Count != 0)
                    {
                        if (acquirersByMethod.ContainsKey((PaymentMethod)paymentContext.Method))
                        {
                            var acquirers = acquirersByMethod[(PaymentMethod)paymentContext.Method];
                            if (acquirers.Count == 1)
                            {
                                paymentContext.AcquirerCode = acquirers.First().Key;
                            }
                            else
                            {
                                int index = Utils.ShowDialog(acquirers.Values.ToList(), "Select acquirer");
                                paymentContext.AcquirerCode = acquirers.Keys.ToList()[Math.Max(0, index)];
                            }
                        }
                    }
                }
            }

            try
            {
                m_PaymentController.StartPayment(paymentContext);
                log(DIVIDER);
                log(string.Format("STARTING NEW PAYMENT : {0}{1}", Environment.NewLine, JsonConvert.SerializeObject(paymentContext, Formatting.Indented)));
            }
            catch (InvalidOperationException e)
            {
                log(string.Format("ERROR : {0}", e.Message));
                return;
            }
        }