支付宝支付的接口使用

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.utils.AlipayConfig;


public class AliPayServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
		
		//设置请求参数
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		//alipayRequest.setReturnUrl(AlipayConfig.return_url);//同步返回地址
		//alipayRequest.setNotifyUrl(AlipayConfig.notify_url);//支付宝服务器主动通知商户服务器里指定的页面http/https路径。
		
		//商户订单号,商户网站订单系统中唯一订单号,必填
		String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
		//付款金额,必填
		String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
		//订单名称,必填
		String subject = new String(request.getParameter("WIDsubject"));
		//商品描述,可空
		String body = new String(request.getParameter("WIDbody"));
		
		alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
				+ "\"total_amount\":\""+ total_amount +"\"," 
				+ "\"subject\":\""+ subject +"\"," 
				+ "\"body\":\""+ body +"\"," 
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		
		//若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
		//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
		//		+ "\"total_amount\":\""+ total_amount +"\"," 
		//		+ "\"subject\":\""+ subject +"\"," 
		//		+ "\"body\":\""+ body +"\"," 
		//		+ "\"timeout_express\":\"10m\"," 
		//		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
		
		//请求
		String result="";
		try {
			//调用SDK生成表单
			result = alipayClient.pageExecute(alipayRequest).getBody();
		} catch (AlipayApiException e) {
			
			e.printStackTrace();
		}
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		//输出
		out.println(result);
		out.flush();
		out.close();
	}
	
}

AlipayConfig.java//支付宝接口相关的配置文件


import java.io.FileWriter;
import java.io.IOException;

/* *
 *类名:AlipayConfig
 *功能:基础配置类
 *详细:设置帐户有关信息及返回路径
 */

public class AlipayConfig {
	


	// 应用ID,APPID,收款账号既是您的APPID对应支付宝账号
	public static String app_id = "2016091100484730";
	
	// 商户私钥,您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCE7EGYdUqewzdoIgorZsjXZ60pd4M2ipmD2CofC8t2sOrL8u2P4ktbi/VEzHGKeKHQ4iMlhRFAnRVhsX01zq5BkngUkK0VRItJGFiC0BYB3eXa5HOtK0TaEeYpukVzFYUzLtIqIZsYuvbNY2jSxbImBbf3pCUelkketcNi11PGNw5ZAk6ifSVASR8uQ8/TVLQsF741Pec+CBI8uqY9UwvifTpPHH8IWODool15NNK55imr9KHrcUZBkZ11ZBmi1NF6NLBF9t/KCacZEgC5HVrfgrusfDU84yQg/h1zx8q8dS9Ab/VN+OXDK8AAjKmCWacf5VNPhuJKCQkDvjxOCKrTAgMBAAECggEARJYDYr9iuXjRZhHqXWqzxzHfz72zHrl1bOplEY/sdFnlTUcT/WIusBYPgGhdTa3hEP8eU4XN2SE0nywOB22VzI/fFf1w0cCEfjrpsNXeBS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BiEXTXDM2fkSxvcovLH83fQcESfYWqIpA==";
	
	// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwv1ISkXMM8QZgVzkYLnlktC0dyWTy7ELivUd60W5/Mv5g6e05y4WzU+KZixCUnsj5tJR7Jsg9qhTo0zS4t8oaXb3Bzm4YLtRBU9C0wlsq8yyDCBAZEM1053fslqLCgSpz48CnT8+2uUEL/cLKMCfzAUoOteaijBYAyvbc3INERoFRyQcT7IOYjP6O4ymkicNgoR2A0n+3qgX9/MSbMG33owbBmAnh9LnT/dTZaH+Fr2j2V7LFS7RgE/s3qSTwBGZrRUZl6Gr8s6b3tybyDAQy03FLkkOm4gzjvhUnbSjLZmVdCC/1Aq4sKNKwi959pXIj2lIQQ5bSWaYqqPCRt0r9wIDAQAB";

	// 服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String notify_url = "http://tkyiqm.natappfree.cc/AliPay/notify_url.jsp";

	// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String return_url = "http://tkyiqm.natappfree.cc/AliPay/return_url.jsp";

	// 签名方式
	public static String sign_type = "RSA2";
	
	// 字符编码格式
	public static String charset = "utf-8";
	
	// 支付宝网关
	public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
	

	public static String log_path = "D:\\";




    /** 
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     * @param sWord 要写入日志里的文本内容
     */
    public static void logResult(String sWord) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


导包,看文档,官方dome,这是支付的接口,严格注意编码问题,不然容易报错,报公钥和私钥不匹配的错误,要小心。

你可能感兴趣的:(Java,支付)