第一步、将购物车生成的单据信息绑定显示在要提交表单中并发送支付请求如下:
<form id="form1" name="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> 相关的单据信息如商品名称,总金额,订单号等。 <input name="Paypal" type="button" style="height: 25px; margin-right: 20px; cursor: pointer; margin-left: 40px; width: 80px;" value="Pay Now" onclick="javaScript:if(submitorder()==true){this.form.submit();}" /> <div style="display: none"> <!-- 支付方式:--> <input type="text" name="cmd" value="_xclick" /><br /> <!--这里填写你的paypal账户email如你的qq邮箱注册的paypay帐号 邮箱: --> <input type="hidden" name="business" value="[email protected]" /><br /> <!--订单信息: 这里填写客户订单的一些相关信息,当客户连到paypal网站付款的时候将看到这些信息 【写备注】--> <input type="hidden" name="item_name" value=" Confirm Order information" /><br /> <!--订单编号--> <input type="hidden" name="item_number" value="<%=_ordernum %>" /><br /> <!--订单总金额 --> <input type="hidden" name="amount" value="<%=_amount %>" /><br /> <!-- 订单数量: <%=_Number %> --> <input type="hidden" name="quantity" value="1" /><br /> <!-- 订单币种: 订单总金额对应的货币类型 ,客户可以用其他币种来付款,比如这里订单币种是美元USD,客户可以用欧元EUR来付款,由paypal根据当前汇率自动实现币种之间的换算--> <input type="hidden" name="currency_code" value="USD" /><br /> <!--编码格式 --> <input type="hidden" name="charset" value="utf-8" /><br /> <%-- <input type="hidden" name="noshipping" value="1"><br />--%> <!--运费 --> <input type="hidden" name="shipping" value="<%=ReceivedPostFee.ToString("f2") %>" /><br /> <!--验证加密 password_validate 可额外设置加密方式--> <input type="hidden" name="custom" value="<%=itemname %> " /><br /> <!-- 返回的路径:付款后提示给客户如支付成功。单号是多少。金额是多少,大概什么时候收到货等--> <input type="hidden" name="return" value="backurl_info" /><br /> <!-- 取消后返回的页面:如用户不想支付可以跳到指定的页面--> <input type="hidden" name="cancel_return" value="cancelbackurl_info" /> <!--这里显示商户的logo图片在提交的时候显示,如果没有则显示邮箱的账户--> <input type="hidden" name="image_url" value="url/pay.png" /> <!--这里告诉paypal付款的通信url,即当客户付款后调用这个url通知系统--> <input type="text" name="notify_url" style="width: 500px" value="responsepost_url" /><br /> </div> </form>
第二步、paypal服务器接收通信请求后返回数据进行校验,根据校验结果做相应的处理:
private DataContext db = new DataContext(); protected void Page_Load(object sender, EventArgs e) { // CUSTOMIZE THIS: This is the seller‘s Payment Data Transfer authorization token. // Replace this with the PDT token in "Website Payment Preferences" under your account. string authToken = "商户号【authtoken】一系列长串的字符,注册paypal商户号上会有"; string txToken = Request.QueryString["tx"]; string query = "cmd=_notify-synch&tx=" + txToken + "&at=" + authToken; //Post back to either sandbox or live string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr"; //string strLive = "https://www.paypal.com/cgi-bin/webscr"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox); //Set values for the request back req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = query.Length; //Send the request to PayPal and get the response StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII); streamOut.Write(query); streamOut.Close(); StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()); string strResponse = streamIn.ReadToEnd(); streamIn.Close(); Dictionary<string, string> results = new Dictionary<string, string>(); if (strResponse != "") { StringReader reader = new StringReader(strResponse); string line = reader.ReadLine(); if (line == "SUCCESS") { while ((line = reader.ReadLine()) != null) { results.Add(line.Split(‘=‘)[0], line.Split(‘=‘)[1]); } } else if (line == "FAIL") { //退款后会返回信息。 Response.Write("Unable to retrive transaction detail"); } } else { Response.Write("ERROR"); } }
第三、根据支付请求返回来的参数进行验证是否支付成功并修改订单状态和提示客户相关支付信息等:
private DataContext db=new DataContext(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string st = "Completed"; string item_numberget = ""; string tx = ""; if (StringHandler.isCheckQueryString(Request["tx"].ToString().Trim())) { tx = Request["tx"].ToString().Trim(); //税编号 } string cm = ""; if (StringHandler.isCheckQueryString(Request["cm"].ToString().Trim())) { cm = Request["cm"].ToString().Trim(); ///加密 } if (StringHandler.isCheckQueryString(Request["st"].ToString().Trim())) { st = Request["st"].ToString().Trim(); ///状态 } string amt = ""; if (StringHandler.isCheckQueryString(Request["amt"].ToString().Trim())) { amt = Request["amt"].ToString().Trim(); ///购买总额 } string cc = ""; if (StringHandler.isCheckQueryString(Request["cc"].ToString().Trim())) { cc = Request["cc"].ToString().Trim(); ///币种USD(美元) } if (StringHandler.isCheckQueryString(Request["item_number"].ToString().Trim())) { item_numberget = Request["item_number"].ToString().Trim(); ///get的返回值订单号 } if (st == "Completed" && item_numberget != "") { //修改对应的单据状态如已支付。并提示支付成功给用户知道 this.lblstatus.InnerText = "Paid already."; } else { //支付失败 this.lblstatus.InnerText = "Error"; } } }
第四、aspx显示支付结果并数秒后自动跳转到指定页面,从而实现整个支付流程完成交易:
<script type="text/javascript"> var num = 5; function countDown() { if (num >= 0) { var str = ‘‘; str += ‘Will return to the home page in ‘ + num + ‘ seconds.‘; document.getElementById(‘daojishi‘).innerHTML = str; num--; setTimeout(countDown, 1000); } else { window.location.href = "home.aspx"; } } window.onload = countDown; </script>
时间: 2024-11-14 14:10:56