支付宝沙盒应用

在这里插入图片描述
添加功能 沙盒应用
https://openhome.alipay.com/platform/manageHome.htm

获得以下主要的4个部分,APPID 网关 密钥 支付宝公钥
在这里插入图片描述
配置自己的接口

package com.lzm.utils;

import org.springframework.stereotype.Controller;

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

/**
 * @author lzm
 * @create 2019- 07- 19- 10:29
 */
@Controller
public class AlipayConfig {

    //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2016101000237";

    // 商户私钥,您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQquXa8ZWaCHvQrIBwl3HGAxOTh34QSDZjb9/GGaImrcwaU3R+c4zkzacnBEv3HADDmqly7oNxk7BcwmDBctEw///jQ5sqTPYWXmmVztakPzM67h/uYJLAwHl9aupW7K4al0M+6YO1RF/Z18SkTY44sjEBUP/gqLn88d+ldBAr4/WiwdGSq/PT6Yunuq+smMVkap3YNP5ZwF/gH2eyoqiN8ZORBP8XFE/vlwvJZKfRD7FPpamTo41Rqz/PPz2i9eC6yEUGWITozbAgMBAAECggEBAIVeuIfPvu8BsTwhPEwQopg9j9eryyGxoUqtAe09BouIIKmIlNZbKuJpYheLU4leLKooNN27XDziPm5V2SOe+ZKmAD+BvVQTHbBjnA27YXdNznytP3cNeQUWqvOhx6KFKq8XzW7g0vbqrJLB3Tr23QtL+IZjmgDiUQFp8As4YB0sbkkBsyZbNxE9SpkIelD4En7Xoic1/YhYizD9+Hv2AbhHD6I+5BVw9qGHGg4Rt3xQJtLvMB1BhmBsMX27v00A7uu5tQHvwIelRc3D7xwAsv1nSKq5ANGvSeCtOnWBxCxSemn2jdXSlhIF06aTQN/L2NqJ4SpAVhp8E8XWeLp46XkCgYEA8TJlFn1jcodLo73Vm0KC3w82eaCv02R4l7rZNtGpMdxbZ6IdnI+QwSlZzcd62jLuVCs16ZuQF8dAjRwFTgND8V7HW26O3n3CVKlfSIjqzxz26VUd1XYw5d39KDN72BymLtBxDqbnVKZ8eTiwnwgQmG7gLP94enpzfj8uXRe9mP0CgYEAmYvd6I7AOFnVcYU4P1wEJM0v39um54a0ghSiD00gKaGj59M9iEHG5stA6/CJ+oF7HjPaXiDTGbWbeaaDZiB2r4LVUKIot1Zlr+7Z22AgHLcWnt17TCSFEyqruycUrS1ri8I+HbkiBYK703p1r7B0AGs53sVK+00y2H/OlNGN8LcCgYEAovqmi/fWRwFc/xu7Rto+jHM/IBiQNothYUWEmSI67dI0gWZuE+h+q8dBQ2dTgDWCDaXVHDFdwHOHDtKsUvap5mNg2ktuhrD7qzF3jQ/XenPb8/br3d2rVOgXtB51xKY3NNLsUjY9glmtKkxALHXKoPXqzP0jb79mYxxWO9la6BECgYBEPm36lrfIU+UR92vwfS06ThghHrcn4C8KQbFlq5nH5xe6SaDnlpSYr23Db71joXjfC5zaiSxKGPVpNCDimzsk9rlohSojkjrP3xDk9+xN1vIu2bGbPgAHbMe2/r+p2K4cB9Ej1MzSNjDU5XC5DWwBY6bs5DNXThBzORrP+fNkMQKBgHgDj5YiZLtybYuT+UrLisuhhzOzkSBtFbX5d1cj079oe3F7tKoMk4MguTUpsGgJkVq+jEv/euKCd1kvMyuXVsLrnpPAy8SaVduJIizaWDQwR3VQHBXXCSs4Acc3qq1zz52DVIOl7lYaIbd0Li3627C4NUo0aPvOGbhs+TiIR8dW";

    // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAik6rnRbRNKU1RxZ2qjc26qlxmhqOqcUbuWpvgXMoAGQQ1BHnrAHTKCsaPRs3g6FebkV4THPh7+0HAjBy7Z70HrM4jxsJkGhNy1yIk3hAVKUyZ8LGY9YBVgK1VdCZp8jta3J1dmIJ9+Q+sDnDVdxAIE0lKhH/EHCvGHdeCSBCjgkinly3rRrQXqr3e4dD7RPLUNpkqeUoCSUPauEXaOM4I1G1hcJ/0fnYKqKK5tc6F1ypLUxYK4Uxv11oGfH1FXf7JizU++aHjj+6QQ6nKg4Oy9XAIfKuDPA/tUz5T9GiD83lVA2ZH4LCvuS76Ej3LEw+L1/fqxAAO9BSrpQIDAQAB";

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

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

