C#实现支付宝沙箱支付的项目实践
作者:IT_YAO 时间:2021-07-29 17:18:18
一,准备沙箱环境
1,登录支付宝,进入 应用列表界面 https://openhome.alipay.com/dev/workspace
2,如下图选择进入沙箱
进入如下页面:
1,这里的APPID很有用
2,在这里只测试网页支付,用系统默认的密钥
3,查看公钥有如下界面:
需要用到的是应用私钥(非JAVA)和支付宝公钥
二,认识官方提供的Demo示例
1,下载demo示例
进入网页 https://opendocs.alipay.com/open/270/106291
下载.NET版的demo,如下:
App_Code下有Config.cs文件
关于参数:
1,app_id 就填写沙箱界面的APPID
2,需要将gatewayUrl 改为:https://openapi.alipaydev.com/gateway.do 这才是测试版本
3,商户私钥 复制沙箱界面系统默认密钥的 应用私钥
4,支付宝公钥 复制沙箱界面的支付宝公钥
wappay里是具体的调用API接口
wappay是发送调用支付接口
close是关闭订单接口
query是查询订单接口
refund是退款接口
三,编写一个ASP.NET的程序
1,创建ASP.NET web项目
2,解决方案导入Demo示例中的AopSdk模块,如下:
3,前台代码:
<table>
<tr>
<td>订单名称:</td>
<td>
<asp:TextBox ID="tbxOrderName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>订单金额:</td>
<td>
<asp:TextBox ID="tbxOrderAmount" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>订单描述:</td>
<td>
<asp:TextBox ID="tbxOrderDesc" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btn" runat="server" Text="支付" OnClick="btn_Click"/>
</td>
</tr>
</table>
4,后台代码:
// 应用ID,您的APPID
public static string app_id = "20210******";
// 支付宝网关
public static string gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// 商户私钥,您的原始格式RSA私钥
public static string private_key = "MIIE******";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static string alipay_public_key = "MIIBI******";
// 签名方式
public static string sign_type = "RSA2";
// 编码格式
public static string charset = "UTF-8";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
DefaultAopClient client = new DefaultAopClient(gatewayUrl, app_id, private_key, "json", "1.0", sign_type, alipay_public_key, charset, false);
// 外部订单号,商户网站订单系统中唯一的订单号
string out_trade_no = DateTime.Now.ToString("yyyyMMddHHmmss");
// 订单名称
string subject = this.tbxOrderName.Text;
// 付款金额
string total_amout = this.tbxOrderAmount.Text;
// 商品描述
string body = this.tbxOrderDesc.Text;
// 支付中途退出返回商户网站地址
string quit_url = "https://localhost:44334/PayFailed.aspx?tradeNo=" + out_trade_no;
// 支付成功返回商户网站页面
string return_url = "https://localhost:44334/PaySuccess.aspx?tradeNo=" + out_trade_no;
// 设置支付完成异步通知接收地址
string notify_url = "https://localhost:44334/Notify.aspx";
// 组装业务参数model
AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
model.Body = body;
model.Subject = subject;
model.TotalAmount = total_amout;
model.OutTradeNo = out_trade_no;
model.ProductCode = "QUICK_WAP_WAY";
model.QuitUrl = quit_url;
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
// 设置支付完成同步回调地址
request.SetReturnUrl(return_url);
// 设置支付完成异步通知接收地址
request.SetNotifyUrl(notify_url);
// 将业务model载入到request
request.SetBizModel(model);
AlipayTradeWapPayResponse response = null;
try
{
response = client.pageExecute(request, null, "post");
Response.Write(response.Body);
}
catch (Exception exp)
{
throw exp;
}
说明:
1,这里的app_id等配置,就是来源于 Demo示例中的config.cs文件 需要从自己的支付宝沙箱中将相应的值复制进来
2,https://localhost:44334 这个地址和端口号是当前自己的项目的端口号
3,分别建立PaySuccess.aspx PayFailed.aspx 两个个窗体 分别用于支付成功和失败的跳转页面
4,Notify.aspx用于支付成功的异步通知用,可以不加
四,开始测试
调用成功,则进入如下界面
点击继续浏览器付款
点击支付宝账号登录
注意!这里不是真实的支付宝账号,需要用沙箱环境的支付宝账号,在沙箱界面中找:
支付成功后,点击右上角完成,则进入代码中设定的PaySuccess.aspx页面,同时也将订单编号等信息带过去。
至此,模拟支付成功,更多相关C# 支付宝沙箱支付内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://blog.csdn.net/IT_YAO/article/details/123752463
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分布式系统下调用链追踪技术面试题
![](https://img.aspxhome.com/file/2023/6/60126_0s.png)
C#枚举类型与位域枚举Enum
eclipse怎么引入spring boot项目插件的方法
![](https://img.aspxhome.com/file/2023/3/78353_0s.jpg)
Spring事务失效问题分析及解决方案
一文搞懂Java MD5算法的原理及实现
java多线程复制文件的实例代码
SpringBoot中自定义注解实现参数非空校验的示例
![](https://img.aspxhome.com/file/2023/0/70260_0s.webp)
Java的JNI快速入门教程(推荐)
浅谈java异常链与异常丢失
Java并发编程之同步容器
![](https://img.aspxhome.com/file/2023/8/70308_0s.png)
C#基础:Dispose()、Close()、Finalize()的区别详解
C#使用Task实现异步方法
![](https://img.aspxhome.com/file/2023/6/70436_0s.jpg)
使用Maven Helper解决Maven插件冲突的方法
![](https://img.aspxhome.com/file/2023/1/58751_0s.jpg)
Spring Security入门demo案例
Unity实现俄罗斯方块
![](https://img.aspxhome.com/file/2023/8/69198_0s.jpg)
解决IDEA鼠标点击光标变大问题
![](https://img.aspxhome.com/file/2023/4/79964_0s.jpg)
Java 常见排序算法代码分享
![](https://img.aspxhome.com/file/2023/5/78265_0s.png)
Java中ArrayList和LinkedList之间的区别_动力节点Java学院整理
C# Newtonsoft.Json 解析多嵌套json 进行反序列化的实例
![](https://img.aspxhome.com/file/2023/1/71061_0s.jpg)
Spring中集成Groovy的四种方式(小结)
![](https://img.aspxhome.com/file/2023/7/73117_0s.jpg)