//特别提示：本程序未在.NET环境进行测试，如有问题，请及时反馈给我们，谢谢！
//#############################################################
//#               贺喜支付平台支付接口提交程序
//#                          v2.2.NET
//#                   http://www.168reg.cn
//#
//#  版权所有: 株洲市和谐网络科技发展有限公司
//#
//#  主页地址: http://www.168reg.cn
//#  技术支持: support@168reg.cn
//#  技 术 QQ: 359465724
//#  技术QQ群：8246422，7048987
//#
//#############################################################
//# ·郑重声明:
//# ·此程序不得随意公开发布、销售或进行其他非法转让。
//# ·如对此程序有任何疑问，请在管理区留言。
//#############################################################
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

using BookShopOnline.Model;

public partial class PayMent:Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserState.CheckUserLogin();

        User userLogined = UserState.GetUserLogined();
        string orderID = PageHelper.GetUrlParmValue(UrlParmName.OrderID);
        decimal hasNotPayedPrice =Convert.ToDecimal( PageHelper.GetSession(SessionName.HasNotPayedPrice));

        //接收订单号
        if (string.IsNullOrEmpty(orderID))
        {
            Response.Write("传递参数OrderID错误！");
            return;
        }

          //支付金额，以分为单位。
          String amount = "";
          checked
          {
              amount = ((UInt64)(hasNotPayedPrice * 100)).ToString();
          }

        /*以下的信息从Web.Config中读取的*/

        //您在贺喜支付平台的商户编号，请将0改为自己的商户编号，商户编号可通过管理区首页、支付平台－参数设置获得。
        String reg_userid = ConfigurationManager.AppSettings["reg_userid"];

        //使用贺喜支付平台支付接口的商户密钥，请通过支付平台－参数设置修改商户密钥并替换掉空值。
        String reg_key = ConfigurationManager.AppSettings["reg_key"];

        //贺喜支付平台支付接口支付结果返回地址设置，请修改成自己的接收网址。
        String receive_url = ConfigurationManager.AppSettings["receive_url"]+"?OrderID="+orderID;

        //服务名称,长度：50
        String servicename = ConfigurationManager.AppSettings["servicename"];

        //自定义字段，请在生成加密字符串之前设置好custom1、custom2，custom1name和custom2name是在支付平台显示的自定义字段名称，商户可自行设置。
        String custom1name =ConfigurationManager.AppSettings["custom1name"];
        String custom1 =userLogined.UserName;
        String custom2name =ConfigurationManager.AppSettings["custom2name"];
        String custom2 =ConfigurationManager.AppSettings["custom2"];

        //选择的支付途径,1：银行卡支付，2：声讯电话支付，3：互联星空支付，4：手机短信注册，5：腾讯财付通支付，6：腾讯Q币支付，7：神州行充值卡支付，8：盛大游戏点卡支付，9：支付宝支付，10：手机银行支付，11：北京宽带支付，12：联通充值卡支付。
        String payvia =ConfigurationManager.AppSettings["payvia"];

        //运行模式，不启用测试模式可不设置此变量，将值设置为testmode即为测试模式。
        String paymode = ConfigurationManager.AppSettings["paymode"];

        
        if (amount != null) {
					//商户订单号，可自定义。示例中的订单号由年月日+小时分钟秒 生成，30个字符以内，必填
                    DateTime dt = DateTime.Now;
                    //string ymd = dt.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); // yyyyMMdd
                    //string timeStr = dt.ToString("HHmmss", DateTimeFormatInfo.InvariantInfo); // HHmmss
                    string ymd = dt.ToString("yyyyMMdd"); // yyyyMMdd
                    //string timeStr = dt.ToString("HHmmss"); // HHmmss
                    //string billno = ymd + timeStr;

            string billno = orderID;
					
			    String PayURL = "http://pay.168reg.cn/pay.do";

					//拼凑串里尽量不要使用中文字符，目前MD5在ASP、asp.net、java等环境下对中文处理可能有偏差现象
					//拼凑串为：商户编号+amount+ymd+receive_url+billno+custom1+custom2+payvia+商户密钥+paymode
					String post_key=reg_userid+amount+ymd+receive_url+billno+custom1+custom2+payvia+reg_key+paymode;
					
					//贺喜支付平台支付接口对MD5值只认大写字符串，所以小写的MD5值得转换为大写
					String post_md5info = FormsAuthentication.HashPasswordForStoringInConfigFile(post_key, "MD5");  //计算MD5的值
                if (post_md5info != null) { 
                    Response.Write("<body onload='document.forms[0].submit();'>");
										Response.Write("<form method=\"get\" name=\"orderForm\" action=\""+PayURL+"\">");
										Response.Write("	<input type=\"hidden\" name=\"reg_userid\" value=\"" + reg_userid + "\">");
										Response.Write("	<input type=\"hidden\" name=\"servicename\" value=\"" + servicename + "\">");
										Response.Write("	<input type=\"hidden\" name=\"amount\" value=\"" + amount + "\">");
										Response.Write("	<input type=\"hidden\" name=\"ymd\" value=\"" + ymd + "\">");
										Response.Write("	<input type=\"hidden\" name=\"receive_url\" value=\"" + receive_url + "\">");
										Response.Write("	<input type=\"hidden\" name=\"billno\" value=\"" + billno + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom1name\" value=\"" + custom1name + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom1\" value=\"" + custom1 + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom2name\" value=\"" + custom2name + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom2\" value=\"" + custom2 + "\">");
										Response.Write("	<input type=\"hidden\" name=\"paymode\" value=\"" + paymode + "\">");
										Response.Write("	<input type=\"hidden\" name=\"payvia\" value=\"" + payvia + "\">");
					          Response.Write("	<input type=\"hidden\" name=\"post_md5info\" value=\"" + post_md5info + "\">");
										Response.Write("</form>");
										Response.Write("<form method=\"get\" name=\"orderForm2\" action=\""+PayURL+"\">");
										Response.Write("	<input type=\"hidden\" name=\"reg_userid\" value=\"" + reg_userid + "\">");
										Response.Write("	<input type=\"hidden\" name=\"servicename\" value=\"" + servicename + "\">");
										Response.Write("	<input type=\"hidden\" name=\"amount\" value=\"" + amount + "\">");
										Response.Write("	<input type=\"hidden\" name=\"ymd\" value=\"" + ymd + "\">");
										Response.Write("	<input type=\"hidden\" name=\"receive_url\" value=\"" + receive_url + "\">");
										Response.Write("	<input type=\"hidden\" name=\"billno\" value=\"" + billno + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom1name\" value=\"" + custom1name + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom1\" value=\"" + custom1 + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom2name\" value=\"" + custom2name + "\">");
										Response.Write("	<input type=\"hidden\" name=\"custom2\" value=\"" + custom2 + "\">");
										Response.Write("	<input type=\"hidden\" name=\"paymode\" value=\"" + paymode + "\">");
										Response.Write("	<input type=\"hidden\" name=\"payvia\" value=\"" + payvia + "\">");
					          Response.Write("	<input type=\"hidden\" name=\"post_md5info\" value=\"" + post_md5info + "\">");
					          Response.Write("订单已生成，请点击<input type='submit' value='立即支付' name='submit'>。");
										Response.Write("</form>");
									  Response.Write("</body>");
                } else{
				    //log.error(encryptString + ":" + "MD5加密异常！");
                    Response.Write("MD5加密异常！");
			    }
        } else {
            Response.Write("参数错误！");
        }
    }
}
