Exemplo n.º 1
0
        public static BaseResponse ParsePaymentNotify(string xml)
        {
            logger.Info("ParsePaymentNotify.............");
            PaymentNotifyResponse res = null;
            XmlDocument           doc = null;

            try
            {
                if (!string.IsNullOrEmpty(xml))
                {
                    doc = new XmlDocument();
                    doc.LoadXml(xml);
                    res = new PaymentNotifyResponse();
                    XmlNode return_code = doc.SelectSingleNode("/xml/return_code");
                    if (return_code != null)
                    {
                        logger.Info(return_code.InnerText);
                        res.return_code = ResponseHelper.ParseResultState(return_code.InnerText);
                    }
                    XmlNode return_msg = doc.SelectSingleNode("/xml/return_msg");
                    if (return_msg != null)
                    {
                        res.return_msg = return_msg.InnerText.Trim();
                    }

                    if (res.return_code == ResultState.FAIL)
                    {
                        return(res);
                    }
                    XmlNode sign = doc.SelectSingleNode("/xml/sign");
                    if (sign != null)
                    {
                        res.sign = sign.InnerText.Trim();
                    }
                    XmlNode transaction_id = doc.SelectSingleNode("/xml/transaction_id");
                    if (transaction_id != null)
                    {
                        res.transaction_id = transaction_id.InnerText.Trim();
                    }
                    XmlNode out_trade_no = doc.SelectSingleNode("/xml/out_trade_no");
                    if (out_trade_no != null)
                    {
                        res.out_trade_no = out_trade_no.InnerText.Trim();
                    }
                    XmlNode result_code = doc.SelectSingleNode("/xml/result_code");
                    if (result_code != null)
                    {
                        res.result_code = result_code.InnerText.Trim();
                    }

                    XmlNode appid = doc.SelectSingleNode("/xml/appid");
                    if (appid != null)
                    {
                        logger.Info(appid.InnerText);
                        res.appid = appid.InnerText.Trim();
                    }

                    XmlNode mch_id = doc.SelectSingleNode("/xml/mch_id");
                    if (mch_id != null)
                    {
                        res.mch_id = mch_id.InnerText.Trim();
                    }

                    XmlNode sub_mch_id = doc.SelectSingleNode("/xml/sub_mch_id");
                    if (sub_mch_id != null)
                    {
                        res.sub_mch_id = sub_mch_id.InnerText.Trim();
                    }

                    XmlNode is_subscribe = doc.SelectSingleNode("/xml/is_subscribe");
                    if (is_subscribe != null)
                    {
                        res.is_subscribe = is_subscribe.InnerText.Trim();
                    }

                    XmlNode nonce_str = doc.SelectSingleNode("/xml/nonce_str");
                    if (nonce_str != null)
                    {
                        res.nonce_str = nonce_str.InnerText.Trim();
                    }

                    XmlNode openid = doc.SelectSingleNode("/xml/openid");
                    if (openid != null)
                    {
                        res.openid = openid.InnerText.Trim();
                    }

                    XmlNode err_code = doc.SelectSingleNode("/xml/err_code");
                    if (err_code != null)
                    {
                        res.err_code = err_code.InnerText.Trim();
                    }
                    XmlNode err_code_des = doc.SelectSingleNode("/xml/err_code_des");
                    if (err_code_des != null)
                    {
                        res.err_code_des = err_code_des.InnerText.Trim();
                    }

                    XmlNode trade_type = doc.SelectSingleNode("/xml/trade_type");
                    if (trade_type != null)
                    {
                        res.trade_type = ResponseHelper.ParseTradeType(trade_type.InnerText);
                    }

                    XmlNode bank_type = doc.SelectSingleNode("/xml/bank_type");
                    if (bank_type != null)
                    {
                        res.bank_type = bank_type.InnerText.Trim();
                    }

                    XmlNode total_fee = doc.SelectSingleNode("/xml/total_fee");
                    if (total_fee != null)
                    {
                        res.total_fee = int.Parse(total_fee.InnerText.Trim());
                    }

                    XmlNode cash_fee = doc.SelectSingleNode("/xml/cash_fee");
                    if (cash_fee != null)
                    {
                        res.cash_fee = int.Parse(cash_fee.InnerText.Trim());
                    }

                    XmlNode fee_type = doc.SelectSingleNode("/xml/fee_type");
                    if (total_fee != null)
                    {
                        res.fee_type = fee_type.InnerText.Trim();
                    }

                    XmlNode attach = doc.SelectSingleNode("/xml/attach");
                    if (attach != null)
                    {
                        res.attach = attach.InnerText.Trim();
                    }
                    XmlNode time_end = doc.SelectSingleNode("/xml/time_end");
                    if (time_end != null)
                    {
                        res.time_end = time_end.InnerText.Trim();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            logger.Info("Done ParsePaymentNotify.............");
            return(res);
        }
Exemplo n.º 2
0
        public HttpResponseMessage WeChatPayBack()
        {
            var    resp      = new HttpResponseMessage(HttpStatusCode.OK);
            string returnXML = null;

            logger.Info("PaymentController.WeChatPayBack is being called by Wechat payment notify service.........................");
            Stream stream = Request.InputStream;

            if (stream != null)
            {
                StreamReader rs = null;
                try
                {
                    rs = new StreamReader(stream);
                    string result = rs.ReadToEnd();
                    if (!string.IsNullOrEmpty(result))
                    {
                        logger.Info("Below is the data posted by wechat payment service");
                        logger.Info(result);
                        string paraValues = WeChatPaymentWrapper.ParsePaymentNotifySignParas(result);
                        logger.Info(string.Format("{0} needs to be signed", paraValues));
                        BaseResponse          baseresponse = WeChatPaymentWrapper.ParsePaymentNotify(result);
                        PaymentNotifyResponse response     = null;
                        if (baseresponse != null)
                        {
                            response = (PaymentNotifyResponse)baseresponse;
                        }
                        logger.Info(string.Format("Signature sent by wechat is {0}", response.sign));
                        WeChatPayConfig config = PersistentValueManager.config;
                        paraValues += "&key=" + config.ShopSecret;
                        string sign = UrlSignUtil.GetMD5(paraValues).ToUpper();
                        logger.Info(string.Format("Signature caculated by localsystem is {0}", sign));
                        if (sign != response.sign)
                        {
                            logger.Error("Two signatures are different, the request was not sent by wechat payment system.");
                            returnXML    = "<xml><return_code>FAIL</return_code><return_msg>签名不正确</return_msg></xml>";
                            resp.Content = new StringContent(returnXML, System.Text.Encoding.UTF8, "text/plain");
                        }
                        logger.Info("Sign verification passed");
                        OrderManagement   orderMgr = new OrderManagement(0);
                        PaymentManagement payMgr   = new PaymentManagement(0);
                        int paymentId = 0;
                        int.TryParse(response.out_trade_no, out paymentId);
                        if (paymentId > 0)
                        {
                            logger.Info("Going to process payment id" + paymentId);
                            ChargeResult cResult = null;
                            try
                            {
                                BPaymentHistory        payment  = null;
                                int                    total    = 0;
                                List <BPaymentHistory> payments = payMgr.FindUnProcessedOnLinePayments(paymentId, 0, 0, out total);
                                if (payments != null && payments.Count == 1)
                                {
                                    payment = payments[0];
                                    if (payment.PayType == 0)//直冲用户支付
                                    {
                                        logger.Info("OpenId:" + response.openid);
                                        logger.Info("OpenTradeNo:" + response.transaction_id);
                                        cResult = orderMgr.ProcessOrderAfterPaid(paymentId, response.transaction_id, response.openid);
                                        logger.Info(cResult.Status.ToString());
                                        logger.Info(cResult.Message);
                                        if (cResult.Status == ChargeStatus.SUCCEED)
                                        {
                                            logger.Info("The payment status has been successfully synced to local system.");
                                            returnXML    = "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>";
                                            resp.Content = new StringContent(returnXML, System.Text.Encoding.UTF8, "text/plain");
                                            return(resp);
                                        }
                                        else
                                        {
                                            logger.Error(cResult.Message);
                                            returnXML    = "<xml><return_code>FAIL</return_code><return_msg>unexpected error</return_msg></xml>";
                                            resp.Content = new StringContent(returnXML, System.Text.Encoding.UTF8, "text/plain");
                                            return(resp);
                                        }
                                    }
                                }
                                else
                                {
                                    logger.Warn("Didn't find payment by id:" + paymentId);
                                    returnXML    = "<xml><return_code>FAIL</return_code><return_msg>out_trade_no is wrong</return_msg></xml>";
                                    resp.Content = new StringContent(returnXML, System.Text.Encoding.UTF8, "text/plain");
                                }
                            }
                            catch (KMBitException e)
                            {
                                returnXML    = "<xml><return_code>FAIL</return_code><return_msg>unexpected error</return_msg></xml>";
                                resp.Content = new StringContent(returnXML, System.Text.Encoding.UTF8, "text/plain");
                                logger.Error(e);
                            }
                            catch (Exception ex)
                            {
                                returnXML    = "<xml><return_code>FAIL</return_code><return_msg>unexpected error</return_msg></xml>";
                                resp.Content = new StringContent(returnXML, System.Text.Encoding.UTF8, "text/plain");
                                logger.Fatal(ex);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
            }
            logger.Info("Done...................");
            return(resp);
        }