2011-08-27 231 views
1

我寫在.net小購物車,訂單完成後,將有人來貝寶進行支付。送他們到貝寶我有一個通用的「paypal.aspx」頁上的以下表單上有沒有真正的用戶功能:的Chrome/Safari瀏覽器後問題

<form action="http://www.paypal.com/cgi-bin/webscr" method="post" id="paypal"> 
    <input type="hidden" name="cmd" value="_cart"/> 
    <input type="hidden" name="upload" value="1"/> 
    <input type="hidden" name="business" value="[email protected]"/> 
    <input type="hidden" name="tax_cart" id="tax_cart" /> 
    <input type="hidden" name="handling_cart" id="handling_cart" /> 
    <input type="hidden" name="return" id="return" value='<%="http://" + Request.Url.Authority + "/" %>' /> 
    <input type="hidden" name="custom" id="custom" /> 
    <input type="hidden" name="first_name" id="first_name" /> 
    <input type="hidden" name="last_name" id="last_name" /> 
    <input type="hidden" name="address1" id="address1" /> 
    <input type="hidden" name="address2" id="address2" /> 
    <input type="hidden" name="city" id="city" /> 
    <input type="hidden" name="state" id="state" /> 
    <input type="hidden" name="zip" id="zip" /> 
    <input type="submit" id="paypalButton" value="PayPal" style="display: none; visibility: hidden;"/> 
</form> 

當含有這種形式加載的頁面,還有另一種形式的正下方有一堆隱藏的字段用來自實際訂單(基於Guid查詢字符串值拉取)的數據填充。 JavaScript的在填寫表單中的值,貝寶需要這樣的:

<form runat="server"> 
<asp:ScriptManager runat="server" /> 
<div id="ppItems"> 
    <asp:Repeater runat="server" ID="rpItems"> 
     <ItemTemplate> 
      <input type="hidden" id='<%#"hv_quantity_" + Container.ItemIndex %>' value='<%#Eval("Quantity") %>' /> 
      <input type="hidden" id='<%#"hv_item_" + Container.ItemIndex %>' value='<%#Eval("Color") + " Tactical Hat" %>' /> 
      <input type="hidden" id='<%#"hv_amount_" + Container.ItemIndex %>' value='<%#Eval("UnitCost", "{0:n2}") %>' /> 
     </ItemTemplate> 
    </asp:Repeater> 
</div> 

<asp:HiddenField runat="server" ID="hvCustom" /> 
<asp:HiddenField runat="server" ID="hvTax" /> 
<asp:HiddenField runat="server" ID="hvShipping" /> 
<asp:HiddenField runat="server" ID="hvFirstName" /> 
<asp:HiddenField runat="server" ID="hvLastName" /> 
<asp:HiddenField runat="server" ID="hvAddress1" /> 
<asp:HiddenField runat="server" ID="hvAddress2" /> 
<asp:HiddenField runat="server" ID="hvCity" /> 
<asp:HiddenField runat="server" ID="hvState" /> 
<asp:HiddenField runat="server" ID="hvZip" /> 

<script type="text/javascript" language="javascript"> 
    var paypal = document.getElementById('paypal'); 

    function load() { 
     Sys.Application.remove_load(load); 

     //set the items 
     var ppItems = document.getElementById('ppItems'); 
     var items = ppItems.getElementsByTagName('input'); 
     var itemsCount = items.length/3; 

     for (var i = 0; i < itemsCount; ++i) { 
      var quantity = document.getElementById('hv_quantity_' + i).value; 
      var item = document.getElementById('hv_item_' + i).value; 
      var amount = document.getElementById('hv_amount_' + i).value; 

      addFormItem('item_name_' + (i + 1), quantity + " " + item); 
      addFormItem('amount_' + (i + 1), amount); 
      addFormItem('quantity_' + (i + 1), quantity); 
     } 

     //set global items 
     var custom = document.getElementById('<%=this.hvCustom.ClientID %>').value; 
     document.getElementById('tax_cart').value = document.getElementById('<%=hvTax.ClientID %>').value; 
     document.getElementById('handling_cart').value = document.getElementById('<%=this.hvShipping.ClientID %>').value; 
     document.getElementById('return').value += 'thanks.aspx?p=' + custom; 
     document.getElementById('custom').value = custom; 
     document.getElementById('first_name').value = document.getElementById('<%=this.hvFirstName.ClientID %>').value; 
     document.getElementById('last_name').value = document.getElementById('<%=this.hvLastName.ClientID %>').value; 
     document.getElementById('address1').value = document.getElementById('<%=this.hvAddress1.ClientID %>').value; 
     document.getElementById('address2').value = document.getElementById('<%=this.hvAddress2.ClientID %>').value; 
     document.getElementById('city').value = document.getElementById('<%=this.hvCity.ClientID %>').value; 
     document.getElementById('state').value = document.getElementById('<%=this.hvState.ClientID %>').value; 
     document.getElementById('zip').value = document.getElementById('<%=this.hvZip.ClientID %>').value; 

     //submit to paypal 
     document.getElementById('paypalButton').click(); 
    } 

    function addFormItem(name, value) { 
     var item = document.createElement('input'); 
     item.setAttribute('type', 'hidden'); 
     item.setAttribute('name', name); 
     item.setAttribute('id', name); 
     item.setAttribute('value', value); 

     paypal.appendChild(item); 
    } 

    Sys.Application.add_load(load); 

