项目结构
PaymentController.cs
using AlipayIntegrationMVC.ViewModels;
using Com.Alipay;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AlipayIMVC.Controllers
{
public class PaymentController : Controller
{
public ActionResult Buy()
{
return View();
}
[HttpPost]
public void SubmitAlipay()
{
////////////////////////////////////////////请求参数////////////////////////////////////////////
//支付类型
string payment_type = "1";
//必填,不能修改
//服务器异步通知页面路径
string notify_url = "http://www.商户网址.com/payment/alipaynotify";
//需http://格式的完整路径,不能加?id=123这类自定义参数
//页面跳转同步通知页面路径
string return_url = "http://www.商户网址.com/payment/alipayreturn";
//需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/
//卖家支付宝帐户
string seller_email = "";
//必填
//商户订单号
string out_trade_no = "换成唯一订单号";
//商户网站订单系统中唯一订单号,必填
//订单名称
string subject = "换成订单名称";
//必填
//付款金额
string total_fee = "0.01";
//必填
//订单描述
string body = "换成订单描述";
//商品展示地址
string show_url = "http://www.商户网址.com/";
//需以http://开头的完整路径,例如:http://www.商户网址.com/myorder.html
//防钓鱼时间戳
string anti_phishing_key = "";
//若要使用请调用类文件submit中的query_timestamp函数
//客户端的IP地址
string exter_invoke_ip = "";
//非局域网的外网IP地址,如:221.0.0.1
////////////////////////////////////////////////////////////////////////////////////////////////
//把请求参数打包成数组
SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
sParaTemp.Add("partner", Config.Partner);
sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
sParaTemp.Add("service", "create_direct_pay_by_user");
sParaTemp.Add("payment_type", payment_type);
sParaTemp.Add("notify_url", notify_url);
sParaTemp.Add("return_url", return_url);
sParaTemp.Add("seller_email", seller_email);
sParaTemp.Add("out_trade_no", out_trade_no);
sParaTemp.Add("subject", subject);
sParaTemp.Add("total_fee", total_fee);
sParaTemp.Add("body", body);
sParaTemp.Add("show_url", show_url);
sParaTemp.Add("anti_phishing_key", anti_phishing_key);
sParaTemp.Add("exter_invoke_ip", exter_invoke_ip);
//建立请求
string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认");
Response.Write(sHtmlText);
}
// 同步调用,只发生一次
public ActionResult AlipayReturn()
{
var model = new AlipayReturnViewModel();
SortedDictionary<string, string> sPara = GetRequestGet();
if (sPara.Count > 0)//判断是否有带返回参数
{
Notify aliNotify = new Notify();
bool verifyResult = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"]);
if (verifyResult)//验证成功
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代码
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表
//商户订单号
model.out_trade_no = Request.QueryString["out_trade_no"];
//支付宝交易号
model.trade_no = Request.QueryString["trade_no"];
//交易状态
model.trade_status = Request.QueryString["trade_status"];
if (Request.QueryString["trade_status"] == "TRADE_FINISHED" || Request.QueryString["trade_status"] == "TRADE_SUCCESS")
{
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
model.message = "支付成功";
}
else
{
model.message = "支付失败";
}
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else//验证失败
{
model.message = ("验证失败");
}
}
else
{
model.message = ("无返回参数");
}
return View(model);
}
/// <summary>
/// 获取支付宝GET过来通知消息,并以“参数名=参数值”的形式组成数组
/// </summary>
/// <returns>request回来的信息组成的数组</returns>
public SortedDictionary<string, string> GetRequestGet()
{
int i = 0;
SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
NameValueCollection coll;
//Load Form variables into NameValueCollection variable.
coll = Request.QueryString;
// Get names of all forms into a string array.
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
}
return sArray;
}
// 异步调用,支付宝会在24小时内多次调用,直到成功为止
public ActionResult AlipayNotify()
{
SortedDictionary<string, string> sPara = GetRequestPost();
if (sPara.Count > 0)//判断是否有带返回参数
{
Notify aliNotify = new Notify();
bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);
if (verifyResult)//验证成功
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代码
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表
//商户订单号
string out_trade_no = Request.Form["out_trade_no"];
//支付宝交易号
string trade_no = Request.Form["trade_no"];
//交易状态
string trade_status = Request.Form["trade_status"];
if (Request.Form["trade_status"] == "TRADE_FINISHED")
{
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
//注意:
//该种交易状态只在两种情况下出现
//1、开通了普通即时到账,买家付款成功后。
//2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。
}
else if (Request.Form["trade_status"] == "TRADE_SUCCESS")
{
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
//注意:
//该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。
}
else
{
}
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
Response.Write("success"); //请不要修改或删除
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else//验证失败
{
Response.Write("fail");
}
}
else
{
Response.Write("无通知参数");
}
return View();
}
/// <summary>
/// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
/// </summary>
/// <returns>request回来的信息组成的数组</returns>
public SortedDictionary<string, string> GetRequestPost()
{
int i = 0;
SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
NameValueCollection coll;
//Load Form variables into NameValueCollection variable.
coll = Request.Form;
// Get names of all forms into a string array.
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
}
return sArray;
}
}
}
AlipayReturnViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AlipayIMVC.ViewModels
{
public class AlipayReturnViewModel
{
//商户订单号
public string out_trade_no { get; set; }
//支付宝交易号
public string trade_no { get; set; }
//交易状态
public string trade_status { get; set; }
public string message { get; set; }
}
}
Buy.cshtml
@{
ViewBag.Title = "Buy";
Layout = "~/Views/_layoutCMS.cshtml";
}
<script type="text/javascript">
function SelectBank(n) {
$('$bank' + n).checked = true;
}
</script>
<h1>支付宝支付1分钱测试</h1>
@using (Html.BeginForm("SubmitAlipay", "Payment", FormMethod.Post, new { @class = "form-horizontal", id = "profile-form" }))
{
<div class="row">
<label class="col-sm-1 no-padding-right">单价</label>
<label class="col-sm-3"> ¥0.01 </label>
<label class="col-sm-1 no-padding-right">数量</label>
<label class="col-sm-3"> <input id="qty" name="qty" value="1" /> </label>
<label class="col-sm-1 no-padding-right">总金额</label>
<label class="col-sm-3"> ¥0.01 </label>
</div>
<div class="row col-xs-12">
<h4 class="header blue smaller">
请选择支付方式
</h4>
<ul class="alipay_content">
<li>
<input type="radio" name="pay_bank" value="directPay" checked="checked" id="bank0" /> <img src="/assets/images/alipay.gif" onclick="javascript:SelectBank(0);" class="alipay" alt="支付宝" />
</li>
</ul>
<div class="clearfix"></div>
<ul class="alipay_content">
<li>
<input type="radio" name="pay_bank" value="ICBCB2C" id="bank1" /> <span onclick="javascript:SelectBank(1);" class="icon ICBC"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="CMB" id="bank2" /> <span onclick="javascript:SelectBank(2);" class="icon CMB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="CCB" id="bank3" /> <span onclick="javascript:SelectBank(3);" class="icon CCB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="BOCB2C" id="bank4" /> <span onclick="javascript:SelectBank(4);" class="icon BOC"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="ABC" id="bank5" /> <span onclick="javascript:SelectBank(5);" class="icon ABC"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="COMM" id="bank6" /> <span onclick="javascript:SelectBank(6);" class="icon COMM"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="PSBC-DEBIT" id="bank7" /> <span onclick="javascript:SelectBank(7);" class="icon PSBC"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="CEBBANK" id="bank8" /> <span onclick="javascript:SelectBank(8);" class="icon CEB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="SPDB" id="bank9" /> <span onclick="javascript:SelectBank(9);" class="icon SPDB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="GDB" id="bank10" /> <span onclick="javascript:SelectBank(10);" class="icon GDB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="CITIC" id="bank11" /> <span onclick="javascript:SelectBank(11);" class="icon CITIC"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="CIB" id="bank12" /> <span onclick="javascript:SelectBank(12);" class="icon CIB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="SDB" id="bank13" /> <span onclick="javascript:SelectBank(13);" class="icon SDB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="CMBC" id="bank14" /> <span onclick="javascript:SelectBank(14);" class="icon CMBC"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="BJBANK" id="bank15" /> <span onclick="javascript:SelectBank(15);" class="icon BJBANK"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="HZCBB2C" id="bank16" /> <span onclick="javascript:SelectBank(16);" class="icon HZCB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="SHBANK" id="bank17" /> <span onclick="javascript:SelectBank(17);" class="icon SHBANK"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="BJRCB" id="bank18" /> <span onclick="javascript:SelectBank(18);" class="icon BJRCB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="SPABANK" id="bank19" /> <span onclick="javascript:SelectBank(19);" class="icon SPABANK"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="FDB" id="bank20" /> <span onclick="javascript:SelectBank(20);" class="icon FDB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="WZCBB2C-DEBIT" id="bank21" /> <span onclick="javascript:SelectBank(21);" class="icon WZCB"></span>
</li>
<li>
<input type="radio" name="pay_bank" value="NBBANK" id="bank22" /> <span onclick="javascript:SelectBank(22);" class="icon NBBANK"></span>
</li>
</ul>
<hr class="clearboth">
</div>
<div class="clearfix"></div>
<button class="btn btn-success">
<i class="ace-icon fa fa-save icon-on-left"></i>
开始充值
</button>
}
AlipayReturn.cshtml
@{
ViewBag.Title = "AlipayReturn";
Layout = "~/Views/_layoutCMS.cshtml";
}
@model AlipayIMVC.ViewModels.AlipayReturnViewModel
<h1 class="widget-title">支付结果</h1>
<div>
@Model.message<br>
商户订单号: @Model.out_trade_no<br>
支付宝交易号 : @Model.trade_no<br>
交易状态 : @Model.trade_status
</div>
AlipayNotify.cshtml
@{
ViewBag.Title = "AlipayNotify";
Layout = "~/Views/_layoutCMS.cshtml";
}