var System = require("../../system")
const uuidv4 = require('uuid/v4');
const crypto = require('crypto');
const cryptoJS = require("crypto-js");
const querystring = require('querystring');
const logCtl = System.getObject("web.oplogCtl");
var settings = require("../../../config/settings");
var moment = require('moment');

const xml2js = require('xml2js');
const md5 = require("MD5");
class ZxApiTest {
  constructor() {
    this.restClient = System.getObject("util.restClient");
    this.utilszxbankSve = System.getObject("service.utilszxbankSve");
    this.utilstlbankSve = System.getObject("service.utilstlbankSve");
  }

  async resultSign() {
    var jsonObj = {
      "appid": "a20180529001249122",
      "charset": "UTF-8",
      "code_img_url": "http://qr.tnbpay.com/mQW1U",
      "code_url": "weixin://wxpay/bizpayurl?pr=6xFf17u",
      "mch_id": "m20180529001249122",
      "nonce_str": "",//为空不参与签名
      "result_code": "SUCCESS",
      "return_code": "SUCCESS",
      "return_msg": "通讯成功",
      "sign": "99E23AB27F906177F2C9F1B129FE02F8",
      "sign_type": "MD5",
      "version": "2.0.1"
    };

    var signArr = [];
    signArr.push("appid=" + jsonObj.appid);
    signArr.push("charset=" + jsonObj.charset);
    signArr.push("code_img_url=" + jsonObj.code_img_url);
    signArr.push("code_url=" + jsonObj.code_url);//.....................
    signArr.push("mch_id=" + jsonObj.mch_id);
    // signArr.push("nonce_str=" + jsonObj.nonce_str);//????????????????????????????????????
    signArr.push("result_code=" + jsonObj.result_code);//................................
    signArr.push("return_code=" + jsonObj.return_code);
    signArr.push("return_msg=" + jsonObj.return_msg);//................................
    signArr.push("sign_type=" + jsonObj.sign_type);
    signArr.push("version=" + jsonObj.version);//..........................
    signArr.push("key=a7cbd88cd0c4b192b218fdef19ebdba6");
    var signStr = signArr.join("&");
    console.log(signStr, "，signStr.......................");
    var tmpSign = md5(signStr).toUpperCase();
    console.log(tmpSign, "tmpSign....................................");
    // jsonObj.sign = tmpSign;
  }

