项目结构

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" />&nbsp;<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" />&nbsp;<span onclick="javascript:SelectBank(1);" class="icon ICBC"></span></li><li><input type="radio" name="pay_bank" value="CMB" id="bank2" />&nbsp;<span onclick="javascript:SelectBank(2);" class="icon CMB"></span></li><li><input type="radio" name="pay_bank" value="CCB" id="bank3" />&nbsp;<span onclick="javascript:SelectBank(3);" class="icon CCB"></span></li><li><input type="radio" name="pay_bank" value="BOCB2C" id="bank4" />&nbsp;<span onclick="javascript:SelectBank(4);" class="icon BOC"></span></li><li><input type="radio" name="pay_bank" value="ABC" id="bank5" />&nbsp;<span onclick="javascript:SelectBank(5);" class="icon ABC"></span></li><li><input type="radio" name="pay_bank" value="COMM" id="bank6" />&nbsp;<span onclick="javascript:SelectBank(6);" class="icon COMM"></span></li><li><input type="radio" name="pay_bank" value="PSBC-DEBIT" id="bank7" />&nbsp;<span onclick="javascript:SelectBank(7);" class="icon PSBC"></span></li><li><input type="radio" name="pay_bank" value="CEBBANK" id="bank8" />&nbsp;<span onclick="javascript:SelectBank(8);" class="icon CEB"></span></li><li><input type="radio" name="pay_bank" value="SPDB" id="bank9" />&nbsp;<span onclick="javascript:SelectBank(9);" class="icon SPDB"></span></li><li><input type="radio" name="pay_bank" value="GDB" id="bank10" />&nbsp;<span onclick="javascript:SelectBank(10);" class="icon GDB"></span></li><li><input type="radio" name="pay_bank" value="CITIC" id="bank11" />&nbsp;<span onclick="javascript:SelectBank(11);" class="icon CITIC"></span></li><li><input type="radio" name="pay_bank" value="CIB" id="bank12" />&nbsp;<span onclick="javascript:SelectBank(12);" class="icon CIB"></span></li><li><input type="radio" name="pay_bank" value="SDB" id="bank13" />&nbsp;<span onclick="javascript:SelectBank(13);" class="icon SDB"></span></li><li><input type="radio" name="pay_bank" value="CMBC" id="bank14" />&nbsp;<span onclick="javascript:SelectBank(14);" class="icon CMBC"></span></li><li><input type="radio" name="pay_bank" value="BJBANK" id="bank15" />&nbsp;<span onclick="javascript:SelectBank(15);" class="icon BJBANK"></span></li><li><input type="radio" name="pay_bank" value="HZCBB2C" id="bank16" />&nbsp;<span onclick="javascript:SelectBank(16);" class="icon HZCB"></span></li><li><input type="radio" name="pay_bank" value="SHBANK" id="bank17" />&nbsp;<span onclick="javascript:SelectBank(17);" class="icon SHBANK"></span></li><li><input type="radio" name="pay_bank" value="BJRCB" id="bank18" />&nbsp;<span onclick="javascript:SelectBank(18);" class="icon BJRCB"></span></li><li><input type="radio" name="pay_bank" value="SPABANK" id="bank19" />&nbsp;<span onclick="javascript:SelectBank(19);" class="icon SPABANK"></span></li><li><input type="radio" name="pay_bank" value="FDB" id="bank20" />&nbsp;<span onclick="javascript:SelectBank(20);" class="icon FDB"></span></li><li><input type="radio" name="pay_bank" value="WZCBB2C-DEBIT" id="bank21" />&nbsp;<span onclick="javascript:SelectBank(21);" class="icon WZCB"></span></li><li><input type="radio" name="pay_bank" value="NBBANK" id="bank22" />&nbsp;<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";
}

运行结果如图:

