我在.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)无法正常工作的建议,但其他一切似乎都可以吗?
答案 0 :(得分: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@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));
}