    // 签名方式
    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 = "C:\\";


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    /**
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     * @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();
                }
            }
        }
    }
}

    <form id="orderProject" action="${pageContext.request.contextPath}/ToPay.action" method="post"  >
    <!--//商品编号-->
    <input type="hidden" id="WIDout_trade_no" name="WIDout_trade_no"  />
    <input type="hidden" id="WIDsubject" name="WIDsubject"   />
    <input type="hidden" id="WIDtotal_amount" name="WIDtotal_amount"  />
    <input type="hidden" id="WIDbody" name="WIDbody" />
    </form>

点击按钮后提交该表单

//激起付款界面
        $("#orderProject").submit();

在这里插入图片描述

package com.lzm.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lzm.utils.AlipayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

/**
 * @author lzm
 * @create 2019- 07- 19- 10:19
 */
@Controller
public class aliPayController {

    //生成有二维码,可供扫码支付的页面
    @RequestMapping(value = "/ToPay.action",method = RequestMethod.POST)
    public String aliPay(HttpServletResponse response, ModelMap map, String chapterId, HttpServletRequest request,
                         String WIDout_trade_no, String WIDtotal_amount, String WIDsubject, String WIDbody) throws IOException, AlipayApiException, IOException, AlipayApiException {
//      String a,String urlName,String couName....+"&a="+a+"&urlName="+urlName+"&couName="+couName
        //获得初始化的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+"?chapterId="+chapterId);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        //付款ID,必填
        String out_trade_no = WIDout_trade_no;
        //付款金额,必填
        String total_amount = WIDtotal_amount;
        total_amount= URLDecoder.decode(total_amount,"UTF-8");//转码
        //订单名称,必填
        String subject = WIDsubject;
        subject=URLDecoder.decode(subject,"UTF-8");
        //商品描述,可空
        String body = WIDbody;

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(result);
        return "test/alipay.trade.page.pay";
    }
}

通过订单页面传递过来的参数经过aliPayController处理后跳转到生成二维码jsp页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>付款</title>
</head>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="com.alipay.api.AlipayClient"%>
<%@ page import="com.alipay.api.DefaultAlipayClient"%>
<%@ page import="com.alipay.api.request.AlipayTradePagePayRequest"%>
<%@ page import="com.alipay.config.*" %>
<%@ page import="com.lzm.utils.AlipayConfig" %>
<%
	//获得初始化的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);
	
	//商户订单号,商户网站订单系统中唯一订单号,必填
	String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("UTF-8"),"UTF-8");
	//付款金额,必填
	String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("UTF-8"),"UTF-8");
	//订单名称,必填
	String subject = new String(request.getParameter("WIDsubject").getBytes("UTF-8"),"UTF-8");
	//商品描述,可空
	String body = new String(request.getParameter("WIDbody").getBytes("UTF-8"),"UTF-8");
	
	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 = alipayClient.pageExecute(alipayRequest).getBody();
	
	//输出
	out.println(result);
%>
<body>
</body>
</html>

在这里插入图片描述

支付成功后返回处理,在上面配置文件中注明你成功支付后的返回到哪里

    // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String return_url = "http://localhost:8080/Share/alipay_callback.action";
package com.lzm.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.lzm.service.OrderService;
import com.lzm.service.SupportService;
import com.lzm.utils.AlipayConfig;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lzm
 * @create 2019- 07- 19- 17:45
 * 支付成功后的回调函数
 */
@Controller
public class AlipayCallbackController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private SupportService supportService;

    private static Logger logger = LoggerFactory.getLogger(AlipayCallbackController.class);

    private ExecutorService executorService = Executors.newFixedThreadPool(20);

    /**
     * <pre>
     * 第一步:验证签名,签名通过后进行第二步
     * 第二步:按一下步骤进行验证
     * 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
     * 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
     * 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email),
     * 4、验证app_id是否为该商户本身。上述1、2、3、4有任何一个验证不通过,则表明本次通知是异常通知,务必忽略。
     * 在上述验证通过后商户必须根据支付宝不同类型的业务通知,正确的进行不同的业务处理,并且过滤重复的通知结果数据。
     * 在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,支付宝才会认定为买家付款成功。
     * </pre>
     *
     * @return
     */

    @RequestMapping("/alipay_callback.action")
    public String callback(HttpServletRequest request) {
        Map<String, String> params = convertRequestParamsToMap(request); // 将异步通知中收到的待验证所有参数都存放到map中
        String paramsJson = JSON.toJSONString(params);
        try {
            AlipayConfig alipayConfig = new AlipayConfig();// 支付宝配置
            // 调用SDK验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.alipay_public_key, alipayConfig.charset, alipayConfig.sign_type);
            if (signVerified) {
                //付款成功,将订单ispay设为1,并且在support表中添加记录
                //获取订单ID
                String outTradeNo = params.get("out_trade_no");
                orderService.uptateOrderByID(outTradeNo);
                supportService.insertSupportByOrderID(outTradeNo);
                return "client/user-support";
            } else {
                return "/test/error";
            }
        } catch (AlipayApiException e) {
//            logger.error("支付宝回调签名认证失败,paramsJson:{},errorMsg:{}", paramsJson, e.getMessage());
            return "failure";
        }
    }

    // 将request中的参数转换成Map
    private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();

        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();

        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;

            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }

        return retMap;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值