版权声明:本文为博主原创文章,未经博主允许不得转载。
最近公司项目涉及到支付宝支付的接入,说简单也挺简单,说难也挺复杂的。自己做了总结,供大家参考学习。
# 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
alipay.app_id=2016092700608222
# 商户私钥,您的PKCS8格式RSA2私钥
alipay.privateKey=MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCT9FGhs6lslzLvKrjzoglEJYfj3KqdNHN6bEy0uQabTaos+eU4YMw/fr14hADziuLhgPG1/GQEHQHOujiNJUSw6N++E5Wc1p3rOC1a2tnRj7JncgKUoyfEdroaeBBg/WosuhCWor+TFXs5oB8sUscJBNqzeM1cdzjxj9Du6YcCirDU4+61vuAfaladd5WsvMr3kPnkazDOnAiB3FS9tOGExMF2DW44XJtYX69fI7KTAp8HQwf6pyvvorHBArKim1bi8UiOCYCILbgmP5rQzHoRbdL2UeL1PwadfFtwMxQDcuhwOt01VVTKF5rI5NqsZSmQgvpXyL87O/bs+1AgMBAAECggEAKz76rV0qZIc9aBMAxJXUR4sA0WWqqRCvwFTvhtOeTr7Awyy4B2ajzNtq3lMXRBiQgIGEJUGEqn8gDCD6bqc6EMvAunpW/OTue9aLy0a07EAoUXxNrMyTzyU0+I+nmfH4muI3/w/LF17sCcf31KjNbKFym4rv29stVc5Hr82qnN8hYPiWpHveYpaB0r+NtvuXqSPswzmIELussvpCu7AhzHZ8LqvUS2knl+YnjDfMWdjFZyXZRxsjvcsStUI7E3Ygm8OGWimnYXQalDBpV03Z+RSCgwzA50DvGa53nwu+EFizl86hb17o96+RpHk6Emao2WbJSGnHN2GlDnXFjcXwAQKBgQDqFvH4XcVWtqH+wOlKsITwoBonv1WDJOAe1L85YbJVisQuQwlnNozaIQLiw+SErkufyrSiupBYbhvDKkZa/Ur+pMgmqXOFTyWXFcQNrd3Hha/LY+jjTODOOLmI962t0Kl8QfEPh6zv2WFGZ7ZNPTzs/5lFN+PCEXut84KoMYc/AQKBgQChzXl6XYAnVqYt73X23YRdx566GeG6ka6wqCf9Lyz4iZSxR4rpB0xR9ehNmQIank3afEduGODYcis7ytZg/Lpm+b0gNgx/Lb2S2Kpgp7GHx6r+CoL1xMasWcJS/xrRwQDb3ow9AeHuUpgZ4s3jnA8LoLdJ13kkGrsKaUF4IRNEtQKBgQCg9KHvyloT44PDbNV7+5+M7CjPx6l+gO5xhIrvY5OBaea2gHWIuGNk4Gnhjhwkdsuc1X4anfj+NMd2DC1QBvV33cmZe83ojaElH1wso63osK1NRictkwDICwzXdqW+68h9ncGokfuA95EBHfKlbUPlGWJDy8gY+nh2BQeDqnp6AQKBgQCLk0qVag7g4Hhd69Gg8Rn8I/UiqKz2WjGNpqFIYOe0RkqEWyjTOD53/V81gSY1fBXAwbCEouVjEamF3MXtyFWr+wdQwA8rrnN4N3tSAZa9gFpa5wptpB+WEZ/2JKYoBwwMnv/uqK8S/SDEQ8hD7h10dGFoS33uzThIFOArFq0eeQKBgF2kiybVUrEqiXpr6ADLrulsrfkxlqGoiPR9jZrxXomdgutbBTZ/J5G8sFT5mP7/oJxpMMzOg6AdmtQQTkPJEAvW/XWWfxn2rTP39E0TTA0gY6NqLTTLhlhhxShNDPwucHgslMsPqZ6pl1u1HjWAHIpbSthhJ8L5nH7G2zUAUCnb
# 支付宝公钥,查看地址:https://openhome.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
alipay.publicKey=MIIBIjANBgkqhkiG9w0BAQEFCAQ8AMIIBCgKCAQEAx4j7DP30zl7+WahLyJRydSwcayJsEavQ7S1fPWqmHTK6TTVeNSMrcG/blXv42p7Eib3KeXvVIMOnNyoKXoIoOfSBYOvTyPH6cs60W31A0TanTc9aMlGof8X4WaDSDz78T34elagX+cpigV8mqLB/umkl1WoGIAkX7r3Bau+HWnPgftYPmzpL5G1/XdSEgoklGGsLGiiSGfE2KtV035+vJiViLRTiCEIKhygKTOROq36O5BsSIfgapLz08lzPT3ik9g26Jwq/7oEACcO8zg9LkcqHTnxV9LiSVg4+ay1huSmvxRE2vm21iNFNzIo6v+JEoc+cZJ0b9E7RZWKDE1ZXvQIDAQAB
# 服务器异步通知页面路径需http://格式的完整路径,不能加?id=123这类自定义参数 必须是外网才能访问的页面
alipay.notify_url=http://ruv6tc.natappfree.cc
# 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
alipay.return_url=http://localhost:8080/returnUrl
# 签名方式
alipay.sign_type=RSA2
# 字符编码格式
alipay.charset=utf-8
# 支付宝网关
alipay.gatewayUrl=https://openapi.alipaydev.com/gateway.do
# 支付宝网关
alipay.logPath="C:\\"
2请求参数
/**
* 商户订单号,必填
*
*/
private String out_trade_no;
/**
* 订单名称,必填
*/
private String subject;
/**
* 付款金额,必填
* 根据支付宝接口协议,必须使用下划线
*/
private String total_amount;
/**
* 商品描述,可空
*/
private String body;
/**
* 超时时间参数
*/
private String timeout_express= "5m";
/**
* 产品编号
*/
private String product_code= "FAST_INSTANT_TRADE_PAY";
4.进入沙箱,进行注册 私钥和公钥
这里使用sprinboot 做测试
1.使用idea快速创建一个springboot项目
一直Next到
然后点Next----finish
2.项目创建完成打开 pom文件 添加两个依赖
com.alipay.sdk
alipay-sdk-java
3.1.0
com.alibaba
fastjson
1.2.48
3.在resources创建alipay.properties文件
内容:
# 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
alipay.app_id=2016092700608222
# 商户私钥,您的PKCS8格式RSA2私钥
alipay.privateKey=MIIEvgIBADANBgkqhkiG9wAQEFAASCBKgwggSkAgEAAoIBAQCT9FGhs6lslzLvKrjzoglEJYfj3KqdNHN6bEy0uQabTaos+eU4YMw/fr14hADziuLhgPG1/GQEHQHOujiNJUSw6N++E5Wc1p3rOC1a2tnRj7JncgKUoyfEdroaeBBg/WosuhCWor+TFXs5oB8sUscJBNqzeM1cdzjxj9Du6YcCirDU4+61vuAfaladd5WsvMr3kPnkazDOnAiB3FS9tOGExMF2DW44XJtYXzvw6U169fI7KTAp8HQwf6pyvvorHBArKim1bi8UiOCY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nv/uqK8S/SDEQ8hD7h10dGFoS33uzThIFOArFq0eeQKBgF2kiybVUrEqiXpr6ADLrulsrfkxlqGoiPR9jZrxXomdgutbBTZ/J5G8sFT5mP7/oJxpMMzOg6AdmtQQTkPJEAvW/XWWfxn2rTP39E0TTA0gY6NqLTTLhlhhxShNDPwucHgslMsPqZ6pl1u1HjWAHIpbSthhJ8L5nH7G2zUAUCnb
# 支付宝公钥,查看地址:https://openhome.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
alipay.publicKey=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx4j7DP30zl7+WahLyJRydSwcayJsEavQ7S1fPWqmHTK6TTVeMrcG/blXv42p7Eib3KeXvVIMOnNyoKXoIoOfSBYOvTyPH6cs60W31A0TanTc9aMlGof8X4WaDSDz78T34elagX+cpigV8mqLB/umkl1WoGIAkX7r3Bau+HWnPgftYPmzpL5G1/XdSEgoklGGsLGiiSGfE2KtV035+vJiViLRTiCEIKhygKTOROq36O5BsSIfgapLz08lzPT3ik9g26Jwq/7oEACcO8zg9LkcqHTnxV9LiSVg4+ay1huSmvxRE2vm21iNFNzIo6v+JEoc+cZJ0b9E7RZWKDE1ZXvQIDAQAB
# 服务器异步通知页面路径需http://格式的完整路径,不能加?id=123这类自定义参数 必须是外网才能访问的页面
alipay.notify_url=http://ruv6tc.natappfree.cc
# 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
alipay.return_url=http://localhost:8080/returnUrl
# 签名方式
alipay.sign_type=RSA2
# 字符编码格式
alipay.charset=utf-8
# 支付宝网关
alipay.gatewayUrl=https://openapi.alipaydev.com/gateway.do
# 支付宝网关
alipay.logPath="C:\\"
4.在model下建两个类(一个实体类,一个配置类)
1AlipayProperties (配置类,作用:把alipay.properties文件加载到类中)
package com.example.alipay.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value={"classpath:alipay.properties"})
@ConfigurationProperties(prefix = "alipay")
public class AlipayProperties {
private String app_id;
private String privateKey;
private String publicKey;
private String notify_url;
private String return_url;
private String sign_type;
private String charset;
private String gatewayUrl;
private String logPath;
@Override
public String toString() {
return "AlipayProperties{" +
"app_id='" + app_id + '\'' +
", privateKey='" + privateKey + '\'' +
", publicKey='" + publicKey + '\'' +
", notify_url='" + notify_url + '\'' +
", return_url='" + return_url + '\'' +
", sign_type='" + sign_type + '\'' +
", charset='" + charset + '\'' +
", gatewayUrl='" + gatewayUrl + '\'' +
", logPath='" + logPath + '\'' +
'}';
}
public String getApp_id() {
return app_id;
}
public void setApp_id(String app_id) {
this.app_id = app_id;
}
public String getPrivateKey() {
return privateKey;
}
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public String getPublicKey() {
return publicKey;
}
public void setPublicKey(String publicKey) {
this.publicKey = publicKey;
}
public String getNotify_url() {
return notify_url;
}
public void setNotify_url(String notify_url) {
this.notify_url = notify_url;
}
public String getReturn_url() {
return return_url;
}
public void setReturn_url(String return_url) {
this.return_url = return_url;
}
public String getSign_type() {
return sign_type;
}
public void setSign_type(String sign_type) {
this.sign_type = sign_type;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getGatewayUrl() {
return gatewayUrl;
}
public void setGatewayUrl(String gatewayUrl) {
this.gatewayUrl = gatewayUrl;
}
public String getLogPath() {
return logPath;
}
public void setLogPath(String logPath) {
this.logPath = logPath;
}
}
2、AlipayBean(实体类 作用:接收请求参数)
package com.example.alipay.model;
/**
* 支付实体对象
* 根据支付宝接口协议,其中的属性名,必须使用下划线,不能修改
* @author Louis
* @date Dec 12, 2018
*/
public class AlipayBean {
/**
* 商户订单号,必填
*
*/
private String out_trade_no;
/**
* 订单名称,必填
*/
private String subject;
/**
* 付款金额,必填
* 根据支付宝接口协议,必须使用下划线
*/
private String total_amount;
/**
* 商品描述,可空
*/
private String body;
/**
* 超时时间参数
*/
private String timeout_express= "5m";
/**
* 产品编号
*/
private String product_code= "FAST_INSTANT_TRADE_PAY";
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTotal_amount() {
return total_amount;
}
public void setTotal_amount(String total_amount) {
this.total_amount = total_amount;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTimeout_express() {
return timeout_express;
}
public void setTimeout_express(String timeout_express) {
this.timeout_express = timeout_express;
}
public String getProduct_code() {
return product_code;
}
public void setProduct_code(String product_code) {
this.product_code = product_code;
}
}
5.在service.impl下建Alipay类
接入支付中最主要代码
内容
package com.example.alipay.service.impl;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.alipay.model.AlipayBean;
import com.example.alipay.model.AlipayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.UUID;
@Component
public class Alipay {
/**
* 支付接口
* @param alipayBean
* @return
* @throws AlipayApiException
*/
@Autowired
AlipayProperties alipayProperties;
public String pay(AlipayBean alipayBean) throws AlipayApiException {
// 1、获得初始化的AlipayClient
String serverUrl = alipayProperties.getGatewayUrl();
String appId = alipayProperties.getApp_id();
String privateKey = alipayProperties.getPrivateKey();
String format = "json";
String charset = alipayProperties.getCharset();
String alipayPublicKey = alipayProperties.getPublicKey();
String signType = alipayProperties.getSign_type();
String returnUrl = alipayProperties.getReturn_url();
String notifyUrl = alipayProperties.getNotify_url();
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
// 2、设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
// 页面跳转同步通知页面路径
alipayRequest.setReturnUrl(returnUrl);
// 服务器异步通知页面路径
alipayRequest.setNotifyUrl(notifyUrl);
//账户订单号
String out_trade_no = new Date().getTime()+ UUID.randomUUID().toString();
alipayBean.setOut_trade_no(out_trade_no);
// 封装参数
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
// 3、请求支付宝进行付款,并获取支付结果
String result = alipayClient.pageExecute(alipayRequest).getBody();
// 返回付款信息
return result;
}
}
6.在controller下建OrderController类
内容
package com.example.alipay.controller;
import com.alipay.api.AlipayApiException;
import com.example.alipay.model.AlipayBean;
import com.example.alipay.service.impl.Alipay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class OrderController {
@Autowired
Alipay alipay;
@PostMapping(value = "alipay")
@ResponseBody
// String outTradeNo, String subject, String totalAmount, String body
public String alipay(AlipayBean alipayBean) throws AlipayApiException {
return alipay.pay(alipayBean);
}
}
7.最后在static下建一个测试的html页面(index.html)
Title
支付测试
到此 代码就完成了
1.在浏览器输入 localhost:8080
2点击支付宝支付
3.注意:因为我们是用沙箱环境测试开发的,所有在这里是需要下载一个沙箱般的支付宝手机app 才能扫码支付(沙箱般的是做测试用的,不会涉及到财产)
沙箱下载: