c#支付宝支付

1、进入支付宝去申请appid和密钥 https://open.alipay.com/platform/home.htm 支付宝开放平台

2、下载sdk根据自己是啥开发语言下载啥 

3、开始写代码

    后台请求参数和方式

    

/// <summary>
        /// 支付宝支付
        /// </summary>
        /// <param name="model"></param>
        /// <param name="configPath"></param>
        /// <returns></returns>
        public  string Alipay(HttpContext context)
        {
            string OrderNumber = "" ;//订单号,此单号必须唯一
            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\":\"" + OrderNumber + "\"," +   //商家唯一订单,填写你项目里生成的唯一订单号
            "    \"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;
        }

   页面上的请求方式(这里为了方便写的非常简单)

<head runat="server">
 <meta name="viewport" content="width=device-width" />
    <script src="Scripts/jquery-1.7.1.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="支付啊" OnClick="Button1_Click" />
    </div>
        <div id="formDiv" style="display:none;">
        </div>
    </div>
          <script type="text/javascript">
              $(function(){
                  $("#Button1").click(function () {
                      $.ajax({
                          url: "Handler1.ashx",  //页面上是到一般处理程序中处理的
                          data: {"action": "Alipay" },
                          type: "POST",
                          cache: false,
                          async: true,
                          success: function (data) {
                              alert(data);
                              if (data != "") {
                                  $("#formDiv").append(data);
                                  $("#alipaysubmit").submit(); //返回的是form表单 直接提交就可以了.
                              }
                          }
                      });
                                        });
                  });
              });
    </script>
    </form>
</body>

猜你喜欢

转载自www.cnblogs.com/superMay/p/9474214.html