  convertDate(time) {
    if (time == null) {
      return "";
    }
    var date = new Date(Number(time * 1000));
    var y = 1900 + date.getYear();
    var m = "0" + (date.getMonth() + 1);
    var d = "0" + date.getDate();
    return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
  }
  async wxgateway() {
    try {

      var jsonObj = {
        appid: "a20180529001249122",
        body: "商标担保注册",
        mch_id: "m20180529001249122",
        method: "mbupay.wxpay.native",
        nonce_str: "testsy01",
        notify_url: "http://igirl.gongsibao.com/api/xbg/transferNotify",
        out_trade_no: "ceshisy006",
        product_id: "1001",
        spbill_create_ip: "127.0.0.1",
        // time_expire:,
        // time_start:,
        total_fee: 1,//单位为分
        version: "2.0.1",
        wx_appid: ""
      };
      var signArr = [];
      signArr.push("appid=" + jsonObj.appid);
      signArr.push("body=" + jsonObj.body);
      signArr.push("mch_id=" + jsonObj.mch_id);
      signArr.push("method=" + jsonObj.method);//.....................
      signArr.push("nonce_str=" + jsonObj.nonce_str);
      signArr.push("notify_url=" + jsonObj.notify_url);//................................
      signArr.push("out_trade_no=" + jsonObj.out_trade_no);
      signArr.push("product_id=" + jsonObj.product_id);//................................
      signArr.push("spbill_create_ip=" + jsonObj.spbill_create_ip);
      signArr.push("total_fee=" + jsonObj.total_fee);
      signArr.push("version=" + jsonObj.version);//..........................
      signArr.push("key=a7cbd88cd0c4b192b218fdef19ebdba6");
      var signStr = signArr.join("&");
      console.log(signStr, "，signStr.......................");
      var tmpSign = md5(signStr).toUpperCase();
      console.log(tmpSign, "tmpSign....................................");
      jsonObj.sign = tmpSign;

      var jsonBuilder = new xml2js.Builder({
        // rootName: '',
        xmldec: {
          version: '1.0',
          'encoding': 'UTF-8',
          'standalone': false
        }
      }); // jons -> xml
      var json2xml = jsonBuilder.buildObject(jsonObj);
      console.log('json解析成xml:' + json2xml);

      var reqUrl = "https://api.citicbankpay.com/pay/gateway";
      var rtn = await this.restClient.execPostXml(json2xml, reqUrl);
      console.log(rtn, "rtn.........................................!!!!!!!!!")
      var rtnResult = { code: 1, msg: "ok", data: {} };
      if (rtn && rtn.stdout) {
        var xmlParser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true }); // xml -> json
        xmlParser.parseString(rtn.stdout, function (err, result) {
          console.log('xml解析成json:' + JSON.stringify(result));
          rtnResult.data = JSON.stringify(result);
        });
      } else {
        rtnResult.code = 0;
        rtnResult.msg = '结算接口返回空';
      }
      return rtnResult;
    } catch (e) {
      console.log(e.stack, "error.................................?????")
    }

  }

  async aligateway() {
    try {

      var jsonObj = {
        appid: "a20180529001249122",
        body: "商标担保注册-支",
        mch_id: "m20180529001249122",
        method: "mbupay.alipay.native",
        nonce_str: "testsy01",
        notify_url: "http://igirl.gongsibao.com/api/xbg/transferNotify",
        out_trade_no: "ceshisy006",
        product_id: "1001",
        spbill_create_ip: "127.0.0.1",
        // time_expire:,
        // time_start:,
        total_fee: 1,//单位为分
        version: "2.0.1",
        wx_appid: ""
      };
      var signArr = [];
      signArr.push("appid=" + jsonObj.appid);
      signArr.push("body=" + jsonObj.body);
      signArr.push("mch_id=" + jsonObj.mch_id);
      signArr.push("method=" + jsonObj.method);//.....................
      signArr.push("nonce_str=" + jsonObj.nonce_str);
      signArr.push("notify_url=" + jsonObj.notify_url);//................................
      signArr.push("out_trade_no=" + jsonObj.out_trade_no);
      signArr.push("product_id=" + jsonObj.product_id);//................................
      signArr.push("spbill_create_ip=" + jsonObj.spbill_create_ip);
      signArr.push("total_fee=" + jsonObj.total_fee);
      signArr.push("version=" + jsonObj.version);//..........................
      signArr.push("key=a7cbd88cd0c4b192b218fdef19ebdba6");
      var signStr = signArr.join("&");
      console.log(signStr, "，signStr.......................");
      var tmpSign = md5(signStr).toUpperCase();
      console.log(tmpSign, "tmpSign....................................");
      jsonObj.sign = tmpSign;

      var jsonBuilder = new xml2js.Builder({
        // rootName: '',
        xmldec: {
          version: '1.0',
          'encoding': 'UTF-8',
          'standalone': false
        }
      }); // jons -> xml
      var json2xml = jsonBuilder.buildObject(jsonObj);
      console.log('json解析成xml:' + json2xml);

      var reqUrl = "https://api.citicbankpay.com/pay/gateway";
      var rtn = await this.restClient.execPostXml(json2xml, reqUrl);
      console.log(rtn, "rtn.........................................!!!!!!!!!")
      var rtnResult = { code: 1, msg: "ok", data: {} };
      if (rtn && rtn.stdout) {
        var xmlParser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true }); // xml -> json
        xmlParser.parseString(rtn.stdout, function (err, result) {
          console.log('xml解析成json:' + JSON.stringify(result));
          rtnResult.data = JSON.stringify(result);
        });
      } else {
        rtnResult.code = 0;
        rtnResult.msg = '结算接口返回空';
      }
      return rtnResult;

    } catch (e) {
      console.log(e.stack, "error.................................?????")
    }

  }

  async queryOrder(obj) {
    // {
    //   "company_id":4,
    //   "orderno":"ceshi06",
    //   "type":1
    // }

    // var result = await this.utilszxbankSve.queryOrder(4, "ceshisy006", 1);
    //中信支付
    // var result = await this.utilszxbankSve.queryOrder(obj.company_id, obj.orderno, obj.type);
    //通联支付
    var result = await this.utilstlbankSve.queryOrder(obj.company_id, obj.orderno);
    console.log(result, "result............................");
    return result;
  }
  async createOrder(obj) {
    // {
    //   "company_id": 4, 
    //   "order_num": "ceshisy005", 
    //   "total_fee": 1, 
    //   "product_id": "1001", 
    //   "body_desc": "商标担保注册", 
    //   "create_ip": "127.0.0.1",
    //   "type":1
    // }
    //中信支付
    // var result = await this.utilszxbankSve.getQrCode(obj.company_id, obj.order_num, obj.total_fee, obj.product_id, obj.body_desc, obj.create_ip, obj.type);
    //通联支付
    var result = await this.utilstlbankSve.getQrCode(obj.company_id, obj.order_num, obj.total_fee, obj.product_id, obj.body_desc, obj.create_ip, obj.type);
    console.log(result, "result............................");
    return result;
  }
  async callBackOrder(obj) {
    // {
    //   "buyer_logon_id":"306***@qq.com",
    //   "method":"mbupay.alipay.native",
    //   "openid":"2088302381536494",
    //   "out_trade_no":"ALI201906251050DVMVs",
    //   "pass_trade_no":"2000120190625207469817",
    //   "total_fee":"10",
    //   "trade_state":"SUCCESS",
    //   "transaction_id":"201906252200143649"
    //   }
    //中信支付
    // var result = await this.utilszxbankSve.opBackNotify(obj, "测试回调");

      // {
      //   "trxcode":"VSP501",
      //   "trxid":"111994120000846666",
      //   "out_trade_no":"ALI201906251050DVMVs",
      //   "trxamt":"10",
      //   "chnltrxid":"2000120190625207469817"
      // }
    //通联支付
    var result = await this.utilstlbankSve.opBackNotify(obj, "测试回调");
    console.log(result, "result............................");
    return result;
  }
}
module.exports = ZxApiTest;
