package com.tzy.s.wx.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tzy.s.wx.dto.Result;
import com.tzy.s.wx.dto.SnsapiUserInfo;
import com.tzy.s.wx.payment.PaymentUtil;
import com.tzy.s.wx.payment.service.PaymentService;
import com.tzy.s.wx.util.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid", "wx10bd35e2ea404835");
        jsonObject.put("mchid", "1612475613");
        jsonObject.put("description", "球星卡-Jordan-card");
        jsonObject.put("out_trade_no", "GU2021082176432030");
        jsonObject.put("notify_url", "https://mini.hobbystock.cn/api/wx/paymentBack");
        JSONObject amount = new JSONObject();
        amount.put("total", 1);
        amount.put("currency", "CNY");
        jsonObject.put("amount", amount);
        JSONObject payer = new JSONObject();
        payer.put("openid","o7-jx4mhKK9WOZIB80HVioxC8ixI");
        jsonObject.put("payer",payer);
        PaymentServiceImpl paymentService = new PaymentServiceImpl();
        Result result = paymentService.sendJsapi(jsonObject);
    }
    private static String api_http  = "https://api.mch.weixin.qq.com";
    //jsapi
    private static String JSAPI = "/v3/pay/transactions/jsapi";
    private static String certificates = "/v3/certificates";

    @Override
    public Result sendJsapi( JSONObject jsonObject){
        String url = api_http+certificates;
//        String s = jsonObject.toString();
        String s = "\n";
        try {
            long l = System.currentTimeMillis() / 1000;
            String serial_no = "2E5F31CB04CBCE92EE6B24EAFC767255145B0551";
            String sign = PaymentUtil.encode("GET", certificates, l, s);
            StringBuffer authorization = new StringBuffer("WECHATPAY2-SHA256-RSA2048 ");
            authorization.append("mchid=\""+jsonObject.getString("mchid")+"\",")
            .append("serial_no=\""+serial_no+"\",")
                    .append("nonce_str=\""+ UUID.randomUUID().toString().replaceAll("-","")+"\",")
            .append("timestamp=\""+l+"\",")
            .append("signature=\""+sign+"\"")
            ;
            Map header = new HashMap<>();
            String auth = authorization.toString();
            System.out.println(auth);
            header.put("Authorization",auth);
            String ret = HttpClient.httpsRequest(url, "GET", null,header);
            if (StringUtils.isEmpty(ret)) return Result.error(-1, "请求失败");

            JSONObject retJson = JSONObject.parseObject(ret);
            String prepay_id = retJson.getString("prepay_id");
            return Result.ok().put("prepay_id",prepay_id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(-1, "请求异常，请联系开发人员");
        }
    }



}
