实现WireCard支付

 

实现WireCard支付,暂未完成

 

WireCardController.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Collections.Specialized;
  4 using System.Security.Cryptography;
  5 using System.Text;
  6 using System.Web;
  7 using System.Web.Mvc;
  8 
  9 namespace Payment
 10 {
 11     public class WireCardController : Controller
 12     {
 13         /// <summary>
 14         /// 创建表单支付
 15         /// 使用 WireCard 的HPP(Host Payment Page)支付方式 
 16         /// </summary>
 17         /// <returns></returns>
 18         public ActionResult Index()
 19         {
 20             var reqFormData = new NameValueCollection();
 21             //表单数据
 22             reqFormData.Add("request_time_stamp", DateTime.UtcNow.ToString("yyyyMMddHHmmss")); //支付时间:人家指定要UtcNow
 23             reqFormData.Add("request_id", Guid.NewGuid().ToString()); //请求编号
 24             reqFormData.Add("merchant_account_id", "your_account_id"); //商户编号:WireCard帐号,等同支付宝帐号,用来打款
 25             reqFormData.Add("transaction_type", "authorization");
 26             reqFormData.Add("requested_amount", "100.00"); //付款金额
 27             reqFormData.Add("requested_amount_currency", "CNY");
 28             reqFormData.Add("redirect_url", "http://localhost/Payment/WireCard/Result"); //支付结果
 29             reqFormData.Add("ip_address", "127.0.0.1"); // 本机外网IP:
 30             reqFormData.Add("secret_key", "secret_key");
 31             //数字签名,防止传输过程中数据被篡改
 32             reqFormData.Add("request_signature",
 33                 getSHA256(
 34                 reqFormData["request_time_stamp"] +
 35                 reqFormData["request_id"] +
 36                 reqFormData["merchant_account_id"] +
 37                 reqFormData["transaction_type"] +
 38                 reqFormData["requested_amount"] +
 39                 reqFormData["requested_amount_currency"] +
 40                 reqFormData["redirect_url"] +
 41                 reqFormData["ip_address"] +
 42                 reqFormData["secret_key"]
 43                 )
 44             );
 45             reqFormData.Add("attempt_three_d", "true"); // With the 3D Secure
 46             reqFormData.Add("card_type", "mastercard");
 47             reqFormData.Add("notification_url_1", "http://127.0.0.1/Payment/WireCard/Notification"); //付款事务通知
 48             reqFormData.Add("notification_transaction_state_1", "success");
 49 
 50             //生成支付表单,自动并提交到付款平台入口
 51             Response.Write(generalForm(reqFormData, "UTF-8", "https://testapi.ep2-global.com/engine/hpp/"));
 52 
 53             return null;
 54         }
 55 
 56         /// <summary>
 57         /// 收到支付事务通知
 58         /// </summary>
 59         /// <returns></returns>
 60         public ActionResult Notification()
 61         {
 62             /*
 63              * 服务端Java做的:
 64              *
 65              * 支付平台会包含三个事务步骤,
 66              * 走完三个步骤才算完成支付,
 67              * 期间会有三次 Notification
 68              * 注:返回的状态在Header中,参考三次Headers[request_id]的值的变化,Form里没数据的
 69              * 
 70              */
 71             return View();
 72         }
 73 
 74         /// <summary>
 75         /// 最终返回支付结果
 76         /// </summary>
 77         /// <returns></returns>
 78         public ActionResult Result()
 79         {
 80             /*
 81              * 最终返回的付款结果,相关支付详细信息都在Form里
 82              */
 83 
 84             return View();
 85         }
 86 
 87         /// <summary>
 88         /// 加密
 89         /// </summary>
 90         /// <param name="text"></param>
 91         /// <returns></returns>
 92         private string getSHA256(string text)
 93         {
 94             byte[] hasValue;
 95             byte[] message = Encoding.UTF8.GetBytes(text);
 96 
 97             var hashString = new SHA256Managed();
 98             string hex = "";
 99 
100             hasValue = hashString.ComputeHash(message);
101             foreach (byte x in hasValue)
102             {
103                 hex += String.Format("{0:x2}", x);
104             }
105 
106             return hex.Trim();
107         }
108 
109         /// <summary>
110         /// 生成Form表单
111         /// </summary>
112         /// <param name="collections">Form数据,NameValueCollection</param>
113         /// <param name="charset">字符类型</param>
114         /// <param name="PostUrl">付款平台的入口</param>
115         /// <returns></returns>
116         private string generalForm(NameValueCollection collections, string charset, string PostUrl)
117         {
118             var values = collections;
119             var html = new StringBuilder();
120             html.AppendLine("<html>").AppendLine("<head>");
121             html.AppendFormat("<meta http-equiv=\"Content-Type\" content=\"application/x-www-form-urlencoded; charset={0}\" />", charset).AppendLine();
122             html.AppendLine("<style type=\"text/css\">#pay_form { margin: 30px auto 0 auto; width: 700px; text-align: left; } label{ width:250px;} input{ width: 220px;}</style>");
123             html.AppendLine("</head>");
124             html.AppendLine("<body onload=\"\">"); //javascript:document.pay_form.submit();
125             //html.AppendLine("<div id=\"search_flight_loading\"><img src=\"/Content/img/oval.svg\" /></div>");
126             html.AppendFormat("<form id=\"pay_form\" name=\"pay_form\" action=\"{0}\" method=\"POST\">", PostUrl).AppendLine();
127             foreach (string k in values.AllKeys)
128             {
129                 html.AppendFormat("<label for=\"{0}\">{0}</label>: <input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" readOnly=\"true\" /> <br/>", k, values[k]).AppendLine();
130             }
131             html.AppendLine("<input type=\"submit\" style=\"display:block;\" value=\"提交\" />");
132             html.AppendLine("</form>").AppendLine("</body>").AppendLine("</html>");
133             return html.ToString();
134         }
135 
136     }
137 }

 

posted @ 2016-08-10 18:13  Marksion  阅读(1076)  评论(0编辑  收藏  举报