Exemplo n.º 1
0
        //Invoice Report
        public void Invoice(string invoiceId)
        {
            var invoice   = StripeHelper.GetInvoice(invoiceId);
            var unitprice = ((double)invoice.StripeInvoiceLineItems.Data.First().Amount) / 100;
            var quantity  = 1;
            var tax       = invoice.Tax == null ? 0 : (double)(invoice.Tax) / 100;
            var total     = unitprice * quantity + tax;
            var amount    = (double)invoice.Total / 100; //after discount
            var discount  = Math.Round(total - amount, 2, MidpointRounding.AwayFromZero);
            var username  = HttpContext.User.Identity.Name + " " + SessionData.LastName;
            var email     = SessionData.Email;
            var desc      = "Monthly Plan - " + invoice.StripeInvoiceLineItems.Data.First().Plan.Name;
            var fileName  = "Invoice_MPP_" + invoice.StripeInvoiceLineItems.Data.First().Plan.Name + "_" + DateTime.Now.ToShortDateString();


            Document document   = new Document(PageSize.A4, 88f, 88f, 50f, 10f);
            Font     NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);

            using (MemoryStream ms = new MemoryStream())
            {
                PdfWriter writer = PdfWriter.GetInstance(document, ms);
                Phrase    phrase = null;
                PdfPCell  cell   = null;
                PdfPTable table  = null;
                BaseColor color  = null;

                document.Open();

                //row1

                //Header Table
                table             = new PdfPTable(2);
                table.TotalWidth  = 500f;
                table.LockedWidth = true;
                table.SetWidths(new float[] { 0.5f, 0.5f });



                //Company Name and Address
                phrase = new Phrase();
                phrase.Add(new Chunk("INVOICE", FontFactory.GetFont("Arial", 20, Font.BOLD, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                cell.VerticalAlignment = Element.ALIGN_TOP;
                table.AddCell(cell);
                //Company Logo
                cell = ImageCell("~/content/images/logo.png", 80f, Element.ALIGN_RIGHT);
                table.AddCell(cell);
                document.Add(table);
                // separation line header
                color = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));
                //line1
                DrawLine(writer, 25f, document.Top - 50f, document.PageSize.Width - 25f, document.Top - 50f, color);
                DrawLine(writer, 25f, document.Top - 51f, document.PageSize.Width - 25f, document.Top - 51f, color);


                //vendor or user details
                table             = new PdfPTable(3);
                table.TotalWidth  = 500f;
                table.LockedWidth = true;
                table.SetWidths(new float[] { 0.3f, 0.3f, 0.4f });
                table.SpacingBefore = 80f;

                //row2

                //user address
                phrase = new Phrase();
                phrase.Add(new Chunk(username + "\n\n" + email, FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Date
                phrase = new Phrase();
                phrase.Add(new Chunk("Date\n\n" + invoice.Date.Value.ToShortDateString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_CENTER);
                table.AddCell(cell);
                //vendor address
                phrase = new Phrase();
                phrase.Add(new Chunk("My PPC Pal, LLC \n P.O. Box 2126 \n Yorba Linda, CA 92885", FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_RIGHT);
                table.AddCell(cell);
                document.Add(table);


                //row 3
                table               = new PdfPTable(1);
                table.TotalWidth    = 500f;
                table.LockedWidth   = true;
                table.SpacingBefore = 35f;

                phrase = new Phrase();
                phrase.Add(new Chunk("Invoice Number : " + invoice.Id + "\n", FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                document.Add(table);

                DrawLine(writer, 25f, document.Top - 190f, document.PageSize.Width - 25f, document.Top - 190f, BaseColor.BLACK); //line 2

                //row 4
                var FontColor  = new BaseColor(51, 122, 183);
                var HeaderFont = FontFactory.GetFont("Arial", 10, Font.BOLD, FontColor);

                table               = new PdfPTable(5);
                table.TotalWidth    = 500f;
                table.LockedWidth   = true;
                table.SpacingBefore = 15f;
                table.SetWidths(new float[] { 0.4f, 0.2f, 0.2f, 0.1f, 0.1f });

                //description
                phrase = new Phrase();
                phrase.Add(new Chunk("Description", HeaderFont));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Unit price
                phrase = new Phrase();
                phrase.Add(new Chunk("Unit Price", HeaderFont));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Discount
                phrase = new Phrase();
                phrase.Add(new Chunk("Discount", HeaderFont));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Tax
                phrase = new Phrase();
                phrase.Add(new Chunk("Tax", HeaderFont));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Amount
                phrase = new Phrase();
                phrase.Add(new Chunk("Amount", HeaderFont));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);

                document.Add(table);
                DrawLine(writer, 25f, document.Top - 220, document.PageSize.Width - 25f, document.Top - 220f, BaseColor.BLACK); //line 3
                //row 5
                table               = new PdfPTable(5);
                table.TotalWidth    = 500f;
                table.LockedWidth   = true;
                table.SpacingBefore = 25f;
                table.SetWidths(new float[] { 0.4f, 0.2f, 0.2f, 0.1f, 0.1f });

                //description
                phrase = new Phrase();
                phrase.Add(new Chunk(desc, FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Unit price
                phrase = new Phrase();
                phrase.Add(new Chunk("$ " + unitprice.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Discount
                phrase = new Phrase();
                phrase.Add(new Chunk("$ " + discount.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Tax
                phrase = new Phrase();
                phrase.Add(new Chunk("$ " + tax.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                //Amount
                phrase = new Phrase();
                phrase.Add(new Chunk("$ " + amount.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);

                document.Add(table);
                DrawLine(writer, 25f, document.Top - 260, document.PageSize.Width - 25f, document.Top - 260f, BaseColor.BLACK); //line 3
                //row 6
                table               = new PdfPTable(2);
                table.TotalWidth    = 500f;
                table.LockedWidth   = true;
                table.SpacingBefore = 25f;
                table.SetWidths(new float[] { 0.9f, 0.1f });

                //Total
                phrase = new Phrase();
                phrase.Add(new Chunk("Total:", FontFactory.GetFont("Arial", 10, Font.BOLD, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_RIGHT);
                table.AddCell(cell);
                //Total Amount
                phrase = new Phrase();
                phrase.Add(new Chunk("$ " + amount.ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                table.AddCell(cell);
                document.Add(table);

                //row 7
                table               = new PdfPTable(1);
                table.TotalWidth    = 500f;
                table.LockedWidth   = true;
                table.SpacingBefore = 80f;
                //declaration
                var card = StripeHelper.GetCard(SessionData.StripeCustId);
                if (SessionData.StripeCustId != null && card != null && card.FirstOrDefault() != null)
                {
                    var str = "Your card on file ending in " + StripeHelper.GetCard(SessionData.StripeCustId).FirstOrDefault().Last4 + " will be billed automatically for the total amount shown on this invoice";
                    phrase = new Phrase();
                    phrase.Add(new Chunk(str, FontFactory.GetFont("Arial", 9, Font.NORMAL, BaseColor.BLACK)));
                    cell = PhraseCell(phrase, Element.ALIGN_LEFT);
                    table.AddCell(cell);
                    document.Add(table);
                }



                //row 8
                table               = new PdfPTable(1);
                table.TotalWidth    = 500f;
                table.LockedWidth   = true;
                table.SpacingBefore = 50f;
                //declaration

                var renewals = @"Renewals: This order renews for additional 1 month periods, unless either party provides the other with a notice of non-renewal prior to the renewal date. 
                              Terms: This order is governed by the terms of the Subscription Services Agreement between the parties, which terms are incorporated into this order for all purposes.
                             If there is a conflict between the terms of this order and the agreement, this order governs.
                             This order and the agreement are the entire agreement between the parties, and they supersede and replace all prior and contemporaneous negotiations, agreements, representations and discussions regarding this subject matter.
                             Only a signed writing of the parties may amend this order.";
                renewals = renewals.Replace(Environment.NewLine, String.Empty).Replace("  ", String.Empty);
                Chunk  beginning = new Chunk(renewals, FontFactory.GetFont("Arial", 6));
                Phrase p1        = new Phrase(beginning);


                cell = PhraseCell(p1, Element.ALIGN_LEFT);

                table.AddCell(cell);
                document.Add(table);

                document.Close();
                byte[] bytes = ms.ToArray();
                ms.Close();
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
                Response.ContentType = "application/pdf";
                Response.Buffer      = true;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(bytes);
                Response.End();
                Response.Close();
            }
        }
Exemplo n.º 2
0
        public void WebHook()
        {
            try
            {
                var paymentAlertStatus = 0;
                var postdata           = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();
                var data       = JObject.Parse(postdata);
                var signature  = Request.Headers["Stripe-Signature"];
                var eventid    = data["id"].ToString();
                var custid     = data["data"].First().First["customer"].ToString();
                var msg        = "";
                var emailboday = "";
                var eventdata  = StripeHelper.GetStripeEvent(eventid);

                if (eventdata != null && (eventdata.Type == "invoice.payment_succeeded" || eventdata.Type == "invoice.payment_failed"))
                {
                    String coupon_id    = null;
                    var    list         = data["data"].First().First["lines"];
                    var    Amount       = ((double)list["data"].First["amount"]) / 100;
                    var    Amount_off   = ((double)data["data"].First().First["amount_due"]) / 100;
                    var    Discount     = data["data"].First().First["discount"];
                    var    start        = (double)list["data"].First["period"]["start"];
                    var    end          = (double)list["data"].First["period"]["end"];
                    var    startperiod  = MppUtility.UnixTimeStampToDateTime(start);
                    var    endperiod    = MppUtility.UnixTimeStampToDateTime(end);
                    var    discount     = Math.Round(Amount - Amount_off, 2, MidpointRounding.AwayFromZero);
                    var    customerdata = StripeHelper.GetStripeCustomer(custid);
                    var    email        = customerdata.Email;
                    var    last4        = "";
                    var    carddetails  = StripeHelper.GetCard(custid);
                    foreach (var card in carddetails)
                    {
                        last4 = card.Last4;
                    }

                    if (Discount.Count() > 0)
                    {
                        coupon_id = Discount["coupon"]["id"].ToString();
                    }

                    switch (eventdata.Type)
                    {
                    case "invoice.payment_succeeded":
                        msg                = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 1);
                        emailboday         = "Payment Success. A payment of $" + Amount_off + " was made using your card on file ending with <b> " + last4 + " </b>";
                        paymentAlertStatus = 1;
                        break;

                    case "invoice.payment_failed":
                        msg                = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 0);
                        emailboday         = "Payment failed.Payment $" + Amount_off + " failed from your card ending with <b> " + last4 + " </b> ";
                        paymentAlertStatus = 2;
                        break;
                    }
                    if (msg == "" && Amount_off != 0)
                    {
                        var respo = EmailAlert.PaymentAlertMail(email, emailboday);
                        if (respo == "success")
                        {
                            sellerData.UpdatePaymentAlert(paymentAlertStatus, custid);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                String msg = ex.Message;
            }
        }
Exemplo n.º 3
0
        public HttpStatusCode WebHook()
        {
            try
            {
                var    paymentAlertStatus = 0;
                string secret             = MppUtility.ReadConfig("MS_WebHookReceiverSecret_Custom");
                var    postdata           = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();
                var    emailboday         = "";
                var    signature          = Request.Headers["Stripe-Signature"];
                var    stripeEvent        = StripeEventUtility.ConstructEvent(postdata, signature, secret);
                var    eventid            = stripeEvent.Id;
                var    eventdata          = StripeHelper.GetStripeEvent(eventid);
                var    data   = JObject.Parse(postdata);
                var    custid = data["data"].First().First["customer"].ToString();
                var    msg    = "";

                if (eventdata != null && (eventdata.Type == "invoice.payment_succeeded" || eventdata.Type == "invoice.payment_failed"))
                {
                    String coupon_id    = null;
                    var    list         = data["data"].First().First["lines"];
                    var    Amount       = ((double)list["data"].First["amount"]) / 100;
                    var    Amount_off   = ((double)data["data"].First().First["amount_due"]) / 100;
                    var    Discount     = data["data"].First().First["discount"];
                    var    start        = (double)list["data"].First["period"]["start"];
                    var    end          = (double)list["data"].First["period"]["end"];
                    var    startperiod  = MppUtility.UnixTimeStampToDateTime(start);
                    var    endperiod    = MppUtility.UnixTimeStampToDateTime(end);
                    var    discount     = Math.Round(Amount - Amount_off, 2, MidpointRounding.AwayFromZero);
                    var    customerdata = StripeHelper.GetStripeCustomer(custid);
                    var    email        = customerdata.Email;
                    var    last4        = "";
                    var    carddetails  = StripeHelper.GetCard(custid);
                    foreach (var card in carddetails)
                    {
                        last4 = card.Last4;
                    }

                    if (Discount.Count() > 0)
                    {
                        coupon_id = Discount["coupon"]["id"].ToString();
                    }

                    switch (eventdata.Type)
                    {
                    case "invoice.payment_succeeded":
                        msg                = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 1);
                        emailboday         = "A payment of $" + Amount_off + " was made using your card on file ending with <b> " + last4 + " </b>";
                        paymentAlertStatus = 1;
                        break;

                    case "invoice.payment_failed":
                        msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount_off, coupon_id, discount, startperiod, endperiod, 0);
                        upData.UpdatePlanData(custid, 0);
                        StripeHelper.UnSubscribe(custid);
                        emailboday         = @"A payment $" + Amount_off + " failed from your card ending with <b> " + last4 + " </b>" + "Please check your card details or add new card.";
                        paymentAlertStatus = 2;
                        break;
                    }
                    if (msg == "" && Amount_off != 0)
                    {
                        var respo = EmailAlert.PaymentAlertMail(email, emailboday);
                        if (respo == "success")
                        {
                            sellerData.UpdatePaymentAlert(paymentAlertStatus, custid);
                        }
                    }
                }
                else if (stripeEvent.Type == "charge.failed")
                {
                    var Amount          = ((double)data["data"].First().First["amount"]) / 100;
                    var failure_message = data["data"].First().First["failure_message"].ToString();
                    // msg = AccountData.UpdatePaymentStatus(custid, Amount, Amount, 0, 0.00, startperiod, endperiod, 0);
                    if (Amount > 1)//Card verification charge
                    {
                        upData.UpdatePlanData(custid, 0);
                        StripeHelper.UnSubscribe(custid);
                    }
                    var last4        = "";
                    var carddetails  = StripeHelper.GetCard(custid);
                    var customerdata = StripeHelper.GetStripeCustomer(custid);
                    var email        = customerdata.Email;
                    foreach (var card in carddetails)
                    {
                        last4 = card.Last4;
                    }
                    emailboday = @"A payment $" + Amount + " failed from your card ending with <b> " + last4 + " </b>" + "Please check your card details or add new card.</b>Possible reason:" + failure_message;
                    var respo = EmailAlert.PaymentAlertMail(email, emailboday);
                    if (respo == "success" && Amount > 1)
                    {
                        sellerData.UpdatePaymentAlert(2, custid);
                    }
                }
            }
            catch (Exception ex)
            {
                String msg = ex.Message;
                LogFile.WriteLog(msg);
            }
            return(HttpStatusCode.OK);
        }