一:獲取微信支付 MCHID,KEY,APPID,APPSecret 四個支付關鍵值.
微信支付商戶平臺 https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F
1.登陸微信支付商戶平臺獲取到商戶號(MCHID),
2.在"帳號中心"欄目下"API安全"欄目裏設置API密鑰(KEY)
微信公衆號: https://mp.weixin.qq.com/
1.登陸微信公衆在"基本配置"欄獲取應用ID(APPID)和應用密鑰(APPSecret)
2.在"接口權限"欄目下"網頁帳號"綁定正式支付的域名 (如:××××.net,不要http:和"/"斜槓)
3.在"微信支付"欄目下"開發配置"裏面設置公衆支付的支付受權目錄(如:××××.net/WeChatWeb/)javascript
二:把WxPayAPI添加到製做項目中,在Config.cs文件裏修改獲取到的MCHID,KEY,APPID,APPSecret四個關鍵值以及NOTIFY_URL值(NOTIFY_URL是支付受權目錄),並在MVC項目裏建一個WeChatWeb控制器,裏面加上邏輯代碼.並傳遞微信jsapi支付所需的參數.代碼示例以下:
後臺Action代碼
/// <summary>
/// 獲取微信支付相關信息
/// </summary>
/// <returns></returns>
[HttpGet]
public virtual ActionResult Index()
{
JsApiPay jsApiPay = new JsApiPay();
OStudent model = null;
try
{
//調用【網頁受權獲取用戶信息】接口獲取用戶的openid和access_token
jsApiPay.GetOpenidAndAccessToken();
//獲取微信支付者公共id
jsApiPay.openid = jsApiPay.openid;
string ID = Request["ID"]; //若是要獲取頁面傳遞過來的值,需修改GetOpenidAndAccessToken()方法裏面的代碼,加上Request.Url.Query獲取參數
model = OStudentSiteService.GetByKey(id).AppendData as OStudent;
if (model != null)
{
jsApiPay.total_fee = 1;//測試 訂單金額(1表示分,正式金額要*100)
jsApiPay.Order_ID = model.order_ID; //訂單號(本身定義訂單號)
}
//JSAPI支付預處理
//調用統一下單,得到下單結果
WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
//從統一下單成功返回的數據中獲取微信瀏覽器調起jsapi支付所需的參數
var wxJsApiParam = jsApiPay.GetJsApiParameters(); //獲取到的是json格式字符串
ViewBag.wxJsApiParam = wxJsApiParam; //前臺頁面js調用
Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam);
}
catch (Exception ex)
{
Response.Write(ex.Message + "," + ex.Source);
Response.End();
}
return View(model);
}
//修改支付方式
[HttpPost]
public virtual JsonResult PayMethod()
{
AjaxJsonResult ajax = new AjaxJsonResult() { err = true, msg = string.Empty, timeout = 3 };
string id = Request.Form["id"];
string payMethod = Request.Form["payMethod"];
var model = (Project.Core.Models.Model.OStudent)OStudentSiteService.GetByKey(id).AppendData;
model.payMethod = payMethod; //支付方式
OperationResult result = OStudentSiteService.Modify(model);
if (result.ResultType == OperationResultType.Success)
{
ajax.err = false;
ajax.msg = "操做成功";
}
return Json(ajax);
}
/// <summary>
/// 修改支付狀態
/// </summary>
/// <param name="userID"></param>
/// <returns></returns>
[HttpPost]
public virtual string EditPayStatus(Guid userID)
{
string msg = "error";
var model = OStudentSiteService.GetByKey(userID).AppendData as OStudent;
model.Status = (int)X.Project.Site.Models.Enum.PayStatus.Success; //付款成功
OperationResult result = OStudentSiteService.Modify(model);
if (result.ResultType == OperationResultType.Success)
{
msg = "ok";
}
return msg;
}
前臺Index.chtml視圖頁面JS代碼php
<script type="text/javascript">
var _wxJsApiParam = eval('(@Html.Raw(ViewBag.wxJsApiParam))');
function callpay() {
//選擇支付方式
var payMethod = $("input[name='PayMethod']:checked").val();
if (payMethod == "" || payMethod == null) {
layer.msg("請選擇支付方式", function () { })
return false;
}
$.ajax({
type: "POST",
dataType: "JSON",
url: '@Url.Action("PayMethod", "WeChatWeb")',
data: {
payMethod: payMethod,
id: '@Model.ID'
},
success: function (data) {
//表示修改支付方式成功
if (!data.err) {
//1表示微信支付,則調用微信支付
if (payMethod == "1") {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}
else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}
else {
jsApiCall();
}
} else if (payMethod == "2") {
layer.alert('恭喜您,操做成功!', function () {
window.location.href = "@Url.Action(MVC.Default.Index())" ; //操做成功後的跳轉頁面
});
}
} else {
layer.msg("操做失敗", function () { })
return false;
}
}
});
}
//調用微信JS api 支付
function jsApiCall() {
WeixinJSBridge.invoke('getBrandWCPayRequest', _wxJsApiParam,
function (res) {
if (res.err_msg == "get_brand_wcpay_request:cancel") {html
layer.msg("已取消支付", function () { });
return false;
} else if (res.err_msg == "get_brand_wcpay_request:ok") {
//支付成功
//ajax
$.ajax({
type: "POST",
dataType: "text",
url: '@Url.Action("EditPayStatus", "WeChatWeb")',
data: {
userID: '@Model.ID'
},
error: function (request) {java
layer.msg("服務器錯誤!", function () { });
return false;
},
success: function (data) {
window.location.href = "@Url.Action(MVC.Default.Index())"; //支付成功後跳轉的頁面
}
}); //ajax end
}
});
}
</script>ajax
公司最近有微信支付功能,看了半天微信支付資料,也在網上查找了些資料,可是對於獲取微信商戶支付平臺和微信公衆號所需的東西無從下手.根據本身的摸索已經網上的指導,趁熱把微信支付流程記錄下來,供本身下次使用,也當是一次從新學習的機會.本女子不才,若有錯誤,還請各位大牛們見諒,也請你們多多指教!(第一次在博客園發表信息心裏是激動且煎熬的)json