源码下载
config
java">import java.io.FileWriter;
import java.io.IOException;public class AlipayConfig {// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号public static String app_id = "";// 商户私钥,您的PKCS8格式RSA2私钥public static String merchant_private_key = "";// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。public static String alipay_public_key = "";// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问public static String notify_url = "http://www.bing.com";// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问// 即支付成功之后,需要跳转到的页面,一般为网站的首页public static String return_url = "http://www.baidu.com";// 签名方式public static String sign_type = "RSA2";// 字符编码格式public static String charset = "utf-8";// 支付宝网关public static String gatewayUrl = "https://openapi-sandbox.dl.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();}}}}
}
AliPayController
java">import com.dysy.alipay.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Date;@CrossOrigin
@RequestMapping("/pay")
@Controller
public class AliPayController {@Autowiredprivate AlipayService alipayService;@GetMapping("/hello")public String hello() {return "index";}/*** 跳转到支付界面* @return* @throws Exception*/@GetMapping("/topay")@ResponseBodypublic String pay() throws Exception {String form = alipayService.toPay(String.valueOf(new Date().getTime()),720.0, "易购商城", "订单描述");return form;}
}
Service
AlipayService
java">service
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.dysy.alipay.config.AlipayConfig;
import org.springframework.stereotype.Service;@Service
public interface AlipayService {public String toPay(String orderId, double price, String orderName, String orderDesc) throws Exception;}
AlipayServiceImpl
java">import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.dysy.alipay.config.AlipayConfig;
import com.dysy.alipay.service.AlipayService;
import org.springframework.stereotype.Service;@Service
public class AlipayServiceImpl implements AlipayService {public String toPay(String orderId, double price, String orderName, String orderDesc) throws Exception{//获得初始化的AlipayClientAlipayClient 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 = orderId;//付款金额,必填String total_amount = String.valueOf(price);//订单名称,必填String subject = orderName;//商品描述,可空String body = orderDesc;alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+ "\"total_amount\":\""+ total_amount +"\","+ "\"subject\":\""+ subject +"\","+ "\"body\":\""+ body +"\","+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");String form = "";AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest);if (response.isSuccess()) {form = alipayClient.pageExecute(alipayRequest).getBody();}// 这里返回的 form 是一个字符串,里面封装了支付的表单信息//(即 html 标签 和 javascript 代码),直接将这个 form 输出到页面即可。return form;}
}
util
FileUtils
java">import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;public class FileUtils {public static String readFileOfTxt(String path){StringBuilder sb = new StringBuilder();try(BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {char[] buf = new char[1024];int len = -1;while ((len = bufferedReader.read(buf)) != -1) {sb.append(new String(buf, 0, len));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return sb.toString();}
}
JSONUtils
java">package com.dysy.alipay.util;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.Map;public class JSONUtils {private static final ObjectMapper mapper = new ObjectMapper();public static Map<String, Object> jsonToMap(String jsonStr) throws JsonProcessingException {Map<String, Object> map = mapper.readValue(jsonStr, Map.class);return map;}
}
templates
<form action="/pay/topay"><button type="submit">付款</button>
</form>