【问题标题】:Chrome/Safari POST IssuesChrome/Safari POST 问题
【发布时间】:2011-08-27 01:35:02
【问题描述】:

我在 .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>

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

我遇到的问题是这在 IE7+ 和 FF3+ 中运行良好,但 Safari 和 Chrome 不能很好地运行。在 Chrome 中,我被重定向到 PayPal 主页,而不是被要求付款。 Safari 甚至更奇怪,因为它们都重定向到 PayPal 支付屏幕(如 IE7+ 和 FF3+),但在 Mac 版本上,它不会预先填充所有字段(特别是名称/地址),而是其他字段(如金额,税收等)确实会填充。

谁能提供任何关于为什么 Chrome 和 Safari(仅限 Mac)不能正常工作,但其他一切似乎都正常的建议?

【问题讨论】:

  • 如果 'cmd' 找不到合适的值,PayPal 将重定向到主页。我认为您的代码对于它需要做的事情过于复杂;据我所知,这里根本不需要任何 JavaScript。
  • 为什么你认为不需要 JS?我应该改用 HttpWebRequest/POST 吗?
  • 实际上,我认为这行不通,不是吗?因为我实际上是在尝试重定向到 PP 作为 POST 选项的一部分。

标签: post paypal cross-browser


【解决方案1】:

我终于通过旧学校路线和重定向而不是执行 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@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());

AppendValue 方法是 StringBuilder 的扩展方法,它看起来像这样:

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

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 2011-04-30
    • 2014-05-15
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    相关资源
    最近更新 更多