Пример #1
0
        /// <summary>
        /// Import into the system PPCs previously generated.  After importing, the status will be inactive
        /// </summary>
        /// <param name="context">DBContext to be used</param>
        /// <param name="streamReader">Source stream to be used as input</param>
        static public void Import(IPTV2Entities context, StreamReader streamReader)
        {
            var firstLine = streamReader.ReadLine();
            var firstLineSplit = firstLine.Split('|');
            var ppcType = context.PpcTypes.Find(Convert.ToInt32(firstLineSplit[0]));

            if (ppcType != null)
            {
                while (!streamReader.EndOfStream)
                {
                    var ppcLine = streamReader.ReadLine();
                    var ppcLineSplit = ppcLine.Split('|');
                    if (firstLineSplit[0] != ppcLineSplit[0])
                    {
                        throw new Exception("PPC file inconsistent.");
                    }

                    if (ppcLineSplit[1].StartsWith(ppcType.PpcProductCode))
                    {
                        if (ppcType is ReloadPpcType)
                        {
                            var reloadPpc = new ReloadPpc
                            {
                                PpcType = ppcType,
                                SerialNumber = ppcLineSplit[1],
                                Pin = ppcLineSplit[2],
                                ExpirationDate = DateTime.Parse(ppcLineSplit[3]),
                                Amount = ppcType.Amount,
                                Currency = ppcType.Currency
                            };
                            context.Ppcs.Add(reloadPpc);
                        }
                        else if (ppcType is SubscriptionPpcType)
                        {
                            var subscriptionPpcType = (SubscriptionPpcType)ppcType;
                            var subscriptionPpc = new SubscriptionPpc
                            {
                                PpcType = subscriptionPpcType,
                                SerialNumber = ppcLineSplit[1],
                                Pin = ppcLineSplit[2],
                                ExpirationDate = DateTime.Parse(ppcLineSplit[3]),
                                Amount = subscriptionPpcType.Amount,
                                Currency = subscriptionPpcType.Currency,
                                Duration = subscriptionPpcType.Duration,
                                DurationType = subscriptionPpcType.DurationType,
                                ProductId = subscriptionPpcType.ProductId
                            };
                            context.Ppcs.Add(subscriptionPpc);
                        }
                        else
                        {
                            throw new Exception("invalid PPC type");
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid PPC file.");
                    }
                }
                context.SaveChanges();
            }
            else
            {
                throw new Exception("invalid PPC type.");
            }
        }
Пример #2
0
        private static ErrorCodes PayViaPrepaidCard()
        {
            try
            {
                // setup test variables
                var context = new IPTV2Entities();
                int offeringId = 2; // TFC.tv
                var currencyCode = "USD";
                int productId = 2; // TFC.tv Premium 10 Days
                SubscriptionProductType subscriptionType = SubscriptionProductType.Package;
                System.Guid userId = new System.Guid("896CA57E-67BC-4A28-86EA-A52E5426B693"); // albin's account

                string serial = "1";
                string pin = "72656";

                var testPPC = context.Ppcs.FirstOrDefault(p => p.SerialNumber == serial);
                if (testPPC != null)
                {
                    context.Ppcs.Remove(testPPC);
                }

                var thisUser = context.Users.FirstOrDefault(u => u.UserId == userId);

                foreach (var t in thisUser.Transactions)
                {
                    if (t is PpcPaymentTransaction)
                    {
                        thisUser.Transactions.Remove(t);
                    }
                }

                context.SaveChanges();

                var newPpc = new SubscriptionPpc
                {
                    SerialNumber = serial,
                    Pin = pin,
                    Amount = 4.99m,
                    Currency = "USD",
                    Duration = 10,
                    DurationType = "d",
                    PpcProductId = 523,
                    ProductId = productId,
                    ExpirationDate = DateTime.Now.AddYears(1)
                };
                context.Ppcs.Add(newPpc);
                context.SaveChanges();
                // end setup test variables

                DateTime registDt = DateTime.Now;
                User user = context.Users.FirstOrDefault(u => u.UserId == userId);
                Offering offering = context.Offerings.FirstOrDefault(o => o.OfferingId == offeringId);
                Product product = context.Products.FirstOrDefault(p => p.ProductId == productId);
                ProductPrice priceOfProduct = product.ProductPrices.FirstOrDefault(p => p.CurrencyCode == currencyCode);
                Ppc ppc = context.Ppcs.FirstOrDefault(p => p.SerialNumber == serial);

                if (ppc == null) //Serial does not exist
                    return ErrorCodes.IsInvalidPpc;
                if (!(ppc.SerialNumber == serial && ppc.Pin == pin)) //Invalid serial/pin combination
                    return ErrorCodes.IsInvalidCombinationPpc;
                if (ppc.UserId != null) // Ppc has already been used
                    return ErrorCodes.IsUsedPpc;
                if (!(ppc is SubscriptionPpc)) // Ppc is not of type Subscription
                    return ErrorCodes.IsReloadPpc;
                if (registDt > ppc.ExpirationDate) // Ppc is expired
                    return ErrorCodes.IsExpiredPpc;
                SubscriptionPpc sPpc = (SubscriptionPpc)ppc;
                if (sPpc.ProductId != product.ProductId) // Ppc product is invalid.
                    return ErrorCodes.IsProductIdInvalidPpc;
                if (ppc.Currency.Trim() != currencyCode) // Ppc not valid in your country
                    return ErrorCodes.IsNotValidInCountryPpc;
                if (ppc.Amount != priceOfProduct.Amount) // Ppc is of invalid amount
                    return ErrorCodes.IsNotValidAmountPpc;

                Purchase purchase = new Purchase()
                {
                    Date = registDt,
                    Remarks = "Payment via Ppc"
                };
                user.Purchases.Add(purchase);

                PurchaseItem item = new PurchaseItem()
                {
                    RecipientUserId = userId,
                    ProductId = product.ProductId,
                    Price = priceOfProduct.Amount,
                    Currency = priceOfProduct.Currency.Code,
                    Remarks = product.Name
                };
                purchase.PurchaseItems.Add(item);

                PpcPaymentTransaction transaction = new PpcPaymentTransaction()
                {
                    Currency = priceOfProduct.Currency.Code,
                    Reference = serial.ToUpper(),
                    Amount = purchase.PurchaseItems.Sum(p => p.Price),
                    Product = product,
                    Purchase = purchase,
                    // SubscriptionPpc = sPpc,
                    SubscriptionPpcId = ppc.PpcId,
                    Date = registDt
                };

                user.Transactions.Add(transaction);
                // purchase.PaymentTransaction.Add(transaction);
                // product.PpcPaymentTransactions.Add(transaction);

                //item.SubscriptionProduct = (SubscriptionProduct)product;

                //switch (subscriptionType)
                //{
                //    case SubscriptionProductType.Show:

                //        break;
                //    case SubscriptionProductType.Package:

                //        if (product is PackageSubscriptionProduct)
                //        {
                //            registDt = registDt.AddMinutes(1);
                //            // DateAndTime.DateAdd(DateInterval.Minute, 1, registDt);
                //            PackageSubscriptionProduct subscription = (PackageSubscriptionProduct)product;

                //            //EntitlementRequest request = new EntitlementRequest()
                //            //{
                //            //    DateRequested = registDt,
                //            //    EndDate = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
                //            //    Product = product,
                //            //    Source = String.Format("{0}-{1}", "Ppc", ppc.PpcId.ToString()),
                //            //    ReferenceId = purchase.PurchaseId.ToString()
                //            //};
                //            //user.EntitlementRequests.Add(request);

                //            foreach (var package in subscription.Packages)
                //            {
                //                PackageEntitlement currentPackage = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == package.PackageId);
                //                DateTime endDate = registDt;
                //                if (currentPackage != null)
                //                {
                //                    currentPackage.EndDate = getEntitlementEndDate(subscription.Duration, subscription.DurationType, ((currentPackage.EndDate > registDt) ? currentPackage.EndDate : registDt));
                //                    endDate = currentPackage.EndDate;
                //                }
                //                else
                //                {
                //                    PackageEntitlement entitlement = new PackageEntitlement()
                //                    {
                //                        EndDate = getEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
                //                        Package = (Package)package.Package,
                //                        OfferingId = offeringId
                //                    };

                //                    user.PackageEntitlements.Add(entitlement);
                //                }

                //                EntitlementRequest request = new EntitlementRequest()
                //                {
                //                    DateRequested = registDt,
                //                    EndDate = endDate,
                //                    Product = product,
                //                    Source = String.Format("{0}-{1}", "Ppc", ppc.PpcId.ToString()),
                //                    ReferenceId = purchase.PurchaseId.ToString()
                //                };
                //                user.EntitlementRequests.Add(request);
                //            }
                //        }
                //        break;

                //    case SubscriptionProductType.Episode: break;
                //}

                ////update the Ppc
                //ppc.UserId = userId;
                //ppc.UsedDate = registDt;

                if (context.SaveChanges() > 0)
                {
                    return ErrorCodes.Success;
                }
                return ErrorCodes.EntityUpdateError;
            }

            catch (Exception e) { Debug.WriteLine(e.InnerException); throw; }
        }