asp.net mvc内微信pc端、H5、JsApi支付方式总结

时间:2019-12-13 09:20:42   收藏:0   阅读:289

转自:http://www.80cxy.com/Blog/ArticleView?arId=201912122203555530g0wwI8

本文提供技术支持QQ:806693619   V:kwstugdb

微信提供了各种支付方式,有针对手机APP开发的支付方式,有针对pc web端的,手机端的等,pc端支付使用场景是在pc端完整内使用的支付方式,JsApi只能在微信内被调用,H5不限于微信内使用,手机端浏览器内也可以使用,个人理解的不知道对不对,本文主要介绍pc端、H5、JsApi支付方式,废话不多说直接上代码,代码只提供JsApi部分,pc端web及H5端的代码直接下载附件,附件内包括全部代码及相关类库。

微信JsApi支付之前必须通过后台获取支付调起参数,参数获得后通过js方法调起支付才行。

一、前端支付Js代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<script>

        var _wxJsApiParam;

        function callpay() {

            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();

            }

        }

        //调用微信JS api 支付

        function jsApiCall() {

            WeixinJSBridge.invoke(‘getBrandWCPayRequest‘, _wxJsApiParam,

              function (res) {

                  if (res.err_msg == "get_brand_wcpay_request:cancel") {

                      $.messager.alert(‘提示信息‘, ‘支付已经取消!‘, ‘info‘);

                      return false;

                  } else if (res.err_msg == "get_brand_wcpay_request:ok") {

                      //支付成功

                      $.messager.alert(‘提示信息‘, ‘支付成功!‘, ‘info‘);

                  }

              });

        }

        //获取微信支付参数

        function rePay(obj) {

            //调起微信公众号JsApi支付

            $.ajax({

                url: "/Pay/RePayJsApi",

                type: ‘post‘,

                cache: false,

                dataType: ‘html‘,

                data: {

                    OrderId: obj

                },

                success: function (data) {

                    if (data != "Error") {

                        _wxJsApiParam = eval(‘(‘ + data + ‘)‘);

                        callpay();

                    }

                    else $.messager.alert(‘提示信息‘, ‘生成订单失败!‘, ‘info‘);

                },

                error: function (XMLHttpRequest, textStatus, errorThrown) {

                    alert(textStatus); return;

                }

            });

        }

    </script>

二、后台代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

/// <summary>

/// 重新发起支付

/// </summary>

/// <param name="tmpModel"></param>

/// <returns></returns>

[HttpPost]

public ActionResult RePayJsApi()

{

    try

    {

        string orderId = ClassesLib.GetString("OrderId");

        string newOrderId = ClassesLib.GetIdByTime();

        var model = db.MEMBER_ORDER.Find(orderId);

        model.ORDERID = newOrderId;

        db.SaveChanges();

        //登录判断

        if (ClassesLib.WxSessionIsNull())

        {

            return Content("<script>alert(‘登录超时,请重新登录!‘);window.location.href=‘/WeiXin/Login?returnUrl=/WeiXin/ViewProduct?objId=" + model.OBJECTID + "&salesmanId=" + model.SALEID + "‘</script>");

        }

        //调用支付

        JsApiPay jsApiPay = new JsApiPay();

        jsApiPay.bodyStr = model.OBJECTNAME + "-" + ClassesLib.getWxLoginName();

        jsApiPay.attachStr = model.OBJECTNAME + "-" + ClassesLib.getWxLoginName();

        jsApiPay.orderId = newOrderId;

        jsApiPay.openid = model.OPENID;

        jsApiPay.total_fee = Convert.ToInt32(model.MONEY * 100);//测试 订单金额(1表示分,正式金额要*100)

        //JSAPI支付预处理

        //调用统一下单,获得下单结果

        WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();

        //从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数

        var wxJsApiParam = jsApiPay.GetJsApiParameters(); //获取到的是json格式字符串

        return Content(wxJsApiParam);

    }

    catch (Exception e)

    {

        return Content("Error");

    }

}

[HttpPost]

public virtual ActionResult JsApiNotifyUrl()

{

    ClassesLib.InsertLog("支付测试1", "支付");

    WxPayData notifyData = GetNotifyData();

    //检查支付结果中transaction_id是否存在

    if (!notifyData.IsSet("transaction_id"))

    {

        //若transaction_id不存在,则立即返回结果给微信支付后台

        WxPayData res = new WxPayData();

        res.SetValue("return_code", "FAIL");

        res.SetValue("return_msg", "支付结果中微信订单号不存在");

        Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());

        return Content("支付结果中微信订单号不存在");

    }

    ClassesLib.InsertLog("支付测试2", "支付");

    string transaction_id = notifyData.GetValue("transaction_id").ToString();

    string trade_no = notifyData.GetValue("out_trade_no").ToString();

    //查询订单,判断订单真实性

    if (!QueryOrder(transaction_id))

    {

        //若订单查询失败,则立即返回结果给微信支付后台

        WxPayData res = new WxPayData();

        res.SetValue("return_code", "FAIL");

        res.SetValue("return_msg", "订单查询失败");

        Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());

        return Content("订单查询失败");

    }

    //查询订单成功

    else

    {

        ClassesLib.InsertLog("支付测试3", "支付");

        WxPayData res = new WxPayData();

        res.SetValue("return_code", "SUCCESS");

        res.SetValue("return_msg", "OK");

        Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());

        //更新支付状态

        var model = db.MEMBER_ORDER.Where(c => c.ORDERID == trade_no).FirstOrDefault();

        if (model != null && model.PAYSTATE == "未支付")

        {

            model.PAYSTATE = "已支付";

            model.PAYDATE = DateTime.Now;

            //发送支付成功提示,通过微信消息通知收银员已经收到付款

            SendTemplateMessage.SendMessage("oPCLNv5B1T8kYM5AYgXWHOiD24_s", "您好,用户" + model.MEMBERNAME + "已购买服务", model.OBJECTNAME, (Convert.ToDouble(model.MONEY) * 0.01).ToString() + "元", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            db.SaveChanges();

        }

        return Content("支付成功" + res.ToXml());

    }

}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!