Post Chrome/Safari发布问题

Post Chrome/Safari发布问题,post,paypal,cross-browser,Post,Paypal,Cross Browser,我在.Net中编写了一个小型购物车,在订单完成后将某人发送到PayPal进行付款。要将它们发送到PayPal,我在一个通用的“PayPal.aspx”页面上有以下表单,该页面上没有真正的用户功能: <form action="http://www.paypal.com/cgi-bin/webscr" method="post" id="paypal"> <input type="hidden" name="cmd" value="_cart"/> &l

我在.Net中编写了一个小型购物车,在订单完成后将某人发送到PayPal进行付款。要将它们发送到PayPal,我在一个通用的“PayPal.aspx”页面上有以下表单,该页面上没有真正的用户功能:

<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@email.com"/>
     <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填充PayPal所需的值,如下所示:

    <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>
public static void AppendValue(this StringBuilder sb, string name, string value, bool encode)
{
    sb.Append("&" + name + "=" + ((encode) ? HttpUtility.UrlEncode(value) : value));
}

var paypal=document.getElementById('paypal');
函数加载(){
系统应用。移除负载(负载);
//设置项目
var ppItems=document.getElementById('ppItems');
var items=ppItems.getElementsByTagName('input');
var itemscont=items.length/3;
对于(变量i=0;i

因此,当加载整个页面时,将根据查询字符串guid值从数据库中提取订单详细信息,一堆隐藏字段由数据库中的必需值填充,然后上面的脚本在加载时运行,设置PayPal表单值,然后单击隐藏按钮将所有内容提交给PayPal,以便最终用户可以付款

我的问题是,这在IE7+和FF3+中非常有效,但Safari和Chrome的表现并不好。在Chrome中,我被重定向到PayPal主页,而不是被要求付款。Safari甚至更奇怪,因为两者都重定向到PayPal支付屏幕(如IE7+和FF3+),但在Mac版本上,它并没有预先填充所有字段(特别是名称/地址),而是填充其他字段(如金额、税款等)


有谁能提供一些建议,解释为什么Chrome和Safari(仅Mac版)无法正常工作,但其他一切似乎都正常工作?

我最终找到了答案,选择了老派路线,重新定向,而不是执行帖子:

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@domain.com", 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());

如果PayPal找不到“cmd”的正确值,它将重定向到主页。我认为您的代码对于它需要做的事情来说过于复杂;据我所知,这里根本不需要任何JavaScript。为什么不需要JS呢?我应该改为使用HttpWebRequest/POST吗?事实上,我认为这不管用,不是吗?因为我实际上是想重定向到PP作为POST选项的一部分。