PayPal Auto Return无法正常工作

时间:2013-08-08 21:10:24

标签: paypal paypal-sandbox donations

启用自动退货,捐赠已过帐,但用户必须单击黄色“返回协调员”按钮才能返回。如果你看到这个片段有什么问题,请告诉我..

P.S。我不想使用自定义按钮。

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">                    
    <!-- Identify your business so that you can collect the payments. -->                    
    <input type="hidden" name="business" value="email@gmail.com">                    
    <!-- Specify a Donate button. -->                    
    <input type="hidden" name="cmd" value="_donations">                    
    <!-- Specify details about the contribution -->                    
    <input type="hidden" name="item_name" value="@Resources.Donations.Strings.ThankYou">                    
    <input type="hidden" name="return" value="http://www.return.com/thankyou"/>                    
    <input type="hidden" name="cancel_return" value="http://www.return.com/cancel"/>                    
    <input type="hidden" name="rm" value="2"/>                    
    <input type="hidden" name="item_number" value="5.00">                     
    <select name="amount" onchange="$('input[name=item_number]').val($(this).val())">                        
        <option value="1.00">$1.00</option>                            
        <option value="5.00" selected>$5.00</option>                        
        <option value="10.00">$10.00</option>                        
        <option value="15.00">$15.00</option>                        
        <option value="25.00">$25.00</option>                        
        <option value="50.00">$50.00</option>                        
        <option value="">@Resources.Donations.Labels.Other</option>                    
    </select>                    
    <input type="hidden" name="currency_code" value="USD">                    
    <!-- Display the payment button. -->                    
    <button type="submit" name="submit" class="btn btn-large btn-paypal"></button>                
</form> 

1 个答案:

答案 0 :(得分:0)

正如安德鲁建议的那样,我重构了我的代码以使用IPN。这是我开始使用的一些代码..这是一些带有日志记录的代码,可以帮助您入门..

HTML表单:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <!-- Identify your business so that you can collect the payments. -->
    <input type="hidden" name="business" value="donations@myemail.org">
    <!-- Specify a Donate button. -->
    <input type="hidden" name="cmd" value="_donations">
    <!-- Specify details about the contribution -->
    <input type="hidden" name="item_name" value="@Resources.Donations.Strings.ThankYou">
    <input type="hidden" name="return" value="auto_return_url_here"/>
    <input type="hidden" name="notify_url" value="IPN_notify_url_here"/>
    <input type="hidden" name="cancel_return" value="user_clicked_cancel_url_here"/>
    <input type="hidden" name="rm" value="2" />
    <input type="hidden" name="item_number" value="5.00">
    <select name="amount" onchange="$('input[name=item_number]').val($(this).val())">
        <option value="1.00">$1.00</option>
        <option value="5.00" selected>$5.00</option>
        <option value="10.00">$10.00</option>
        <option value="15.00">$15.00</option>
        <option value="25.00">$25.00</option>
        <option value="50.00">$50.00</option>
        <option value="">Other</option>
    </select>
    <input type="hidden" name="currency_code" value="USD">
    <!-- Display the payment button. -->
    <button type="submit" name="submit" class="btn btn-large btn-paypal"></button>
</form>

IPN处理程序示例:

    [HttpPost]        
    [AllowAnonymous]        
    public ActionResult PayPalDonationIpn(PayPalModel model)        
    {            
        Logger.InfoFormat("{0} - {1}", 
model.txn_id, model.payment_status);            
        Logger.Info(model.ToXml());  

        if (!model.receiver_email.Equals(PayPalEmail, StringComparison.OrdinalIgnoreCase))            
        {               
            Logger.WarnFormat("Email 'receiver_email' value '{0}' does not match our email '{1}'.", model.receiver_email, base.PayPalEmail);            
        }            
        if (model.txn_id == null)            
        {                
            Logger.Warn("Parameter 'txn_id' is empty.");            
        } 

        var request = (HttpWebRequest)WebRequest.Create(PayPalPostUri);            
        request.Method = "POST";            
        request.ContentType = "application/x-www-form-urlencoded";            
        var param = Request.BinaryRead(Request.ContentLength);            
        var message = Encoding.ASCII.GetString(param);            
        message += "&cmd=_notify-validate";            
        request.ContentLength = message.Length;   

        //send the request to PayPal and get the response            
        using (var streamOut = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))            
        {                
            streamOut.Write(message);                
            streamOut.Close();  

            using (var streamIn = new StreamReader(request.GetResponse().GetResponseStream()))               
            {                    
                var response = streamIn.ReadToEnd();                    
                streamIn.Close();                    

                Logger.Info(response);                    
                if (response.Equals("VERIFIED", StringComparison.OrdinalIgnoreCase))                    
                {                        
                    Logger.InfoFormat("{0} - {1}, {2}", model.txn_id, model.mc_gross, model.mc_currency);                        
                    //check the payment_status is Completed                        
                    //check that txn_id has not been previously processed                        
                    //check that payment_amount/payment_currency are correct                        
                    //process payment                    
                }                   
                else if (response.Equals("INVALID", StringComparison.OrdinalIgnoreCase))                   
                {                        
                    Logger.WarnFormat("INVALID response: {0}.", message);                    
                }                   
                else                    
                {                        
                    Logger.WarnFormat("No status. Manual investinagation needed for {0}.", message);                    
                }                
            }            
        }            
        return new EmptyResult();        
    }  

这是带有变量名称的PayPalModel:

public class PayPalModel
    {
        public string mc_gross { get; set; }
        public string protection_eligibility { get; set; }
        public string address_status { get; set; }
        public string payer_id { get; set; }
        public string tax { get; set; }
        public string address_street { get; set; }
        public string payment_date { get; set; }
        public string payment_status { get; set; }
        public string charset { get; set; }
        public string address_zip { get; set; }
        public string first_name { get; set; }
        public string address_country_code { get; set; }
        public string address_name { get; set; }
        public string notify_version { get; set; }
        public string custom { get; set; }
        public string payer_status { get; set; }
        public string address_country { get; set; }
        public string address_city { get; set; }
        public string quantity { get; set; }
        public string payer_email { get; set; }
        public string verify_sign { get; set; }
        public string txn_id { get; set; }
        public string payment_type { get; set; }
        public string payer_business_name { get; set; }
        public string last_name { get; set; }
        public string address_state { get; set; }
        public string receiver_email { get; set; }
        public string pending_reason { get; set; }
        public string txn_type { get; set; }
        public string item_name { get; set; }
        public string mc_currency { get; set; }
        public string item_number { get; set; }
        public string residence_country { get; set; }
        public string test_ipn { get; set; }
        public string transaction_subject { get; set; }
        public string payment_gross { get; set; }
        public string merchant_return_link { get; set; }
        public string auth { get; set; }
        public string ipn_track_id { get; set; }
    }