Exemplo n.º 1
0
            /// <summary>
            /// 统一下单,请求微信后会返回一个结果,此类用来根据响应流构建响应消息对象
            /// 内部会处理sign校验
            /// </summary>
            /// <param name="stream"></param>
            /// <param name="cancellation"></param>
            /// <returns></returns>
            public async Task <WeChatPaymentUnifyOrderResult> LoadAsync(Stream stream, CancellationToken cancellation = default)
            {
                var xd = await XDocument.LoadAsync(stream, LoadOptions.None, cancellation);

                var c = xd.Root;

                var p = new WeChatPaymentUnifyOrderResult(this.securet);

                p.return_code = Enum.Parse <return_code>((string)c.Element("return_code"));
                p.return_msg  = (string)c.Element("return_msg");
                if (p.return_code != return_code.SUCCESS)
                {
                    return(p);
                }

                p.appid        = (string)c.Element("appid");
                p.mch_id       = (string)c.Element("mch_id");
                p.device_info  = (string)c.Element("device_info");
                p.nonce_str    = (string)c.Element("nonce_str");
                p.sign         = (string)c.Element("sign");
                p.result_code  = Enum.Parse <result_code>((string)c.Element("result_code"));
                p.err_code     = Enum.Parse <err_code>((string)c.Element("err_code"));
                p.err_code_des = (string)c.Element("err_code_des");
                if (p.result_code != result_code.SUCCESS)
                {
                    return(p);
                }

                p.trade_type = Enum.Parse <trade_type>((string)c.Element("trade_type"));
                p.prepay_id  = (string)c.Element("prepay_id");
                p.code_url   = (string)c.Element("code_url");


                if (!securet.CheckSign(p))
                {
                    throw new InvalidCastException("微信小程序支付 > 统一下单 > 返回结果 > sign校验失败!");
                }

                return(p);
            }
            /// <summary>
            /// 微信小程序支付结果通知提交过来的Stream 可以通过此方法解析
            /// </summary>
            /// <param name="stream"></param>
            /// <param name="cancellation"></param>
            /// <returns></returns>
            public async Task <WeChatPaymentNoticeResult> LoadAsync(Stream stream, CancellationToken cancellation = default)
            {
                var xd = await XDocument.LoadAsync(stream, LoadOptions.None, cancellation);

                var c = xd.Root;

                var p = new WeChatPaymentNoticeResult();

                p.return_code = Enum.Parse <return_code>((string)c.Element("return_code"));
                p.return_msg  = (string)c.Element("return_msg");
                if (p.return_code != return_code.SUCCESS)
                {
                    return(p);
                }

                p.appid       = (string)c.Element("appid");
                p.mch_id      = (string)c.Element("mch_id");
                p.device_info = (string)c.Element("device_info");
                p.nonce_str   = (string)c.Element("nonce_str");
                p.sign        = (string)c.Element("sign");

                var sign_type = (string)c.Element("sign_type");

                if (!string.IsNullOrWhiteSpace(sign_type))
                {
                    p.sign_type = Enum.Parse <sign_type>(sign_type);
                }

                p.result_code  = Enum.Parse <result_code>((string)c.Element("result_code"));
                p.err_code     = (string)c.Element("err_code");
                p.err_code_des = (string)c.Element("err_code_des");
                if (p.result_code != result_code.SUCCESS)
                {
                    return(p);
                }

                p.openid       = (string)c.Element("openid");
                p.is_subscribe = (bool)c.Element("is_subscribe");
                p.trade_type   = Enum.Parse <trade_type>((string)c.Element("trade_type"));
                p.bank_type    = (string)c.Element("bank_type");
                p.total_fee    = (int)c.Element("total_fee") / 100m;
                var settlement_total_fee = (string)c.Element("settlement_total_fee");

                if (!string.IsNullOrWhiteSpace(settlement_total_fee))
                {
                    p.settlement_total_fee = Convert.ToDecimal(settlement_total_fee) / 100;
                }
                var fee_type = (string)c.Element("fee_type");

                if (!string.IsNullOrWhiteSpace(fee_type))
                {
                    p.fee_type = Enum.Parse <fee_type>((string)c.Element("fee_type"));
                }

                #region 现金
                var cash_fee = (string)c.Element("cash_fee");
                if (!string.IsNullOrWhiteSpace(cash_fee))
                {
                    p.cash_fee = Convert.ToDecimal(cash_fee) / 100;
                }

                var cash_fee_type = (string)c.Element("cash_fee_type");
                if (!string.IsNullOrWhiteSpace(cash_fee_type))
                {
                    p.cash_fee_type = Enum.Parse <fee_type>((string)c.Element("cash_fee_type"));
                }
                #endregion

                #region 代金券

                var coupon_fee = (string)c.Element("coupon_fee");
                if (!string.IsNullOrWhiteSpace(coupon_fee))
                {
                    p.coupon_fee = Convert.ToDecimal(coupon_fee) / 100;
                }


                var coupon_count = (string)c.Element("coupon_count");
                if (!string.IsNullOrWhiteSpace(coupon_count))
                {
                    p.coupon_count = Convert.ToInt32(coupon_count);
                }

                var coupon_type_0 = (string)c.Element("coupon_type_0");
                if (!string.IsNullOrWhiteSpace(coupon_type_0))
                {
                    p.coupon_type_0 = Enum.Parse <coupon_type>((string)c.Element("coupon_type_0"));
                    p.coupon_fee_0  = Convert.ToDecimal(c.Element("coupon_fee_0")) / 100;
                }
                var coupon_type_1 = (string)c.Element("coupon_type_1");
                if (!string.IsNullOrWhiteSpace(coupon_type_1))
                {
                    p.coupon_type_1 = Enum.Parse <coupon_type>((string)c.Element("coupon_type_1"));
                    p.coupon_fee_1  = Convert.ToDecimal(c.Element("coupon_fee_1")) / 100;
                }
                var coupon_type_2 = (string)c.Element("coupon_type_2");
                if (!string.IsNullOrWhiteSpace(coupon_type_2))
                {
                    p.coupon_type_2 = Enum.Parse <coupon_type>((string)c.Element("coupon_type_2"));
                    p.coupon_fee_2  = Convert.ToDecimal(c.Element("coupon_fee_2")) / 100;
                }
                var coupon_type_3 = (string)c.Element("coupon_type_3");
                if (!string.IsNullOrWhiteSpace(coupon_type_3))
                {
                    p.coupon_type_3 = Enum.Parse <coupon_type>((string)c.Element("coupon_type_3"));
                    p.coupon_fee_3  = Convert.ToDecimal(c.Element("coupon_fee_3")) / 100;
                }
                var coupon_type_4 = (string)c.Element("coupon_type_4");
                if (!string.IsNullOrWhiteSpace(coupon_type_4))
                {
                    p.coupon_type_4 = Enum.Parse <coupon_type>((string)c.Element("coupon_type_4"));
                    p.coupon_fee_4  = Convert.ToDecimal(c.Element("coupon_fee_4")) / 100;
                }
                p.coupon_id_0 = (string)c.Element("coupon_id_0");
                p.coupon_id_1 = (string)c.Element("coupon_id_1");
                p.coupon_id_2 = (string)c.Element("coupon_id_2");
                p.coupon_id_3 = (string)c.Element("coupon_id_3");
                p.coupon_id_4 = (string)c.Element("coupon_id_4");
                #endregion

                p.transaction_id = (string)c.Element("transaction_id");
                p.out_trade_no   = (string)c.Element("out_trade_no");
                p.attach         = (string)c.Element("attach");
                p.time_end       = DateTimeOffset.Parse((string)c.Element("time_end"));

                if (!securet.CheckSign(p))
                {
                    throw new InvalidCastException("微信小程序支付 > 支付结果通知 > 微信提交过来的数据 > sign校验失败!");
                }

                return(p);
            }