예제 #1
0
        //public static ErrorCodes PayViaPayPal(IPTV2Entities context, System.Guid userId, int productId, SubscriptionProductType subscriptionType, string TransactionID)
        //{
        //    try
        //    {
        //        //var context = new IPTV2Entities();
        //        DateTime registDt = DateTime.Now;
        //        User user = context.Users.FirstOrDefault(u => u.UserId == userId);
        //        Offering offering = context.Offerings.FirstOrDefault(o => o.OfferingId == GlobalConfig.offeringId);
        //        Product product = context.Products.FirstOrDefault(p => p.ProductId == productId);
        //        ProductPrice priceOfProduct = product.ProductPrices.FirstOrDefault(p => p.CurrencyCode == MyUtility.GetCurrencyOrDefault(user.CountryCode));

        //        Transaction ppt = context.Transactions.FirstOrDefault(t => t.Reference == TransactionID);
        //        if (ppt != null)
        //            return ErrorCodes.IsProcessedPayPalTransaction;

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

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

        //        PaypalPaymentTransaction transaction = new PaypalPaymentTransaction()
        //        {
        //            Currency = priceOfProduct.CurrencyCode,
        //            Reference = TransactionID,
        //            Amount = purchase.PurchaseItems.Sum(p => p.Price),
        //            User = user,
        //            Date = registDt
        //        };

        //        purchase.PaymentTransaction.Add(transaction);

        //        item.SubscriptionProduct = (SubscriptionProduct)product;

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

        //                break;
        //            case SubscriptionProductType.Package:

        //                if (product is PackageSubscriptionProduct)
        //                {
        //                    // DateAndTime.DateAdd(DateInterval.Minute, 1, registDt);
        //                    registDt = registDt.AddMinutes(1);
        //                    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}", "Paypal", TransactionID),
        //                    //    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 = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, ((currentPackage.EndDate > registDt) ? currentPackage.EndDate : registDt));
        //                            endDate = currentPackage.EndDate;
        //                        }
        //                        else
        //                        {
        //                            PackageEntitlement entitlement = new PackageEntitlement()
        //                            {
        //                                EndDate = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
        //                                Package = (Package)package.Package,
        //                                OfferingId = GlobalConfig.offeringId
        //                            };

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

        //                        EntitlementRequest request = new EntitlementRequest()
        //                        {
        //                            DateRequested = registDt,
        //                            EndDate = endDate,
        //                            Product = product,
        //                            Source = String.Format("{0}-{1}", "Paypal", TransactionID),
        //                            ReferenceId = TransactionID
        //                        };
        //                        user.EntitlementRequests.Add(request);
        //                    }
        //                }
        //                break;

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

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

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

        public static ErrorCodes PayViaPayPal(IPTV2Entities context, System.Guid userId, int productId, SubscriptionProductType subscriptionType, string TransactionID, System.Guid recipientUserId, int? cpId, bool IsAutoPaymentRenewal = false)
        {
            try
            {
                //email metadata
                string packageName = String.Empty;
                DateTime endDt = DateTime.Now;
                bool isExtension = false;

                bool isGift = false;
                if (userId != recipientUserId)
                    isGift = true;

                //var context = new IPTV2Entities();
                DateTime registDt = DateTime.Now;
                User user = context.Users.FirstOrDefault(u => u.UserId == userId);
                User recipient = context.Users.FirstOrDefault(u => u.UserId == recipientUserId);
                Offering offering = context.Offerings.FirstOrDefault(o => o.OfferingId == GlobalConfig.offeringId);
                Product product = context.Products.FirstOrDefault(p => p.ProductId == productId);
                ProductPrice priceOfProduct = product.ProductPrices.FirstOrDefault(p => p.CurrencyCode == MyUtility.GetCurrencyOrDefault(user.CountryCode));
                if (priceOfProduct == null)
                    priceOfProduct = product.ProductPrices.FirstOrDefault(p => p.CurrencyCode == GlobalConfig.DefaultCurrency);

                if (!priceOfProduct.Currency.IsPayPalSupported)
                    priceOfProduct = product.ProductPrices.FirstOrDefault(p => p.CurrencyCode == GlobalConfig.DefaultCurrency);

                //Check if this is an upgrade
                if (cpId != null && cpId != 0)
                {
                    bool isUpgradeSuccess = Upgrade(context, userId, product, recipientUserId, cpId);
                }

                if (GlobalConfig.UsePayPalIPNLog)
                {
                    if (context.PaypalIPNLogs.Count(t => String.Compare(t.UniqueTransactionId, TransactionID, true) == 0) > 0)
                        return ErrorCodes.IsProcessedPayPalTransaction;
                }
                else
                {
                    Transaction ppt = context.Transactions.FirstOrDefault(t => String.Compare(t.Reference, TransactionID, true) == 0);
                    if (ppt != null)
                        return ErrorCodes.IsProcessedPayPalTransaction;
                }

                //Transaction ppt = context.Transactions.FirstOrDefault(t => String.Compare(t.Reference, TransactionID, true) == 0);
                //if (ppt != null)
                //    return ErrorCodes.IsProcessedPayPalTransaction;

                /***************************** Check for Early Bird Promo *******************************/
                bool IsEarlyBird = false;
                int FreeTrialConvertedDays = 0;
                Product earlyBirdProduct = null;
                ProductPrice earlyBirdPriceOfProduct = null;

                //REMOVE THIS LINE ON RELEASE OF EARLY BIRD.
                //if (false)
                if (GlobalConfig.IsEarlyBirdEnabled)
                {
                    if (user.IsFirstTimeSubscriber(offering, true, MyUtility.StringToIntList(GlobalConfig.FreeTrialPackageIds), context))
                    {
                        FreeTrialConvertedDays = GetConvertedDaysFromFreeTrial(user);

                        earlyBirdProduct = context.Products.FirstOrDefault(p => p.ProductId == GlobalConfig.FreeTrialEarlyBirdProductId);
                        earlyBirdPriceOfProduct = earlyBirdProduct.ProductPrices.FirstOrDefault(p => p.CurrencyCode == GlobalConfig.TrialCurrency);

                        Purchase earlyBirdPurchase = CreatePurchase(registDt, "Free Trial Early Bird Promo");
                        user.Purchases.Add(earlyBirdPurchase);

                        PurchaseItem earlyBirdItem = CreatePurchaseItem(recipientUserId, earlyBirdProduct, earlyBirdPriceOfProduct);

                        DateTime earlyBirdEndDate = registDt.AddDays(FreeTrialConvertedDays);
                        EntitlementRequest earlyBirdRequest = CreateEntitlementRequest(registDt, earlyBirdEndDate, earlyBirdProduct, String.Format("EBP-{0}-{1}", "Paypal", TransactionID), String.Format("EBP-{0}", TransactionID), registDt);
                        PackageSubscriptionProduct earlyBirdSubscription = (PackageSubscriptionProduct)earlyBirdProduct;
                        var earlyBirdPackage = earlyBirdSubscription.Packages.First();
                        PackageEntitlement EarlyBirdEntitlement = CreatePackageEntitlement(earlyBirdRequest, earlyBirdSubscription, earlyBirdPackage, registDt);


                        earlyBirdItem.EntitlementRequest = earlyBirdRequest;

                        earlyBirdPurchase.PurchaseItems.Add(earlyBirdItem);
                        recipient.EntitlementRequests.Add(earlyBirdRequest);

                        EarlyBirdEntitlement.EndDate = earlyBirdEndDate;
                        EarlyBirdEntitlement.LatestEntitlementRequest = earlyBirdRequest;
                        recipient.PackageEntitlements.Add(EarlyBirdEntitlement);

                        PaypalPaymentTransaction earlyBirdTransaction = new PaypalPaymentTransaction()
                        {
                            Currency = earlyBirdPriceOfProduct.CurrencyCode,
                            Reference = String.Format("EBP-{0}", TransactionID),
                            Amount = earlyBirdPurchase.PurchaseItems.Sum(p => p.Price),
                            User = user,
                            Date = registDt,
                            OfferingId = GlobalConfig.offeringId
                        };


                        earlyBirdPurchase.PaymentTransaction.Add(earlyBirdTransaction);
                        user.Transactions.Add(earlyBirdTransaction);

                        IsEarlyBird = true;

                    }
                }
                /************************************ END OF EARLY BIRD PROMO *************************************/



                Purchase purchase = CreatePurchase(registDt, userId != recipientUserId ? "Gift via Paypal" : "Payment via Paypal");
                user.Purchases.Add(purchase);

                PurchaseItem item = CreatePurchaseItem(recipientUserId, product, priceOfProduct);
                purchase.PurchaseItems.Add(item);

                PaypalPaymentTransaction transaction = new PaypalPaymentTransaction()
                {
                    Currency = priceOfProduct.CurrencyCode,
                    Reference = IsAutoPaymentRenewal ? String.Format("{0} (Auto Payment Renewal)", TransactionID) : TransactionID,
                    Amount = purchase.PurchaseItems.Sum(p => p.Price),
                    User = user,
                    Date = registDt,
                    OfferingId = GlobalConfig.offeringId,
                    StatusId = GlobalConfig.Visible
                };

                var ipnLog = new PaypalIPNLog()
                {
                    User = user,
                    UniqueTransactionId = TransactionID,
                    TransactionDate = registDt
                };

                user.PaypalIPNLogs.Add(ipnLog);

                purchase.PaymentTransaction.Add(transaction);

                item.SubscriptionProduct = (SubscriptionProduct)product;
                string ProductNameBought = product.Description;

                switch (subscriptionType)
                {
                    case SubscriptionProductType.Show:
                        ShowSubscriptionProduct show_subscription = (ShowSubscriptionProduct)product;
                        ProductNameBought = show_subscription.Description;

                        /*** JAN 09 2012****/
                        bool isApplicableForEarlyBird = false;
                        if (IsEarlyBird)
                        {
                            var AlaCarteSubscriptionType = MyUtility.StringToIntList(GlobalConfig.FreeTrialAlaCarteSubscriptionTypes);
                            if (show_subscription.ALaCarteSubscriptionTypeId != null)
                                if (AlaCarteSubscriptionType.Contains((int)show_subscription.ALaCarteSubscriptionTypeId))
                                    isApplicableForEarlyBird = true;
                        }

                        foreach (var show in show_subscription.Categories)
                        {
                            ShowEntitlement currentShow = recipient.ShowEntitlements.FirstOrDefault(s => s.CategoryId == show.CategoryId);
                            DateTime endDate = registDt;
                            EntitlementRequest request = CreateEntitlementRequest(registDt, endDate, product, String.Format("{0}-{1}", "Paypal", TransactionID), TransactionID, registDt);
                            if (currentShow != null)
                            {
                                request.StartDate = currentShow.EndDate;
                                currentShow.EndDate = MyUtility.getEntitlementEndDate(show_subscription.Duration, show_subscription.DurationType, ((currentShow.EndDate > registDt) ? currentShow.EndDate : registDt));

                                /** JAN 09 2012 **/
                                if (IsEarlyBird && isApplicableForEarlyBird)
                                {
                                    currentShow.EndDate = currentShow.EndDate.AddDays(FreeTrialConvertedDays);
                                }

                                endDate = currentShow.EndDate;
                                currentShow.LatestEntitlementRequest = request;
                                request.EndDate = endDate;
                                endDt = endDate;
                                isExtension = true;
                            }
                            else
                            {
                                ShowEntitlement entitlement = CreateShowEntitlement(request, show_subscription, show, registDt);
                                request.EndDate = entitlement.EndDate;

                                /** JAN 09 2012 **/
                                if (IsEarlyBird && isApplicableForEarlyBird)
                                {
                                    entitlement.EndDate = entitlement.EndDate.AddDays(FreeTrialConvertedDays);
                                    request.EndDate = request.EndDate.AddDays(FreeTrialConvertedDays);
                                }

                                recipient.ShowEntitlements.Add(entitlement);
                                endDt = entitlement.EndDate;
                            }
                            recipient.EntitlementRequests.Add(request);
                            item.EntitlementRequest = request; //UPDATED: November 22, 2012
                        }
                        break;
                    case SubscriptionProductType.Package:

                        if (product is PackageSubscriptionProduct)
                        {
                            PackageSubscriptionProduct subscription = (PackageSubscriptionProduct)product;

                            foreach (var package in subscription.Packages)
                            {
                                packageName = package.Package.Description;
                                ProductNameBought = packageName;
                                PackageEntitlement currentPackage = recipient.PackageEntitlements.FirstOrDefault(p => p.PackageId == package.PackageId);
                                DateTime endDate = registDt;
                                EntitlementRequest request = CreateEntitlementRequest(registDt, endDate, product, String.Format("{0}-{1}", "Paypal", TransactionID), TransactionID, registDt);

                                if (currentPackage != null)
                                {
                                    request.StartDate = currentPackage.EndDate;
                                    currentPackage.EndDate = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, ((currentPackage.EndDate > registDt) ? currentPackage.EndDate : registDt));

                                    /** JAN 03 2012 **/
                                    if (IsEarlyBird)
                                    {
                                        currentPackage.EndDate = currentPackage.EndDate.AddDays(FreeTrialConvertedDays);
                                    }

                                    endDate = currentPackage.EndDate;
                                    currentPackage.LatestEntitlementRequest = request;
                                    request.EndDate = endDate;
                                    endDt = endDate;
                                    isExtension = true;
                                }
                                else
                                {
                                    PackageEntitlement entitlement = CreatePackageEntitlement(request, subscription, package, registDt);
                                    request.EndDate = entitlement.EndDate;

                                    /** JAN 03 2012 **/
                                    if (IsEarlyBird)
                                    {
                                        entitlement.EndDate = entitlement.EndDate.AddDays(FreeTrialConvertedDays);
                                        request.EndDate = request.EndDate.AddDays(FreeTrialConvertedDays);
                                    }

                                    recipient.PackageEntitlements.Add(entitlement);
                                    endDt = entitlement.EndDate;
                                }
                                recipient.EntitlementRequests.Add(request);
                                item.EntitlementRequest = request; //UPDATED: November 22, 2012
                                //Update recurring billing if it exists
                                UpdateRecurringBillingIfExist(context, recipient, endDt, (Package)package.Package, isPayPal: true);
                            }
                        }
                        break;

                    case SubscriptionProductType.Episode:
                        EpisodeSubscriptionProduct ep_subscription = (EpisodeSubscriptionProduct)product;
                        foreach (var episode in ep_subscription.Episodes)
                        {
                            EpisodeEntitlement currentEpisode = recipient.EpisodeEntitlements.FirstOrDefault(e => e.EpisodeId == episode.EpisodeId);
                            DateTime endDate = registDt;
                            EntitlementRequest request = CreateEntitlementRequest(registDt, endDate, product, String.Format("{0}-{1}", "Paypal", TransactionID), TransactionID, registDt);
                            if (currentEpisode != null)
                            {
                                request.StartDate = currentEpisode.EndDate;
                                currentEpisode.EndDate = MyUtility.getEntitlementEndDate(ep_subscription.Duration, ep_subscription.DurationType, ((currentEpisode.EndDate > registDt) ? currentEpisode.EndDate : registDt));
                                endDate = currentEpisode.EndDate;
                                currentEpisode.LatestEntitlementRequest = request;
                                request.EndDate = endDate;
                                endDt = endDate;
                                isExtension = true;
                            }
                            else
                            {
                                EpisodeEntitlement entitlement = CreateEpisodeEntitlement(request, ep_subscription, episode, registDt);
                                request.EndDate = entitlement.EndDate;
                                recipient.EpisodeEntitlements.Add(entitlement);
                                endDt = entitlement.EndDate;
                            }
                            recipient.EntitlementRequests.Add(request);
                            item.EntitlementRequest = request; //UPDATED: November 22, 2012
                        }
                        break;
                }

                if (context.SaveChanges() > 0)
                {

                    SendConfirmationEmails(user, recipient, transaction, ProductNameBought, product, endDt, registDt, "Paypal", isGift, isExtension);
                    ////Send email
                    //string emailBody = String.Format(GlobalConfig.SubscribeToProductBodyTextOnly, user.FirstName, ProductNameBought, endDt.ToString("MM/dd/yyyy hh:mm:ss tt"), transaction.TransactionId, product.Name, registDt.ToString("MM/dd/yyyy hh:mm:ss tt"), transaction.Amount.ToString("F2"), transaction.Currency, "Paypal", transaction.Reference);
                    //if (isExtension)
                    //    emailBody = String.Format(GlobalConfig.ExtendSubscriptionBodyTextOnly, user.FirstName, ProductNameBought, endDt.ToString("MM/dd/yyyy hh:mm:ss tt"), transaction.TransactionId, product.Name, registDt.ToString("MM/dd/yyyy hh:mm:ss tt"), transaction.Amount.ToString("F2"), transaction.Currency, "Paypal", transaction.Reference);
                    //try
                    //{
                    //    MyUtility.SendEmailViaSendGrid(user.EMail, GlobalConfig.NoReplyEmail, String.Format("You are now subscribed to {0}", ProductNameBought), emailBody, MailType.TextOnly, emailBody);
                    //}
                    //catch (Exception)
                    //{
                    //}
                    return ErrorCodes.Success;
                }
                return ErrorCodes.EntityUpdateError;
            }

            catch (Exception e) { Debug.WriteLine(e.InnerException); throw; }
        }
