C#支付寶支付介面H5版(手機網頁支付)
首先下載支付寶sdk類庫AopSdk
sdk下載地址:http://download.csdn.net/detail/hw1233456/9759306
前端樣式和指令碼下載地址:http://download.csdn.net/detail/hw1233456/9764832
將專案加入你的解決方案並引用。
後臺C#程式碼:
/// <summary>
/// H5支付
/// </summary>
/// <param name="model"></param>
/// <param name="configPath"></param>
/// <returns></returns>
public static string Alipay()
{
string OrderNumber = "alipay" + DateTime.Now.ToString("yyyyMMddHHmmss");
string app_id = "你的app_id";
string merchant_private_key = "你的應用私鑰";
string alipay_public_key = "你的支付寶公鑰";
string timeout_express = "30m";//訂單有效時間(分鐘)
string postUrl = "https://openapi.alipay.com/gateway.do";
string sign_type = "RSA2";//加簽方式 有兩種RSA和RSA2 我這裡使用的RSA2(支付寶推薦的)
string version = "1.0";//固定值 不用改
string format = "json";//固定值
string Amount = "0.01";//訂單金額
string method = "alipay.trade.wap.pay";//呼叫介面 固定值 不用改
IAopClient client = new DefaultAopClient(postUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, "UTF-8", false);
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.SetNotifyUrl("支付寶後臺通知的地址");
request.SetReturnUrl("支付寶前臺回跳的地址");
request.BizContent = "{" +
" \"body\":\"對一筆交易的具體描述資訊。如果是多種商品,請將商品描述字串累加傳給body。\"," +
" \"subject\":\"商品描述\"," +
" \"out_trade_no\":\"商家唯一訂單,填寫你專案裡生成的唯一訂單號\"," +
" \"timeout_express\":\"" + timeout_express + "\"," +
" \"total_amount\":" + Amount + "," +
" \"product_code\":\""+ method + "\"" +
" }";
AlipayTradeWapPayResponse response = client.pageExecute(request);
string form = response.Body.Substring(0, response.Body.IndexOf("<script>"));
return form;
}
前臺html程式碼:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/CSS/mui.min.css" rel="stylesheet" />
<script src="~/Scripts/mui.min.js"></script>
<script src="~/Scripts/jquery.min.js"></script>
<title>支付平臺</title>
<script type="text/javascript">
jQuery(document).ready(function () {
var alipay = document.getElementById("alipay");
alipay.addEventListener("tap", function () {
var productName = jQuery("#productName").html();
var unitPrice = jQuery("#unitPrice").html();
var quantity = jQuery("#quantity").html();
var amount = jQuery("#amount").html();
var url = "後臺生成訂單的URL";
jQuery.post(url ,
{
productName: productName,
unitPrice: unitPrice,
quantity: quantity,
amount: amount
}, function (data) {
if (data != "")
{
jQuery("#formDiv").append(data);
jQuery("#alipaysubmit").submit();
}
});
});
var wxpay = document.getElementById("wxpay");
wxpay.addEventListener("tap", function () {
});
});
</script>
</head>
<body>
<div>
<header class="mui-bar mui-bar-nav">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">選擇支付方式</h1>
</header>
<!--Infomations-->
<div class="mui-content">
<div class="mui-card">
<ul class="mui-table-view">
<li class="mui-table-view-cell">
商品名稱:<span id="productName">茶葉蛋</span>
</li>
<li class="mui-table-view-cell">
商品單價:<span id="unitPrice">0.01</span>
</li>
<li class="mui-table-view-cell">
購買數量:<span id="quantity">1</span>
</li>
<li class="mui-table-view-cell">
商品總價:<span id="amount">0.01</span>
</li>
</ul>
</div>
<div style="color:red;margin:10px;font-size:14px;">訂單有效時間:30分鐘</div>
<div class="mui-card">
<ul class="mui-table-view">
<li id="alipay" class="mui-table-view-cell">
<a class="mui-navigate-right">
支付寶
</a>
</li>
<li id="wxpay" class="mui-table-view-cell">
<a class="mui-navigate-right">
微信
</a>
</li>
</ul>
</div>
</div>
<!--form-->
<div id="formDiv" style="display:none;">
</div>
</div>
</body>
</html>
寫的挺清楚的了 看完應該都會了 如果還是不明白或者有其他疑問的 可以給我留言 我看到了就回復。
稍後把APP支付的也發上來,使用Hbuilder打包APP呼叫的 也是C#在後臺生成的訂單。