</script> 

所以,當這整體頁面加載,以便細節是從基於數據庫的查詢字符串GUID值拉,一堆隱藏字段中填充了數據庫中所需的值,然後上面的腳本在負載上運行,設置PayPal表單值,然後單擊隱藏按鈕將所有內容提交給PayPal,以便最終用戶可以付款。

我的問題是在IE7 +和FF3 +這個偉大的工程,但Safari和Chrome不是在玩好聽。在Chrome中,我被重定向到貝寶主頁,而不是被要求付費。 Safari瀏覽器甚至更加陌生,因爲它們都重定向到PayPal支付屏幕(如IE7 +和FF3 +),但在Mac版本中,它並不預先填充所有字段(特別是名稱/地址),而是其他字段(如金額,稅收等)填充。

任何人都可以提供任何建議,爲什麼鉻和Safari(Mac只)不能正常工作,但一切似乎?

+1

PayPal將重定向到主頁,如果它不能找到「CMD」一個適當的值。 我認爲你的代碼過於複雜,它需要做什麼;據我所知,這裏不需要任何JavaScript。 – Robert

+0

你爲什麼不認爲JS是需要的?我應該用HttpWebRequest/POST來取代嗎? – RubyHaus

+0

其實,我不認爲這會起作用,是嗎?因爲我實際上試圖重定向到PP作爲POST選項的一部分。 – RubyHaus

回答

0

我終於想通了由去老同學路線和重定向,而不是執行POST:

var order = (from o in data.Orders where o.PayPal == Request.QueryString["p"] select o).Single(); 

//build the generic PP detail 
sb.Append("http://www.paypal.com/cgi-bin/webscr/?cmd=_cart"); 
sb.AppendValue("upload", "1", false); 
sb.AppendValue("business", "[email protected]", true); 
sb.AppendValue("handling_cart", (this.CurrentCart.Tax.HasValue ? this.CurrentCart.Tax.ToString() : "0"), false); 
sb.AppendValue("return", this.Request.Url.Authority, true); 
sb.AppendValue("custom", order.PayPal, true); 
sb.AppendValue("first_name", (order.Customer.Name.IndexOf(" ") > -1) ? order.Customer.Name.Split(' ')[0] : order.Customer.Name, true); 
sb.AppendValue("last_name", (order.Customer.Name.IndexOf(" ") > -1) ? order.Customer.Name.Split(' ')[1] : order.Customer.Name, true); 
sb.AppendValue("address1", order.Customer.Address1, true); 
sb.AppendValue("address2", order.Customer.Address2, true); 
sb.AppendValue("city", order.Customer.City, true); 
sb.AppendValue("state", order.Customer.StateProvince, true); 
sb.AppendValue("zip", order.Customer.PostalCode, true); 

for (int i = 0; i < order.OrderItems.Count; ++i) 
{ 
    string index = (i + 1).ToString(); 
    sb.AppendValue("item_name_" + index, order.OrderItems[i].ProductAttribute.Name, true); 
    sb.AppendValue("amount_" + index, string.Format("{0:n2}", order.OrderItems[i].UnitCost), false); 
    sb.AppendValue("quantity_" + index, order.OrderItems[i].Quantity.ToString(), true); 
} 

Response.Redirect(sb.ToString()); 

的AppendValue方法是一個StringBuilder擴展方法,它看起來像這樣:

public static void AppendValue(this StringBuilder sb, string name, string value, bool encode) 
{ 
    sb.Append("&" + name + "=" + ((encode) ? HttpUtility.UrlEncode(value) : value)); 
}