一、背景
近期,接了一个项目需要完成支付功能。之前还没做过,这跟着做了一下,顺便记录一下。
二、准备工作
- 打开支付宝的支付SDK
支付宝SDK控制平台
进去之后就是这个样子,然后点击研发服务先进行沙箱测试。(也可进行应用创建,需要商户进行签约,审核,通过之后上线即可实际使用)
- 沙箱测试
在此处记住APPID以及支付宝网关即可,然后就需要进入下一步,加密,密钥设置(参考着官方文档进行第三步操作)
- 密钥加密
下载官方提供的秘钥生成工具进行秘钥的生成。
生成之后记得上传公钥。
三、Java实现
- 引入相关依赖
<!-- //官方网站:https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java-->
<!-- //实例: 支付宝支付SDK-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.9.28.ALL</version>
</dependency>
- 编写config配置必要请求参数
package com.xhsw.helpton.config;
import org.springframework.context.annotation.Configuration;
/**
* @AUTHOR LYF
* @DATE 2021/4/3
* @VERSION 1.0
* @DESC
* 支付宝接口
*/
@Configuration
public class AliPayConfig {
/**
* netApp映射地址
*/
private static String neturl = "";
/**
* 应用的APPID
*/
public static String app_id = "";
/**
* 商户私钥,您的PKCS8格式RSA2私钥 刚刚生成的私钥直接复制填写(填写自己利用第三方工具生成的私钥)
*/
public static String merchant_private_key = ;
/**
* 支付宝公钥,对应APPID下的支付宝公钥(填写沙箱环境中的支付宝公钥)
*/
public static String alipay_public_key = ;
/**
* 服务器异步通知页面路径 需http://格式的完整路径(支付宝完成后返回的页面)
*/
public static String notify_url = neturl+"/alipay/notify_url";
/**
* 页面跳转同步通知页面路径 需http://格式的完整路径(支付宝完成后返回的页面)
*/
public static String return_url = neturl+"/alipay/return_url";
/**
* 签名方式
*/
public static String sign_type = "RSA2";
/**
* 字符编码方式
*/
public static String charset = "utf-8";
/**
* 支付宝网关
*/
public static String url = "https://openapi.alipaydev.com/gateway.do";
}
- 编写发起到支付宝服务器的请求类
package com.xhsw.helpton.service.pay;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.xhsw.helpton.config.AliPayConfig;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @AUTHOR LYF
* @DATE 2021/4/3
* @VERSION 1.0
* @DESC
*/
@Service
public class AliPayService2 {
/**
* 支付服务(推荐传入订单号和金额,其余的从其他地方获取)
*/
public String aliPay(String amount,String oderNo,String subject) throws AlipayApiException {
// AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "app_id", "your private_key", "json", "GBK", "alipay_public_key", "RSA2");
DefaultAlipayClient defaultAlipayClient = new DefaultAlipayClient(AliPayConfig.url,
AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json",
AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); //70501111111S001111119
request.setBizContent("{" +
"\"subject\":\"购买会员\"," +
"\"out_trade_no\":"+oderNo+"," +
"\"timeout_express\":\"90m\"," +
"\"total_amount\":"+amount+"," +
"\"seller_id\":\\"," +
"\"product_code\":\"QUICK_MSECURITY_PAY\""+
" }");
//QUICK_MSECURITY_PAY
AlipayTradeWapPayResponse response = defaultAlipayClient.pageExecute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
return response.getBody();
}
public static void main(String[]args) throws AlipayApiException {
AliPayService2 aliPayService2 = new
AliPayService2();
aliPayService2.aliPay("56.00","10000002","购买会员");
}
}
- 编写service和controller
package com.xhsw.helpton.controller.pay;
import com.alipay.api.AlipayApiException;
import com.xhsw.helpton.service.PayService;
import com.xhsw.helpton.service.pay.AliPayService;
import com.xhsw.helpton.service.pay.AliPayService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* @AUTHOR LYF
* @DATE 2021/4/3
* @VERSION 1.0
* @DESC
*/
//@RestController
//@RequestMapping("/pay")
@RestController
public class AliPayController {
@Autowired
AliPayService aliPayService;
@Autowired
AliPayService2 aliPayService2;
@Autowired
PayService payService;
static Integer userId = 0;
static Float amount = 0.00F;
static Integer productId = 0;
static boolean flag = false;
// @ApiOperation("支付接口")
@GetMapping("/alipay/toPay/pay/{userId}/{productId}/{amount}")
public String alipay(@PathVariable(value = "productId")Integer productId,
@PathVariable(value = "amount")Float amount, @PathVariable(value = "userId")Integer userId
) throws AlipayApiException {
flag = true;//正在支付中
this.userId =userId;
this.amount =amount;
this.productId = productId;
System.out.println("userId:"+userId+";productId:"+productId+";amount:"+amount);
return aliPayService2.aliPay(amount+"",productId+"",userId+"TEST");
}
@GetMapping("/alipay/toPay/pay2/{productId}")
public String alipay2(@PathVariable(value = "productId")String productId,@PathVariable(value = "amount")String amount) throws AlipayApiException {
// productId决定什么产品
return aliPayService2.aliPay(amount,productId,"");
}
// @ApiOperation("支付宝异步通知接口")
@GetMapping("/alipay/notify_url")
public String notifyAlipay(){
return "这是异步通知接口";
}
// @ApiOperation("支付成功之后的回调")
@GetMapping("/alipay/return_url")
public String returnAlipay(){
flag = false;
System.out.println(userId+";"+amount+";"+productId);
payService.buyMember(userId+"",amount,productId+"");
return "支付成功后的回调";
}
}
四、测试
输入测试,即可进入支付界面(第一次需要输入账户密码,记得使用沙箱里面的账户密码以及支付密码才能,并不是真实的,实际开发需要在应用里面签约开发)