1、创建应用
登录支付宝开放平台
支付宝开放平台网址 :支付宝 (alipay.com)
支付宝开放平台
下拉最下边
2、获取appId、支付宝公钥、应用私钥
3、开始创建spring boot项目
#支付宝支付配置
alipay:#你的APPIDappId: 902************#你的应用私钥privateKey: MIIE***********#支付宝公钥publicKey: MIIB***********#生产环境网关地址#serverUrl: https://openapi.alipay.com/gateway.do#沙箱环境网关地址serverUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do#生产环境域domain: gw.open.alipay.com#回调urlnoticeUrl: https://ie2204wi1077.vicp.fun
4、导入pom依赖
<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.39.134.ALL</version></dependency><!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk --><dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-easysdk</artifactId><version>2.2.2</version></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency>
5、创建Controller
准备好密钥
import com.alipay.api.AlipayApiException;
import com.pig4cloud.pigx.alipay.util.PayVo;
import com.pig4cloud.pigx.alipay.service.AliPayService;
import com.pig4cloud.pigx.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;@Slf4j
@RequestMapping("/alipay")
@RestController
public class AliPayController {@Resourceprivate AliPayService aliPayService;@RequestMapping("/pay")public R paymoney(@RequestBody PayVo payVo) throws AlipayApiException {return R.ok(aliPayService.pay(payVo));}@PostMapping("/notify") // 注意这里必须是POST接口public R notify(HttpServletRequest request) throws Exception {return R.ok(aliPayService.notify(request));}}
6、创建一个实体类来接受前端传递过来的参数
这是我的
import lombok.Data;@Data
public class PayVo {//订单idprivate String orderId;//会诊idprivate String conId;//支付金额private String money;//支付标题private String title;//成功返回页面private String url;}
7、AliPayService.java
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.alipay.util.PayVo;
import com.pig4cloud.pigx.ybiot.entity.MzRecipe;
import com.pig4cloud.pigx.ybiot.entity.RecipeDTO;
import org.springframework.web.bind.annotation.RequestBody;import javax.servlet.http.HttpServletRequest;/*** 支付宝支付接口** @author gwh* @date 2024-11-11 09:08:14*/
public interface AliPayService extends IService<MzRecipe> {/*** 支付接口* @param payVo* @return*/RecipeDTO pay(@RequestBody PayVo payVo) throws AlipayApiException;RecipeDTO notify(HttpServletRequest request) throws Exception;}
8、AliPayServiceImpl.java
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.easysdk.factory.Factory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.alipay.util.AliPayConfig;
import com.pig4cloud.pigx.alipay.util.PayVo;
import com.pig4cloud.pigx.alipay.service.AliPayService;
import com.pig4cloud.pigx.ybiot.entity.MzRecipe;
import com.pig4cloud.pigx.ybiot.entity.RecipeDTO;
import com.pig4cloud.pigx.ybiot.mapper.MzRecipeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;/*** 支付宝支付实现** @author gwh* @date 2024-11-11 09:39:14*/
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AliPayServiceImpl extends ServiceImpl<MzRecipeMapper, MzRecipe> implements AliPayService {@Autowiredprivate AliPayConfig aliPayConfig;@Overridepublic RecipeDTO pay(PayVo payVo) throws AlipayApiException {RecipeDTO resultDTO = new RecipeDTO();System.out.println(payVo);String appId = aliPayConfig.getAppId();// AppIdString privateKey = aliPayConfig.getPrivateKey(); //必填 自己的应用私钥,即PKCS8格式RSA2私钥String alipayPublicKey = aliPayConfig.getPublicKey(); //必填 自己的支付宝公钥String serverUrl = aliPayConfig.getServerUrl(); //必填 serverUrlString noticeUrl = aliPayConfig.getServerUrl(); //回调urlAlipayConfig alipayConfig = new AlipayConfig();alipayConfig.setServerUrl(serverUrl); //沙箱alipayConfig.setAppId(appId); //必填 自己的AppIdalipayConfig.setPrivateKey(privateKey);alipayConfig.setFormat("json");alipayConfig.setAlipayPublicKey(alipayPublicKey);alipayConfig.setCharset("UTF-8");alipayConfig.setSignType("RSA2");AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();request.setNotifyUrl(noticeUrl); //自己的内网地址, 这个是我的自己可以下载路由下自行配置request.setReturnUrl(payVo.getUrl()); //成功后跳转的地址AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();model.setOutTradeNo(payVo.getOrderId()); //传过来的订单号, 这个我写的 自己写一个默认的测试一下model.setTotalAmount(payVo.getMoney()); //支付金额, 这个我写的 自己写一个默认的测试一下model.setSubject(payVo.getTitle()); //支付标题, 这个我写的 自己写一个默认的测试一下model.setProductCode("QUICK_WAP_WAY");model.setSellerId("2088721041615706");request.setBizModel(model);AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");// 如果需要返回GET请求,请使用// AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");String pageRedirectionData = response.getBody();System.out.println(pageRedirectionData);if (response.isSuccess()) {System.out.println("调用成功");} else {System.out.println("调用失败");// sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接// String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);// System.out.println(diagnosisUrl);}return resultDTO;}@Overridepublic RecipeDTO notify(HttpServletRequest request) throws Exception {RecipeDTO resultDTO = new RecipeDTO();if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {System.out.println("=========支付宝异步回调========");Map<String, String> params = new HashMap<>();Map<String, String[]> requestParams = request.getParameterMap();for (String name : requestParams.keySet()) {params.put(name, request.getParameter(name));// System.out.println(name + " = " + request.getParameter(name));}String tradeNo = params.get("out_trade_no");String gmtPayment = params.get("gmt_payment");String alipayTradeNo = params.get("trade_no");// 支付宝验签if (Factory.Payment.Common().verifyNotify(params)) {// 验签通过System.out.println("交易名称: " + params.get("subject"));System.out.println("交易状态: " + params.get("trade_status"));System.out.println("支付宝交易凭证号: " + params.get("trade_no"));System.out.println("商户订单号: " + params.get("out_trade_no"));System.out.println("交易金额: " + params.get("total_amount"));System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));System.out.println("买家付款时间: " + params.get("gmt_payment"));System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));// 更新订单状态}}return resultDTO;}
}