package org.xxpay.shop.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.xxpay.common.util.PayDigestUtil;
import org.xxpay.common.util.XXPayUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dingzhiwei on 16/5/5.
 */
public class PayOrderDemo {

    // 商户ID
    static final String mchId = "20001223";
    // 项目ID
    static final String payItemId = "7000001";
    // 加签key
    static final String reqKey = "M86l522AV6q613Ii4W6u8K48uW8vM1N6bFgyv769220MdYe9u37N4y7rI5mQ";
    // 验签key
    static final String repKey = "Hpcl522AV6q613KIi46u6g6XuW8vM1N8bFgyv769770MdYe9u37M4y7rIpl8";

    static final String baseUrl = "http://localhost:3020/api";

    public static void main(String[] args) {
        String payOrderId = payOrderTest();
//          String payOrderId = "pay2016120514591893319913549";
  //      if(payOrderId != null ) {
    //        queryOrderTest(payOrderId);
//            //iapNotifyTest(payOrderId);
      //  }

        //reissueOrder();
    }

    // 补单
    static String reissueOrder() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("mchId", "0000010001");                       // 商户ID
        //pay2016110217365533065720596 易宝订单
        //pay2016110710140585712982930 京东PC快捷
        //pay2016110413295519324497135 支付宝wap
        //pay2016110709341803399339691 微信
        paramMap.put("payOrderId", "pay2016111514582547325887888");
        String reqData = XXPayUtil.genUrlParams(paramMap);
        System.out.println("请求支付中心补单接口,请求数据:" + reqData);
        String url = baseUrl + "/pay/reissue_order.htm?";
        String result = XXPayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心补单接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            System.out.println("=========支付中心补单验签成功=========");
        }
        return result;
    }

    // 统一下单
    static String payOrderTest() {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                       // 商户ID
        paramMap.put("mchOrderNo", System.currentTimeMillis());               // 商户订单号
        paramMap.put("channelId", "WX_NATIVE");                    // 支付渠道ID, WX_NATIVE,ALIPAY_WAP
        paramMap.put("amount", 1);                      // 支付金额,单位分
        paramMap.put("currency", "cny");                  // 币种, cny-人民币
        paramMap.put("clientIp", "114.112.124.236");                        // 用户地址,IP或手机号
        paramMap.put("device", "WEB");                       // 支付公司标识
        paramMap.put("subject", "XXPAY支付测试");
        paramMap.put("body", "XXPAY支付测试");
        paramMap.put("notifyUrl", "http://baidu.com");         // 回调URL
        //paramMap.put("notifyUrl", "http://xxpay.ngrok.cc/notify/pay/wxPayNotifyRes.htm");
        paramMap.put("param1", "");                         // 扩展参数1
        paramMap.put("param2", "");                         // 扩展参数2

        paramMap.put("extra", "{\"productId\":\"120989823\",\"openId\":\"o2RvowBf7sOVJf8kJksUEMceaDqo\"}");  // 附加参数

        String reqSign = PayDigestUtil.getSign(paramMap, reqKey);
        paramMap.put("sign", reqSign);   // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心下单接口,请求数据:" + reqData);
        String url = baseUrl + "/pay/create_order?";
        String result = XXPayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心下单接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, repKey, "sign", "payParams");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心下单验签成功=========");
            }else {
                System.err.println("=========支付中心下单验签失败=========");
                return null;
            }
        }
        return retMap.get("payOrderId")+"";
    }

    static void queryOrderTest(String payOrderId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("mchId", mchId);                   // 商户ID
        paramMap.put("payItemId", payItemId);               // 业务项目ID
        paramMap.put("rechargeId", "20160505000968");       // 业务系统流水号
        paramMap.put("payOrderId", payOrderId);             // 支付单号

        String reqSign = PayDigestUtil.getSign(paramMap, reqKey);
        paramMap.put("sign", reqSign);   // 签名
        String reqData = XXPayUtil.genUrlParams(paramMap);
        System.out.println("请求支付中心查单接口,请求数据:" + reqData);
        String url = baseUrl + "/pay/query_order.htm?";
        String result = XXPayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心查单接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);      // fastJson速度更快
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, repKey, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========查询支付订单验签成功=========");
            }else {
                System.err.println("=========查询支付订单验签失败=========");
            }
        }
    }
    //
    static void iapNotifyTest(String payOrderId) {
        // "mchId", "payOrderId", "userId", "rechargeId", "receiptData", "transactionId", "sign"
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String receiptData = " ";
        paramMap.put("mchId", mchId);                   // 商户ID
        paramMap.put("payOrderId", payOrderId);         // 支付单号
        paramMap.put("receiptData", receiptData);
        paramMap.put("transactionId", "111");              // 苹果支付ID
        paramMap.put("userId", 1984l);                  // 用户ID

        String reqSign = PayDigestUtil.getSign(paramMap, reqKey);
        paramMap.put("sign", reqSign);   // 签名

        try {
            paramMap.put("receiptData", URLEncoder.encode(receiptData, "UTF-8"));             // 苹果支付凭据
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String reqData = XXPayUtil.genUrlParams(paramMap);
        System.out.println("请求支付中心苹果通知接口,请求数据:" + reqData);
        String url = baseUrl + "/notify/iapPayNotifyRes.htm?";
        String result = XXPayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心苹果通知接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);      // fastJson速度更快
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, repKey, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========苹果支付验证凭据验签成功=========");
            }else {
                System.err.println("=========苹果支付验证凭据验签失败=========");
            }
        }
    }

    static void iapNotifyTest2(String payOrderId) {
        // "mchId", "payOrderId", "userId", "rechargeId", "receiptData", "transactionId", "sign"
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("mchId", mchId);                   // 商户ID
        //paramMap.put("rechargeId", "20160505000968");   // 业务系统流水号
        paramMap.put("payOrderId", payOrderId);         // 支付单号
        try {
            paramMap.put("receiptData", URLEncoder.encode("abc+123", "UTF-8"));             // 苹果支付凭据
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        paramMap.put("transactionId", "");           // 苹果支付ID
        paramMap.put("userId", 1984l);                  // 用户ID

        String reqSign = PayDigestUtil.getSign(paramMap, reqKey, "receiptData");
        paramMap.put("sign", reqSign);   // 签名
        String reqData = XXPayUtil.genUrlParams(paramMap);
        System.out.println("请求支付中心苹果通知接口,请求数据:" + reqData);
        String url = "http://localhost:8088/h5/test.html?";
        String result = XXPayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心苹果通知接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);      // fastJson速度更快
        if("SUCCESS".equals(retMap.get("retCode"))) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, repKey, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========苹果支付验证凭据验签成功=========");
            }else {
                System.err.println("=========苹果支付验证凭据验签失败=========");
            }
        }
    }


}
