126 messaggi dal 12 febbraio 2007
Ciao a tutti, dovrei realizzare un modulo ordine tramite paypal in asp.net 2.0; ho trovato una classe che mi fa il post del form verso il link di paypal con i parametri.
La classe utilizza l'HttpWebRequest,
concluso tutto...nn mi da errori..ma nn mi rimanda alla pagina di paypal, al click del pulsante rimane dov'è...
i valori dei campi sono giusti è tutto corretto ma la pagina asp.net rimane li ferma...

a qualcuno è capitato? suggerimenti?

Posto il codice:

 
//chiamata

   PostSubmitter _rP = new PostSubmitter();
                      
            _rP.Url = "https://www.paypal.com/cgi-bin/webscr";
            _rP.PostItems.Add("cmd", "_xclick");
            _rP.PostItems.Add("business", "xxx@xxxx.xx");
            _rP.PostItems.Add("item_name", ....
ecc....

            _rP.Type = PostSubmitter.PostTypeEnum.Post;
            string result = _rP.Post();

 public class PostSubmitter
    {
        /// <summary>
        /// determines what type of post to perform.
        /// </summary>
        public enum PostTypeEnum
        {
            /// <summary>
            /// Does a get against the source.
            /// </summary>
            Get,
            /// <summary>
            /// Does a post against the source.
            /// </summary>
            Post
        }

        private string m_url = string.Empty;
        private NameValueCollection m_values = new NameValueCollection();
        private PostTypeEnum m_type = PostTypeEnum.Get;
        /// <summary>
        /// Default constructor.
        /// </summary>
        public PostSubmitter()
        {
        }

        /// <summary>
        /// Constructor that accepts a url as a parameter
        /// </summary>
        /// <param name="url">The url where the post will be submitted to.</param>
        public PostSubmitter(string url)
            : this()
        {
            m_url = url;
        }

        /// <summary>
        /// Constructor allowing the setting of the url and items to post.
        /// </summary>
        /// <param name="url">the url for the post.</param>
        /// <param name="values">The values for the post.</param>
        public PostSubmitter(string url, NameValueCollection values)
            : this(url)
        {
            m_values = values;
        }

        /// <summary>
        /// Gets or sets the url to submit the post to.
        /// </summary>
        public string Url
        {
            get
            {
                return m_url;
            }
            set
            {
                m_url = value;
            }
        }
        /// <summary>
        /// Gets or sets the name value collection of items to post.
        /// </summary>
        public NameValueCollection PostItems
        {
            get
            {
                return m_values;
            }
            set
            {
                m_values = value;
            }
        }
        /// <summary>
        /// Gets or sets the type of action to perform against the url.
        /// </summary>
        public PostTypeEnum Type
        {
            get
            {
                return m_type;
            }
            set
            {
                m_type = value;
            }
        }
        /// <summary>
        /// Posts the supplied data to specified url.
        /// </summary>
        /// <returns>a string containing the result of the post.</returns>
        public string Post()
        {
            StringBuilder parameters = new StringBuilder();
            for (int i = 0; i < m_values.Count; i++)
            {
                EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
            }
            string result = PostData(m_url, parameters.ToString());
            return result;
        }
        /// <summary>
        /// Posts the supplied data to specified url.
        /// </summary>
        /// <param name="url">The url to post to.</param>
        /// <returns>a string containing the result of the post.</returns>
        public string Post(string url)
        {
            m_url = url;
            return this.Post();
        }
        /// <summary>
        /// Posts the supplied data to specified url.
        /// </summary>
        /// <param name="url">The url to post to.</param>
        /// <param name="values">The values to post.</param>
        /// <returns>a string containing the result of the post.</returns>
        public string Post(string url, NameValueCollection values)
        {
            m_values = values;
            return this.Post(url);
        }
        /// <summary>
        /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
        /// </summary>
        /// <param name="postData">The data to post.</param>
        /// <param name="url">the url to post to.</param>
        /// <returns>Returns the result of the post.</returns>
        private string PostData(string url, string postData)
        {
            HttpWebRequest request = null;
            if (m_type == PostTypeEnum.Post)
            {
                Uri uri = new Uri(url);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;
                using (Stream writeStream = request.GetRequestStream())
                {
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] bytes = encoding.GetBytes(postData);
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }
            else
            {
                Uri uri = new Uri(url + "?" + postData);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";
            }
            string result = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// Encodes an item and ads it to the string.
        /// </summary>
        /// <param name="baseRequest">The previously encoded data.</param>
        /// <param name="dataItem">The data to encode.</param>
        /// <returns>A string containing the old data and the previously encoded data.</returns>
        private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
        {
            if (baseRequest == null)
            {
                baseRequest = new StringBuilder();
            }
            if (baseRequest.Length != 0)
            {
                baseRequest.Append("&");
            }
            baseRequest.Append(key);
            baseRequest.Append("=");
            baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
        }
    }
}
Se vuoi un redirect fisico sul sito paypal devi usare un form che posti i tuoi dati all'indirizzo paypal.
Con HttpWebRequest effettui una invio "dietro le quinte" ovvero simuli il comportamento di un browser... quindi ovviamente non c'e' redirect.
supponi che esita una pagina/applicazione che accetti un parametro in ingresso via post e restiuisca a "a video / nel broweser" il parametro inviato...
con HttpWebRequest puoi inviare questo parametro e ottenere da codice la risposta che poi decidi di gestire nel tuo codice...
in altre parole...HttpWebRequest funziona come un webservice ovvero invia una richiesta e ottini la risposta da codice: quando chiami un ws non ti aspetti che venga fisicamente effettuato un redirect su il sito di chi fornisce il ws esatto?
spero di essere stato chiaro
126 messaggi dal 12 febbraio 2007
perfetto
ho capito....
ma quindi nn c'è modo d simulare un post verso paypal?
Premetto che non ho mai usato Paypal...
pero' da quanto dici il modo c'e' ed e' molto piu' semplice di quanto hai realizzato :-)
normalmente i gateway di pagmaneto, nella modalita' + semplice, lavorano in questo modo:
- effettui dal tuo sito un post al sito/applicazione del gateway passando i parametri richiesti dal gateway tra cui 3 url di risposta a cui il gateway inviera' comunque due risposte "ok o ko" e "server2server":
1_url sul tuo sito per msg ok(user friendly)
2_url sul tuo sito per msg ko(user friendly)
3_url sul tuo sito per comunicazione server2server

il post lo effettui nella modalita' richiesta dal gaetway ovvero get o post.
se e' post la modalita' devi semplicemente creare un form che abbia come action l'indirizzo della procedura del gateway e contenga i campi, normalmente hidden field, richiesti dal gateway
ti faccio un esempio
<code>
<form action="http://www.paypal.com/payment....." method="POST">
<input type="hidden" name="business" value="xxx@xxxx.xx" />
<input type="hidden" ....
</form>
</code>
Modificato da crixo il 02 maggio 2007 07.53 -
126 messaggi dal 12 febbraio 2007
ah posso utilizzare il semplice post allora.......



Grazie! è + semplice del previsto in effetti.!

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.