Esempio n. 1
0
        private TokenRequestModel PaymentLogToTokenRequestModel(PayRequestBindingModel model, User user, Merchant merchant)
        {
            TokenRequestModel trModel = new TokenRequestModel()
            {
                msgId                         = 7,
                deviceID                      = user.PhoneDeviceToken,
                tid                           = model.tid,
                gatewayId                     = 0,
                amount                        = model.amount,
                countryCode                   = model.countryCode,
                currencyCode                  = model.currencyCode,
                ttime                         = model.ttime,
                vid                           = model.vid,
                shippingInfo                  = model.shippingInfo,
                supportedNetwork              = merchant.SupportNetwork,
                merchantIdentifier            = model.merchantIdentifier,
                merchantCapabilities          = merchant.MerchantCapabilities,
                merchantName                  = merchant.MerchantName,
                lineItems                     = model.lineItems,
                paymentMethodTokenizationType = merchant.PaymnetMethodTokenizationType,
                publicKey                     = model.publicKey
            };

            return(trModel);
        }
        public IHttpActionResult PayRequest(PayRequestBindingModel model)
        {
            string slog = string.Format("PayRequest Begin model: {0}", JsonConvert.SerializeObject(model));

            logger.Debug(slog);

            PayResponseBindingModel pr = new PayResponseBindingModel()
            {
                msgId  = 2,
                tid    = model.tid,
                token  = "",
                status = 2
            };

            if (!ModelState.IsValid)
            {
                logger.Debug("PayRequest ModelState is invalid");
                pr.status = -1;
                return(Ok(pr));
            }


            ///////////////////////////////////////
            // Check for valid vid or email
            ///////////////////////////////////////
            User user = userService.GetUserByEmail(model.vid);

            if (user == null)
            {
                // Invalid user email
                pr.status = 1;
                return(Ok(pr));
            }
            else if (user != null && user.EmailConfirmed == false)
            {
                // User hasn't confirmed email.
                pr.status = 1;
                return(Ok(pr));
            }

            ///////////////////////////////////////
            // Check for valid merchant
            ///////////////////////////////////////
            Merchant merchant = merchantService.GetMerchantByAlias(model.merchantIdentifier);

            if (merchant == null)
            {
                // Invalid merchant
                pr.status = 2;
                return(Ok(pr));
            }

            var pay = new PaymentLog()
            {
                TId                           = model.tid.ToString(),
                TTime                         = model.ttime,
                ShippingInfo                  = model.shippingInfo,
                Amount                        = model.amount,
                CountryCode                   = model.countryCode,
                CurrencyCode                  = model.currencyCode,
                MerchantId                    = model.merchantIdentifier,
                MechantName                   = model.merchantName,
                LineItems                     = model.lineItems,
                PublicKey                     = model.publicKey,
                Email                         = model.vid,
                MerchantCapability            = merchant.MerchantCapabilities,
                SupportedNetwork              = merchant.SupportNetwork,
                PaymentMethodTokenizationType = merchant.PaymnetMethodTokenizationType,
                CodeCheck                     = string.Empty,
                CodeCheckCount                = 0,
                IsCodeCheckValidated          = false
            };

            string codeCheck           = GetRamdomCodeCheck();
            bool   correctShippingInfo = paymentService.IsValidUserShippingInfo(user, model.shippingInfo);

            if (!correctShippingInfo)
            {
                pay.CodeCheck = codeCheck;
            }

            ///////////////////////////////////////
            // Save payment to database PaymentLog table
            ///////////////////////////////////////
            PaymentLog checkLog = paymentService.GetPaymentByTid(pay.TId);

            if (checkLog == null)
            {
                // Save pay to the database
                string val = paymentService.RequestPay(pay);
                if (string.IsNullOrEmpty(val))
                {
                    //ModelState.AddModelError("", string.Format("Error PayRequest"));
                    //return BadRequest(ModelState);
                    logger.Debug("PayRequest can't add to the database PaymentLog table");
                    pr.status = -1;
                    return(Ok(pr));
                }
            }
            //else
            //{
            //    //ModelState.AddModelError("", string.Format("Error tid is already exist"));

            //    return BadRequest(ModelState);
            //}

            ///////////////////////////////////////
            // Check to make sure Shipping Info is to the vid or email user
            ///////////////////////////////////////
            if (!correctShippingInfo)
            {
                ///////////////////////////////////////
                // Insert key to PayTokenManager PaymentRequest
                ///////////////////////////////////////
                PayTokenManager.Instance.SavePaymentLog(model.tid, pay);

                // Send CodeCheck back to the browser
                CodeCommandBindingModel codeCommand = new CodeCommandBindingModel()
                {
                    msgId = 5,
                    tid   = model.tid,
                    code  = codeCheck
                };
                return(Ok(codeCommand));
            }

            try
            {
                ///////////////////////////////////////
                // Get token value from PayTokenManger
                ///////////////////////////////////////

                TokenRequestModel trModel = PaymentLogToTokenRequestModel(pay, user, merchant);
                string            token   = SendMobileTokenRequest(model.tid, user.Id, trModel);
                if (!string.IsNullOrEmpty(token))
                {
                    pr.status = 0;
                }
                else
                {
                    pr.status = 4;
                }
                pr.token = token;


                /*
                 * ///////////////////////////////////////
                 * // Notify the mobile applet
                 * ///////////////////////////////////////
                 * TokenRequestModel trModel = PaymentLogToTokenRequestModel(model, user, merchant);
                 * notificationService.TokenRequest(Global.NotificationHubPNS.APNS, user.Id, trModel);
                 *
                 * ///////////////////////////////////////
                 * // Insert key to PayTokenManager
                 * ///////////////////////////////////////
                 * PayTokenManager.Instance.Save(model.tid, string.Empty);
                 *
                 * ///////////////////////////////////////
                 * // Waiting for TokenResponse from applet to update the PayTokenManager
                 * ///////////////////////////////////////
                 *
                 * int timerPayRequest = 10;
                 * int.TryParse(ConfigurationManager.AppSettings["TimerPayRequest"].ToString(), out timerPayRequest);
                 *
                 * timerPayRequest *= 1000;
                 *
                 * int tCount = 30;
                 * int interval = timerPayRequest / tCount;
                 * int timer = 0;
                 * while (timer < timerPayRequest)
                 * {
                 *  Thread.Sleep(interval);
                 *  timer += interval;
                 *  string tok;
                 *  PayTokenManager.Instance.Get(model.tid, out tok);
                 *  if (!string.IsNullOrEmpty(tok))
                 *  {
                 *      break;
                 *  }
                 * }
                 *
                 * ///////////////////////////////////////
                 * // Get token value from PayTokenManger
                 * ///////////////////////////////////////
                 * string token = "";
                 * if (PayTokenManager.Instance.Get(model.tid, out token) && !string.IsNullOrEmpty(token))
                 * {
                 *  pr.status = 0;
                 *  PayTokenManager.Instance.Delete(model.tid);
                 * }
                 * else
                 * {
                 *  pr.status = 4;
                 * }
                 * pr.token = token;
                 */

                if (pr.status == 0)
                {
                    paymentService.SavePaymentRequestConfimred(model.tid.ToString());
                }
                else
                {
                    paymentService.SavePaymentRequestRejected(model.tid.ToString());
                }
                PayTokenManager.Instance.Delete(model.tid);
            }
            catch (MessagingException e)
            {
                logger.Error("PayRequest MessageException", e);
                //ReturnGoneIfHubResponseIsGone(e);
                pr.status = 4;
                return(Ok(pr));
            }
            catch (Exception ex)
            {
                logger.Error("PayRequest", ex);
                pr.status = 4;
                return(Ok(pr));
            }

            slog = string.Format("PayRequest Returns: {0}", JsonConvert.SerializeObject(pr));
            logger.Debug(slog);

            return(Ok(pr));
        }
        public IHttpActionResult PayRequest(PayRequestBindingModel model)
        {
            PayResponseBindingModel pr = new PayResponseBindingModel()
            {
                msgId  = 2,
                tid    = model.tid,
                token  = "0",
                status = 1
            };


            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            User user = userService.GetUserByEmail(model.vid);

            if (user == null)
            {
                // Invalid user email
                pr.status = 1;
                return(Ok(pr));
            }

            Merchant merchant = merchantService.GetMerchantByAlias(model.merchantIdentifier);

            if (merchant == null)
            {
                // Invalid merchant
                pr.status = 2;
                return(Ok(pr));
            }

            var pay = new PaymentLog()
            {
                TId                           = model.tid.ToString(),
                TTime                         = DateTime.Parse(model.ttime),
                ShippingInfo                  = model.shippingInfo,
                Amount                        = decimal.Parse(model.amount),
                CountryCode                   = model.countryCode,
                CurrencyCode                  = model.currencyCode,
                MerchantId                    = model.merchantIdentifier,
                MechantName                   = model.merchantName,
                LineItems                     = model.lineItems,
                PublicKey                     = model.publicKey,
                Email                         = model.vid,
                MerchantCapability            = merchant.MerchantCapabilities,
                SupportedNetwork              = merchant.SupportNetwork,
                PaymentMethodTokenizationType = merchant.PaymnetMethodTokenizationType
            };

            string val = paymentService.RequestPay(pay);

            if (string.IsNullOrEmpty(val))
            {
                ModelState.AddModelError("", string.Format("Error PayRequest"));
                return(BadRequest(ModelState));
            }

            TokenRequestModel trModel = PaymentLogToTokenRequestModel(pay);

            notificationService.TokenRequest(Global.NotificationHubPNS.APNS, trModel);

            //Uri locationHeader = new Uri(Url.Link("GetPaymentById", new { id = pay.Id }));
            //return Created(locationHeader, ThePaymentModelFactory.Create(pay));

            return(Ok(pr));
        }