获取ipn option_name_1_1 C#的变量

时间:2013-10-23 20:14:02

标签: c# paypal-ipn

我有一个paypal购物车按钮,有3个选项下拉菜单。第一个是Style - Tee,Sweat,Hood - 这个价格与每个价格相关,然后还有两个尺寸和颜色。这是一个C#ASP.NET应用程序。如果我查看侦听器获取的整个响应(所有变量),则样式为“option_name1_1”,并返回“Style”。但是在列表'option_selection_1_1'=“Sweat”中,这是我选择的,但是当我调用该值时它没有显示任何内容。这是将响应提供给用户的代码。

    if (strResponse.StartsWith("SUCCESS"))
            {

                PDTHolder pdt = PDTHolder.Parse(strResponse);

                Label1.Text =
                    string.Format("" + pdt.PayerFirstName + " " + pdt.PayerLastName   + " for your payment of " + pdt.GrossTotal + " " + pdt.Currency + " option:"+pdt.OptionName+" : "+pdt.OptionOne+":"+pdt.OptionName2+ " :" + pdt.OptionTwo+"!",
                    pdt.PayerFirstName, pdt.PayerLastName,
                    pdt.PayerEmail, pdt.GrossTotal, pdt.Currency);

这是侦听器用来解析响应的类。

    public class PDTHolder
{
    public PDTHolder()
    {
    }
        private double grosstotal;
        public double GrossTotal
        {
            get { return grosstotal; }
            set { grosstotal = value; }
        }

        private int invoicenumber;
        public int InvoiceNumber 
        {
            get { return invoicenumber; }
            set { invoicenumber = value; }
        }

        private string paymentstatus;

        public string PaymentStatus 
        {
            get  {return paymentstatus; }
            set { paymentstatus = value; }
        }

        private string payerfirstname;

        public string PayerFirstName {
            get { return payerfirstname; }
            set { payerfirstname = value; }
        }

        private double paymentfee;

        public double PaymentFee {
            get { return paymentfee; }
            set { paymentfee = value; }
        }

        private string businessemail;
        public string BusinessEmail {
            get { return businessemail; }
            set { businessemail = value; }
        }

        private string payeremail;
        public string PayerEmail {
            get { return payeremail; }
            set { payeremail = value; }
        }

        private string txtoken;
        public string TxToken {
            get { return txtoken; }
            set { txtoken = value; }
        }
        private string payerlastname;
        public string PayerLastName {
            get { return payerlastname; }
            set { payerlastname = value; } 
        }

        private string receiveremail;

        public string ReceiverEmail {
            get { return receiveremail; }
            set { receiveremail = value; } 
        }

        private string itemname;
        public string ItemName {
            get { return itemname; }
            set { itemname = value; }
        }
        private string currency;

        public string Currency {
            get { return currency; }
            set {currency = value; }
        }

        private string transactionid;
        public string TransactionId {
            get { return transactionid; }
            set { transactionid = value; }
        }

        private string subscriberid;
        public string SubscriberId {
            get { return subscriberid; }
            set { subscriberid = value; }
        }

        private string custom;
        public string Custom {
            get { return custom; }
            set { custom = value; }
        }
    private string optionone;
    public string OptionOne{
        get{ return optionone;}
        set{optionone = value;}
    }
    private string optionname;
    public string OptionName
    {
        get { return optionname; }
        set { optionname = value; }
    }

       private string optiontwo;
        public string OptionTwo{
        get{ return optiontwo;}
        set{optiontwo = value;}
    }
    private string optionname2;
    public string OptionName2
    {
        get { return optionname2; }
        set { optionname2 = value; }
    }

    private double price;

        public static PDTHolder Parse(string postData)
        {
            String sKey, sValue;
            PDTHolder ph = new PDTHolder();

            try
            {
                //split response into string array using whitespace delimeter
                String[] StringArray = postData.Split('\n');

                // NOTE:
                /*
                * loop is set to start at 1 rather than 0 because first
                string in array will be single word SUCCESS or FAIL
                Only used to verify post data
                */

                // use split to split array we already have using "=" as delimiter
                int i;
                for (i = 1; i < StringArray.Length - 1; i++)
                {
                    String[] StringArray1 = StringArray[i].Split('=');

                    sKey = StringArray1[0];
                    sValue = HttpUtility.UrlDecode(StringArray1[1]);

                    // set string vars to hold variable names using a switch
                    switch (sKey)
                    {
                        case "mc_gross":
                            ph.GrossTotal = Convert.ToDouble(sValue);
                            break;

                        case "invoice":
                            ph.InvoiceNumber = Convert.ToInt32(sValue);
                            break;

                        case "payment_status":
                            ph.PaymentStatus = Convert.ToString(sValue);
                            break;

                        case "first_name":
                            ph.PayerFirstName = Convert.ToString(sValue);
                            break;

                        case "mc_fee":
                            ph.PaymentFee = Convert.ToDouble(sValue);
                            break;

                        case "business":
                            ph.BusinessEmail = Convert.ToString(sValue);
                            break;

                        case "payer_email":
                            ph.PayerEmail = Convert.ToString(sValue);
                            break;

                        case "Tx Token":
                            ph.TxToken = Convert.ToString(sValue);
                            break;

                        case "last_name":
                            ph.PayerLastName = Convert.ToString(sValue);
                            break;

                        case "receiver_email":
                            ph.ReceiverEmail = Convert.ToString(sValue);
                            break;

                        case "item_name":
                            ph.ItemName = Convert.ToString(sValue);
                            break;

                        case "mc_currency":
                            ph.Currency = Convert.ToString(sValue);
                            break;

                        case "txn_id":
                            ph.TransactionId = Convert.ToString(sValue);
                            break;

                        case "custom":
                            ph.Custom = Convert.ToString(sValue);
                            break;

                        case "subscr_id":
                            ph.SubscriberId = Convert.ToString(sValue);
                            break;

                        case "option_selection1_1":
                            ph.OptionOne = Convert.ToString(sValue);
                            break;

                           case "option_name1_1":
                            ph.OptionName = Convert.ToString(sValue);
                            break;

                            case "option_selection1_2":
                            ph.OptionTwo = Convert.ToString(sValue);
                            break;

                            case "option_name1_2":
                            ph.OptionName2 = Convert.ToString(sValue);
                            break;
                    }
                }

                return ph;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

以下是我的IPN的片段:我希望它有所帮助:

for (var iloop = 1; iloop <= Convert.ToInt32(inTransaction["num_cart_items"]); iloop++)
                {

                    var currentitem = new PurchasedItem(ID, inTransaction["item_name" + iloop],
                                                        inTransaction["mc_gross_" + iloop], inTransaction["tax" + iloop],
                                                        inTransaction["quantity" + iloop], inTransaction["item_number" + iloop],
                                                        inTransaction["mc_shipping" + iloop], inTransaction["mc_handling" + iloop],
                                                        new Dictionary<string, string>());

                    foreach (var argument in inTransaction)
                    {
                        var match = Regex.Match(argument.ToString(),  @"option_name(\d)_" + iloop);
                        if (!match.Success) continue;
                        var buildselection = "option_selection" + match.Groups[1].Value + "_" + iloop;
                        var key = inTransaction[argument.ToString()];
                        var value = inTransaction[buildselection];
                        currentitem.AddAdditionalInfo(new KeyValuePair<string, string>(key, value));
                    }
                    PurchasedItem.Add(currentitem);
                }
                WireAllComponents();