支付宝实时到账的MVC示例相关推荐

  1. 支付宝当面付D1/T1转D0实时到账的解决方案

    支付宝当面付D1/T1转D0实时到账的解决方案 目前支持模式1.主账号交易,子账号收款 2.每个账号收款支持实时到账: 一.把企业支付宝添加到我们系统后台,并进行扫码授权:如下图! 如有需要可以关注留 ...

  2. 支付宝即时到账接口使用历险记

    闲来无事,找来支付宝即时到账接口用了下.遇到一些事情,现在想回放下,看遍电影. 如上图所示,支付宝官方给的使用流程,前3步客户做好自不必说,在第4.5步遇到一些问题: 1.支付宝跳转页面同步通知(re ...

  3. Extjs4 MVC 示例

    http://psbye.iteye.com/blog/1231039 Extjs4 MVC 示例 博客分类:Extjs 或多或少的参照了官方的一些代码 已完成的功能: 布局分成 上 中 下 , 在框 ...

  4. PHP实现支付宝即时到账功能

    本文实例为大家分享了PHP支付宝即时到账功能的实现代码,供大家参考,具体内容如下 首先需要下载即时到账交易接口,传送门https://doc.open.alipay.com/doc2/detail?t ...

  5. 支付宝即时到账接口开发 - DEMO讲解

    支付宝即时到账接口开发 - DEMO讲解 环境要求 PHP5.0以上,且需要开启curl.openssl. 文档地址: https://doc.open.alipay.com/doc2/detail? ...

  6. php 微信实时更新,微信小程序修改data使页面数据实时更新的代码示例

    本篇文章给大家带来的内容是关于微信小程序修改data使页面数据实时更新的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 需求:通过点击button修改dataList中che ...

  7. 支付宝及时到账(新版)配置

    http://www.upwqy.com/details/83.html​ 小蚂哥升级了支付产品,新版的即时到账可以支持原路退款,意味着商家在处理支付宝支付的退款订单时可以自动退款给买家.下面是新版即 ...

  8. spring mvc示例_Spring MVC示例

    spring mvc示例 Welcome to Spring MVC Example. Sometime back in Spring MVC Tutorial, I explained how to ...

  9. 搞定支付接口(一) 支付宝即时到账支付接口详细流程和代码

    搞定支付接口(一) 支付宝即时到账支付接口详细流程和java代码 为避免你们和我一样被支付接口搞得焦头烂额,写一个从申请开始到能收到钱为止的详细教程,实际上各个语言都可以用来集成支付接口,我用java ...

最新文章

  1. mysql whrere 占位_【MySQL】(4)操作数据表中的记录
  2. HTML5 Canvas API 变换(translate函数)
  3. nginx随机变换图片服务器网址来防止盗链
  4. 如何在 ASP.Net Core 中使用 LoggerMessage
  5. 《深入理解分布式事务》第八章 TCC 分布式事务原理
  6. matlab生成exe失败,求助,m文件生成exe遇到的错误
  7. Linux下开源邮件系统Postfix+Extmail+Extman环境部署
  8. python x轴加范围_Python,Matplotlib,subplot:如何设置轴范围?
  9. MySQL 定时任务event
  10. matlab数组、矩阵运算
  11. HTML制作个简单的网页
  12. 三维绘图plot3命令ezplot3命令三维网格命令mesh命令
  13. 考研英语阅读12种解题技巧!码住!
  14. 标准身材计算机法,计算机(程序)基础_堂堂5尺爷们不到100斤_数之美
  15. input标签 设置纯数字输入
  16. androidspring!Android开发究竟该如何学习,薪资翻倍
  17. cuda的安装,及pytorch调用GPU步骤
  18. 科普“知识共享”严重缺失,国内亟待补课
  19. 医疗器械软件开发哪些事(1)医疗器械软件开发不同之处
  20. java后端根据经纬度获取地址(高德地图)

热门文章

  1. vux 显示 html,基于 VUX 构建移动端项目
  2. 什么是热阻?导热材料中热阻的定义及测试方法介绍
  3. 虚拟桌面网络设置技巧
  4. 【NewTek Connect】Studio Monitor无法获取NewTek Connect内容的问题
  5. 资产组减值损失分摊matlab,资产组的认定及减值处理
  6. 微机原理 12-标志寄存器FR
  7. Keil编译下出现axf: Error: L6218E: Undefined symbol
  8. 【React】使用 react-pdf 将数据渲染为pdf并提供下载
  9. Efficient Batched Oblivious PRF -Private Set Intersection
  10. 安装office2016时,错误1406解决方法