package com.cnd3b.restcontroller.system.payment;

import com.cnd3b.utility.Encryption;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.utility.wechatpay.apppay.POJO.APPCreateOrderResponse;
import com.cnd3b.utility.wechatpay.apppay.apppay;
import com.cnd3b.utility.wechatpay.apppay.parameter;
import com.cnd3b.utility.wechatpay.jsapipay.POJO.JSCreateOrderResponse;
import com.cnd3b.utility.wechatpay.jsapipay.jsapipay;
import com.cnd3b.utility.wechatpay.nativepay.nativepay;

import java.util.Calendar;

public class payment extends Controller {
    public payment(JSONObject content) {
        super(content);
    }

    /**
     * 创建微信支付订单
     *
     * @return
     */
    public String createAPPWechatOrder() {
        String fappmodel = "";
        if (content.containsKey("fappmodel") && !"".equals(content.getString("fappmodel"))) {
            fappmodel = content.getString("fappmodel");
        }
        int famount = content.getIntValue("famount");
        String custip = content.getString("custip");

        apppay apppay = new apppay();
        APPCreateOrderResponse response = null;
        if (fappmodel.equals("")) {
            /**
             * 账户充值
             */
            response = apppay.createOrder(siteid, userid, famount, custip);
        } else {
            /**
             * 系统授权?
             */
            response = apppay.createOrder(siteid, userid, fappmodel, famount, custip);
        }

        if ("SUCCESS".equals(response.getReturn_code()) && "SUCCESS".equals(response.getResult_code())) {
            String appid = response.getAppid();//应用ID
            String noncestr = createNonce_str(appid);//随机码
            String package_str = "Sign=WXPay";
            String partnerid = response.getMch_id();//商户号
            String prepayid = response.getPrepay_id();//预支付交易会话ID
            String timestamp = String.valueOf(Calendar.getInstance().getTime().getTime() / 1000);

            JSONObject object = new JSONObject();
            object.put("appid", appid);
            object.put("noncestr", noncestr);
            object.put("package", package_str);
            object.put("partnerid", partnerid);
            object.put("prepayid", prepayid);
            object.put("timestamp", timestamp);
            String A = "appid=" + appid + "&noncestr=" + noncestr + "&package=" + package_str + "&partnerid=" + partnerid + "&prepayid=" + prepayid + "&timestamp=" + timestamp;
            String stringSignTemp = A + "&key=" + parameter.key;
            object.put("sign", new Encryption().Encode_MD5(stringSignTemp).toUpperCase());

            return getSucReturnObject().setData(object).toString();
        } else {
            return getErrReturnObject().setErrMsg(response.getErr_code_des()).toString();
        }
    }

    /**
     * 创建微信小程序支付订单
     *
     * @return
     */
    public String createWechatAPPOrder() {

        int famount = content.getIntValue("famount");
        String custip = content.getString("custip");
        String wechat_code = content.getString("wechat_code");

        jsapipay apppay = new jsapipay();
        JSCreateOrderResponse
                response = apppay.createOrder(siteid, userid, famount, custip, wechat_code);

        if ("SUCCESS".equals(response.getReturn_code()) && "SUCCESS".equals(response.getResult_code())) {
            String appid = response.getAppid();//应用ID
            String nonceStr = createNonce_str(appid);//随机码
            String package_str = "prepay_id=" + response.getPrepay_id();
            String timeStamp = String.valueOf(Calendar.getInstance().getTime().getTime() / 1000);
            String signType = "MD5";

            JSONObject object = new JSONObject();
            object.put("appId", appid);
            object.put("nonceStr", nonceStr);
            object.put("package", package_str);
            object.put("timeStamp", timeStamp);
            object.put("signType", signType);
            String A = "appId=" + appid + "&nonceStr=" + nonceStr + "&package=" + package_str + "&signType=" + signType + "&timeStamp=" + timeStamp;
            String stringSignTemp = A + "&key=" + parameter.key;
            object.put("paySign", new Encryption().Encode_MD5(stringSignTemp).toUpperCase());
            return getSucReturnObject().setData(object).toString();
        } else {
            return getErrReturnObject().setErrMsg(response.getErr_code_des()).toString();
        }
    }

    /**
     * 创建微信扫码支付订单
     *
     * @return
     */
    public String createNativeWechatOrder() {
        String fappmodel = "";
        if (content.containsKey("fappmodel") && !"".equals(content.getString("fappmodel"))) {
            fappmodel = content.getString("fappmodel");
        }
        int famount = content.getIntValue("famount");

        nativepay nativepay = new nativepay();
        try {
            nativepay.setup();
            String codeurl = "";
            if (fappmodel.equals("")) {
                codeurl = nativepay.createOrder(siteid, userid, famount);
            } else {
                codeurl = nativepay.createOrder(siteid, userid, fappmodel, famount);
            }
            nativepay.after();
            if (!codeurl.equals("")) {
                return getSucReturnObject().setData(codeurl).toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getErrReturnObject().setErrMsg("调用失败").toString();
    }

    /**
     * 生成随机字符串
     *
     * @return
     */
    protected String createNonce_str(String str) {
        return new Encryption().Encode_MD5(str + Calendar.getInstance().getTimeInMillis()).substring(0, 30);
    }
}
