写本文章的目的是为了记录工作中遇到的问题,方便以后遇到可以迅速解决问题

H5手机网站接入支付宝的支付接口,推荐使用支付宝提供的SDK来快速开发

我使用的是SDK开发

引用命名空间

using Aop.Api;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api.Util;

首页需要定义一些常量

 static string serverUrl = "https://openapi.alipaydev.com/gateway.do";
static string app_id = "**"; //开发者的应用ID
static string format = "JSON";
static string charset = "utf-8";
static string sign_type = "RSA2"; //签名格式
static string version = "1.0";
string UID = "2088102169707816";//卖家支付宝账户号
//商户私钥
static string merchant_private_key = "***";
//支付宝公钥
static string alipay_public_key = "***";

这里的app_id,merchant_private_key,alipay_public_key 我就没有列出来了,获取的方法需要自己去支付宝平台完成一些操作进行获取

在用户点击网站付款时,我们需要唤醒支付宝,来进行支付

public string H5RequestPayWay(OrderPO order)
{
IAopClient client = new DefaultAopClient(serverUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, charset, false);
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
string address= "http://m." + PathLogic1.RootDomain;
request.SetReturnUrl(address+ "/WebPay/AlipayPayResult");//同步请求
request.SetNotifyUrl(address + "/WebPay/AsyncPay");//异步请求
var lstDetail = Context.Data.OrderDetail.Where(x => x.OrderNo == order.OrderNo).ToSelectList(x=>new { x.SkuName});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < lstDetail.Count(); i++)
{
sb.Append(lstDetail[i].SkuName + ",");
}
request.BizContent = "{" +
"\"body\":\""+sb.ToString().Substring(0,sb.Length-1)+"\"," +
"\"subject\":\"袋鼠巴巴商品支付\"," +
"\"out_trade_no\":\""+order.OrderNo+"\"," +
"\"timeout_express\":\"90m\"," +
"\"total_amount\":"+(order.TotalAmount.Value+order.TotalFreight.Value)+"," +
"\"product_code\":\"QUICK_WAP_PAY\"" +
" }";//这里填写一些发送给支付宝的一些参数 AlipayTradeWapPayResponse response = client.pageExecute(request);
return response.Body;//这里会发送一个表单输出到页面中
}

具体发送给支付宝的参数,自行去查看

执行上面方法后,买家输入自己的支付宝账号密码进行支付,支付成功的结果,支付宝会以post的方式异步请求你的SetNotifyUrl的地址

这个SetNotifyUrl的地址必须要外网可以访问,支付宝的请求才能进来

买家支付成功,商家修改订单状态和数据库的操作,都在异步请求中执行

同步请求

  public ActionResult AlipayPayResult()
{
ViewBag.result = "success";
return View("PayResult");
}
         /// <summary>
        /// 验证通知数据的正确性
        /// </summary>
        /// <param name="out_trade_no"></param>
        /// <param name="total_amount"></param>
        /// <param name="seller_id"></param>
        /// <returns></returns>
private 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;
}
  /// <summary>
/// 验签
/// </summary>
/// <param name="inputPara"></param>
/// <returns></returns>
public Boolean Verify(SortedDictionary<string, string> inputPara)
{
Dictionary<string, string> sPara = new Dictionary<string, string>();
Boolean verifyResult = AlipaySignature.RSACheckV1(inputPara, alipay_public_key, charset,sign_type,false);
return verifyResult;
}

异步请求:

 [HttpPost]
