springboot实现调用支付宝支付接口
springboot实现调⽤⽀付宝⽀付接⼝
1.在l⾥添加alipay的依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.8.10.ALL</version>
</dependency>
2.在application.properties⾥⾯添加访问静态资源
#设置访问静态资源路径
server.port=8080
3.添加配置类,添加商户秘钥和⽀付宝的公钥,这⾥需要下载⼀个⽀付宝开放平台开发助⼿,⾥⾯去⽣成,不懂的百度⼀下,有很多的教程,我这⾥就不重复写了,只要
填对了,我这个就可以看见效果
  注意:⽀付宝的沙箱测试环境的⽀付宝⽹关为:"openapi.alipaydev/gateway.do";
实际开发环境的⽀付宝⽹关为"openapi.alipay/gateway.do";少了⼀个div,仅此⽽已
import java.io.FileWriter;
import java.io.IOException;
public class AlipayConfigInfo {
// ↓↓↓↓↓↓↓↓↓↓请在这⾥配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 应⽤ID,您的APPID,收款账号既是您的APPID对应⽀付宝账号
public static String app_id="20161015006942*";//例:2016082600317257
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbcNwhjRT7nXICPZOpgobihEVSxHwiGLcXUr3FurHJDxLARgX5Fg5O99WjS95********Xjoyqrtz4Um6CTdn9Zbgc=" // ⽀付宝公钥,查看地址:openhome.alipay/platform/keyManage.htm
// 对应APPID下的⽀付宝公钥。
public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgzd570gh8Yw/7UdG5yYUur5FxXYrqaYyfP0h9TI/Y7z8jIzAHlrYlWLJ6Y9qBr+M0FisXl4**********************hDAQAB";
// 服务器异步通知页⾯路径需格式的完整路径,不能加?id=123这类⾃定义参数,必须外⽹可以正常访问
/**
* 返回的时候此页⾯不会返回到⽤户页⾯,只会执⾏你写到控制器⾥的地址
*/
public static String notify_url="2ky69**4.zicp.vip:44622/notify_url.html";
/
/ 页⾯跳转同步通知页⾯路径需格式的完整路径,不能加?id=123这类⾃定义参数,必须外⽹可以正常访问
/**
* 此页⾯是同步返回⽤户页⾯,也就是⽤户⽀付后看到的页⾯,上⾯的notify_url是异步返回商家操作,谢谢
* 要是看不懂就度娘,或者多读⼏遍,或者去看⽀付宝第三⽅接⼝API,不看API直接拿去就⽤,遇坑不怪别⼈,要使⽤外⽹能访问的ip,建议使⽤花⽣壳,内⽹穿透
*/
public static String return_url = "2ky69**64.zicp.vip:44622/return_url.html";
// 签名⽅式
public static String sign_type = "RSA2";
// 字符编码格式
public static String charset = "utf-8";
// ⽀付宝⽹关
public static String gatewayUrl="openapi.alipaydev/gateway.do";
// ⽇志地址,这⾥在d盘下要创建这个⽂件,不然会报错
public static String log_path = "D:/logs/";
// ↑↑↑↑↑↑↑↑↑↑请在这⾥配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写⽇志,⽅便测试(看⽹站需求,也可以改成把记录存⼊数据库)
*
* @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();
}
为什么英雄联盟进不去}
大天蓬歌曲
}
}
}
4.在controller⾥⾯添加⼀个⽅法
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.quest.AlipayTradePagePayRequest;
import fig.AlipayConfigInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
SimpleDateFormat;
import java.util.Date;
@Controller
public class AlipayIumpSum {
/**
* 快捷⽀付调⽤⽀付宝⽀付接⼝
* @param model,id,payables,
* @return Object
*/
@RequestMapping("alipaySum")
public Object alipayIumpSum(Model model, String WIDout_trade_no, String WIDsubject, String WIDtotal_amount,String WIDbody,
HttpServletResponse response)
throws Exception {
System.out.println("请求进来"+WIDtotal_amount);
String payables = WIDout_trade_no;
// 订单名称,必填(必须是数字)
String subject = WIDsubject;
// 付款⾦额,必填
String total_fee = WIDtotal_amount;
// 商品描述,可空
String body = WIDbody;
// 获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigInfo.gatewayUrl, AlipayConfigInfo.app_id,
AlipayConfigInfo.alipay_public_key, AlipayConfigInfo.sign_type);
// 设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.urn_url);
alipayRequest.ify_url);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// 商户订单号,商户⽹站订单系统中唯⼀订单号,必填
String out_trade_no = sdf.format(new Date());
// 付款⾦额,必填
String total_amount = place(",", "");
alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
+ "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// 请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
// System.out.println(result);
//EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
AlipayConfigInfo.logResult(result);// 记录⽀付⽇志
response.setContentType("text/html; charset=gbk");
PrintWriter out = Writer();
out.print(result);
return null;
}
}
研究生自我鉴定范文5.写⼀个测试的page.html
<!DOCTYPE html>
<html lang="en">
端午节高速公路免费吗<head>
<meta charset="UTF-8">
<title>⽀付宝测试</title>
<script src="js/jquery-1.10.2.min.js"></script>
</head>
<body>
<form name=alipayment  id="formDiv" action="alipaySum">
<div id="body1" class="show" name="divcontent">
<dl class="content">
<dt>⾦额:</dt>
<dd>
<input id="WIDout_trade_no" name="WIDout_trade_no" />
</dd>
<hr class="one_line">
<dt>订单名称:</dt>
<dd>
<input id="WIDsubject" name="WIDsubject" />
</dd>
<hr class="one_line">
<dt>备注:</dt>
<dd>
<input id="WIDtotal_amount" name="WIDtotal_amount" />
</dd>
<hr class="one_line">
<dt>商品描述:</dt>
<dd>
<input id="WIDbody" name="WIDbody" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
赞美军人的最佳句子<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
id="btn" >付款</button>
</span>
<span class="note-help">如果您点击“付款”按钮,即表⽰您同意该次的执⾏操作。</span>
</dd>
</dl>
</div>
</form>
<!--<script>
$("#btn").click(function(){
var da = $("#formDiv").serialize();
$.post("alipaySum",da,"json")
})
</script>-->
</body>
</html>
6.⼀个⽀付成功返回的页⾯return_url.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
如果你看到此页⾯说明⽀付成功
</body>
</html>
  注意:沙箱环境必须使⽤沙箱账号和密码,沙箱账号在你沙箱测试环境下的账户信息就能看见,⾦额可以随意设置的,真实的⽀付宝app扫描⼆维码会出现⼆维码失效

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。