Springboot+支付宝沙箱环境 html版

 

最近写了一个springboot集成支付宝html的  

首先自己集成  Thymeleaf   

application.properties文件

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.webflux.static-path-pattern=classpath:/templates/index.html

pom.xml文件

 
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            org.springframework.boot
            spring-boot-starter-web
        

然后自己测试一下 有问题自行百度 因为这个不是重点  重点如下开始测试!!

 

话不多说  首先自己要有蚂蚁金服的开发者账号以及自己去申请沙箱环境。

官网:https://openhome.alipay.com/platform/appDaily.htm

自己先下载官网的demo https://docs.open.alipay.com/194/105201/

然后jar包我自己生成了在本地的maven仓库  

Springboot+支付宝沙箱环境 html版_第1张图片

然后一个支付宝的基本信息配置公共类。

package com.ht.calffinancing.common.alipay;

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

/* *
 *类名:AlipayConfig
 *功能:基础配置类
 *详细:设置帐户有关信息及返回路径
 *修改日期:2017-04-05
 *说明:
 *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
 */

public class AlipayConfig {

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

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

	// 商户私钥,您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "xxxx";

	// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "xxxxx";

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

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

	// 签名方式
	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();
                }
            }
        }
    }
}

然后控制层  一个访问首页的方法  传入根据自己的业务需求去保存


    @RequestMapping("go/applyindex")
    public  String index(BigDecimal tmoney,String val,HttpServletRequest request){
        HttpSession session=request.getSession();
        if (tmoney!=null||val!=null){
            session.setAttribute("tmoney",tmoney);
            session.setAttribute("val",val);
        }
        return "alipy/index.html";
    }

然后index页面  具体根据你们自己的业务去做大概的更改 ,由于只是单机测试所以并没有做的太多验证以及注意一些的别的东西。





    
    支付宝电脑网站支付
    
    
    


支付宝电脑网站支付体验入口页

付 款

交 易 查 询

退 款

退 款 查 询

交 易 关 闭

商户订单号 :

订单名称 :

付款金额 :

银行卡号:

如果您点击“付款”按钮,即表示您同意该次的执行操作。
商户订单号 :

支付宝交易号 :

商户订单号与支付宝交易号二选一,如果您点击“交易查询”按钮,即表示您同意该次的执行操作。
商户订单号 :

支付宝交易号 :

退款金额 :

退款原因 :

退款请求号 :

商户订单号与支付宝交易号二选一,如果您点击“退款”按钮,即表示您同意该次的执行操作。
商户订单号 :

支付宝交易号 :

退款请求号 :

商户订单号与支付宝交易号二选一,如果您点击“退款查询”按钮,即表示您同意该次的执行操作。
商户订单号 :

支付宝交易号 :

商户订单号与支付宝交易号二选一,如果您点击“交易关闭”按钮,即表示您同意该次的执行操作。

然后进入控制层   支付宝得到你的参数会自动响应一个页面

@RequestMapping(value = "/go/applytest", produces = "text/html; charset=UTF-8",method= RequestMethod.POST)
    @ResponseBody
    public  void    applytest(HttpServletRequest request, HttpServletResponse response,String WIDout_trade_no, BigDecimal WIDtotal_amount,String WIDbody) throws IOException, AlipayApiException {
        //获得初始化的AlipayClient
        PrintWriter out = response.getWriter();
        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);
        HttpSession session=request.getSession();
       
        String tmoney="";
        String val="";
        String ddh=(int)((Math.random()*99999)+100000)+"";
        System.out.println(tmoney+"这是跳转的"+val+"ddh"+ddh);
        //商户订单号,商户网站订单系统中唯一订单号,必填
        String out_trade_no = ddh;
        //付款金额,必填
        String total_amount = tmoney;
        //订单名称,必填
        String subject = "个人充值";
        //商品描述,可空
        String body ="余额充值";
        // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
//        String timeout_express = "5m";
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
//                + "\"timeout_express\":\""+timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
        String url = alipayClient.pageExecute(alipayRequest,"get").getBody();
        System.out.println(url);
        //输出

//        request.setAttribute("result",result);
//        response.setContentType("text/html;charset=utf-8");
//        out.print(doc.outerHtml());
//      return result;
        out.print(url);

    }

然后还有2个控制器的方法也就是成功返回后和异步返回后的结果

Springboot+支付宝沙箱环境 html版_第2张图片

然后return到你所想去的页面。

 

然后异步跟同步都差不多  不过还是要根据你具体的业务去做处理。

Springboot+支付宝沙箱环境 html版_第3张图片

你可能感兴趣的:(html)