本文主要是介绍Java网站如何集成支付宝当面付,企业个人都能使用的支付(比较简单),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
创建应用
这个得先去登录 - 支付宝创建应用
相关配置设置
maven配置
<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.38.10.ALL</version></dependency>
支付服务代码
/*** 支付宝支付* @author leell* @date 2023/7/21 16:39:58*/
public interface AliPayService {/*** 预下单* @return 返回二维码链接*/String createPrecreateTrade(AliPrecreateParam param) throws RuntimeException;/*** 检查支付状态* @param orderNo 订单号* @return 支付状态 true:已支付 false:未支付*/boolean checkAlipayStatus(String orderNo) throws RuntimeException;/*** 取消支付* @param orderNo 订单号* @return 取消状态*/boolean cancelPay(String orderNo) throws RuntimeException;/*** 退款* @param orderNo 订单号* @return 取消状态*/boolean refundPay(String orderNo, BigDecimal money) throws RuntimeException;/*** 验证签名* @param params 参数* @return 状态*/boolean rsaCheckV1(Map<String,String> params) throws RuntimeException;/*** 检查回调支付状态* @param params 参数* @return 状态* @throws RuntimeException 异常*/boolean checkNotifyPayStatus(Map<String,String> params) throws RuntimeException;/*** 获取订单号* @param params 支付宝返回参数* @return 订单号*/String getOrderNo(Map<String,String> params);
}
/*** 支付宝支付** @author leell* @date 2023/7/21 16:46:16*/
@Service
public class AliPayServiceImpl implements AliPayService {@Autowiredprivate PayHelper payHelper;@Autowiredprivate ConfigService configService;@Overridepublic String createPrecreateTrade(AliPrecreateParam param) throws RuntimeException {try {AlipayClient alipayClient = payHelper.getAlipayClient();AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//异步接收地址,仅支持http/https,公网可访问request.setNotifyUrl(param.getNotifyUrl());JSONObject bizContent = new JSONObject();bizContent.put("out_trade_no", param.getOutTradeNo());bizContent.put("total_amount", param.getTotalAmount());bizContent.put("subject", param.getSubject());request.setBizContent(bizContent.toString());AlipayTradePrecreateResponse response = alipayClient.execute(request);AssertUtil.isTrue(response.isSuccess(), response.getMsg());return response.getQrCode();} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic boolean checkAlipayStatus(String orderNo) throws RuntimeException {try {AlipayClient alipayClient = payHelper.getAlipayClient();AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();JSONObject bizContent = new JSONObject();bizContent.put("out_trade_no", orderNo);bizContent.put("query_options", new String[]{"trade_settle_info"});request.setBizContent(bizContent.toString());AlipayTradeQueryResponse response = alipayClient.execute(request);JSONObject tradeResponse = JSONObject.parseObject(response.getBody()).getJSONObject("alipay_trade_query_response");String result = tradeResponse.getString("trade_status");return "TRADE_SUCCESS".equals(result);} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic boolean cancelPay(String orderNo) throws RuntimeException {try {AlipayClient alipayClient = payHelper.getAlipayClient();AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();JSONObject bizContent = new JSONObject();bizContent.put("out_trade_no", orderNo);request.setBizContent(bizContent.toString());AlipayTradeCancelResponse response = alipayClient.execute(request);return response.isSuccess();} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic boolean refundPay(String orderNo, BigDecimal money) throws RuntimeException {try {AlipayClient alipayClient = payHelper.getAlipayClient();AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();JSONObject bizContent = new JSONObject();bizContent.put("refund_amount", money);bizContent.put("out_trade_no", orderNo);request.setBizContent(bizContent.toString());AlipayTradeRefundResponse response = alipayClient.execute(request);return response.isSuccess();} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic boolean rsaCheckV1(Map<String, String> params) throws RuntimeException {try {String alipayPublicKey = configService.getValue("pay_alipay_public_key");String charset = configService.getValue("pay_alipay_charset");String signType = configService.getValue("pay_alipay_signType");return AlipaySignature.rsaCheckV1(params, alipayPublicKey, charset, signType);} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic boolean checkNotifyPayStatus(Map<String, String> params) throws RuntimeException {// 取得支付宝POST过来的异步通知参数,进行验签// 注意:调用SDK验证签名需要使用支付宝公钥,不要使用自己生成的RSA公私钥对boolean verifyResult = this.rsaCheckV1(params);if (verifyResult) {// 验签通过String tradeStatus = params.get("trade_status");return "TRADE_SUCCESS".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus);}return false;}@Overridepublic String getOrderNo(Map<String, String> params) {// 处理交易成功的逻辑return params.get("out_trade_no");}
}
支付宝相关配置参数
我的后台服务采用的都是参数配置型,并没有放到yaml文件中。上面的appid primarykey和publickey signtype按照支付宝官网上找到自己的,对于特定的publickey和primarykey可以下载支付宝证书生成工具本地生成,然后同步到支付宝
当面付线上案例:
模板码-网站源码,源码下载,模板插件,商业源码平台 (mubanma.com)
惠集会-专业的宠物用品在线购物平台 (huijihui.com)
这篇关于Java网站如何集成支付宝当面付,企业个人都能使用的支付(比较简单)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!