Beispiel #1
0
        //向支付宝服务器获取支付所需的参数
        //然后发送回给客户端
        public void Pay(Agent agent)
        {
            //价格(元) 名称 标题 订单号...
            AliPayOrderInfo model = new AliPayOrderInfo()
            {
                Body           = "活动时装(测试)",
                Subject        = "超酷的英雄外表,仅限活动期间可以进行购买",                      //商品的标题/交易标题/订单标题/订单关键字等。
                TotalAmount    = "0.10",                                      //价格 单位为元
                ProductCode    = "QUICK_MSECURITY_PAY",                       //官方提供的固定值
                OutTradeNo     = ConvertDateTimeInt(DateTime.Now).ToString(), //唯一订单号
                TimeoutExpress = "30m",                                       //付款超时时间
                clientAgent    = agent,
            };

            //缓存 为了验签使用
            orderDic[model.OutTradeNo] = model;

            string str = GetAliPayParameter(model);

            Console.WriteLine($"调起支付接口的参数:{str}");
            agent.Send(str);
        }
Beispiel #2
0
        /// <summary>解析结果以及校验</summary>
        public void CheckAliPayResult(IAsyncResult ar)
        {
            try
            {
                HttpListenerContext context = httpListener.EndGetContext(ar);
                HttpListenerRequest request = context.Request;//请求对象
                if (context != null)
                {
                    StreamReader body       = new StreamReader(request.InputStream, Encoding.UTF8);   //读取流,用来获取支付宝请求的数据
                    string       pay_notice = HttpUtility.UrlDecode(body.ReadToEnd(), Encoding.UTF8); //HttpUtility.UrlDecode:解码 url编码,将字符串格式为%的形式,解码就是将%转化为字符串信息

                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    Console.WriteLine("支付结果来了:" + pay_notice);


                    Dictionary <string, string> aliPayResultDic = StringToDictionary(pay_notice);
                    // 验签 API
                    bool result = AlipaySignature.RSACheckV1(aliPayResultDic, AliPay_Public_Key,
                                                             "UTF-8", "RSA2", false);

                    if (result)
                    {
                        //缓存的订单
                        AliPayOrderInfo souceDic = orderDic[aliPayResultDic["out_trade_no"]];
                        //订单的判定
                        if (souceDic.OutTradeNo.Equals(aliPayResultDic["out_trade_no"]))
                        {
                            Console.WriteLine("存在相同的订单");

                            if (souceDic.TotalAmount.Equals(aliPayResultDic["total_amount"]))
                            {
                                Console.WriteLine("金额也是一致的:" +
                                                  aliPayResultDic["total_amount"] + "元");
                                souceDic.clientAgent.Send("支付成功");
                            }
                            else
                            {
                                Console.WriteLine("金额不一致");
                                //souceDic.clientAgent.Send("支付失败");
                            }
                        }
                        else
                        {
                            Console.WriteLine("未存在的订单记录:" +
                                              aliPayResultDic["out_trade_no"]);
                        }

                        //验签(支付)成功 告诉客户端 加钻石,给数据库加记录,等。。。
                        //另外官方建议:最好将返回数据中的几个重要信息,与支付时候对应的参数去对比。
                        //返回的所有参数都在这里:StringToDictionary(pay_notice)
                        //请求的参数 在:Get_equest_Str()

                        //成功了就需要给支付宝回消息“success”
                        //https://docs.open.alipay.com/204/105301/
                        HttpListenerResponse response = context.Response;


                        string responseString = "success";
                        byte[] buffer         = Encoding.UTF8.GetBytes(responseString);
                        response.ContentLength64 = buffer.Length;
                        Stream output = response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);//响应支付宝服务器本次的通知
                        output.Close();
                        response.Close();

                        //给客户端发送道具

                        //  OutTradeNo(订单号),道具ID,道具数量
                        //string toClientMsg = "sendProps" + "," + souceDic.OutTradeNo + "," + souceDic.objID + "," + souceDic.objCount;
                        //byte[] sendByte = Encoding.UTF8.GetBytes(toClientMsg);
                        //souceDic.clientAgent.SendBytes(sendByte);
                    }
                    else
                    {
                        Console.WriteLine("验签失败");
                    }

                    Console.WriteLine("验签结果:" + (result == true ? "支付成功" : "支付失败"));

                    if (aliPayResultDic.ContainsKey("trade_status"))
                    {
                        switch (aliPayResultDic["trade_status"])
                        {
                        case "WAIT_BUYER_PAY":
                            Console.WriteLine("交易状态:" + "交易创建,等待买家付款");
                            break;

                        case "TRADE_CLOSED":
                            Console.WriteLine("交易状态:" + "未付款交易超时关闭,或支付完成后全额退款");
                            break;

                        case "TRADE_SUCCESS":
                            Console.WriteLine("交易状态:" + "交易支付成功");
                            break;

                        case "TRADE_FINISHED":
                            Console.WriteLine("交易结束,不可退款");
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            if (httpListener.IsListening)
            {
                try
                {
                    httpListener.BeginGetContext(new AsyncCallback(CheckAliPayResult), null);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }