using Org.BouncyCastle.Ocsp;
using System;
using System.Collections.Generic;
using System.Text;

namespace PaymentSDK
{
    public class WeiPayHelper
    {

        public WeixinPayRespose UnifiedOrder(OrderQuery query)
        {
            var resp = new WeixinPayRespose();
            try
            {
                Random rd = new Random();
                //外部商户订单号
                var payNum = DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(0, 1000).ToString().PadLeft(3, '0');
                var data = new WxPayData();
                data.SetValue("body", query.body);
                data.SetValue("out_trade_no", payNum);
                data.SetValue("detail", query.detail);
                data.SetValue("total_fee", Convert.ToInt32(query.total_fee * 100));
                data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
                data.SetValue("notify_url", WeiXinPayConfig.NotifyUrl);

                data.SetValue("trade_type", "APP");
                //data.SetValue("openid", query.openid);
                //可以将用户Id和订单Id同时封装在attach中
                data.SetValue("attach", string.Format("{0}-{1}", query.OrderId, payNum));
                WxPayData result = WxPayData.UnifiedOrder(data);
                if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
                {
                    resp.err_code_des = result.GetValue("err_code_des").ToString();
                    resp.err_code = result.GetValue("err_code").ToString();
                    resp.result_code = result.GetValue("result_code").ToString();
                    return resp;
                }
                else
                {
                    resp.nonce_str = result.GetValue("nonce_str").ToString();
                    resp.appid = result.GetValue("appid").ToString();
                    resp.mchi_id = result.GetValue("mch_id").ToString();
                    resp.result_code = result.GetValue("result_code").ToString();
                    resp.prepay_id = "prepay_id=" + result.GetValue("prepay_id").ToString();

                    resp.trade_type = result.GetValue("trade_type").ToString();
                    var signType = "MD5";
                    var timeStamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).Ticks) / 10000).ToString();
                    WxPayData applet = new WxPayData();
                    applet.SetValue("appId", resp.appid);
                    applet.SetValue("nonceStr", resp.nonce_str);
                    applet.SetValue("package", resp.prepay_id);
                    applet.SetValue("signType", signType);
                    applet.SetValue("timeStamp", timeStamp);
                    resp.sign = applet.MakeSign();
                    resp.timeStamp = timeStamp;

                    //订单号
                    //applet.SetValue("orderid", query.OrderId);
                    resp.orderid = query.OrderId;
                }
            }
            catch (Exception ex)
            {
            }
            return resp;
        }

    }
}
