protected void ButtonExecute_Click(object sender, EventArgs e)
    {
        if (!_authority.HasPermission(Permission.CanDoEconomyTransactions, thisInvoice.OrganizationId, -1, Authorization.Flag.ExactOrganization))
        {
            throw new UnauthorizedAccessException("Access Denied");
        }

        if (this.RadioManualMap.Checked)
        {
            int transactionId = Int32.Parse(this.DropTransactions.SelectedValue);
            FinancialTransaction transaction = FinancialTransaction.FromIdentity(transactionId);

            Payment payment = Payment.CreateSingle(thisInvoice.Organization, transaction.DateTime, thisInvoice.Currency, thisInvoice.AmountCents, thisInvoice, _currentUser);
            payment.AddInformation(PaymentInformationType.Freeform, "Mapped manually in PirateWeb");

            transaction.AddRow(thisInvoice.Organization.FinancialAccounts.AssetsOutboundInvoices, -thisInvoice.AmountCents, _currentUser);
            transaction.Dependency = payment.Group;
        }
        else if (this.RadioCreditInvoice.Checked)
        {
            thisInvoice.Credit(_currentUser);
        }

        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", true);
    }
Esempio n. 2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        // THIS CODE IS TAKEN FROM PAYPAL'S TEMPLATE.

        Person notifyPerson = Person.FromIdentity(1);


        //Post back to either sandbox or live
        string         strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req     = (HttpWebRequest)WebRequest.Create(strLive);

        //Set values for the request back
        req.Method      = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param           = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest      = Encoding.ASCII.GetString(param);
        string originalRequest = strRequest;

        strRequest       += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;

        //Send the request to PayPal and get the response
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);

        streamOut.Write(strRequest);
        streamOut.Close();
        StreamReader streamIn    = new StreamReader(req.GetResponse().GetResponseStream());
        string       strResponse = streamIn.ReadToEnd();

        streamIn.Close();

        // Decode parameters
        List <string> temp = new List <string>();
        Dictionary <string, string> parameters = new Dictionary <string, string>();

        string[] parameterStrings = originalRequest.Split('&');
        foreach (string parameter in parameterStrings)
        {
            string[] data = parameter.Split('=');

            string dataKey   = Server.UrlDecode(data[0]);
            string dataValue = Server.UrlDecode(data[1]);

            parameters[dataKey] = dataValue;
            temp.Add(string.Format("{0,-20} {1}", dataKey, dataValue));
        }


        notifyPerson.SendNotice("Paypal listener point A", String.Join("\r\n", temp.ToArray()), 1);

        bool proceed = true;

        if (strResponse == "INVALID")
        {
            notifyPerson.SendNotice("Paypal listener FAIL - Response is INVALID", string.Empty, 1);
            proceed = false;
        }
        if (!parameters.ContainsKey("payment_status"))
        {
            notifyPerson.SendNotice("Paypal listener FAIL - payment_status was not supplied", string.Empty, 1);
            proceed = false;
        }
        else if (parameters["payment_status"] != "Completed" && parameters["payment_status"] != "Refunded")
        {
            notifyPerson.SendNotice("Paypal listener FAIL - payment_status is not Completed / Refunded", string.Empty, 1);
            proceed = false;
        }
        if (parameters["receiver_email"] != "*****@*****.**")
        {
            notifyPerson.SendNotice("Paypal listener FAIL - receiver_email is not [email protected]", string.Empty, 1);
            proceed = false;  // HACK -- adjust for multiple orgs
        }
        if (parameters["mc_currency"] != "SEK")
        {
            notifyPerson.SendNotice("Paypal listener FAIL - mc_currency is not SEK", string.Empty, 1);
            proceed = false; // HACK -- adjust for multiple orgs
        }
        if (!parameters.ContainsKey("mc_gross") || String.IsNullOrEmpty(parameters["mc_gross"]))
        {
            notifyPerson.SendNotice("Paypal listener FAIL - mc_net is null or empty", string.Empty, 1);
            proceed = false;
        }

        if (strResponse == "VERIFIED" && proceed)
        {
            string transactionId = parameters["txn_id"];
            string identifier    = null;

            if (parameters.ContainsKey("invoice"))
            {
                identifier = parameters["invoice"];
            }

            OutboundInvoice invoice = null;

            if (!String.IsNullOrEmpty(identifier))
            {
                invoice = OutboundInvoice.FromReference(identifier);

                notifyPerson.SendNotice("Paypal listener - invoice identified as #" + invoice.Identity.ToString(), string.Empty, 1);
            }

            string grossString = parameters["mc_gross"];

            string feeString = "0.0";

            if (parameters.ContainsKey("mc_fee") && !string.IsNullOrEmpty(parameters["mc_fee"]))
            {
                feeString = parameters["mc_fee"];
            }

            string comment = "Paypal ";

            if (parameters["payment_status"] == "Completed")
            {
                comment += parameters["txn_type"];
            }
            else
            {
                // Refund

                comment += "Refund";
            }

            if (parameters.ContainsKey("item_name"))
            {
                comment = parameters["item_name"];
            }

            if (invoice != null)
            {
                comment = "Outbound Invoice #" + invoice.Identity.ToString() + " payment Paypal";
            }

            DateTime dateTime = DateTime.ParseExact(parameters["payment_date"].Replace("PDT", "-07").Replace("PST", "-08"), "HH:mm:ss MMM dd, yyyy zz", CultureInfo.InvariantCulture).ToLocalTime();

            Organization org = Organization.PPSE;

            Int64 feeCents   = Int64.Parse(feeString.Replace(".", ""));
            Int64 grossCents = Int64.Parse(grossString.Replace(".", ""));

            Int64 amountCents = grossCents - feeCents;

            FinancialTransaction transaction = FinancialTransaction.ImportWithStub(org.Identity, dateTime,
                                                                                   org.FinancialAccounts.AssetsPaypal.Identity, amountCents,
                                                                                   comment, transactionId,
                                                                                   0);  // TODO: Convert to cents

            if (transaction != null)
            {
                // The transaction was created. Examine if the autobook criteria are true.

                if (amountCents < 0 && parameters["payment_status"] == "Completed")
                {
                    // Do not balance the transaction -- will have to be balanced manually
                }
                else if (amountCents > 0)
                {
                    if (invoice != null)
                    {
                        Int64 expectedNetCents = invoice.AmountCents;

                        transaction.AddRow(org.FinancialAccounts.CostsBankFees, expectedNetCents - amountCents, null);  // can be negative
                        transaction.AddRow(org.FinancialAccounts.AssetsOutboundInvoices, -expectedNetCents, null);

                        // Hack: Add a line to the outbound invoice HERE with the PayPal surcharge

                        invoice.AddItem("PayPal/Credit Card surcharge, 5%", (Int64)(invoice.AmountCents * 0.05));

                        Payment payment = Payment.CreateSingle(invoice.Organization, DateTime.Now, invoice.Currency,
                                                               expectedNetCents, invoice, null);  // HACK HACK HACK: says we always received the expected amount

                        payment.AddInformation(PaymentInformationType.RefundInformation, "PayPal " + transactionId);
                        payment.AddInformation(PaymentInformationType.Name, parameters["first_name"] + " " + parameters["last_name"]);
                        payment.AddInformation(PaymentInformationType.Email, parameters["payer_email"]);

                        transaction.Dependency = payment.Group;
                    }
                    else if (feeCents > 0)
                    {
                        // This is always an autodeposit

                        transaction.AddRow(org.FinancialAccounts.CostsBankFees, feeCents, null);
                        transaction.AddRow(org.FinancialAccounts.IncomeDonations, -grossCents, null);
                    }
                    else
                    {
                        transaction.AddRow(org.FinancialAccounts.IncomeDonations, -amountCents, null);
                    }
                }
            }
        }
    }
