• c# 支付宝查单补单


    谁谁谁手机那边支付宝支付成功扣钱了,游戏里面却没有充钱,唯有写个补单程序。

    首先要查单,查单接口(交易信息查询接口:https://mapi.alipay.com/gateway.do),服务名称:single_trade_query。

    这个接口权限要到支付宝联系客服开通

    用4点积分去CSDN下载了个demo,我共享吧:https://yunpan.cn/cqavWFY3Tccua  访问密码 2860

    demo是web形式的,我要的是winfrom效果,在服务器每隔几分钟就运行一次,实现自动补单效果。

    主要代码:

                DataControl gData = new DataControl();
                try
                {
                    string url = "https://mapi.alipay.com/gateway.do?";
                    
    
                    //查询未付款的支付宝订单
                    DataTable dt = gData.GetDataTable("select .... from ....");
                    string post_data = "";
                    string requestData = "";
    
                    foreach (DataRow r in dt.Rows)
                    {
                        //把请求参数打包成数组
                        SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
                        sParaTemp.Add("partner", Config.Partner);
                        sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
                        sParaTemp.Add("service", "single_trade_query");
                        sParaTemp.Add("trade_no", "");
                        sParaTemp.Add("out_trade_no", r["OrderID"].ToString());
                        post_data = AlipaySubmit.BuildRequest(sParaTemp);
    
                        requestData = gData.CallWebApi(url + post_data);
    
                        QueryOrder queryorder = new QueryOrder();
                        queryorder = InsQueryOrder(requestData);
    
                        if (queryorder.is_success == "T")
                        {
                            if (queryorder.trade_status == "TRADE_SUCCESS")
                            {
                                WriteReturnZFBDetail(queryorder);
                                FilliedOnline(queryorder.out_trade_no);
                            }
                        }
                    }
    
                    
                }
                catch(Exception ex) { }

    post_data是api需要的参数,得到的效果是这样的:

    service=single_trade_query&sign=签名&partner=支付宝合作身份者ID&out_trade_no=商户订单号&sign_type=MD5

    可以直接浏览器这样访问:

    https://mapi.alipay.com/gateway.do?service=single_trade_query&sign=签名&partner=支付宝合作身份者ID&out_trade_no=商户订单号&sign_type=MD5

    结果:

    前提是准备好了自己的相关数据。

    requestData = gData.CallWebApi(url + post_data);

    这个函数可以得到API返回来的数据,该API返回数据格式是xml,数据是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <alipay>
        <is_success>T</is_success>
        <request>
            <param name="trade_no">2010073000030344</param>
            <param name="service">single_trade_query</param>
            <param name="partner">2088002007018916</param>
        </request>
        <response>
            <trade>
                <body>合同催款通知</body>
                <buyer_email>ltrade008@alitest.com</buyer_email>
                <buyer_id>2088102002723445</buyer_id>
                <discount>0.00</discount>
                <gmt_create>2010-07-30 12:26:33</gmt_create>
                <gmt_last_modified_time>2010-07-30 12:30:29
                </gmt_last_modified_time>
                <gmt_payment>2010-07-30 12:30:29</gmt_payment>
                <is_total_fee_adjust>F</is_total_fee_adjust>
                <out_trade_no>1280463992953</out_trade_no>
                <payment_type>1</payment_type>
                <price>1.00</price>
                <quantity>1</quantity>
                <seller_email>chao.chenc1@alipay.com</seller_email>
                <seller_id>2088002007018916</seller_id>
                <subject>合同催款通知</subject>
                <total_fee>1.00</total_fee>
                <trade_no>2010073000030344</trade_no>
                <trade_status>TRADE_FINISHED</trade_status>
                <use_coupon>F</use_coupon>
            </trade>
        </response>
        <sign>56ae9c3286886f76e57e0993625c71fe</sign>
        <sign_type>MD5</sign_type>
    </alipay>        

    访问接口并返回接口数据的函数CallWebApi:

        public string CallWebApi(string url)
        {
            string html = "";
            try
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.ContentType = "multipart/form-data";
                req.Accept = "*/*";
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
                req.Timeout = 30000;//30秒连接不成功就中断 
                req.Method = "GET";
    
                HttpWebResponse response = req.GetResponse() as HttpWebResponse;
                using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    html = sr.ReadToEnd();
                }
            }
            catch { }
            return html;
        }

    QueryOrder类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    
    namespace AlipayCheck
    {
        [DataContract]
        public class QueryOrder
        {
            /// <summary>
            /// 是否成功,T代表成功,F代表失败
            /// </summary>
            public string is_success { get; set; }
    
            /// <summary>
            /// 签名
            /// </summary>
            public string sign { get; set; }
    
            /// <summary>
            /// 签名方式只支持DSA、RSA、MD5
            /// </summary>
            public string sign_type { get; set; }
    
            /// <summary>
            /// 只有当查询交易失败后,才返回错误代码
            /// </summary>
            public string error { get; set; }
    
            /// <summary>
            /// 买家支付宝账号
            /// </summary>
            public string buyer_email { get; set; }
    
            /// <summary>
            /// 买家支付宝账号对应的支付宝唯一用户号
            /// </summary>
            public string buyer_id { get; set; }
    
            /// <summary>
            /// 交易状态
            /// </summary>
            public string trade_status { get; set; }
    
            /// <summary>
            /// 交易金额是否调整过
            /// </summary>
            public string is_total_fee_adjust { get; set; }
    
            /// <summary>
            /// 商户网站唯一订单号
            /// </summary>
            public string out_trade_no { get; set; }
    
            /// <summary>
            /// 支付宝交易号
            /// </summary>
            public string trade_no { get; set; }
    
            /// <summary>
            /// 商品名称
            /// </summary>
            public string subject { get; set; }
    
            /// <summary>
            /// 交易冻结状态
            /// </summary>
            public string flag_trade_locked { get; set; }
    
            /// <summary>
            /// 商品描述
            /// </summary>
            public string body { get; set; }
    
            /// <summary>
            /// 交易创建时间
            /// </summary>
            public string gmt_create { get; set; }
    
            /// <summary>
            /// 卖家的支付宝账号
            /// </summary>
            public string seller_email { get; set; }
    
            /// <summary>
            /// 卖家支付宝账号对应的支付宝唯一用户号
            /// </summary>
            public string seller_id { get; set; }
    
            /// <summary>
            /// 交易总金额
            /// </summary>
            public string total_fee { get; set; }
    
            /// <summary>
            /// 商品单价
            /// </summary>
            public string price { get; set; }
    
            /// <summary>
            /// 购买数量
            /// </summary>
            public string quantity { get; set; }
    
            /// <summary>
            /// 邮费
            /// </summary>
            public string logistics_fee { get; set; }
    
            /// <summary>
            /// 红包折扣
            /// </summary>
            public string coupon_discount { get; set; }
    
            /// <summary>
            /// 是否使用过红包
            /// </summary>
            public string use_coupon { get; set; }
    
            /// <summary>
            /// 折扣
            /// </summary>
            public string discount { get; set; }
    
            /// <summary>
            /// 退款状态
            /// </summary>
            public string refund_status { get; set; }
    
            /// <summary>
            /// 物流状态
            /// </summary>
            public string logistics_status { get; set; }
    
            /// <summary>
            /// 交易附加状态
            /// </summary>
            public string additional_trade_status { get; set; }
    
            /// <summary>
            /// 交易最近一次修改时间
            /// </summary>
            public string gmt_last_modified_time { get; set; }
    
            /// <summary>
            /// 付款时间
            /// </summary>
            public string gmt_payment { get; set; }
    
            /// <summary>
            /// 卖家发货时间
            /// </summary>
            public string gmt_send_goods { get; set; }
    
            /// <summary>
            /// 退款时间
            /// </summary>
            public string gmt_refund { get; set; }
    
            /// <summary>
            /// 主超时时间
            /// </summary>
            public string time_out { get; set; }
    
            /// <summary>
            /// 交易关闭时间,只有为TRADE_CLOSE状态的交易才存在此时间
            /// </summary>
            public string gmt_close { get; set; }
    
            /// <summary>
            /// 物流状态更新时间
            /// </summary>
            public string gmt_logistics_modify { get; set; }
    
            /// <summary>
            /// 主超时间类型
            /// </summary>
            public string time_out_type { get; set; }
    
            /// <summary>
            /// 退款金额
            /// </summary>
            public string refund_fee { get; set; }
    
            /// <summary>
            /// 退款流程
            /// </summary>
            public string refund_flow_type { get; set; }
    
            /// <summary>
            /// 退款唯一标识号
            /// </summary>
            public string refund_id { get; set; }
    
            /// <summary>
            /// 退现金金额
            /// </summary>
            public string refund_cash_fee { get; set; }
    
            /// <summary>
            /// 退红包金额
            /// </summary>
            public string refund_coupon_fee { get; set; }
    
            /// <summary>
            /// 退积分金额
            /// </summary>
            public string refund_agent_pay_fee { get; set; }
    
            /// <summary>
            /// 使用红包的金额
            /// </summary>
            public string coupon_used_fee { get; set; }
    
            /// <summary>
            /// 累计的已经退款金额
            /// </summary>
            public string to_buyer_fee { get; set; }
    
            /// <summary>
            /// 累计的已打款给买家的金额
            /// </summary>
            public string to_seller_fee { get; set; }
    
            /// <summary>
            /// 资金单据列表,xml格式,由一个或多个FundBillDetail标签组成。
            /// </summary>
            public string fund_bill_list { get; set; }
    
            /// <summary>
            /// 收款类型
            /// </summary>
            public string payment_type { get; set; }
    
            /// <summary>
            /// 交易的创建人角色
            /// </summary>
            public string operator_role { get; set; }
    
            /// <summary>
            /// 接口类型
            /// </summary>
            public string service { get; set; }
        }
    }
    View Code

    获取xml数据塞入queryorder类:

            private static QueryOrder InsQueryOrder(string requestData)
            {
                QueryOrder queryorder = new QueryOrder();
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(requestData);
    
                //获取节点列表  
                queryorder.service = "single_trade_query";
    
                XmlNodeList topM = xmldoc.SelectNodes("alipay");
                foreach (XmlElement element in topM)
                {
                    queryorder.is_success = element.GetElementsByTagName("is_success")[0].InnerText;
                    if (queryorder.is_success == "F") return queryorder;
                    queryorder.sign = element.GetElementsByTagName("sign")[0].InnerText;
                    queryorder.sign_type = element.GetElementsByTagName("sign_type")[0].InnerText;
                }
    
                topM = xmldoc.SelectNodes("alipay/response/trade");
                foreach (XmlElement element in topM)
                {
                    queryorder.buyer_email = element.GetElementsByTagName("buyer_email")[0].InnerText;
                    queryorder.buyer_id = element.GetElementsByTagName("buyer_id")[0].InnerText;
                    queryorder.discount = element.GetElementsByTagName("discount")[0].InnerText;
                    queryorder.flag_trade_locked = element.GetElementsByTagName("flag_trade_locked")[0].InnerText;
                    queryorder.gmt_create = element.GetElementsByTagName("gmt_create")[0].InnerText;
                    queryorder.gmt_last_modified_time = element.GetElementsByTagName("gmt_last_modified_time")[0].InnerText;
                    queryorder.gmt_payment = element.GetElementsByTagName("gmt_payment")[0].InnerText;
                    queryorder.is_total_fee_adjust = element.GetElementsByTagName("is_total_fee_adjust")[0].InnerText;
                    queryorder.operator_role = element.GetElementsByTagName("operator_role")[0].InnerText;
                    queryorder.out_trade_no = element.GetElementsByTagName("out_trade_no")[0].InnerText;
                    queryorder.payment_type = element.GetElementsByTagName("payment_type")[0].InnerText;
                    queryorder.price = element.GetElementsByTagName("price")[0].InnerText;
                    queryorder.quantity = element.GetElementsByTagName("quantity")[0].InnerText;
                    queryorder.seller_email = element.GetElementsByTagName("seller_email")[0].InnerText;
                    queryorder.seller_id = element.GetElementsByTagName("seller_id")[0].InnerText;
                    queryorder.subject = element.GetElementsByTagName("subject")[0].InnerText;
                    queryorder.time_out = element.GetElementsByTagName("time_out")[0].InnerText;
                    queryorder.time_out_type = element.GetElementsByTagName("time_out_type")[0].InnerText;
                    queryorder.to_buyer_fee = element.GetElementsByTagName("to_buyer_fee")[0].InnerText;
                    queryorder.to_seller_fee = element.GetElementsByTagName("to_seller_fee")[0].InnerText;
                    queryorder.total_fee = element.GetElementsByTagName("total_fee")[0].InnerText;
                    queryorder.trade_no = element.GetElementsByTagName("trade_no")[0].InnerText;
                    queryorder.trade_status = element.GetElementsByTagName("trade_status")[0].InnerText;
                    queryorder.use_coupon = element.GetElementsByTagName("use_coupon")[0].InnerText;
                } 
                return queryorder;
            }
        }

    然后可以根据订单状态写自己的业务逻辑了:

                        if (queryorder.is_success == "T")
                        {
                            if (queryorder.trade_status == "TRADE_SUCCESS")
                            {
                                WriteReturnZFBDetail(queryorder);
                                FilliedOnline(queryorder.out_trade_no);
                            }
                        }
  • 相关阅读:
    软件设计项目进展01 2019/8/19
    mysql中使用utf-8乱码
    mysql学习
    eclipse中怎么查看jsp生成的java代码
    tomcat配置出现问题
    关掉win10下面的ctrl+alt+up/dowm
    java程序运行时内存分析
    java内部类
    Struts2 ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging
    win10下 Edge和IE浏览器都不能上网,而其他浏览器可以。怎么办?
  • 原文地址:https://www.cnblogs.com/vinsonLu/p/5405192.html
Copyright © 2020-2023  润新知