支付宝支付,服务器订单生成

static String privateKey = "";

 static String publicKey = "";

static {

        // 从文件中获取私密和公钥
        BufferedReader mBufferedReader = null;
        try {
            FileReader mFileReader = new FileReader(new File(
                    "rsa_private_key_pkcs82.pem"));
            mBufferedReader = new BufferedReader(mFileReader);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while (null != (line = mBufferedReader.readLine())) {
                if (!line.startsWith("-----")) {
                    sb.append(line);
                }
            }
            privateKey = sb.toString();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != mBufferedReader) {
                try {
                    mBufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        
        try {
            FileReader mFileReader = new FileReader(new File(
                    "rsa_public_key.pem"));
            mBufferedReader = new BufferedReader(mFileReader);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while (null != (line = mBufferedReader.readLine())) {
                if (!line.startsWith("-----")) {
                    sb.append(line);
                }
            }
            publicKey = sb.toString();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != mBufferedReader) {
                try {
                    mBufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 {
            Map<String, String> orderInfoM = new HashMap<>();
            orderInfoM.put("app_id", "2016092601974191");
            orderInfoM.put("charset", "utf-8");
            orderInfoM.put("format", "json");
            orderInfoM.put("method", "alipay.trade.app.pay");
            orderInfoM.put("notify_url", "http://www.mytian.com.cn/myt_pay/payAction_alipayNotify.do");
            orderInfoM.put("sign_type", "RSA");
            orderInfoM.put("timestamp", "2016-10-19 10:29:10");
            orderInfoM.put("version", "1.0");
            
            Map<String, String> bizContent = new LinkedHashMap<>();
            bizContent.put("timeout_express", "30m");
            bizContent.put("seller_id", "xiaosong.xu@mytian.com.cn");
            bizContent.put("product_code", "QUICK_MSECURITY_PAY");
            bizContent.put("total_amount", "0.01");
            bizContent.put("subject", "进阶套餐54元");
            bizContent.put("body", "原价120(4.5折)6000麦粒");
            bizContent.put("out_trade_no", "2961476844150427");
            orderInfoM.put("biz_content", JSONObject.toJSONString(bizContent));
            String content = AlipaySignature.getSignContent(orderInfoM);
            String sign = "";
            try {
                sign = AlipaySignature.rsaSign(content, privateKey, "utf-8");
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            System.out.println(content+"&sign="+sign);

 

        // 签名之后要将所有的value进行URLEncoder编码


            try {
                System.out.println(getSignContent(orderInfoM)+"&sign="+URLEncoder.encode(sign, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

public static String getSignContent(Map<String, String> sortedParams) {
        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList<String>(sortedParams.keySet());
        Collections.sort(keys);
        int index = 0;
        for (int i = 0; i < keys.size(); ++i) {
            String key = (String) keys.get(i);
            String value = (String) sortedParams.get(key);
            if (StringUtils.areNotEmpty(new String[] { key, value })) {
                try {
                    content.append(((index == 0) ? "" : "&") + key + "=" + URLEncoder.encode(value, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    content.append(((index == 0) ? "" : "&") + key + "=" + value);
                }
                ++index;
            }
        }
        return content.toString();
    }

转载于:https://my.oschina.net/u/917917/blog/760568

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值