Esempio n. 3
0
    protected void ImportBgmaxText(string text)
    {
        string[] lines = text.Split("\r\n".ToCharArray());

        DateTime timestamp    = DateTime.MinValue;
        int      bgMaxVersion = 0;

        PaymentGroup curPaymentGroup            = null;
        Payment      curPayment                 = null;
        Currency     currency                   = null;
        Int64        curPaymentGroupAmountCents = 0;

        foreach (string line in lines)
        {
            if (line.Length < 2)
            {
                continue; // CR/LF split causes every other line to be empty
            }

            switch (line.Substring(0, 2))
            {
            case "01":     // BGMAX intro
                string bgmaxmarker = line.Substring(2, 20).Trim();
                if (bgmaxmarker != "BGMAX")
                {
                    throw new Exception("bad format -- not bgmax");
                }
                bgMaxVersion = Int32.Parse(line.Substring(22, 2));
                timestamp    = DateTime.ParseExact(line.Substring(24, 20), "yyyyMMddHHmmssffffff",
                                                   CultureInfo.InvariantCulture);
                break;

            case "05":     // Begin payment group
                if (bgMaxVersion < 1)
                {
                    throw new InvalidOperationException("BGMax record must precede first payment group");
                }
                currency                   = Currency.FromCode(line.Substring(22, 3));
                curPaymentGroup            = PaymentGroup.Create(Organization.PPSE, timestamp, currency, _currentUser);
                curPaymentGroupAmountCents = 0;
                break;

            case "20":     // Begin payment
                if (curPaymentGroup == null)
                {
                    throw new InvalidOperationException("Payment group start must precede first payment");
                }
                string fromAccount = line.Substring(2, 10);
                string reference   = line.Substring(12, 25).Trim();   // left space padded in BgMax format
                Int64  amountCents = Int64.Parse(line.Substring(37, 18), CultureInfo.InvariantCulture);
                string key         = "SEBGM" + DateTime.Today.Year.ToString() + line.Substring(57, 12);
                bool   hasImage    = (line[69] == '1' ? true : false);

                // TODO: Check if existed already -- must do -- IMPORTANT

                curPayment = curPaymentGroup.CreatePayment(amountCents, reference, fromAccount, key, hasImage);
                curPaymentGroupAmountCents += amountCents;
                break;

            case "25":     // Payment info: Freeform
                if (curPayment == null)
                {
                    throw new InvalidOperationException("Payment start must precede payment information");
                }
                curPayment.AddInformation(PaymentInformationType.Freeform, line.Substring(2, 50).Trim());
                break;

            case "26":     // Payment info: Name
                if (curPayment == null)
                {
                    throw new InvalidOperationException("Payment start must precede payment information");
                }
                curPayment.AddInformation(PaymentInformationType.Name, line.Substring(2, 35).Trim());
                break;

            case "27":     // Payment info: Street, postal code
                if (curPayment == null)
                {
                    throw new InvalidOperationException("Payment start must precede payment information");
                }
                curPayment.AddInformation(PaymentInformationType.Street, line.Substring(2, 35).Trim());
                curPayment.AddInformation(PaymentInformationType.PostalCode, line.Substring(37, 9).Replace(" ", ""));     // also removes inspace
                break;

            case "28":     // Payment info: City, Country
                if (curPayment == null)
                {
                    throw new InvalidOperationException("Payment start must precede payment information");
                }
                curPayment.AddInformation(PaymentInformationType.City, line.Substring(2, 35).Trim());
                curPayment.AddInformation(PaymentInformationType.Country, line.Substring(37, 35).Trim());
                curPayment.AddInformation(PaymentInformationType.CountryCode, line.Substring(72, 2).Trim());
                break;

            case "29":     // Payment info: City, Country
                if (curPayment == null)
                {
                    throw new InvalidOperationException("Payment start must precede payment information");
                }
                curPayment.AddInformation(PaymentInformationType.OrgNumber, line.Substring(2, 12).Trim());
                break;

            case "15":     // End payment group
                if (curPaymentGroup == null)
                {
                    throw new InvalidOperationException("Payment group start must precede payment group end");
                }
                string tag = DateTime.Today.Year.ToString() + line.Substring(45, 5);
                Int64  reportedAmountCents = Int64.Parse(line.Substring(50, 18), CultureInfo.InvariantCulture);
                curPaymentGroup.AmountCents = curPaymentGroupAmountCents;
                curPaymentGroup.Tag         = tag;
                curPaymentGroup.Open        = true; // flags payment group as ready

                curPaymentGroup.MapTransaction();

                curPaymentGroup = null;
                break;

            case "70":     // BGMAX termination
                break;     // don't care

            default:
                break;     // don't care about other fields
            }
        }

        ScriptManager.RegisterStartupScript(this, Page.GetType(), "alldone",
                                            "alert ('Timestamp: " + timestamp.ToString("yyyy-MM-dd HH:mm:ss.ffffff") + "');",
                                            true);
    }