cxi実現のAlipay支払い
C萼は宝掃コードの支払いを示しています。具体的なコードは下記の通りです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace ZK_ECommerce.Shop.pay.alipay
{
public class AliPay
{
public static string GetMD5(string s, string _input_charset)
{
/// <summary>
/// ASP MD5
/// </summary>
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
public static string[] BubbleSort(string[] r)
{
/// <summary>
///
/// </summary>
int i, j; //
string temp;
bool exchange;
for (i = 0; i < r.Length; i++) // R.Length-1
{
exchange = false; // ,
for (j = r.Length - 2; j >= i; j--)
{
if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0) //
{
temp = r[j + 1];
r[j + 1] = r[j];
r[j] = temp;
exchange = true; // ,
}
}
if (!exchange) // ,
{
break;
}
}
return r;
}
public string CreatUrl(
string gateway,
string service,
string partner,
string sign_type,
string out_trade_no,
string subject,
string body,
string payment_type,
string total_fee,
string show_url,
string seller_email,
string key,
string return_url,
string _input_charset,
string notify_url,
string extra_common_param = ""
)
{
return CreatUrl(
gateway,
service,
partner,
sign_type,
out_trade_no,
subject,
body,
payment_type,
total_fee,
show_url,
seller_email,
key,
return_url,
_input_charset,
notify_url,
"",
extra_common_param);
}
public string CreatUrl(
string gateway,
string service,
string partner,
string sign_type,
string out_trade_no,
string subject,
string body,
string payment_type,
string total_fee,
string show_url,
string seller_email,
string key,
string return_url,
string _input_charset,
string notify_url,
string token,
string extra_common_param = ""
)
{
/// <summary>
///2015 11 17 14:08:00
/// </summary>
int i;
string[] Oristr;
// ;
if (!string.IsNullOrEmpty(token))
{
if (extra_common_param == "COD2MOTOPAY")
{
Oristr = new[]{
"service="+service,
"partner=" + partner,
"subject=" + subject,
"body=" + body,
"out_trade_no=" + out_trade_no,
"total_fee=" + total_fee,
"show_url=" + show_url,
"payment_type=" + payment_type,
"seller_email=" + seller_email,
"notify_url=" + notify_url,
"_input_charset="+_input_charset,
"return_url=" + return_url,
"token="+token,
"extra_common_param="+extra_common_param
};
}
else
{
Oristr = new[]{
"service="+service,
"partner=" + partner,
"subject=" + subject,
"body=" + body,
"out_trade_no=" + out_trade_no,
"total_fee=" +total_fee,
"show_url=" + show_url,
"payment_type=" + payment_type,
"seller_email=" + seller_email,
"notify_url=" + notify_url,
"_input_charset="+_input_charset,
"return_url=" + return_url,
"token="+token
};
}
}
else
{
if (extra_common_param == "COD2MOTOPAY")
{
Oristr = new[]{
"service="+service,
"partner=" + partner,
"subject=" + subject,
"body=" + body,
"out_trade_no=" + out_trade_no,
"total_fee="+total_fee,
"show_url=" + show_url,
"payment_type=" + payment_type,
"seller_email=" + seller_email,
"notify_url=" + notify_url,
"_input_charset="+_input_charset,
"return_url=" + return_url,
"extra_common_param="+extra_common_param
};
}
else
{
Oristr = new[]{
"service="+service,
"partner=" + partner,
"subject=" + subject,
"body=" + body,
"out_trade_no=" + out_trade_no,
"total_fee=" + total_fee,
"show_url=" + show_url,
"payment_type=" + payment_type,
"seller_email=" + seller_email,
"notify_url=" + notify_url,
"_input_charset="+_input_charset,
"return_url=" + return_url
};
}
}
// ;
string[] Sortedstr = BubbleSort(Oristr);
// md5 ;
StringBuilder prestr = new StringBuilder();
for (i = 0; i < Sortedstr.Length; i++)
{
if (i == Sortedstr.Length - 1)
{
prestr.Append(Sortedstr[i]);
}
else
{
prestr.Append(Sortedstr[i] + "&");
}
}
prestr.Append(key);
// Md5 ;
string sign = GetMD5(prestr.ToString(), _input_charset);
// Url;
char[] delimiterChars = { '=' };
StringBuilder parameter = new StringBuilder();
parameter.Append(gateway);
for (i = 0; i < Sortedstr.Length; i++)
{
parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&");
}
parameter.Append("sign=" + sign + "&sign_type=" + sign_type);
// Url;
return parameter.ToString();
}
public string CreatUrl_MissPayments(
string gateway,
string service,
string partner,
string sign_type,
string out_trade_no,
string key,
string _input_charset
)
{
/// <summary>
/// created by sunzhizhi 2006.5.21,[email protected]。
/// </summary>
int i;
// ;
string[] Oristr ={
"service="+service,
"partner=" + partner,
"out_trade_no=" + out_trade_no,
"_input_charset="+_input_charset
};
// ;
string[] Sortedstr = BubbleSort(Oristr);
// md5 ;
StringBuilder prestr = new StringBuilder();
for (i = 0; i < Sortedstr.Length; i++)
{
if (i == Sortedstr.Length - 1)
{
prestr.Append(Sortedstr[i]);
}
else
{
prestr.Append(Sortedstr[i] + "&");
}
}
prestr.Append(key);
// Md5 ;
string sign = GetMD5(prestr.ToString(), _input_charset);
// Url;
char[] delimiterChars = { '=' };
StringBuilder parameter = new StringBuilder();
parameter.Append(gateway);
for (i = 0; i < Sortedstr.Length; i++)
{
parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&");
}
parameter.Append("sign=" + sign + "&sign_type=" + sign_type);
// Url;
return parameter.ToString();
}
public static string GetTranSign(string TranData)
{
if (string.IsNullOrWhiteSpace(TranData))
return null;
string sign = GetMD5(TranData + "alskdjfaow;fjel;asdjf", "utf-8");
return sign;
}
}
}
ページ処理:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using ZK_ECommerce.Shop.pay.alipay;
namespace Alipay.Controllers
{
public class ApliayDemoController : Controller
{
/// <summary>
///
/// </summary>
/// <param name="ordercode"></param>
/// <returns></returns>
public ActionResult Index(string ordercode)
{
string out_trade_no = ordercode;
string gateway = "https://mapi.alipay.com/gateway.do?"; //
string service = "create_direct_pay_by_user";
string partner = ""; //partner ID
string sign_type = "MD5";
string payment_type = "1"; //
string show_url = "http://www.letao.com";
string seller_email = ""; //
string key = ""; //partner
string return_url = "http://localhost:1396/ApliayDemo/ApliyReturn"; //
string notify_url = "http://localhost:1396/ApliayDemo/AlipayNotify"; //
string _input_charset = "utf-8";
string extra_common_param = "COD2MOTOPAY";//
string token = string.Empty;
if (string.IsNullOrEmpty(token))
token = "";
string subject = " "; // subject
string body = " "; //
string total_fee = "0.01";
AliPay ap = new AliPay();
string aliay_url = ap.CreatUrl(gateway, service, partner, sign_type, out_trade_no, subject, body, payment_type,
total_fee, show_url, seller_email, key, return_url, _input_charset, notify_url, token, extra_common_param);
return Redirect(aliay_url);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult ApliyReturn()
{
string alipayNotifyURL = "https://mapi.alipay.com/gateway.do?";
string key = ""; //partner ( )
string _input_charset = "utf-8";
string partner = ""; //partner id( )
alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_id=" + Request.QueryString["notify_id"];
// ATN ,true ,false
string responseTxt = Get_Http(alipayNotifyURL, 120000);
int i;
NameValueCollection coll;
coll = Request.QueryString;
String[] requestarr = coll.AllKeys;
// ;
string[] Sortedstr = BubbleSort(requestarr);
// md5 ;
StringBuilder prestr = new StringBuilder();
for (i = 0; i < Sortedstr.Length; i++)
{
if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
{
if (i == Sortedstr.Length - 1)
{
prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]);
}
else
{
prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&");
}
}
}
prestr.Append(key);
string mysign = GetMD5(prestr.ToString(), _input_charset);
string sign = Request.QueryString["sign"];
string trade_status = Request.QueryString["trade_status"];
if (mysign == sign && responseTxt == "true" && trade_status == "TRADE_SUCCESS") // ,
{
string order_amount = Request.QueryString["total_fee"].ToString().Trim();
string order_code = Request.QueryString["out_trade_no"].ToString().Trim();
//updateOrder(order_code);//
Response.Redirect(""); //
}
else
{
Response.Write(" 。");
Response.Write("<br>Result:responseTxt=" + responseTxt);
Response.Write("<br>Result:mysign=" + mysign);
Response.Write("<br>Result:sign=" + sign);
}
return View();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult AlipayNotify()
{
string alipayNotifyURL = "https://mapi.alipay.com/gateway.do?";
string partner = ""; //partner id( )
string key = ""; //partner ( )
alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_id=" + Request.Form["notify_id"];
// ATN ,true ,false
string responseTxt = Get_Http(alipayNotifyURL, 120000);
int i;
NameValueCollection coll;
//Load Form variables into NameValueCollection variable.
coll = Request.Form;
// Get names of all forms into a string array.
String[] requestarr = coll.AllKeys;
// ;
string[] Sortedstr = BubbleSort(requestarr);
// md5 ;
StringBuilder prestr = new StringBuilder();
for (i = 0; i < Sortedstr.Length; i++)
{
if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
{
if (i == Sortedstr.Length - 1)
{
prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]);
}
else
{
prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&");
}
}
}
prestr.Append(key);
string mysign = GetMD5(prestr.ToString());
string sign = Request.QueryString["sign"];
string trade_status = Request.QueryString["trade_status"];
if (mysign == sign && responseTxt == "true" && trade_status == "TRADE_SUCCESS") // ,
{
string order_amount = Request.QueryString["total_fee"].ToString().Trim();
string order_code = Request.QueryString["out_trade_no"].ToString().Trim();
// updateOrder(order_code);//
Response.Redirect(""); //
}
else
{
Response.Write(" 。");
Response.Write("<br>Result:responseTxt=" + responseTxt);
Response.Write("<br>Result:mysign=" + mysign);
Response.Write("<br>Result:sign=" + sign);
}
return View();
}
public static string GetMD5(string s, string _input_charset)
{
/// <summary>
/// ASP MD5
/// </summary>
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
public static string GetMD5(string s)
{
/// <summary>
/// ASP MD5
/// </summary>
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
public static string[] BubbleSort(string[] r)
{
/// <summary>
///
/// </summary>
int i, j; //
string temp;
bool exchange;
for (i = 0; i < r.Length; i++) // R.Length-1
{
exchange = false; // ,
for (j = r.Length - 2; j >= i; j--)
{
if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0) //
{
temp = r[j + 1];
r[j + 1] = r[j];
r[j] = temp;
exchange = true; // ,
}
}
if (!exchange) // ,
{
break;
}
}
return r;
}
// ATN
public String Get_Http(String a_strUrl, int timeout)
{
string strResult;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
myReq.Timeout = timeout;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine());
}
strResult = strBuilder.ToString();
}
catch (Exception exp)
{
strResult = " :" + exp.Message;
}
return strResult;
}
}
}
以上はcxiのAlipay決済の詳細です。cxiのAlipay決済に関する資料は他の関連記事に注目してください。