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支付和单笔支付退款的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程