예제 #2
0
        public static ErrorCodes ReloadViaPayPal(IPTV2Entities context, System.Guid userId, decimal Amount, string TransactionID)
        {
            try
            {
                //var context = new IPTV2Entities();
                DateTime registDt = DateTime.Now;
                User user = context.Users.FirstOrDefault(u => u.UserId == userId);
                Offering offering = context.Offerings.FirstOrDefault(o => o.OfferingId == GlobalConfig.offeringId);
                UserWallet wallet = user.UserWallets.FirstOrDefault(w => w.Currency == MyUtility.GetCurrencyOrDefault(user.CountryCode));
                string CurrencyCode = MyUtility.GetCurrencyOrDefault(user.CountryCode);
                Currency currency = context.Currencies.FirstOrDefault(c => c.Code == CurrencyCode);


                if (GlobalConfig.UsePayPalIPNLog)
                {
                    if (context.PaypalIPNLogs.Count(t => String.Compare(t.UniqueTransactionId, TransactionID, true) == 0) > 0)
                        return ErrorCodes.IsProcessedPayPalTransaction;
                }
                else
                {
                    Transaction ppt = context.Transactions.FirstOrDefault(t => String.Compare(t.Reference, TransactionID, true) == 0);
                    if (ppt != null)
                        return ErrorCodes.IsProcessedPayPalTransaction;
                }

                //Transaction ppt = context.Transactions.FirstOrDefault(t => String.Compare(t.Reference, TransactionID, true) == 0);
                //if (ppt != null)
                //    return ErrorCodes.IsProcessedPayPalTransaction;

                PaypalReloadTransaction transaction = new PaypalReloadTransaction()
                {
                    Currency = wallet.Currency,
                    Reference = TransactionID,
                    Amount = Amount,
                    UserWallet = wallet,
                    Date = registDt,
                    OfferingId = GlobalConfig.offeringId,
                    StatusId = GlobalConfig.Visible
                };

                var ipnLog = new PaypalIPNLog()
                {
                    User = user,
                    UniqueTransactionId = TransactionID,
                    TransactionDate = registDt
                };

                user.PaypalIPNLogs.Add(ipnLog);

                user.Transactions.Add(transaction);

                wallet.Balance += Amount;
                wallet.LastUpdated = registDt;

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

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