Ejemplo n.º 1
0
        private void ProcessIPN(string transactionType, string strRequest)
        {
            int UserID = 20;
            Decimal amountPaid = 0;
            int? invoiceID = null;
            try
            {
                Decimal.TryParse(Request["mc_gross"], out amountPaid);

                if (transactionType == "recurring_payment_profile_created" ||
                    transactionType == "recurring_payment_profile_cancel" ||
                    transactionType == "recurring_payment_skipped" ||
                    transactionType == "recurring_payment")
                {
                    Int32.TryParse(Request["rp_invoice_id"], out UserID);
                }
                else
                {
                    Int32.TryParse(Request["custom"], out UserID);

                    Int32 tempInvoiceID = 0;
                    if (true == Int32.TryParse(Request["invoice"], out tempInvoiceID))
                    {
                        invoiceID = tempInvoiceID;
                    }
                }
            }
            catch (Exception)
            {
            }

            int? midiID = null;
            short? packageID = null;
            short paymentType = -1;
            Invoice invoice = null;

            if (invoiceID.HasValue)
            {
                invoice = crumbsRepository.FindInvoice(invoiceID.Value);
                if (invoice != null)
                {
                    paymentType = invoice.Type;
                    midiID = invoice.MidiID;
                    packageID = invoice.PackageID;
                }
            }

            User user = (crumbsRepository as IUserRepository).FindUserByID(UserID);
            if (user == null)
            {
                user = (crumbsRepository as IUserRepository).FindUserByID(20);
            }

            DateTime creationDate = DateTime.Now;
            DateTime expirationDate = creationDate;

            if (paymentType == 10 || paymentType >= 20) // MIDI Payment, MIDI Package
            {
                string email = Request["payer_email"];
                string lastName = Request["last_name"];
                string firstName = Request["first_name"];

                if (transactionType == "cart")
                {
                    OrderHelper.ProcessOrder(crumbsRepository, invoice, Request.ServerVariables["APPL_PHYSICAL_PATH"], email, true);
                }
            }
            else if (transactionType == "express_checkout" || transactionType == "cart")
            {
                if (isProvideTrialPeriod)
                {
                    expirationDate = creationDate.AddDays(14);

                    try
                    {
                        FreePlay prevTrial = crumbsRepository.FindFreePlay(user.UserID, 8);
                        if (prevTrial != null)
                        {
                            prevTrial.Comment = "Warning: Multiple trials detected.";
                            crumbsRepository.InsertOrUpdateFreePlay(prevTrial);
                        }

                        FreePlay newTrial = new FreePlay();
                        newTrial.User = user;
                        newTrial.Promo = crumbsRepository.FindPromotionByID(8);
                        newTrial.Date = creationDate;
                        newTrial.Expiration = expirationDate;
                        crumbsRepository.InsertOrUpdateFreePlay(newTrial);
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    PrepaidOption option = crumbsRepository.FindPrepaidOption(paymentType);
                    if (option != null)
                    {
                        if (option.Months == 12)
                        {
                            expirationDate = creationDate.AddYears(1);
                        }
                        else
                        {
                            expirationDate = creationDate.AddMonths(option.Months);
                        }
                    }
                }
            }
            else if (transactionType == "subscr_payment" || transactionType == "recurring_payment")
            {
                string next_payment_date = Request["next_payment_date"];

                DateTime dtNextPayment = ConvertPayPalDateTime(next_payment_date);
                if(dtNextPayment > DateTime.MinValue) {

                    expirationDate = dtNextPayment.AddDays(1);
                }
                else if (true == DateTime.TryParse(next_payment_date, out dtNextPayment))
                {
                    expirationDate = dtNextPayment.AddDays(1);
                }
            }
            else if (transactionType == "subscr_signup" || transactionType == "recurring_payment_profile_created")
            {
                string PaymentID = Request["recurring_payment_id"];

                Price price = crumbsRepository.FindPriceForUser(user.UserID, 0);
                UserPrice uPrice = crumbsRepository.FindUserPriceByUserID(user.UserID, 0);
                if (uPrice != null)
                {
                    uPrice.Price = price;
                    uPrice.Subscribed = 1;
                    uPrice.Amount = price.Amount;
                    uPrice.PaymentID = PaymentID;
                    crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                }
                else
                {
                    // No special promotions found - create a new one
                    UserPrice newUserPrice = new UserPrice()
                    {
                        User = user,
                        Price = price,
                        Subscribed = 1,
                        Amount = price.Amount,
                        PaymentID = PaymentID
                    };
                    crumbsRepository.InsertOrUpdateUserPrice(newUserPrice);
                }
            }
            else if (transactionType == "subscr_cancel" || transactionType == "recurring_payment_profile_cancel")
            {
                string PaymentID = Request["recurring_payment_id"];

                UserPrice uPrice = crumbsRepository.FindUserPriceByPaymentID(PaymentID);
                if (uPrice != null)
                {
                    uPrice.Subscribed = 0;
                    crumbsRepository.InsertOrUpdateUserPrice(uPrice);
                }
            }

            Payment paymentmodel = new Payment()
            {
                UserID = user.UserID,
                Method = transactionType,
                Date = creationDate,
                Expiration = expirationDate,
                Paid = amountPaid,
                InvoiceID = invoiceID,
                MidiID = midiID,
                PackageID = packageID,
                Comment = strRequest
            };
            crumbsRepository.InsertOrUpdatePayment(paymentmodel);
            crumbsRepository.Save();
        }
Ejemplo n.º 2
0
        //
        // GET: /Order/Success
        public ActionResult Success(string token, string payerID)
        {
            // Send GetExpressCheckoutDetails request
            string strECDRequest = BuildECDRequest(token);
            Hashtable htECDResponse = SendRequest(strECDRequest);
            string strAck = htECDResponse["ACK"].ToString();
            if (strAck == "Success" || strAck == "SuccessWithWarning")
            {
                int invoiceID = -1;

                try
                {
                    string strInvoiceID = htECDResponse["INVNUM"].ToString();
                    Int32.TryParse(strInvoiceID, out invoiceID);
                }
                catch (Exception ex)
                {
                    return View("Failed", (object)ex.Message);
                }

                Invoice invoice = crumbsRepository.FindInvoice(invoiceID);
                if (invoice == null)
                {
                    return View("Failed", (object)string.Format("invoice {0} not found.", invoiceID));
                }

                // Send DoExpressCheckoutPayment request
                string strDECPRequest = BuildDECPRequest(token, payerID, invoice);
                Hashtable htDECPResponse = SendRequest(strDECPRequest);
                strAck = htDECPResponse["ACK"].ToString();
                if (strAck == "Success" || strAck == "SuccessWithWarning")
                {
                    if (invoice.Type == 10 || invoice.Type >= 20) // MIDI Payment, MIDI Package
                    {
                        // Add temporary authoriazation while waiting for IPN
                        DateTime creationDate = DateTime.Now;
                        Payment paymentmodel = new Payment()
                        {
                            UserID = invoice.UserID,
                            Method = "temp_auth",
                            Date = creationDate,
                            Expiration = creationDate,
                            Paid = 0,
                            InvoiceID = invoiceID,
                            MidiID = invoice.MidiID,
                            PackageID = invoice.PackageID,
                            Comment = strDECPRequest
                        };
                        crumbsRepository.InsertOrUpdatePayment(paymentmodel);
                        crumbsRepository.Save();
                    }
                    else
                    {
                        // Add temporary authoriazation while waiting for IPN
                        DateTime creationDate = DateTime.Now;
                        Payment paymentmodel = new Payment()
                        {
                            UserID = invoice.UserID,
                            Method = "temp_auth",
                            Date = creationDate,
                            Expiration = creationDate.AddHours(1),
                            Paid = 0,
                            InvoiceID = invoiceID,
                            Comment = strDECPRequest
                        };
                        crumbsRepository.InsertOrUpdatePayment(paymentmodel);
                        crumbsRepository.Save();
                    }
                }
                else
                {
                    string strErr = "Error: " + htDECPResponse["L_LONGMESSAGE0"].ToString();
                    string strErrcode = "Error code: " + htDECPResponse["L_ERRORCODE0"].ToString();

                    invoice.Comment = strErr;
                    crumbsRepository.InsertOrUpdateInvoice(invoice);
                    crumbsRepository.Save();

                    return View("Failed", (object)strErr);
                }

                if (invoice.Type == 0 || invoice.Type == 1 || invoice.Type == 2 || invoice.Type == 3)
                {
                    // Send CreateRecurringPaymentsProfile request
                    string strCRPRequest = BuildCRPRequest(token, invoice);
                    Hashtable htCRPResponse = SendRequest(strCRPRequest);
                    strAck = htCRPResponse["ACK"].ToString();
                    if (strAck == "Success" || strAck == "SuccessWithWarning")
                    {
                    }
                    else
                    {
                        string strErr = "Error: " + htCRPResponse["L_LONGMESSAGE0"].ToString();
                        string strErrcode = "Error code: " + htCRPResponse["L_ERRORCODE0"].ToString();

                        invoice.Comment = strErr;
                        crumbsRepository.InsertOrUpdateInvoice(invoice);
                        crumbsRepository.Save();

                        return View("Failed", (object)strErr);
                    }
                }
            }
            else
            {
                string strErr = "Error: " + htECDResponse["L_LONGMESSAGE0"].ToString();
                string strErrcode = "Error code: " + htECDResponse["L_ERRORCODE0"].ToString();

                return View("Failed", (object)strErr);
            }

            return View();
        }