public void AsyncPay()
{
SortedDictionary<string, string> sPara = GetRequestPost();//将post请求过来的参数传化为SortedDictionary
if (sPara.Count > 0)
{
AlipayTradeWayPayServer pay = new AlipayTradeWayPayServer();
Boolean VerifyResult = pay.Verify(sPara);//验签if (VerifyResult)
{
try
{
//商户订单号
string out_trade_no = Request.Form["out_trade_no"];
//支付宝交易号
string trade_no = Request.Form["trade_no"];
//支付金额
decimal total_amount = Request.Form["total_amount"].ConvertType(Decimal.Zero);
//实收金额
//decimal receipt_amount = Request.Form["receipt_amount"].ConvertType(Decimal.Zero);
//交易状态
string trade_status = Request.Form["trade_status"];
//卖家支付宝账号
string seller_id = Request.Form["seller_id"]; //商品描述
string body = Request.Form["body"];
//交易创建时间
DateTime gmt_create = DateTime.Parse(Request.Form["gmt_create"]);
//交易付款时间
DateTime gmt_payment = DateTime.Parse(Request.Form["gmt_payment"]);
string appid = Request.Form["app_id"];
WriteError("验证参数开始");
Boolean DataValidity = pay.CheckInform(out_trade_no, total_amount, seller_id, appid);//商家判断参数时候是否匹配if (DataValidity)
{
if (Request.Form["trade_status"] == "TRADE_FINISHED")
{
AlipayWayPayPO model = CreateAlipayWayPay(out_trade_no, trade_no, trade_status, gmt_create, gmt_payment);
pay.PaySuccess(out_trade_no, model);//修改订单
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
}
else if (Request.Form["trade_status"] == "TRADE_SUCCESS")
{
AlipayWayPayPO model = CreateAlipayWayPay(out_trade_no, trade_no, trade_status, gmt_create, gmt_payment);
pay.PaySuccess(out_trade_no, model);//修改订单
//注意:
//付款完成后,支付宝系统发送该交易状态通知
}
else
{ } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— Response.Write("success"); //请不要修改或删除 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
catch (Exception ex)
{ }
}
else//验证失败
{
Response.Write("fail");
}
}
else
{
Response.Write("无通知参数");
}
}

H5网站接入支付宝的支付接口的更多相关文章

  1. 支付宝WAP支付接口开发(Node/Coffee语言)

    此博客不更新很久了, 更新的文档在这, 有兴趣到这里围观: http://neutra.github.io/2013/%E6%94%AF%E4%BB%98%E5%AE%9DWAP%E6%94%AF%E ...

  2. php支付宝在线支付接口开发教程【转】

    php支付宝在线支付接口开发教程 这篇文章主要为大家详细介绍了php支付宝在线支付接口开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下   1.什么是第三方支付 所谓第三方支付,就是一些和各 ...

  3. 支付宝WAP支付接口开发

    支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...

  4. 【转】支付宝WAP支付接口开发

    支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...

  5. 支付宝php支付接口说明

    直接把该代码放到PHP服务器下,直接访问index.php.1.文件列表: alipay_config.php    (基本参数配置页面,填写商家的支付宝安全校验码,合作id,支付宝帐号等内容)ind ...

  6. Django对接支付宝Alipay支付接口

    最新博客更新见我的个人主页: https://xzajyjs.cn 我们在使用Django构建网站时常需要对接第三方支付平台的支付接口,这里就以支付宝为例(其他平台大同小异),使用支付宝开放平台的沙箱 ...

  7. 别无分号只此一家,Python3接入支付宝身份认证接口( alipay.user.certify)体系(2021年最新攻略)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_184 目前国内身份认证体系做的比较不错的大抵就是支付宝和微信两家了,支付宝的身份验证基于支付宝app的实人认证能力,采用多因子认证 ...

  8. phpt5支付宝登陆支付接口解析

    先看效果图 下面的源码来源网络,自己对照修改. 放入一个插件库中,方便管理 创建支付类 1.发起支付 public function init() { $order_id = $_REQUEST['o ...

  9. H5 网站支付宝支付(前端部分)包含微信浏览器中的处理方法。

    手机网站唤起支付宝支付: H5 网站实现支付宝支付是一个很常见的需求: 实现方式主要是在后台配置和预支付, 前端需要做的就是唤起 支付宝App 然后就可以输入密码支付. 这个其实难度很低, 主要就是在 ...

随机推荐

  1. edgex0.7.1_1.0.1的X86编译和交叉编译

    一. X86编译 1. 安装zeromq库 根据setup script安装: wget https://github.com/zeromq/libzmq/releases/download/v4.2 ...

  2. [转] 雷电三和typec傻傻分不清

    原文:https://club.lenovo.com.cn/thread-4921715-1-1.html 因为形状完全一致,所以很多人都误以为Type-C=雷电3. 实际上,雷电3只是采用了Type ...

  3. [IC]浅谈嵌入式MCU软件开发之中断优先级与中断嵌套

    转自:https://mp.weixin.qq.com/s?__biz=MzI0MDk0ODcxMw==&mid=2247483680&idx=1&sn=c5fd069ab3f ...

  4. WebForm SignalR 实时消息推送

    原文:https://www.jianshu.com/p/ae25d0d77011 官方文档:https://docs.microsoft.com/zh-cn/aspnet/signalr/ 实现效果 ...

  5. linux系统编程综合练习-实现一个小型的shell程序(三)

    上节中已经实现了对普通命令的解析,包括输入重定向,输出重定向,管道,后台作业,这次就来执行已经解析好的命令,对应的函数为:execute_command(),首先对带有管道的命令进行执行: 比如:&q ...

  6. git push 缓存密码和用户名

    https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password git remote -v -- ...

  7. 07 c++中的内联函数inline

    文章链接: 问题描述:类中成员函数缺省默认是内联的,如果在类定义时就在类内给出函数定义,那当然最好.如果在类中未给出成员函数定义,而又想内联该函数的话,那在类外要加上 inline,否则就认为不是内联 ...

  8. keras模块学习之Sequential模型学习笔记

    本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! Sequential是多个网络层的线性堆叠 可以通过向Sequential模型传递一个layer的list来构造该模型: from ...

  9. 前端学习笔记--css案例

    要实现的案例: 1.分析布局 2.划分文件结构: 3.编写css代码 * { padding: 0; margin: 0; } body { font-size: 16px; color: burly ...

  10. SVM: 直观上理解大间距分类器

    在SVM中,增加安全的间距因子 那么增加了这个间距因子后,会出现什么样的结果呢,我们将C设置为很大(C=100000) SVM决策边界 当我们将C设置得很大进,要想SVM的cost function最 ...