前端ajax
function doRefund()
{
$.ajax({ type: "POST", catch: false, url: "@Url.Action("AliBarPay", "Alipay")", data: { strTradeNo: $("input[name='strTradeNo']").val(), strAuthcode: $("input[name='strAuthcode']").val(), strTotalAmount: $("input[name='strTotalAmount']").val() }, dataType:'json',//返回数据类型为json类型 success: function (json) { if (json.result) { alert(json.msg) } else { alert(json.msg); } }, error: function (json) { alert(json.msg); } });
}
后端controller中代码:
public ActionResult AliBarPay(string strTradeNo, string strAuthcode, string strTotalAmount) { AlipayTradePayContentBuilder builder = BuildPayContent(strTradeNo, strAuthcode, strTotalAmount); string out_trade_no = builder.out_trade_no; AlipayF2FPayResult payResult = serviceClient.tradePay(builder); bool bResult = false; switch (payResult.Status) { case ResultEnum.SUCCESS: DoSuccessProcess(payResult); bResult = true; strResult = "支付成功!"; break; case ResultEnum.FAILED: DoFailedProcess(payResult); break; case ResultEnum.UNKNOWN: strResult = "网络异常,请检查网络配置后,更换外部订单号重试"; break; } var vJson = new { result = bResult, msg = strResult }; return Json(vJson);//返回json类型 }
$.ajax({ type: "POST", url: "/Alipay/AlipayRefund", data: { strTradeNo: $("input[name='strTradeNo3']").val(), strProductId: $("input[name='strProductId']").val(), strRefoundAmount: $("input[name='strRefundAmount']").val(), }, dataType: "text",//返回类型为string的 success: function (result) { if (result == "" || result == null) { alert("无返回值"); return; } $("#refundDetail").html(result); alert(result); } });
后端controller:
public ActionResult AlipayRefund(string strTradeNo,string strProductId,string strRefoundAmount) { AlipayTradeRefundContentBuilder builder = BuildRefundContent(strTradeNo, strProductId, strRefoundAmount); AlipayF2FRefundResult refundResult = serviceClient.tradeRefund(builder); switch (refundResult.Status) { case ResultEnum.SUCCESS: strResult = DoRefundSuccessProcess(refundResult); break; case ResultEnum.FAILED: strResult = DoRefundFailedProcess(refundResult); break; case ResultEnum.UNKNOWN: if (refundResult.response == null) { strResult = "配置或网络异常,请检查"; } else { strResult = "系统异常,请走人工退款流程"; } break; } return Content(strResult);//返回string类型 }
其中必须注意的是,如果是button来调用ajax,必须指定button的type='button',不然在ajax中不会返回值:
<div> <span>支付宝退费</span> <form> <span>商户订单号:</span><input type="text" name="strTradeNo3" /> <span>支付宝订单号:</span><input type="text" name="strProductId" /> <span>总金额金额:</span><input type="text" name="strRefundAmount" /> <button id="doSubmit4" type="button" onclick="doRefund()">退费提交</button>//type='button' 必须写,不然在方法中执行没得返回值,但chrom中调试可以显示,不调试时,就是显示不出来!!! </form> <span id="refundDetail"></span> </div>