var system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctlms.base");

class PayCtl extends CtlBase {
  constructor() {
    super();
    this.QrCodeUrl = "http://igirl.gongsibao.com/api/tlapi/getQrCodeInfo";
    this.PayStatusUrl = "http://igirl.gongsibao.com/api/tlapi/getPayStatus";
    this.orderSve = system.getObject("service.order.orderSve");
  }

  /*
  请求参数
  {
    order_no:订单号,
    op_type:支付方式:["wx"，"alipay"],
    total_sum:订单金额
  }
  返回结果
  {
    成功返回:
    {"code":1,
      "msg":"SUCCESS",
      "data":
      {"payinfo":"https://syb.allinpay.com/apiweb/h5unionpay/native?key=qbWZUuwa4pu2J%2Bj38mkp8863",支付的URL
      "reqsn":"333_40_1",
      "chnltrxid":"121905790000021961",
      "trxid":"121905790000021961",
      "trxstatus":"0000",
      "wxPayOrderCode":"121905790000021961",微信码
      "aliPayOrderCode":"333",支付宝码}
    }
    失败返回:
    { code: 0, msg: '错误信息', data: {} }
  }
  */
  async getQrCodeInfo(pobj, pobj2, req) {
    if (!pobj.order_no || !pobj.op_type || !pobj.total_sum) {
      return system.getResult(null, "请求参数错误");
    }
    var parm = {};
    parm.orderNo = pobj.order_no;
    parm.company_id = 1;
    parm.other_company_id = 45;
    parm.op_type = pobj.op_type;
    parm.totalSum = pobj.total_sum;
    parm.sveItemCode = "product001";
    parm.sveItemName = "个体工商户注册";
    parm.create_ip = system.get_client_ip(req);
    //parm.create_ip = "127.0.0.1";
    parm.req_key = "sygyqbpogszc";

    console.log(parm.orderNo);
    var rc = system.getObject("util.restClient");
    var data = querystring.stringify(parm);
    var rtn = null;
    try {
      rtn = await rc.execPost2(data, this.QrCodeUrl);
      console.log(rtn, "-------------------请求支付二维码------------------");
      var result = JSON.parse(rtn.stdout);

      if (result.code == 1) {
        return system.getResultSuccess(result.data);
      } else {
        return system.getResult(null, "请求失败");
      }
      return rtn.stdout;
    } catch (e) {
      return system.getResultFail(500, "接口错误");
    }
  }

  /*
  请求参数
  {
    order_no:订单号,
    op_type:支付方式:["wx"，"alipay"],
    aliPay_order_code: 支付宝码,
    wx_pay_order_code:微信码
  }
  返回结果
  返回值：{code":1"msg":""} code为1则支付成功
  {"code":-330,"msg":"超时未支付，请刷新页面"}
  {"code":-130,"msg":"请及时支付"}
  { code: 0, msg: '错误信息', data: {} }
  */
  async getPayStatus(pobj, pobj2, req) {
    if (!pobj.op_type || !pobj.order_no) {
      return system.getResult(null, "请求参数错误");
    }
    var parm = {
      aliPayOrderCode: pobj.aliPay_order_code,
      company_id: 1,
      other_company_id: 45,
      op_type: pobj.op_type,
      orderNo: pobj.order_no,
      wxPayOrderCode: pobj.wx_pay_order_code,
      req_key: "sygyqbpogszc"
    };

    var rc = system.getObject("util.restClient");
    var data = querystring.stringify(parm);
    var rtn = null;
    try {
      rtn = await rc.execPost2(data, this.PayStatusUrl);
      var result = JSON.parse(rtn.stdout);
      return system.getResultSuccess(result);
    } catch (e) {
      return system.getResultFail(500, "接口错误");
    }
  }

  async getReCall(obj) {
    if (obj.trxstatus == "0000") {
      try {
        await this.orderSve.orderPay({
          id: obj.out_trade_no
        });
        return system.getResult("更改支付状态成功");
      } catch (e) {
        console.log(e);
        return system.getResult("更改支付状态失败");
      }

    }
  }
}
module.exports = PayCtl;

// var tesk = new PayCtl();
// var par = {
//   "trxcode": "VSP501",
//   "trxamt": "100",
//   "reqsn": "order000001_45_1",
//   "chnltrxid": "4200000410201910151445633333",
//   "trxid": "121905790000021960",
//   "trxstatus": "0000",
//   "fintime": "20191015225231",
//   "client_ip": "",
//   "out_trade_no": "order000001",
//   "company_id": "1",
//   "other_company_id": "45",
//   "req_accountType": "wx",
//   "req_key" :""
// };
// tesk.getReCall(par).then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });