package com.ruoyi.common.pay.tm;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import java.util.HashMap;

public class Application {

	private final static String HTTP = "https://pos.weixincore.com";
	private final static String SIGNKEY = "ac6d97e67b444b7a43edfc9182634786";

	public static void main(String[] args) {
		orderQuery("tmwx1727172604248");
//		miniPay();
	}

	/**
	 * 订单查询
	 */
	public static void orderQuery(String outTradeId) {
		HashMap<String, Object> body = new HashMap<>();
		body.put("outTradeId", outTradeId);
		body.put("terminalType", "1");
		body.put("shopId", "488");
		doPost("/open/Pay/orderQuery", body);
	}

	/**
	 * 退款
	 */
	public static void refund() {
		HashMap<String,Object> body = new HashMap<>();
		body.put("refundFee", "0.01");
		body.put("terminalType", "1");
		body.put("tradeId", "1");
		body.put("shopId", "488");
		doPost("/open/Pay/refund", body);
	}


//	/**
//	 * jsapi支付
//	 */
//	public static void pay() {
//		HashMap<String, String> body = new HashMap<String, String>();
//		body.put("payAmount", "1");
//		body.put("terminalType", "1");
//		body.put("shopId", "488");
//		// 填充必填字段
//		body.put("payType", "wx_pay");       // 支付方式，可以是 wx.pay, ali.pay, union.online
//		body.put("outTradeId", "tradeId123");  // 商户订单号
//		body.put("body", "商品描述");         // 商品描述
//		body.put("notifyUrl", "https://yourdomain.com/notify"); // 异步回调URL
//		body.put("frontUrl", "https://yourdomain.com/front");   // 前端页面跳转URL
//		doPost("/open/Pay/unifiedOrder", body);
//	}


	/**
	 * 小程序支付
	 */
	public static void miniPay() {
		HashMap<String, Object> body = new HashMap<>();
		body.put("payAmount", "1");
		body.put("terminalType", "1");
		body.put("shopId", "488");
		// 填充必填字段
		body.put("payType", "wx_pay");       // 支付方式，可以是 wx.pay, ali.pay, union.online
		body.put("outTradeId", IdUtils.getOrderNo("tmwx"));  // 商户订单号
		System.out.println("----------------------------------:"+body.get("outTradeId"));
		body.put("openid", "o6yEK7Z0OdWM2N_d8ehItn-5NBH8");  // openid
		body.put("body", "商品描述");         // 商品描述
		body.put("notifyUrl", "https://yourdomain.com/notify"); // 异步回调URL
		body.put("frontUrl", "https://yourdomain.com/front");   // 前端页面跳转URL
		doPost("/open/Pay/miniPay", body);
	}

	private static void doPost(String url, HashMap<String, Object> body) {
		body.put("developerId", "100001");
		body.put("version", "1.0");
		body.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
		body.put("nonceStr", TmPayUtil.getRandomString(16));
		String bodyStr = TmPayUtil.getAsciiSort(body);
		String sign = TmPayUtil.getMD5Code(bodyStr+"&key="+SIGNKEY).toUpperCase();
		body.put("sign", sign);

		HashMap<String, String> headerData = new HashMap<String, String>();
		headerData.put("Content-Type", "application/json");

		String response = HttpUtils.sendPostWithHeaders(HTTP + url, headerData,JSON.toJSONString(body));
		System.out.println("API Response: " + response);
	}

}
