支付宝签名方式选择:普通公钥、公钥证书
下面介绍公钥证书方式的接入:
1、生成RSA秘钥,官方参考文档:
https://docs.open.alipay.com/291/105971/
这里我选择第一种,不需要下载直接在线生成密钥。
此时生成了应用公钥,应用私钥。
点击生成csr按钮
说明:
组织/公司 名称一定要和开发者中心门户账号信息的公司名称保持一致,否则会导致后续步骤中上传 CSR 证书文件校验失败。
沙箱环境下 组织/公司 名称应填写为 沙箱环境。
2、控制台配置:
登录开发者中心控制台 >研发服务>沙箱应用:
点击设置:
加签模式选择公钥证书 ,上传证书文选择上传 CSR 文件在线生成证书或者上传已申请证书,即可完成公钥证书的设置。上传证书文件。即可完成公钥证书的设置。
选择上传CS 文件在线生成证书并点击上传 CSR 文件在线生成证书
选择 上传已申请证书,点击,选择上一步骤生成的 .csr 文件上传。上传完成证书后,系统会自动识别证书的加密方式。
3、编码:
pom依赖
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.0.0</version>
</dependency>
一、支付接口
控制层:
/**
* 支付
* @param response
* @throws IOException
* @throws AlipayApiException
*/
@ResponseBody
@GetMapping("/toPay")
public CommonResult toPay(Order order, HttpServletResponse response) throws IOException, AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json",
AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
// 保存订单
Boolean flag = orderService.save(order);
flag = goodsService.saveBatch(order.getGoods());
if (flag){
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//商户订单号,商户网站订单系统中唯一订单号,必填
alipayRequest.setBizContent("{\"out_trade_no\":\""+ order.getOrderId() +"\","
//付款金额,必填
+ "\"total_amount\":\""+ order.getTotalPrice() +"\","
//订单名称,必填
+ "\"subject\":\""+ order.getOrderName() +"\","
//商品描述,可空
+ "\"body\":\""+ order.getRemarks() +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String form="";
try {
form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
response.getWriter().write(form);//直接将完整的表单html输出到页面
response.getWriter().flush();
response.getWriter().close();
return CommonResult.ok();
}else{
return CommonResult.fail();
}
}
配置类:
import java.io.FileWriter;
import java.io.IOException;
public class AlipayConfig {
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号,开发时使用沙箱提供的APPID,生产环境改成自己的APPID
// public static String APP_ID = "2021000117629172";
//
// // 商户私钥,您的PKCS8格式RSA2私钥
// public static String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzlxgJf4PVN1PTXK/p62/WyfHu4NlnLpgwvRzpbA09+5FPR8QgYR6OJOozGnN32YcwQ6LkRN4smkxejwNtCY5S7vSrWA9aJ2b3juMfHToOEM13/j+M5eKowK54A49R2Tr3PPvGx4I0tBkcWGrEkbyUYnwpWQDZTK8lBD/GmoWJQx4UR6a0i+7K+xKbxCcdd91R0AY3JYyRum13nUhM0RfSMjaLLHSk8Hg131orWyuHcQ/B4B70MRBQyY0MNfdrSnd/qOEhjh9mSCBoNc36fXstcxnX6TxeLyoWCrMmpcp2MTx7Ji7lv1SKEpInLqG2IpgxYBFyBQhrAGk70m4tVj+3AgMBAAECggEATGFJjtiV2DvkwfMcui8w7LgM1lz4+aJehfQYdMpIw4DX51ENf/vuQ+Q9Wyi3zVRN4+AQSmdj2rJxIpJOkbRLvMf2dV1Jgj1Z1JYH0Lbcdxj8WjlfC+zFvqHcUhypetUZ+AcOyhcWMJYcS+rq1dlpy+4rA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查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
// public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnvecwFXRgdDes5uqtS0VnP1KKhfjzfpy0U+HEFvxJQBlykbt3QQrTqBpZgI3lhbG1hf1oQ9tg+utKfFyKt/n5KPlBWuBqFrljlRx0K8PhUxnahRgWpcI2QtB+f6qN7qcuh1+0AmEvMeOqE/v067fMM61O+I8QpRVqSrUHBhWiVJ45gZ6jFYm/c3s7NNL2Hh7w8KZ7acYxPI6GHsff/xAR5X+FO+BcD3PelXLvOMLUuATrFBeEdqTTzGBymZGOgikZndhegTkMyfrpIBaG+6kzYveajgiDaEtsBmZwmRdM7YCV14NP6VRrTdQKWyzxLYJnINmINS//FlHapLTIdpBiwIDAQAB";
public static String APP_ID = "2021000117629172";//在后台获取(必须配置)
//私钥直接使用工具的私钥
public static String APP_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCNniucv7WVTDk4Cr5b/fz8VgtfqFIfqCpAdvKX6qEDVQyDtfCoB4O2Ixj0FIA/HfaRPBXjccWKYNPbB7YUsU7jItAIvi8RY35JsBfLASfK3VVWGpRGmsRYj8K+l7dFu+Y5cXLKeHFkaQ500uZ6epaTjZebYL5Ee5x1ENgghO2vx9vamEW1WLdBz1i+Og8RY3PVwyFk19hsv98otc/yfFcu1QHj0QndneXo8NrLBcT66iGXLRrP+enTjEWt3i09x//Sj21ksDSeo/6ycLFNIFHUl7au5Wz+beYjP9vKxfVJcGOGa+fskBvma9kOGliONV03tWhvhRSc49j/sYFSkwInAgMBAAECggEARa8m1qKevzAal7DxqmJ4MdJFFnTEtD8TS1U8WfRh9jnLRqQoHwZTkTQD+1Z7JJOh2CUwwBIjmcu7c3N/7P6ySqG+rA6pKm1Cnw+/JW0UH4Z5jzzU6UkOhGV49j/BNtcWmf1w9p/9a+Ml8eY08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平台。从平台获取
public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnvecwFXRgdDes5uqtS0VnP1KKhfjzfpy0U+HEFvxJQBlykbt3QQrTqBpZgI3lhbG1hf1oQ9tg+utKfFyKt/n5KPlBWuBqFrljlRx0K8PhUxnahRgWpcI2QtB+f6qN7qcuh1+0AmEvMeOqE/v067fMM61O+I8QpRVqSrUHBhWiVJ45gZ6jFYm/c3s7NNL2Hh7w8KZ7acYxPI6GHsff/xAR5X+FO+BcD3PelXLvOMLUuATrFBeEdqTTzGBymZGOgikZndhegTkMyfrpIBaG+6kzYveajgiDaEtsBmZwmRdM7YCV14NP6VRrTdQKWyzxLYJnINmINS//FlHapLTIdpBiwIDAQAB";
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://www.baidu.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.alipaydev.com/gateway.do";
// 支付宝网关
public static String log_path = "C:\\";
/**
* 商户订单号,商户网站订单系统中唯一订单号,必填
*/
public static String outTradeNo = "5110";
/**
* 付款金额,必填
*/
public static String totalAmount = "666";
/**
* 订单名称,必填
*/
public static String subject ="下饭操作";
/**
* 商品描述,可空
*/
public static String body ="这波能五杀";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @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();
}
}
}
}
}
二、退款接口
控制层:
/**
* 退款
* @return
* @throws AlipayApiException
*/
@ResponseBody
@GetMapping("/toRefund")
public CommonResult toRefund(HttpServletResponse response, HttpSession session) throws AlipayApiException {
response.setContentType("text/html;charset=utf-8");
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY,
"json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
//设置请求参数
AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
//商户订单号,必填
String out_trade_no = new String(AlipayConfig.outTradeNo);
//需要退款的金额,该金额不能大于订单金额,必填
String refund_amount = new String("500");
//标识一次退款请求,同一笔交易多次退款需要保证唯一。如需部分退款,则此参数必传;不传该参数则代表全额退款
String out_request_no = new String(UUID.randomUUID().toString());
// 字符转义很重要
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"refund_amount\":\""+ refund_amount +"\","
+ "\"out_request_no\":\""+ out_request_no +"\"}");
//请求
String result = alipayClient.execute(alipayRequest).getBody();
//输出
return CommonResult.ok().setResult("返回结果:"+result);
}
配置类:
同上