예제 #1
0
        public static async Task <string> PostRefundAsync(long orderid, decimal total, decimal cash)
        {
            string orderno = orderid.ToString();

            XElem xo = new XElem("xml");

            xo.AddChild("appid", appid);
            xo.AddChild("mch_id", mchid);
            xo.AddChild("nonce_str", noncestr);
            xo.AddChild("op_user_id", mchid);
            xo.AddChild("out_refund_no", orderno);
            xo.AddChild("out_trade_no", orderno);
            xo.AddChild("refund_fee", ((int)(cash * 100)).ToString());
            xo.AddChild("total_fee", ((int)(total * 100)).ToString());
            string sign = Sign(xo);

            xo.AddChild("sign", sign);

            XElem  xi          = (await WCPay.PostAsync <XElem>(null, "/secapi/pay/refund", xo.Dump())).B;
            string return_code = xi.Child(nameof(return_code));

            if (return_code != "SUCCESS")
            {
                string return_msg = xi.Child(nameof(return_msg));
                return(return_msg);
            }
            string result_code = xi.Child(nameof(result_code));

            if (result_code != "SUCCESS")
            {
                string err_code_des = xi.Child(nameof(err_code_des));
                return(err_code_des);
            }
            return(null);
        }
예제 #2
0
        public static async Task <decimal> PostOrderQueryAsync(long orderid)
        {
            XElem x = new XElem("xml");

            x.AddChild("appid", appid);
            x.AddChild("mch_id", mchid);
            x.AddChild("nonce_str", noncestr);
            x.AddChild("out_trade_no", orderid.ToString());
            string sign = Sign(x);

            x.AddChild("sign", sign);

            XElem xe = (await WCPay.PostAsync <XElem>(null, "/pay/orderquery", x.Dump())).B;

            sign = xe.Child(nameof(sign));
            xe.Sort();
            if (sign != Sign(xe, "sign"))
            {
                return(0);
            }

            string return_code = xe.Child(nameof(return_code));

            if (return_code != "SUCCESS")
            {
                return(0);
            }

            decimal cash_fee = xe.Child(nameof(cash_fee));

            return(cash_fee);
        }
예제 #3
0
        public static async Task <string> PostUnifiedOrderAsync(long orderid, decimal total, string openid, string ip, string notifyurl)
        {
            XElem x = new XElem("xml");

            x.AddChild("appid", appid);
            x.AddChild("body", BODY_DESC);
            x.AddChild("mch_id", mchid);
            x.AddChild("nonce_str", noncestr);
            x.AddChild("notify_url", notifyurl);
            x.AddChild("openid", openid);
            x.AddChild("out_trade_no", orderid.ToString());
            x.AddChild("spbill_create_ip", ip);
            x.AddChild("total_fee", ((int)(total * 100)).ToString());
            x.AddChild("trade_type", "JSAPI");
            string sign = Sign(x);

            x.AddChild("sign", sign);

            XElem  xe        = (await WCPay.PostAsync <XElem>(null, "/pay/unifiedorder", x.Dump())).B;
            string prepay_id = xe.Child(nameof(prepay_id));

            if (prepay_id == null)
            {
                prepay_id = xe.Child("err_code");
            }

            return(prepay_id);
        }
예제 #4
0
        static string Sign(XElem xe, string exclude = null)
        {
            StringBuilder sb = new StringBuilder(1024);

            for (int i = 0; i < xe.Count; i++)
            {
                XElem child = xe.Child(i);

                // not include the sign field
                if (exclude != null && child.Tag == exclude)
                {
                    continue;
                }

                if (sb.Length > 0)
                {
                    sb.Append('&');
                }
                sb.Append(child.Tag).Append('=').Append(child.Text);
            }

            sb.Append("&key=").Append(key);

            return(StrUtility.MD5(sb.ToString()));
        }
예제 #5
0
        public static async Task <string> PostRefundQueryAsync(long orderid)
        {
            XElem xo = new XElem("xml");

            xo.AddChild("appid", appid);
            xo.AddChild("mch_id", mchid);
            xo.AddChild("nonce_str", noncestr);
            xo.AddChild("out_trade_no", orderid.ToString());
            string sign = Sign(xo);

            xo.AddChild("sign", sign);

            XElem xi = (await WCPay.PostAsync <XElem>(null, "/pay/refundquery", xo.Dump())).B;

            sign = xi.Child(nameof(sign));
            xi.Sort();
            if (sign != Sign(xi, "sign"))
            {
                return("返回结果签名错误");
            }

            string return_code = xi.Child(nameof(return_code));

            if (return_code != "SUCCESS")
            {
                string return_msg = xi.Child(nameof(return_msg));
                return(return_msg);
            }

            string result_code = xi.Child(nameof(result_code));

            if (result_code != "SUCCESS")
            {
                return("退款订单查询失败");
            }

            string refund_status_0 = xi.Child(nameof(refund_status_0));

            if (refund_status_0 != "SUCCESS")
            {
                return(refund_status_0 == "PROCESSING" ? "退款处理中" : refund_status_0 == "REFUNDCLOSE" ? "退款关闭" : "退款异常");
            }

            return(null);
        }
예제 #6
0
        public static bool Notified(XElem xe, out long out_trade_no, out decimal cash)
        {
            cash         = 0;
            out_trade_no = 0;

            string appid     = xe.Child(nameof(appid));
            string mch_id    = xe.Child(nameof(mch_id));
            string nonce_str = xe.Child(nameof(nonce_str));

            if (appid != WeiXinUtility.appid || mch_id != mchid || nonce_str != noncestr)
            {
                return(false);
            }

            string result_code = xe.Child(nameof(result_code));

            if (result_code != "SUCCESS")
            {
                return(false);
            }

            string sign = xe.Child(nameof(sign));

            xe.Sort();
            if (sign != Sign(xe, "sign"))
            {
                return(false);
            }

            int cash_fee = xe.Child(nameof(cash_fee)); // in cent

            cash         = ((decimal)cash_fee) / 100;
            out_trade_no = xe.Child(nameof(out_trade_no)); // 商户订单号
            return(true);
        }
예제 #7
0
        public static async Task <string> PostTransferAsync(int id, string openid, string username, decimal cash, string desc)
        {
            XElem x = new XElem("xml");

            x.AddChild("amount", ((int)(cash * 100)).ToString());
            x.AddChild("check_name", "FORCE_CHECK");
            x.AddChild("desc", desc);
            x.AddChild("mch_appid", appid);
            x.AddChild("mchid", mchid);
            x.AddChild("nonce_str", noncestr);
            x.AddChild("openid", openid);
            x.AddChild("partner_trade_no", id.ToString());
            x.AddChild("re_user_name", username);
            x.AddChild("spbill_create_ip", spbillcreateip);

            string sign = Sign(x);

            x.AddChild("sign", sign);

            XElem  xe          = (await WCPay.PostAsync <XElem>(null, "/mmpaymkttransfers/promotion/transfers", x.Dump())).B;
            string return_code = xe.Child(nameof(return_code));

            if ("SUCCESS" == return_code)
            {
                string result_code = xe.Child(nameof(result_code));
                if ("SUCCESS" != result_code)
                {
                    string err_code_des = xe.Child(nameof(err_code_des));
                    return(err_code_des);
                }
            }
            else
            {
                string return_msg = xe.Child(nameof(return_msg));
                return(return_msg);
            }
            return(null);
        }