2021-07-10 java实现支付宝的H5支付和单笔支付退款
2021/7/10 17:12:04
本文主要是介绍2021-07-10 java实现支付宝的H5支付和单笔支付退款,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
逻辑参数属性什么的了不多说,自己去支付宝开发文档上看,博主这边只使用了那些必需的参数,其他要加的自己后续加上。上代码
首先导入支付宝
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.7.89.ALL</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.53</version> </dependency>
接着实现方法
import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradeRefundRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.response.AlipayTradeRefundResponse; import com.alipay.api.response.AlipayTradeWapPayResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class H5PayDto { @ResponseBody @GetMapping("/zfbpay") public String pay() throws AlipayApiException { String appid ="APPID"; String privateKey ="开发者私钥"; String Ali_publicKey ="支付宝公钥"; //这里的参数属性根据自己的实际配置来填 AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appid,privateKey,"json","utf-8",Ali_publicKey,"RSA"); AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.setBizContent("{" + "\"body\":\"Iphone6 16G\"," + "\"subject\":\"标题\"," + "\"out_trade_no\":\"2021070922001473791438611504\"," + "\"total_amount\":1.00," + "\"quit_url\":\"回调地址\"," + "\"product_code\":\"QUICK_WAP_PAY\"}"); //默认 AlipayTradeWapPayResponse response = alipayClient.pageExecute(request); System.out.println(response.getBody()); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); } return response.getBody(); } @ResponseBody @GetMapping("/Zfbrefund") public String Zfbrefund() throws AlipayApiException { String appid ="APPID"; String privateKey ="开发者私钥"; String Ali_publicKey ="支付宝公钥"; AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appid,privateKey,"json","utf-8",Ali_publicKey,"RSA"); AlipayTradeRefundRequest request = new AlipayTradeRefundRequest(); request.setBizContent("{" + "\"out_trade_no\":\"2021070922001473791438611503\"," + //订单日期一定要记得 // "\"trade_no\":\"2014112611001004680073956707\"," + "\"refund_amount\":1.00," + "\"refund_reason\":\"正常退款\" }"); AlipayTradeRefundResponse response = alipayClient.execute(request); System.out.println(response.getMsg()+" ---------- "+response.getBody()+"-------- "+response.getSubMsg()); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); } return "ok"; } }
其他一些小问题列下 先是密钥私钥这些。
然后在我们请求返回的支付宝信息中,如果直接打开,他会提示你签名错误,公钥和私钥不匹配,这个是有个原因在于你直接使用浏览器打开,需要将这个信息写入html中,从html中打开。如何还提示参数或者啥的问题,再可以一个个的去排除。
项目地址:添加链接描述
这篇关于2021-07-10 java实现支付宝的H5支付和单笔支付退款的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22项目:远程温湿度检测系统
- 2024-12-21《鸿蒙HarmonyOS应用开发从入门到精通(第2版)》简介
- 2024-12-21后台管理系统开发教程:新手入门全指南
- 2024-12-21后台开发教程:新手入门及实战指南
- 2024-12-21后台综合解决方案教程:新手入门指南
- 2024-12-21接口模块封装教程:新手必备指南
- 2024-12-21请求动作封装教程:新手必看指南
- 2024-12-21RBAC的权限教程:从入门到实践
- 2024-12-21登录鉴权实战:新手入门教程
- 2024-12-21动态权限实战入门指南